Codebase list libhtml-wikiconverter-wikkawiki-perl / 7533348
[svn-inject] Installing original source of libhtml-wikiconverter-wikkawiki-perl Jonas Smedegaard 15 years ago
12 changed file(s) with 705 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 # Change log for HTML::WikiConverter::WikkaWiki
1
2 version: 0.50
3 date: Tue Jan 10 2006
4 changes:
5 - Branched from main HTML::WikiConverter codebase.
0 Changes
1 MANIFEST
2 META.yml # Will be created by "make dist"
3 Makefile.PL
4 README
5 lib/HTML/WikiConverter/WikkaWiki.pm
6 t/00-load.t
7 t/boilerplate.t
8 t/pod-coverage.t
9 t/pod.t
10 t/wikkawiki.t
11 t/runtests.pl
0 # http://module-build.sourceforge.net/META-spec.html
1 #XXXXXXX This is a prototype!!! It will change in the future!!! XXXXX#
2 name: HTML-WikiConverter-WikkaWiki
3 version: 0.50
4 version_from: lib/HTML/WikiConverter/WikkaWiki.pm
5 installdirs: site
6 requires:
7 HTML::WikiConverter: 0.5
8 Test::More: 0
9 URI: 1.35
10
11 distribution_type: module
12 generated_by: ExtUtils::MakeMaker version 6.17
0 use strict;
1 use warnings;
2 use ExtUtils::MakeMaker;
3
4 WriteMakefile(
5 NAME => 'HTML::WikiConverter::WikkaWiki',
6 AUTHOR => 'David J. Iberri <diberri@cpan.org>',
7 VERSION_FROM => 'lib/HTML/WikiConverter/WikkaWiki.pm',
8 ABSTRACT_FROM => 'lib/HTML/WikiConverter/WikkaWiki.pm',
9 PL_FILES => {},
10 PREREQ_PM => {
11 'Test::More' => 0,
12 'URI' => 1.35,
13 'HTML::WikiConverter' => 0.50,
14 },
15 dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
16 clean => { FILES => 'HTML-WikiConverter-WikkaWiki-*' },
17 );
0 HTML::WikiConverter::WikkaWiki version 0.50
1 ===========================================
2
3 HTML::WikiConverter::WikkaWiki adds the WikkaWiki dialect to
4 HTML::WikiConverter allowing the conversion of HTML to WikkaWiki
5 markup.
6
7 SYNOPSIS
8
9 Converting HTML to wiki markup is easy:
10
11 use HTML::WikiConverter;
12 my $wc = new HTML::WikiConverter( dialect => 'WikkaWiki' );
13 print $wc->html2wiki( $html );
14
15 Or from the command line:
16
17 % html2wiki --dialect WikkaWiki input.html > output.wiki
18
19 There's also a web interface if you're so inclined:
20
21 http://diberri.dyndns.org/html2wiki.html
22
23 VERSION
24
25 This is HTML::WikiConverter::WikkaWiki 0.50.
26
27 Prior versions of HTML::WikiConverter supported the WikkaWiki dialect
28 out of the box. As of 0.50, each dialect was branched off into a
29 separate CPAN package.
30
31 CHANGES IN 0.50
32
33 * WikkaWiki dialect now has its own CPAN package
34
35 DEPENDENCIES
36
37 * HTML::WikiConverter version 0.50
38
39 INSTALLATION
40
41 To install this module, run the following commands:
42
43 perl Makefile.PL
44 make
45 make test
46 make install
47
48 SUPPORT AND DOCUMENTATION
49
50 After installing, you can find documentation for this module with the
51 perldoc command.
52
53 perldoc HTML::WikiConverter::WikkaWiki
54
55 You can also look for information at:
56
57 Search CPAN
58 http://search.cpan.org/dist/HTML-WikiConverter-WikkaWiki
59
60 CPAN Request Tracker:
61 http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTML-WikiConverter-WikkaWiki
62
63 AnnoCPAN, annotated CPAN documentation:
64 http://annocpan.org/dist/HTML-WikiConverter-WikkaWiki
65
66 CPAN Ratings:
67 http://cpanratings.perl.org/d/HTML-WikiConverter-WikkaWiki
68
69 COPYRIGHT AND LICENCE
70
71 Copyright (C) 2006 David J. Iberri
72
73 This program is free software; you can redistribute it and/or modify
74 it under the same terms as Perl itself.
0 package HTML::WikiConverter::WikkaWiki;
1
2 use warnings;
3 use strict;
4
5 use base 'HTML::WikiConverter';
6
7 use URI;
8 our $VERSION = '0.50';
9
10 =head1 NAME
11
12 HTML::WikiConverter::WikkaWiki - Convert HTML to WikkaWiki markup
13
14 =head1 SYNOPSIS
15
16 use HTML::WikiConverter;
17 my $wc = new HTML::WikiConverter( dialect => 'WikkaWiki' );
18 print $wc->html2wiki( $html );
19
20 =head1 DESCRIPTION
21
22 This module contains rules for converting HTML into WikkaWiki
23 markup. See L<HTML::WikiConverter> for additional usage details.
24
25 =cut
26
27 sub rules {
28 my %rules = (
29 b => { start => '**', end => '**' },
30 strong => { alias => 'b' },
31
32 i => { start => '//', end => '//' },
33 em => { alias => 'i' },
34
35 u => { start => '__', end => '__' },
36 tt => { start => '##', end => '##' },
37 code => { alias => 'tt' },
38
39 strike => { start => '++', end => '++' },
40 kbd => { start => '#%', end => '#%' },
41 center => { start => '@@', end => '@@' },
42 br => { replace => '---' },
43
44 # table
45 tr => { line_format => 'single', start => '|| ', end => "\n" },
46 td => { end => ' || ' },
47 th => { alias => 'td' },
48
49 ul => { line_format => 'multi', block => 1 },
50 ol => { alias => 'ul' },
51 li => { line_format => 'multi', start => \&_li_start, trim => 'leading' },
52
53 img => { replace => \&_image },
54 a => { replace => \&_link },
55
56 p => { block => 1, trim => 'both', line_format => 'multi' },
57 hr => { replace => "\n----\n" },
58 );
59
60 for( 1..5 ) {
61 my $str = ( '=' ) x (7 - $_ );
62 $rules{"h$_"} = { start => "$str ", end => " $str", block => 1, trim => 'both', line_format => 'single' };
63 }
64 $rules{h6} = { alias => 'h5' };
65
66 return \%rules;
67 }
68
69 # {{image class="center" alt="DVD logo" title="An Image Link" url="images/dvdvideo.gif" link="RecentChanges"}}
70 sub _image {
71 my( $self, $node, $rules ) = @_;
72 return '' unless $node->attr('src');
73 $node->attr( src => URI->new($node->attr('src'))->rel($self->base_uri) );
74 my $attr_str = $self->get_attr_str( $node, qw/ alt title src wikka_link / );
75 return "{{image $attr_str}}";
76 }
77
78 sub _li_start {
79 my( $self, $node, $rules ) = @_;
80 my @parent_lists = $node->look_up( _tag => qr/ul|ol/ );
81 my $depth = @parent_lists;
82
83 my $bullet = $node->parent->tag eq 'ol' ? '1)' : '-';
84 my $indent = ( '~' ) x $depth;
85
86 return "\n".$indent.$bullet.' ';
87 }
88
89 sub _link {
90 my( $self, $node, $rules ) = @_;
91 my $url = $node->attr('href') || '';
92 my $text = $self->get_elem_contents($node) || '';
93
94 if( my $title = $self->get_wiki_page($url) ) {
95 $title =~ s/_/ /g;
96 return $text if lc $title eq lc $text and $self->is_camel_case($text);
97 return "[[$title $text]]";
98 } else {
99 return $url if $url eq $text;
100 return "[[$url $text]]";
101 }
102 }
103
104 sub preprocess_node {
105 my( $self, $node ) = @_;
106 # FIXME: What if img isn't the only thing under this anchor tag?
107 return unless $node->tag eq 'img' and $node->parent->tag eq 'a';
108 $node->attr( wikka_link => $node->parent->attr('href') );
109 $node->parent->replace_with_content()->delete;
110 }
111
112 =head1 AUTHOR
113
114 David J. Iberri, C<< <diberri at cpan.org> >>
115
116 =head1 BUGS
117
118 Please report any bugs or feature requests to
119 C<bug-html-wikiconverter-wikkawiki at rt.cpan.org>, or through the web
120 interface at
121 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-WikiConverter-WikkaWiki>.
122 I will be notified, and then you'll automatically be notified of
123 progress on your bug as I make changes.
124
125 =head1 SUPPORT
126
127 You can find documentation for this module with the perldoc command.
128
129 perldoc HTML::WikiConverter::WikkaWiki
130
131 You can also look for information at:
132
133 =over 4
134
135 =item * AnnoCPAN: Annotated CPAN documentation
136
137 L<http://annocpan.org/dist/HTML-WikiConverter-WikkaWiki>
138
139 =item * CPAN Ratings
140
141 L<http://cpanratings.perl.org/d/HTML-WikiConverter-WikkaWiki>
142
143 =item * RT: CPAN's request tracker
144
145 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTML-WikiConverter-WikkaWiki>
146
147 =item * Search CPAN
148
149 L<http://search.cpan.org/dist/HTML-WikiConverter-WikkaWiki>
150
151 =back
152
153 =head1 COPYRIGHT & LICENSE
154
155 Copyright 2006 David J. Iberri, all rights reserved.
156
157 This program is free software; you can redistribute it and/or modify
158 it under the same terms as Perl itself.
159
160 =cut
161
162 1;
0 #!perl -T
1
2 use Test::More tests => 1;
3
4 BEGIN {
5 use_ok( 'HTML::WikiConverter::WikkaWiki' );
6 }
7
8 diag( "Testing HTML::WikiConverter::WikkaWiki $HTML::WikiConverter::WikkaWiki::VERSION, Perl $], $^X" );
0 #!perl -T
1
2 use strict;
3 use warnings;
4 use Test::More tests => 3;
5
6 sub not_in_file_ok {
7 my ($filename, %regex) = @_;
8 open my $fh, "<", $filename
9 or die "couldn't open $filename for reading: $!";
10
11 my %violated;
12
13 while (my $line = <$fh>) {
14 while (my ($desc, $regex) = each %regex) {
15 if ($line =~ $regex) {
16 push @{$violated{$desc}||=[]}, $.;
17 }
18 }
19 }
20
21 if (%violated) {
22 fail("$filename contains boilerplate text");
23 diag "$_ appears on lines @{$violated{$_}}" for keys %violated;
24 } else {
25 pass("$filename contains no boilerplate text");
26 }
27 }
28
29 not_in_file_ok(README =>
30 "The README is used..." => qr/The README is used/,
31 "'version information here'" => qr/to provide version information/,
32 );
33
34 not_in_file_ok(Changes =>
35 "placeholder date/time" => qr(Date/time)
36 );
37
38 sub module_boilerplate_ok {
39 my ($module) = @_;
40 not_in_file_ok($module =>
41 'the great new $MODULENAME' => qr/ - The great new /,
42 'boilerplate description' => qr/Quick summary of what the module/,
43 'stub function definition' => qr/function[12]/,
44 );
45 }
46
47 module_boilerplate_ok('lib/HTML/WikiConverter/WikkaWiki.pm');
0 #!perl -T
1
2 use Test::More;
3 eval "use Test::Pod::Coverage 1.04";
4 plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" if $@;
5 all_pod_coverage_ok( { also_private => [
6 # These methods are documented in HTML::WikiConverter::Dialects
7 qr/
8 get_elem_contents
9 |get_wiki_page
10 |get_attr_str
11 |is_camel_case
12 |attributes
13 |preprocess_node
14 |postprocess_output
15 |caption2para
16 |strip_aname
17 |rules
18 /x
19 ] } );
0 #!perl -T
1
2 use Test::More;
3 eval "use Test::Pod 1.14";
4 plan skip_all => "Test::Pod 1.14 required for testing POD" if $@;
5 all_pod_files_ok();
0 #!/usr/bin/perl
1 use warnings;
2 use strict;
3
4 use Test::More;
5 use File::Spec;
6 use HTML::Entities;
7 use HTML::WikiConverter;
8 *e = \&encode_entities;
9
10 my $more_tests = <<END_TESTS;
11 __NEXT__
12 entities (1)
13 __H__
14 To enter a '&lt;' in your input, use "&amp;lt;"
15 __W__
16 To enter a '&lt;' in your input, use "&amp;lt;"
17 __NEXT__
18 entities (2)
19 __H__
20 To enter a '<' in your input, use "&amp;lt;"
21 __W__
22 To enter a '&lt;' in your input, use "&amp;lt;"
23 __NEXT__
24 strip comments
25 __H__
26 A <!-- stripped --> comment
27 __W__
28 A comment
29 __NEXT__
30 strip head
31 __H__
32 <html>
33 <head><title>fun stuff</title></head>
34 <body>
35 <p>Crazy stuff here</p>
36 </body>
37 </html>
38 __W__
39 Crazy stuff here
40 __NEXT__
41 strip scripts
42 __H__
43 <html>
44 <head><script>bogus stuff</script></head>
45 <body>
46 <script>maliciousCode()</script>
47 <p>benevolent text</p>
48 </body>
49 </html>
50 __W__
51 benevolent text
52 END_TESTS
53
54 sub runtests {
55 my %arg = @_;
56
57 $arg{strip_comments} = 1;
58 $arg{wrap_in_html} = 1;
59 $arg{base_uri} ||= 'http://www.test.com';
60 my $minimal = $arg{minimal} || 0;
61
62 my $data = $arg{data} || '';
63 $data .= entity_tests() . $more_tests unless $minimal;
64
65 my @tests = split /__NEXT__\n/, $data;
66 my $numtests = @tests;
67 #$numtests += 1 unless $minimal; # file test
68 plan tests => $numtests;
69
70 # Delete unrecognized HTML::WikiConverter options
71 delete $arg{$_} for qw/ data minimal /;
72
73 my $wc = new HTML::WikiConverter(%arg);
74 foreach my $test ( @tests ) {
75 $test =~ s/^(.*?)\n//; my $name = $1;
76 my( $html, $wiki ) = split /__W__\n/, $test;
77 $html =~ s/__H__\n//;
78
79 for( $html, $wiki ) { s/^\n+//; s/\n+$// }
80 is( $wc->html2wiki($html), $wiki, $name );
81 }
82
83 #file_test($wc) unless $minimal;
84 }
85
86 sub entity_tests {
87 my $tmpl = "__NEXT__\n%s\n__H__\n%s\n__W__\n%s\n"; # test-name, html-input, expected-wiki-output
88
89 my $data = '';
90 my @chars = ( '<', '>', '&' );
91 foreach my $char ( @chars ) {
92 ( my $charname = e($char) ) =~ s/[&;]//g;
93 $data .= sprintf $tmpl, "literal ($charname)", $char, e($char)
94 . sprintf $tmpl, "encode ($charname)", e($char), e($char)
95 . sprintf $tmpl, "meta ($charname)", e(e($char)), e(e($char));
96 }
97
98 return $data;
99 }
100
101 sub _slurp {
102 my $path = shift;
103 open H, $path or die "couldn't open $path: $!";
104 local $/;
105 my $c = <H>;
106 close H;
107 return $c;
108 }
109
110 sub file_test {
111 my $wc = shift;
112 my $lc_dialect = lc $wc->dialect;
113 my $infile = File::Spec->catfile( 't', 'complete.html' );
114 my $outfile = File::Spec->catfile( 't', "complete.$lc_dialect" );
115
116 SKIP: {
117 skip "Couldn't find $infile (ignore this)", 1 unless -e $infile;
118 skip "Couldn't find $outfile (ignore this)", 1 unless -e $outfile;
119 my( $got, $expect ) = ( $wc->html2wiki( file => $infile, slurp => 1 ), _slurp($outfile) );
120 for( $got, $expect ) { s/^\n+//; s/\n+$// }
121 is( $got, $expect, 'read from file' );
122 };
123 }
124
125 1;
0 local $/;
1 require 't/runtests.pl';
2 runtests( data => <DATA>, dialect => 'WikkaWiki' );
3 close DATA;
4
5 __DATA__
6 h1
7 __H__
8 <h1>one</h1>
9 __W__
10 ====== one ======
11 __NEXT__
12 h2
13 __H__
14 <h2>two</h2>
15 __W__
16 ===== two =====
17 __NEXT__
18 h3
19 __H__
20 <h3>three</h3>
21 __W__
22 ==== three ====
23 __NEXT__
24 h4
25 __H__
26 <h4>four</h4>
27 __W__
28 === four ===
29 __NEXT__
30 h5
31 __H__
32 <h5>five</h5>
33 __W__
34 == five ==
35 __NEXT__
36 h6
37 __H__
38 <h6>six</h6>
39 __W__
40 == six ==
41 __NEXT__
42 bold
43 __H__
44 <b>bold text</b>
45 __W__
46 **bold text**
47 __NEXT__
48 strong
49 __H__
50 <strong>strong text</strong>
51 __W__
52 **strong text**
53 __NEXT__
54 italic
55 __H__
56 <i>italic text</i>
57 __W__
58 //italic text//
59 __NEXT__
60 emphasized
61 __H__
62 <em>em text</em>
63 __W__
64 //em text//
65 __NEXT__
66 ul
67 __H__
68 <ul>
69 <li>one
70 <li>two
71 <li>three
72 </ul>
73 __W__
74 ~- one
75 ~- two
76 ~- three
77 __NEXT__
78 ul (nested)
79 __H__
80 <ul>
81 <li>one
82 <ul>
83 <li>one.one</li>
84 <li>one.two</li>
85 <li>one.three</li>
86 </ul>
87 </li>
88 <li>two
89 <ul>
90 <li>two.one</li>
91 <li>two.two</li>
92 </ul>
93 </li>
94 <li>three</li>
95 <li>four</li>
96 </ul>
97 __W__
98 ~- one
99 ~~- one.one
100 ~~- one.two
101 ~~- one.three
102 ~- two
103 ~~- two.one
104 ~~- two.two
105 ~- three
106 ~- four
107 __NEXT__
108 ol
109 __H__
110 <ol>
111 <li>one
112 <li>two
113 <li>three
114 </ol>
115 __W__
116 ~1) one
117 ~1) two
118 ~1) three
119 __NEXT__
120 ol (nested)
121 __H__
122 <ol>
123 <li>one
124 <ol>
125 <li>one.one</li>
126 <li>one.two</li>
127 <li>one.three</li>
128 </ol>
129 </li>
130 <li>two
131 <ol>
132 <li>two.one</li>
133 <li>two.two</li>
134 </ol>
135 </li>
136 <li>three</li>
137 <li>four</li>
138 </ol>
139 __W__
140 ~1) one
141 ~~1) one.one
142 ~~1) one.two
143 ~~1) one.three
144 ~1) two
145 ~~1) two.one
146 ~~1) two.two
147 ~1) three
148 ~1) four
149 __NEXT__
150 ul/ol (nested)
151 __H__
152 <ul>
153 <li>one
154 <ol>
155 <li>one.one</li>
156 <li>one.two</li>
157 <li>one.three</li>
158 </ol>
159 </li>
160 <li>two
161 <ol>
162 <li>two.one</li>
163 <li>two.two</li>
164 </ol>
165 </li>
166 <li>three</li>
167 <li>four</li>
168 </ul>
169 __W__
170 ~- one
171 ~~1) one.one
172 ~~1) one.two
173 ~~1) one.three
174 ~- two
175 ~~1) two.one
176 ~~1) two.two
177 ~- three
178 ~- four
179 __NEXT__
180 table
181 __H__
182 <table border="1" class="thingy">
183 <tr>
184 <td>one</td>
185 <td><em>two</em></td>
186 <td>three</td>
187 </tr>
188 <tr>
189 <td>four</td>
190 <td>five</td>
191 <td><b>six</b></td>
192 </tr>
193 </table>
194 __W__
195 || one || //two// || three ||
196 || four || five || **six** ||
197 __NEXT__
198 image (internal)
199 __H__
200 <img src="images/logo.png" alt="Logo" title="Our logo" />
201 __W__
202 {{image alt="Logo" title="Our logo" src="images/logo.png"}}
203 __NEXT__
204 image (external)
205 __H__
206 <img src="http://www.example.com/logo.png" alt="Logo" title="Example logo" />
207 __W__
208 {{image alt="Logo" title="Example logo" src="http://www.example.com/logo.png"}}