This file is indexed.

/usr/share/perl5/HTML/WikiConverter/WebApp.pm is in libhtml-wikiconverter-perl 0.68-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package HTML::WikiConverter::WebApp;
use base 'CGI::Application';

use HTML::WikiConverter;
use XML::Writer;
use Tie::IxHash;
use Encode;

=head1 NAME

HTML::WikiConverter::WebApp - Web interface to HTML::WikiConverter

=head1 SYNOPSIS

Inside the index.cgi instance script (which is included with this
distribution):

  #!/usr/bin/perl
  use HTML::WikiConverter::WebApp;
  
  my %config = (
    template_path => '/path/to/web/templates',
  );

  HTML::WikiConverter::WebApp->new( PARAMS => \%config )->run;

=head1 DESCRIPTION

This module provides a L<CGI::Application> interface to
L<HTML::WikiConverter> and all installed dialect modules.

Refer to the INSTALL file for installation instructions.

=head1 QUERY PARAMETERS

This application accepts a number of query parameters to control its
behavior. The most basic is the run mode parameter, C<m>. This
application can be in one of two run modes: C<"new"> or C<"convert">.
(These correspond to the C<new_page()> and C<output_page()> methods,
respectively.) C<"new"> causes a new page to be displayed, while
C<"convert"> displays the results of html-to-wiki conversion.

Additional query parameters can be passed independent of run mode:

=over 4

=item * source_type

One of C<"raw_html">, C<"from_uri">, or C<"sample_html">.

=item * dialect

Any installed dialect, eg C<"MediaWiki">.

=item * base_uri

Base URL to be used for converting relative links to absolute ones.

=item * wiki_uri

Template for wiki URLs. When scanning the HTML source, if a URL (as in
an C<a href> or C<img src> attribute value) is found matching this
template, it will be treated to a link to a wiki
article/image. Consult L<HTML::WikiConverter/ATTRIBUTES> for more
information.

Note that this is a bit less flexible than using the C<wiki_uri>
attribute directly in L<HTML::WikiConverter>. That attribute allows
multiple templates, allows coderefs, and regexps, etc. This option
only accepts a single scalar representing a very simple wiki URL
template.

=item * show_parsed_html

If enabled, an additional textarea containing the parsed HTML will be
displayed.

=item * escape_entities

If enabled, unsafe HTML entities ("E<lt>", "E<gt>", and "E<amp>") will
be encoded using L<HTML::Entities>.

=item * format

One of C<"html"> or C<"xml">. Determines the type of output displayed
by this application.

=back

=head1 METHODS

=head2 setup

Sets up the app for L<CGI::Application>.

=cut

sub setup {
  my $self = shift;
  $self->error_mode( 'display_error' );
  $self->tmpl_path( $self->param('template_path') );
  $self->mode_param( 'm' );
  $self->start_mode( 'new' );
  $self->run_modes(
    new     => 'new_page',
    convert => 'output_page',
  );
  $self->header_add( -charset => 'utf-8' );
}

=head2 new_page

Corresponds to the C<new> run mode. Returns a blank form. If arguments
are available on the CGI query string, these are used as default
values for the form fields.

=cut

sub new_page {
  my $self = shift;
  my $q = $self->query;

  my $tmpl = $self->load_template( 'main.html' );
  $tmpl->param(
    $self->_default_template_params,
  );

  $tmpl->output;
}

=head2 output_page

Corresponds to the C<convert> run mode. Same as C<new_page()> but returns
the wiki markup for the provided html as well.

=cut

