| 1 | package Apache::AxKit::Provider::File::Formatter; | ||
| 2 | |||
| 3 | #use 5.008004; | ||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use Apache::AxKit::Provider::File; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Apache::AxKit::Exception; | ||
| 8 | use XML::LibXML; | ||
| 9 | |||
| 10 | our @ISA = qw(Apache::AxKit::Provider::File); | ||
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | our $VERSION = '0.96'; | ||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | Apache::AxKit::Provider::File::Formatter - An AxKit Provider that can | ||
| 21 | use any Formatter API module | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | In your Apache config, you can configure this Provider like this example: | ||
| 26 | |||
| 27 | <Files *.html> | ||
| 28 | PerlHandler AxKit | ||
| 29 | AxAddProcessor text/xsl /transforms/xhtml2html.xsl | ||
| 30 | AxContentProvider Apache::AxKit::Provider::File::Formatter | ||
| 31 | PerlSetVar FormatterModule Formatter::HTML::HTML | ||
| 32 | </Files> | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | This is an AxKit Provider that may be used to apply any module that | ||
| 37 | conforms with the Formatter API (v0.93) to a file. At the time of this | ||
| 38 | writing, there are three modules in the C<Formatter::> namespace, one | ||
| 39 | that can clean existing HTML using L<HTML::Tidy>, one for formatting | ||
| 40 | the L<Text::Textile> syntax, and one to add minimal HTML markup to a | ||
| 41 | preformatted plain text. | ||
| 42 | |||
| 43 | The Provider can be configured like any other Provider, to apply to a | ||
| 44 | directory, a file ending, etc. The only thing that is special about it | ||
| 45 | is that it needs a C<FormatterModule> variable to tell it which module | ||
| 46 | will do the actual formatting. It can be set like in the example in | ||
| 47 | the SYNOPSIS. | ||
| 48 | |||
| 49 | Make sure you have the module you specify installed, otherwise an | ||
| 50 | error will result. Also, you may need to copy a supplied XSLT | ||
| 51 | stylesheet to an appropriate location from the C</transforms/> | ||
| 52 | directory. For example, if you would like to output HTML like in the | ||
| 53 | above example, you need the C<xhtml2html.xsl> stylesheet. | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | sub get_dom { | ||
| 58 | my $self = shift; | ||
| 59 | my $r = $self->apache_request(); | ||
| 60 | |||
| 61 | # Let the superclass A:A:P:File handle the request if | ||
| 62 | # this is a directory. Nacho++ | ||
| 63 | if ($self->_is_dir()) { | ||
| 64 | return $self->SUPER::get_strref(); | ||
| 65 | } | ||
| 66 | |||
| 67 | # From SUPER: | ||
| 68 | my $fh = $self->SUPER::get_fh(); | ||
| 69 | local $/; | ||
| 70 | my $contents = <$fh>; | ||
| 71 | |||
| 72 | my $whichformatter = $r->dir_config('FormatterModule'); | ||
| 73 | AxKit::Debug(5, "Formatter Provider configured to use " . $whichformatter); | ||
| 74 | unless ($whichformatter =~ m/^Formatter::\w+::\w+$/) { | ||
| 75 | throw Apache::AxKit::Exception::Error( -text => "$whichformatter doesn't look like a formatter to me"); | ||
| 76 | } | ||
| 77 | eval "use $whichformatter"; | ||
| 78 | throw Apache::AxKit::Exception::Error( -text => $whichformatter . " not found, you may need to install it from CPAN") if $@; | ||
| 79 | my $formatter = "$whichformatter"->format($contents); | ||
| 80 | my $parser = XML::LibXML->new(); | ||
| 81 | return $parser->parse_html_string($formatter->document); | ||
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | |||
| 86 | sub get_strref { return \ shift->get_dom->toString; } | ||
| 87 | |||
| 88 | |||
| 89 | sub get_fh { | ||
| 90 | throw Apache::AxKit::Exception::IO( -text => "Can't get fh for Formatter" ); | ||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | =head1 SEE ALSO | ||
| 95 | |||
| 96 | The L<Formatter> API specification. | ||
| 97 | |||
| 98 | The currently existing Formatters: L<Formatter::HTML::HTML>, | ||
| 99 | L<Formatter::HTML::Textile> and L<Formatter::HTML::Preformatted>. Some | ||
| 100 | other Providers may also be of interest: | ||
| 101 | L<Apache::AxKit::Provider::File>, | ||
| 102 | L<Apache::AxKit::Provider::File::Syntax> | ||
| 103 | |||
| 104 | =head1 TODO | ||
| 105 | |||
| 106 | It should, in principle, be possible to use a chain of Formatter | ||
| 107 | modules to process a file in stages. This could be an interesting | ||
| 108 | exercise for the future, but then there are also many other pipeline | ||
| 109 | based paradigms that me be better suited. | ||
| 110 | |||
| 111 | =head1 AUTHOR | ||
| 112 | |||
| 113 | Kjetil Kjernsmo, E<lt>kjetilk@cpan.orgE<gt> | ||
| 114 | |||
| 115 | =head1 COPYRIGHT AND LICENSE | ||
| 116 | |||
| 117 | Copyright (C) 2005 by Kjetil Kjernsmo | ||
| 118 | |||
| 119 | This library is free software; you can redistribute it and/or modify | ||
| 120 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | ||
| 121 | at your option, any later version of Perl 5 you may have available. | ||
| 122 | |||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | 1; | ||
| 127 | __END__ | ||