Debian Codegraph @

bin/html2wiki

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/bin/perl
2
3 package main;
4 use warnings;
5 use strict;
6
7 use HTML::WikiConverter;
8
9 my %o = H::WC::GetOpts->get_opts();
10 my $wc = new HTML::WikiConverter( %o );
11
12 my $html = do { local $/; <> };
13 print $wc->html2wiki($html), "\n";
14
15 #
16 # Attribute/option handling
17 #
18
19 package H::WC::GetOpts;
20
21 use Params::Validate ':types';
22 use Getopt::Long;
23 use Pod::Usage;
24
25 sub get_opts {
26 my %attrs = known_attributes();
27 my %o = map { $_ => undef } keys %attrs;
28
29 my @optspec = (
30 \%o,
31 'list' => sub { warn "Installed dialects:\n"; print map "$_\n", HTML::WikiConverter->available_dialects; exit(1); },
32 'help' => sub { pod2usage( -exit => 1, -verbose => 0 ) },
33 'options' => sub { warn "Accepted options:\n"; print map "--$_\n", sort keys %attrs; exit(1); },
34 attrs2optspecs(\%attrs)
35 );
36
37 eval { GetOptions( @optspec ) or pod2usage(2) };
38 die "problem parsing command-line options: $@" if $@;
39
40 $o{dialect} ||= $ENV{WCDIALECT};
41 pod2usage(2) unless $o{dialect};
42
43 remove_ignored_opts(\%o);
44 return %o;
45 }
46
47 sub known_attributes {
48 my %attributes = (
49 %{ HTML::WikiConverter->__default_attribute_specs },
50 );
51
52 my @modules = map { "HTML::WikiConverter::$_" } HTML::WikiConverter->available_dialects;
53 foreach my $module ( @modules ) {
54 next unless eval "use $module; 1";
55 my %attrs = %{ $module->attributes };
56 foreach my $attr ( keys %attrs ) {
57 $attributes{$attr} = $attrs{$attr};
58 }
59 }
60
61 delete $attributes{$_} for IGNORED_ATTRS();
62
63 # Normalize attr name with dashes
64 foreach my $attr ( keys %attributes ) {
65 my $new_attr = $attr;
66 if( $new_attr =~ s/_/-/g ) {
67 $attributes{$new_attr} = $attributes{$attr};
68 delete $attributes{$attr};
69 }
70 }
71
72 return %attributes;
73 }
74
75 sub attrs2optspecs {
76 my $attrs = shift;
77 my @specs = ( );
78
79 my %forced_optspecs = FORCED_OPTSPECS();
80
81 while( my( $attr, $spec ) = each %$attrs ) {
82 my $type = $spec->{type} ? $spec->{type} : BOOLEAN;
83 next if $type == CODEREF;
84 next unless $attr;
85
86 if( my $fspec = $forced_optspecs{$attr} ) {
87 push @specs, $fspec;
88 } else {
89 my $reqopt_sym = '';
90 my $type_sym = '';
91 my $neg_sym = '';
92
93 $reqopt_sym = exists $spec->{optional} && !$spec->{optional} ? '=' : ':';
94 $type_sym = 's' if $type == SCALAR;
95 $type_sym = 's@' if $type == ARRAYREF or $type == ( SCALAR | ARRAYREF );
96 $neg_sym = '!' if $type == BOOLEAN and $spec->{default} and $spec->{default} eq '1';
97
98 # No required/optional symbol without a type to follow it
99 $reqopt_sym = '' unless $type_sym;
100
101 my $spec = join '', $attr, $reqopt_sym, $type_sym, $neg_sym;
102 push @specs, $spec;
103 }
104 }
105
106 return sort @specs;
107 }
108
109 sub remove_ignored_opts {
110 my $o = shift;
111 my %ignored_attrs = map { $_ => 1 } IGNORED_ATTRS();
112 foreach my $key ( keys %$o ) {
113 delete $o->{$key}, next if $ignored_attrs{$key};
114 delete $o->{$key}, next if !defined $o->{$key};
115 my $new_key = $key;
116 if( $new_key =~ s/-/_/g ) {
117 $o->{$new_key} = $o->{$key};
118 delete $o->{$key};
119 }
120 }
121 }
122
123 sub IGNORED_ATTRS { qw/ list help options slurp / }
124
125 # For forward compatibility until 'type' is specified for all dialect attributes
126 sub FORCED_OPTSPECS { (
127 'base-uri' => "base-uri:s",
128 'header-style' => "header-style:s",
129 'image-style' => "image-style:s",
130 'link-style' => "link-style:s",
131 'ordered-list-style' => "ordered-list-style:s",
132 'strip-tags' => "strip-tags:s\@",
133 'unordered-list-style' => "unordered-list-style:s",
134 'wiki-uri' => "wiki-uri:s\@",
135 ) }
136
137 __END__
138
139 =head1 NAME
140
141 html2wiki - convert HTML into wiki markup
142
143 =head1 SYNOPSIS
144
145 html2wiki [options] [file]
146
147 Commonly used options:
148
149 --dialect=dialect Dialect name, e.g. "MediaWiki" (required unless
150 the WCDIALECT environment variable is used)
151 --encoding=encoding Source encoding (default is 'utf-8')
152 --base-uri=uri Base URI for relative links
153 --wiki-uri=uri URI fragment for wiki links
154 --wrap-in-html Wrap input in <html> and </html> (enabled by default).
155 Use --no-wrap-in-html to disable.
156 --escape-entities Escape HTML entities within text elements (enabled by
157 default). Use --no-escape-entities to disable.
158
159 --list List installed dialects and exit
160 --options List all recognized options (except for negations
161 such as --no-wrap-in-html)
162 --help Show this message and exit
163
164 Additional options, including those corresponding to dialect
165 attributes, are also supported. Consult the html2wiki man page for
166 details.
167
168 Example:
169
170 html2wiki --dialect MediaWiki --encoding iso-8859-1 \
171 --base-uri http://en.wikipedia.org/wiki/ \
172 --wiki-uri http://en.wikipedia.org/wiki/ \
173 input.html > output.wiki
174
175 =head1 DESCRIPTION
176
177 C<html2wiki> is a command-line interface to L<HTML::WikiConverter>,
178 which it uses to convert HTML to wiki markup.
179
180 =head1 DIALECTS
181
182 If the dialect you provide in C<--dialect> is not installed on your
183 system (e.g. if you specify C<MediaWiki> but have not installed its
184 dialect module, L<HTML::WikiConverter::MediaWiki>) a fatal error will
185 be issued. Use C<html2wiki --list> to list all available dialects on
186 your system. Additional dialects may be downloaded from the CPAN.
187
188 =head1 OPTIONS
189
190 =head2 Correspondence of options and attributes
191
192 Each of the options accepted by C<html2wiki> corresponds to an
193 HTML::WikiConverter attribute. Commonly used options described in
194 C<html2wiki --help> therefore correspond to attributes discussed in
195 L<HTML::WikiConverter/ATTRIBUTES>. That section also contains other
196 attributes that may be used as C<html2wiki> command-line options.
197
198 =head2 Mapping an attribute name to an option name
199
200 While related, option names are not identical to their corresponding
201 attribute names. The only difference is that attribute names use
202 underscores to separate words while option names use hyphens. For
203 example, the C<base_uri> attribute corresponds to the C<--base-uri>
204 command-line option.
205
206 =head2 Additional options defined in dialect modules
207
208 Individual dialects may define their own attributes, and therefore
209 make available their own command-line options to C<html2wiki>, in
210 addition to the ones defined by C<HTML::WikiConverter>. The same rules
211 described above apply for converting between these attribute names and
212 their corresponding command-line option names. For example, Markdown
213 supports an C<unordered_list_style> attribute that takes a string
214 value. To use this attribute on the command line, one would use the
215 C<--unordered-list-style> option. Consult individual dialect man pages
216 for a list of supported attributes.
217
218 =head2 Options that are enabled by default
219
220 Attributes that take boolean values may be enabled by default. The
221 C<wrap_in_html> attribute is one such example. Because of this,
222 C<html2wiki> will effectively behave by default as if
223 C<--wrap-in-html> had been specified in every invokation. If this is
224 not desired, the option name may be prefixed with C<no-> to disable
225 the option, as in C<--no-wrap-in-html>.
226
227 =head2 Options that take multiple values
228
229 Some attributes (eg, C<wiki_uri> and C<strip_tags>) accept an array of
230 values. To accommodate this in C<html2wiki>, such options can be
231 specified more than once on the command line. For example, to specify
232 that only comment and script elements should be stripped from HTML:
233
234 % html2wiki --strip-tags ~comment --strip-tags script ...
235
236 =head1 INPUT/OUTPUT
237
238 Input is taken from STDIN, so you may pipe the output from another
239 program into C<html2wiki>. For example:
240
241 curl http://example.com/input.html | html2wiki --dialect MediaWiki
242
243 You may also specify a file to read HTML from:
244
245 html2wiki --dialect MediaWiki input.html
246
247 Output is sent to STDOUT, though you may redirect it on the command
248 line:
249
250 html2wiki --dialect MediaWiki input.html > output.wiki
251
252 Or you may pipe it into another program:
253
254 html2wiki --dialect MediaWiki input.html | less
255
256 =head1 AUTHOR
257
258 David J. Iberri, C<< <[email protected]> >>
259
260 =head1 COPYRIGHT & LICENSE
261
262 Copyright 2006 David J. Iberri, all rights reserved.
263
264 This program is free software; you can redistribute it and/or modify it
265 under the same terms as Perl itself.
266
267 =head1 SEE ALSO
268
269 L<HTML::WikiConverter>
270
271 =cut