sub output_page {
  my $self = shift;
  my $q = $self->query;

  my $source_type = $q->param('source_type') or die "need source_type";
  my $dialect = $q->param('dialect') or die "need dialect";

  die "unknown source_type '$source_type'" unless $self->_known_source_type( $q->param('source_type') );

  my %source;
  SWITCH: {
    %source = ( html => $q->param('html') ),   last if $source_type eq 'raw_html';
    %source = ( html => $self->_sample_html ), last if $source_type eq 'sample_html';
    %source = ( uri  => $q->param('uri') ),    last if $source_type eq 'from_uri';
  };

  die sprintf( "no %s was provided", (keys %source)[0] ) unless ( (values %source)[0] );

  my $wc = new HTML::WikiConverter(
    dialect         => $dialect,
    base_uri        => $q->param('base_uri'),
    wiki_uri        => $q->param('wiki_uri'),
    escape_entities => $q->param('escape_entities'),
  );

  my $wiki = $wc->html2wiki( %source );
  my $parsed_html = $wc->parsed_html;

  $source{html} = decode( $wc->encoding, $source{html} ) if $source{html};
  $wiki         = decode( $wc->encoding, $wiki )         if $wiki;

  my $format = $q->param('format') || 'html';
  if( $format eq 'xml' ) {
    tie( my %default_template_params, 'Tie::IxHash' );
    %default_template_params = $self->_default_template_params;

    my %ignore_params = map { $_ => 1 } qw(
      dialects
      error
      raw_html
      from_uri
      sample_html
      uri
    );

    my $xml = '';
    my $writer = new XML::Writer( OUTPUT => \$xml, DATA_MODE => 1, DATA_INDENT => 2 );
    $writer->xmlDecl('utf-8');
    $writer->startTag( 'wikitool', application => 'html2wiki' );
    
    $writer->startTag( 'query' );
      $writer->startTag( 'source', type => $source_type );
      $writer->characters( (values %source)[0] );
      $writer->endTag();

      $writer->startTag( 'options' );
      while( my( $f, $v ) = each %default_template_params ) {
        next if $ignore_params{$f};
        $writer->startTag( $f );
        $writer->characters( $v );
        $writer->endTag();
      }
      $writer->endTag();
    $writer->endTag();

    $writer->startTag( 'response' );
      if( $q->param('show_parsed_html') ) {
        $writer->startTag( 'parsed_html' );
        $writer->characters( $parsed_html );
        $writer->endTag();
      }

      $writer->startTag( 'wiki_markup' );
      $writer->characters( $wiki );
      $writer->endTag();

      $writer->endTag();
    $writer->endTag();

    $self->header_add( -type => 'text/xml' );
    return $xml;
  }

  my $temp = $self->load_template( 'main.html' );
  $temp->param(
    $self->_default_template_params,
    uri         => $source{uri},
    html        => $source{html},
    parsed_html => $parsed_html,
    wiki_markup => $wiki,
  );

  return $temp->output;
}

sub _default_template_params {
  my $self = shift;
  my $q = $self->query;

  my $source_type = $self->_known_source_type( $q->param('source_type') ) ? $q->param('source_type') : 'raw_html';

  return (
    dialect => $q->param('dialect') || '',
    $source_type => 1,

    uri => $q->param('uri') || '',

    base_uri => $q->param('base_uri') || '',
    wiki_uri => $q->param('wiki_uri') || '',

    show_parsed_html => $q->param('show_parsed_html') || 0,
    escape_entities  => $q->param('escape_entities') || 0,

    dialects => $self->_dialects,
    error => '',
  );
}

my %known_source_types = map { $_ => 1 } qw(
  from_uri
  raw_html
  sample_html
);

sub _known_source_type {
  my( $self, $type ) = @_;
  return exists $known_source_types{$type};
}

sub _dialects {
  my $self = shift;
  my $q = $self->query;

  my $selected_dialect = $q->param('dialect') || '';

  my @dialects;
  my %seen;
  foreach my $dialect ( HTML::WikiConverter->available_dialects ) {
    next if $seen{$dialect}++;
    push @dialects, { dialect => $dialect, selected => ( $selected_dialect eq $dialect ) };
  }

  return \@dialects;
}

=head2 load_template

Loads the specified L<HTML::Template> template.

=cut

sub load_template {
  my( $self, $file ) = @_;
  return $self->load_tmpl( $file, die_on_bad_params => 1, loop_context_vars => 1, cache => 1 );
}

sub _sample_html {
  my $self = shift;
  return $self->load_template( 'sample_html.html' )->output;
}

=head2 display_error

Error-catching method called by L<CGI::Application> if a run mode
fails for any reason. Displays a basic form with a styled error
message up top.

=cut

sub display_error {
  my( $self, $error ) = @_;

  if( $error =~ /Dialect .* could not be loaded/ ) {
    $error = sprintf q{The "%s" dialect either doesn't exist or has not been installed.}, $self->query->param('dialect');
  }

  $error =~ s{(.*?) at \S+ line \d+\.}{$1.};
  $error = ucfirst $error;

  my $tmpl = $self->load_template( 'main.html' );
  $tmpl->param(
    $self->_default_template_params,
    error => $error
  );

  return $tmpl->output;
}

=head1 AUTHOR

David J. Iberri, C<< <diberri at cpan.org> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-html-wikiconverter
at rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-WikiConverter>.
I will be notified, and then you'll automatically be notified of
progress on your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

  perldoc HTML::WikiConverter::WebApp

You can also look for information at:

=over 4

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/HTML-WikiConverter-WebApp>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/HTML-WikiConverter-WebApp>

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTML-WikiConverter-WebApp>

=item * Search CPAN

L<http://search.cpan.org/dist/HTML-WikiConverter-WebApp>

=back

=head1 COPYRIGHT & LICENSE

Copyright (c) David J. Iberri, all rights reserved.

This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;