| 1 | package HTML::WikiConverter::WebApp; | ||
| 2 | 1 | ▶ | use base 'CGI::Application'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | use HTML::WikiConverter; | ||
| 5 | use XML::Writer; | ||
| 6 | use Tie::IxHash; | ||
| 7 | use Encode; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | HTML::WikiConverter::WebApp - Web interface to HTML::WikiConverter | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | Inside the index.cgi instance script (which is included with this | ||
| 16 | distribution): | ||
| 17 | |||
| 18 | #!/usr/bin/perl | ||
| 19 | use HTML::WikiConverter::WebApp; | ||
| 20 | |||
| 21 | my %config = ( | ||
| 22 | template_path => '/path/to/web/templates', | ||
| 23 | ); | ||
| 24 | |||
| 25 | HTML::WikiConverter::WebApp->new( PARAMS => \%config )->run; | ||
| 26 | |||
| 27 | =head1 DESCRIPTION | ||
| 28 | |||
| 29 | This module provides a L<CGI::Application> interface to | ||
| 30 | L<HTML::WikiConverter> and all installed dialect modules. | ||
| 31 | |||
| 32 | Refer to the INSTALL file for installation instructions. | ||
| 33 | |||
| 34 | =head1 QUERY PARAMETERS | ||
| 35 | |||
| 36 | This application accepts a number of query parameters to control its | ||
| 37 | behavior. The most basic is the run mode parameter, C<m>. This | ||
| 38 | application can be in one of two run modes: C<"new"> or C<"convert">. | ||
| 39 | (These correspond to the C<new_page()> and C<output_page()> methods, | ||
| 40 | respectively.) C<"new"> causes a new page to be displayed, while | ||
| 41 | C<"convert"> displays the results of html-to-wiki conversion. | ||
| 42 | |||
| 43 | Additional query parameters can be passed independent of run mode: | ||
| 44 | |||
| 45 | =over 4 | ||
| 46 | |||
| 47 | =item * source_type | ||
| 48 | |||
| 49 | One of C<"raw_html">, C<"from_uri">, or C<"sample_html">. | ||
| 50 | |||
| 51 | =item * dialect | ||
| 52 | |||
| 53 | Any installed dialect, eg C<"MediaWiki">. | ||
| 54 | |||
| 55 | =item * base_uri | ||
| 56 | |||
| 57 | Base URL to be used for converting relative links to absolute ones. | ||
| 58 | |||
| 59 | =item * wiki_uri | ||
| 60 | |||
| 61 | Template for wiki URLs. When scanning the HTML source, if a URL (as in | ||
| 62 | an C<a href> or C<img src> attribute value) is found matching this | ||
| 63 | template, it will be treated to a link to a wiki | ||
| 64 | article/image. Consult L<HTML::WikiConverter/ATTRIBUTES> for more | ||
| 65 | information. | ||
| 66 | |||
| 67 | Note that this is a bit less flexible than using the C<wiki_uri> | ||
| 68 | attribute directly in L<HTML::WikiConverter>. That attribute allows | ||
| 69 | multiple templates, allows coderefs, and regexps, etc. This option | ||
| 70 | only accepts a single scalar representing a very simple wiki URL | ||
| 71 | template. | ||
| 72 | |||
| 73 | =item * show_parsed_html | ||
| 74 | |||
| 75 | If enabled, an additional textarea containing the parsed HTML will be | ||
| 76 | displayed. | ||
| 77 | |||
| 78 | =item * escape_entities | ||
| 79 | |||
| 80 | If enabled, unsafe HTML entities ("E<lt>", "E<gt>", and "E<amp>") will | ||
| 81 | be encoded using L<HTML::Entities>. | ||
| 82 | |||
| 83 | =item * format | ||
| 84 | |||
| 85 | One of C<"html"> or C<"xml">. Determines the type of output displayed | ||
| 86 | by this application. | ||
| 87 | |||
| 88 | =back | ||
| 89 | |||
| 90 | =head1 METHODS | ||
| 91 | |||
| 92 | =head2 setup | ||
| 93 | |||
| 94 | Sets up the app for L<CGI::Application>. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub setup { | ||
| 99 | my $self = shift; | ||
| 100 | $self->error_mode( 'display_error' ); | ||
| 101 | $self->tmpl_path( $self->param('template_path') ); | ||
| 102 | $self->mode_param( 'm' ); | ||
| 103 | $self->start_mode( 'new' ); | ||
| 104 | $self->run_modes( | ||
| 105 | new => 'new_page', | ||
| 106 | convert => 'output_page', | ||
| 107 | ); | ||
| 108 | $self->header_add( -charset => 'utf-8' ); | ||
| 109 | } | ||
| 110 | |||
| 111 | =head2 new_page | ||
| 112 | |||
| 113 | Corresponds to the C<new> run mode. Returns a blank form. If arguments | ||
| 114 | are available on the CGI query string, these are used as default | ||
| 115 | values for the form fields. | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | sub new_page { | ||
| 120 | my $self = shift; | ||
| 121 | my $q = $self->query; | ||
| 122 | |||
| 123 | my $tmpl = $self->load_template( 'main.html' ); | ||
| 124 | $tmpl->param( | ||
| 125 | $self->_default_template_params, | ||
| 126 | ); | ||
| 127 | |||
| 128 | $tmpl->output; | ||
| 129 | } | ||
| 130 | |||
| 131 | =head2 output_page | ||
| 132 | |||
| 133 | Corresponds to the C<convert> run mode. Same as C<new_page()> but returns | ||
| 134 | the wiki markup for the provided html as well. | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | sub output_page { | ||
| 139 | my $self = shift; | ||
| 140 | my $q = $self->query; | ||
| 141 | |||
| 142 | my $source_type = $q->param('source_type') or die "need source_type"; | ||
| 143 | my $dialect = $q->param('dialect') or die "need dialect"; | ||
| 144 | |||
| 145 | die "unknown source_type '$source_type'" unless $self->_known_source_type( $q->param('source_type') ); | ||
| 146 | |||
| 147 | my %source; | ||
| 148 | SWITCH: { | ||
| 149 | %source = ( html => $q->param('html') ), last if $source_type eq 'raw_html'; | ||
| 150 | %source = ( html => $self->_sample_html ), last if $source_type eq 'sample_html'; | ||
| 151 | %source = ( uri => $q->param('uri') ), last if $source_type eq 'from_uri'; | ||
| 152 | }; | ||
| 153 | |||
| 154 | die sprintf( "no %s was provided", (keys %source)[0] ) unless ( (values %source)[0] ); | ||
| 155 | |||
| 156 | my $wc = new HTML::WikiConverter( | ||
| 157 | dialect => $dialect, | ||
| 158 | base_uri => $q->param('base_uri'), | ||
| 159 | wiki_uri => $q->param('wiki_uri'), | ||
| 160 | escape_entities => $q->param('escape_entities'), | ||
| 161 | ); | ||
| 162 | |||
| 163 | my $wiki = $wc->html2wiki( %source ); | ||
| 164 | my $parsed_html = $wc->parsed_html; | ||
| 165 | |||
| 166 | $source{html} = decode( $wc->encoding, $source{html} ) if $source{html}; | ||
| 167 | $wiki = decode( $wc->encoding, $wiki ) if $wiki; | ||
| 168 | |||
| 169 | my $format = $q->param('format') || 'html'; | ||
| 170 | if( $format eq 'xml' ) { | ||
| 171 | tie( my %default_template_params, 'Tie::IxHash' ); | ||
| 172 | %default_template_params = $self->_default_template_params; | ||
| 173 | |||
| 174 | my %ignore_params = map { $_ => 1 } qw( | ||
| 175 | dialects | ||
| 176 | error | ||
| 177 | raw_html | ||
| 178 | from_uri | ||
| 179 | sample_html | ||
| 180 | uri | ||
| 181 | ); | ||
| 182 | |||
| 183 | my $xml = ''; | ||
| 184 | my $writer = new XML::Writer( OUTPUT => \$xml, DATA_MODE => 1, DATA_INDENT => 2 ); | ||
| 185 | $writer->xmlDecl('utf-8'); | ||
| 186 | $writer->startTag( 'wikitool', application => 'html2wiki' ); | ||
| 187 | |||
| 188 | $writer->startTag( 'query' ); | ||
| 189 | $writer->startTag( 'source', type => $source_type ); | ||
| 190 | $writer->characters( (values %source)[0] ); | ||
| 191 | $writer->endTag(); | ||
| 192 | |||
| 193 | $writer->startTag( 'options' ); | ||
| 194 | while( my( $f, $v ) = each %default_template_params ) { | ||
| 195 | next if $ignore_params{$f}; | ||
| 196 | $writer->startTag( $f ); | ||
| 197 | $writer->characters( $v ); | ||
| 198 | $writer->endTag(); | ||
| 199 | } | ||
| 200 | $writer->endTag(); | ||
| 201 | $writer->endTag(); | ||
| 202 | |||
| 203 | $writer->startTag( 'response' ); | ||
| 204 | if( $q->param('show_parsed_html') ) { | ||
| 205 | $writer->startTag( 'parsed_html' ); | ||
| 206 | $writer->characters( $parsed_html ); | ||
| 207 | $writer->endTag(); | ||
| 208 | } | ||
| 209 | |||
| 210 | $writer->startTag( 'wiki_markup' ); | ||
| 211 | $writer->characters( $wiki ); | ||
| 212 | $writer->endTag(); | ||
| 213 | |||
| 214 | $writer->endTag(); | ||
| 215 | $writer->endTag(); | ||
| 216 | |||
| 217 | $self->header_add( -type => 'text/xml' ); | ||
| 218 | return $xml; | ||
| 219 | } | ||
| 220 | |||
| 221 | my $temp = $self->load_template( 'main.html' ); | ||
| 222 | $temp->param( | ||
| 223 | $self->_default_template_params, | ||
| 224 | uri => $source{uri}, | ||
| 225 | html => $source{html}, | ||
| 226 | parsed_html => $parsed_html, | ||
| 227 | wiki_markup => $wiki, | ||
| 228 | ); | ||
| 229 | |||
| 230 | return $temp->output; | ||
| 231 | } | ||
| 232 | |||
| 233 | sub _default_template_params { | ||
| 234 | my $self = shift; | ||
| 235 | my $q = $self->query; | ||
| 236 | |||
| 237 | my $source_type = $self->_known_source_type( $q->param('source_type') ) ? $q->param('source_type') : 'raw_html'; | ||
| 238 | |||
| 239 | return ( | ||
| 240 | dialect => $q->param('dialect') || '', | ||
| 241 | $source_type => 1, | ||
| 242 | |||
| 243 | uri => $q->param('uri') || '', | ||
| 244 | |||
| 245 | base_uri => $q->param('base_uri') || '', | ||
| 246 | wiki_uri => $q->param('wiki_uri') || '', | ||
| 247 | |||
| 248 | show_parsed_html => $q->param('show_parsed_html') || 0, | ||
| 249 | escape_entities => $q->param('escape_entities') || 0, | ||
| 250 | |||
| 251 | dialects => $self->_dialects, | ||
| 252 | error => '', | ||
| 253 | ); | ||
| 254 | } | ||
| 255 | |||
| 256 | my %known_source_types = map { $_ => 1 } qw( | ||
| 257 | from_uri | ||
| 258 | raw_html | ||
| 259 | sample_html | ||
| 260 | ); | ||
| 261 | |||
| 262 | sub _known_source_type { | ||
| 263 | my( $self, $type ) = @_; | ||
| 264 | return exists $known_source_types{$type}; | ||
| 265 | } | ||
| 266 | |||
| 267 | sub _dialects { | ||
| 268 | my $self = shift; | ||
| 269 | my $q = $self->query; | ||
| 270 | |||
| 271 | my $selected_dialect = $q->param('dialect') || ''; | ||
| 272 | |||
| 273 | my @dialects; | ||
| 274 | my %seen; | ||
| 275 | foreach my $dialect ( HTML::WikiConverter->available_dialects ) { | ||
| 276 | next if $seen{$dialect}++; | ||
| 277 | push @dialects, { dialect => $dialect, selected => ( $selected_dialect eq $dialect ) }; | ||
| 278 | } | ||
| 279 | |||
| 280 | return \@dialects; | ||
| 281 | } | ||
| 282 | |||
| 283 | =head2 load_template | ||
| 284 | |||
| 285 | Loads the specified L<HTML::Template> template. | ||
| 286 | |||
| 287 | =cut | ||
| 288 | |||
| 289 | sub load_template { | ||
| 290 | my( $self, $file ) = @_; | ||
| 291 | return $self->load_tmpl( $file, die_on_bad_params => 1, loop_context_vars => 1, cache => 1 ); | ||
| 292 | } | ||
| 293 | |||
| 294 | sub _sample_html { | ||
| 295 | my $self = shift; | ||
| 296 | return $self->load_template( 'sample_html.html' )->output; | ||
| 297 | } | ||
| 298 | |||
| 299 | =head2 display_error | ||
| 300 | |||
| 301 | Error-catching method called by L<CGI::Application> if a run mode | ||
| 302 | fails for any reason. Displays a basic form with a styled error | ||
| 303 | message up top. | ||
| 304 | |||
| 305 | =cut | ||
| 306 | |||
| 307 | sub display_error { | ||
| 308 | my( $self, $error ) = @_; | ||
| 309 | |||
| 310 | if( $error =~ /Dialect .* could not be loaded/ ) { | ||
| 311 | $error = sprintf q{The "%s" dialect either doesn't exist or has not been installed.}, $self->query->param('dialect'); | ||
| 312 | } | ||
| 313 | |||
| 314 | $error =~ s{(.*?) at \S+ line \d+\.}{$1.}; | ||
| 315 | $error = ucfirst $error; | ||
| 316 | |||
| 317 | my $tmpl = $self->load_template( 'main.html' ); | ||
| 318 | $tmpl->param( | ||
| 319 | $self->_default_template_params, | ||
| 320 | error => $error | ||
| 321 | ); | ||
| 322 | |||
| 323 | return $tmpl->output; | ||
| 324 | } | ||
| 325 | |||
| 326 | =head1 AUTHOR | ||
| 327 | |||
| 328 | David J. Iberri, C<< <diberri at cpan.org> >> | ||
| 329 | |||
| 330 | =head1 BUGS | ||
| 331 | |||
| 332 | Please report any bugs or feature requests to C<bug-html-wikiconverter | ||
| 333 | at rt.cpan.org>, or through the web interface at | ||
| 334 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-WikiConverter>. | ||
| 335 | I will be notified, and then you'll automatically be notified of | ||
| 336 | progress on your bug as I make changes. | ||
| 337 | |||
| 338 | =head1 SUPPORT | ||
| 339 | |||
| 340 | You can find documentation for this module with the perldoc command. | ||
| 341 | |||
| 342 | perldoc HTML::WikiConverter::WebApp | ||
| 343 | |||
| 344 | You can also look for information at: | ||
| 345 | |||
| 346 | =over 4 | ||
| 347 | |||
| 348 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 349 | |||
| 350 | L<http://annocpan.org/dist/HTML-WikiConverter-WebApp> | ||
| 351 | |||
| 352 | =item * CPAN Ratings | ||
| 353 | |||
| 354 | L<http://cpanratings.perl.org/d/HTML-WikiConverter-WebApp> | ||
| 355 | |||
| 356 | =item * RT: CPAN's request tracker | ||
| 357 | |||
| 358 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTML-WikiConverter-WebApp> | ||
| 359 | |||
| 360 | =item * Search CPAN | ||
| 361 | |||
| 362 | L<http://search.cpan.org/dist/HTML-WikiConverter-WebApp> | ||
| 363 | |||
| 364 | =back | ||
| 365 | |||
| 366 | =head1 COPYRIGHT & LICENSE | ||
| 367 | |||
| 368 | Copyright (c) David J. Iberri, all rights reserved. | ||
| 369 | |||
| 370 | This program is free software; you can redistribute it and/or modify | ||
| 371 | it under the same terms as Perl itself. | ||
| 372 | |||
| 373 | =cut | ||
| 374 | |||
| 375 | 1; | ||