| 1 | package Mediawiki::POD; | ||
| 2 | |||
| 3 | our $VERSION = '0.06'; | ||
| 4 | |||
| 5 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 3 | ▶ | use Pod::Simple::HTML; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use Mediawiki::POD::HTML; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | |||
| 9 | sub new | ||
| 10 | { | ||
| 11 | my $class = shift; | ||
| 12 | |||
| 13 | my $self = bless {}, $class; | ||
| 14 | |||
| 15 | # some defaults | ||
| 16 | $self->{remove_newlines} = 1; | ||
| 17 | $self->{body_only} = 1; | ||
| 18 | |||
| 19 | $self; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub remove_newlines | ||
| 23 | { | ||
| 24 | my $self = shift; | ||
| 25 | |||
| 26 | $self->{remove_newlines} = ( $_[0] ? 1 : 0 ) if @_ > 0; | ||
| 27 | $self->{remove_newlines}; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub body_only | ||
| 31 | { | ||
| 32 | my $self = shift; | ||
| 33 | |||
| 34 | $self->{body_only} = ( $_[0] ? 1 : 0 ) if @_ > 0; | ||
| 35 | $self->{body_only}; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub as_html | ||
| 39 | { | ||
| 40 | my ($self, $input) = @_; | ||
| 41 | |||
| 42 | my $parser = Mediawiki::POD::HTML->new(); | ||
| 43 | |||
| 44 | my ($html, $headlines); | ||
| 45 | |||
| 46 | $parser->output_string( \$html ); | ||
| 47 | |||
| 48 | $parser->parse_string_document( $input ); | ||
| 49 | |||
| 50 | # remove form-feeds and tabs | ||
| 51 | $html =~ s/[\f\t]+//g; | ||
| 52 | |||
| 53 | # remove comments | ||
| 54 | $html =~ s/<!--(.|\n)*?-->//g; | ||
| 55 | |||
| 56 | if ($self->{body_only}) | ||
| 57 | { | ||
| 58 | # remove the unwanted HTML sections | ||
| 59 | $html =~ s/<head>(.|\n)*<\/head>//; | ||
| 60 | |||
| 61 | $html =~ s/<\/?(html|body).*?>//g; | ||
| 62 | } | ||
| 63 | |||
| 64 | # clean up some crazy tags | ||
| 65 | # converting "<code lang='und' xml:lang='und'>" to "<code> | ||
| 66 | $html =~ s/<(pre|code|p)\s.*?>/<$1>/g; | ||
| 67 | |||
| 68 | # insert a class for <a name=""> | ||
| 69 | $html =~ s/<a name="/<a class="u" name="/g; | ||
| 70 | |||
| 71 | # make it readable again :) | ||
| 72 | $html =~ s/\'/'/g; | ||
| 73 | $html =~ s/\"/"/g; | ||
| 74 | |||
| 75 | # remove empty paragraphs before a closing </div> (for instance for X keywords) | ||
| 76 | $html =~ s/<p><\/p>\n<\/div>/<\/div>/g; | ||
| 77 | |||
| 78 | # if the last item is a keyword, we need to add a closing </div> | ||
| 79 | $html =~ s/<p><\/p>\s*\z/<\/div>/; | ||
| 80 | |||
| 81 | # make '>"foo"</a>' to '>foo</a>' | ||
| 82 | $html =~ s/class="podlinkpod"\s*>"(.*?)"<\/a>/class="podlinkpod">$1<\/a>/g; | ||
| 83 | |||
| 84 | # convert newlines between <pre> tags to <br> | ||
| 85 | # remove all new lines and tabs | ||
| 86 | $html = $self->_parse_output($html) if $self->{remove_newlines}; | ||
| 87 | |||
| 88 | $html = $self->_generate_toc( $parser->get_headlines() ) . $html; | ||
| 89 | |||
| 90 | # return the result | ||
| 91 | $html; | ||
| 92 | }; | ||
| 93 | |||
| 94 | ########################################################################### | ||
| 95 | # We need to remove all new lines, other Mediawiki will insert spurious | ||
| 96 | # linebreaks. However, inside <pre></pre> we need to replace them with | ||
| 97 | # <br> so that verbatim sections render properly. A nested regexp could | ||
| 98 | # solve this, but is not possible. So we implement a very basic parser | ||
| 99 | # that recognizes three things: <tag>, </tag>, anything else. | ||
| 100 | |||
| 101 | # This routine assumes that the <pre> tags are not nested. | ||
| 102 | |||
| 103 | sub _parse_output | ||
| 104 | { | ||
| 105 | my ($self, $input) = @_; | ||
| 106 | |||
| 107 | my $in_pre = 0; | ||
| 108 | |||
| 109 | my $qr_tag = qr/^(<\w+(.|\n)*?>)/; | ||
| 110 | my $qr_end_tag = qr/^(<\/\w+>)/; | ||
| 111 | my $qr_else = qr/^((?:.|\n)+?)(<|\z)/; | ||
| 112 | |||
| 113 | my $last_len = 1; | ||
| 114 | my $output = ''; | ||
| 115 | while (length($input) > 0) | ||
| 116 | { | ||
| 117 | $last_len = length($input); | ||
| 118 | # math the start of the input, and remove the matching part | ||
| 119 | if ($input =~ $qr_tag) | ||
| 120 | { | ||
| 121 | $input =~ s/$qr_tag//; | ||
| 122 | my $tag = $1; | ||
| 123 | $tag =~ s/[\n\r\t]/ /g; | ||
| 124 | $output .= $tag; | ||
| 125 | if ($tag =~ /^<pre.*?>/i) | ||
| 126 | { | ||
| 127 | $in_pre++; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | elsif ($input =~ $qr_end_tag) | ||
| 131 | { | ||
| 132 | $input =~ s/$qr_end_tag//; | ||
| 133 | my $tag = $1; | ||
| 134 | $tag =~ s/[\n\r\t]/ /g; | ||
| 135 | $output .= $tag; | ||
| 136 | if ($tag =~ /^<\/pre.*?>/i) | ||
| 137 | { | ||
| 138 | $in_pre--; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | else | ||
| 142 | { | ||
| 143 | $input =~ s/$qr_else/$2/; | ||
| 144 | # remove newlines | ||
| 145 | my $else = $1; | ||
| 146 | if ($in_pre > 0) | ||
| 147 | { | ||
| 148 | # also remove excessive leading whitespace | ||
| 149 | $else =~ s/[\n\r\t]\s*/<br> /g; | ||
| 150 | $else =~ s/^\s*/ /; | ||
| 151 | } | ||
| 152 | else | ||
| 153 | { | ||
| 154 | $else =~ s/[\n\r\t]/ /g; | ||
| 155 | } | ||
| 156 | $output .= $else; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | $output; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub _generate_toc | ||
| 163 | { | ||
| 164 | my ($self, $headlines) = @_; | ||
| 165 | |||
| 166 | my $toc = '<table id="toc" class="toc" summary="Contents"><tr><td><div id="toctitle"><h2>Contents</h2></div>'; | ||
| 167 | $toc .= "\n<ul>\n"; | ||
| 168 | |||
| 169 | my $level = 1; | ||
| 170 | my @cur_nr = ( 0 ); | ||
| 171 | for my $headline (@$headlines) | ||
| 172 | { | ||
| 173 | $headline =~ /^head([1-9]) (.*)/; | ||
| 174 | |||
| 175 | my $cur_level = $1; | ||
| 176 | my $txt = $2; | ||
| 177 | my $link = $txt; $link =~ s/ /_/g; $link =~ s/"<>//g; | ||
| 178 | #print STDERR "$headline $cur_level $level\n"; | ||
| 179 | |||
| 180 | # we enter a scope | ||
| 181 | if ($cur_level > $level) | ||
| 182 | { | ||
| 183 | my $levels_up = $cur_level - $level; | ||
| 184 | for (1..$levels_up) | ||
| 185 | { | ||
| 186 | push @cur_nr, 0; | ||
| 187 | $toc .= '<ul>'; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | elsif ($cur_level < $level) | ||
| 191 | { | ||
| 192 | my $levels_down = $level - $cur_level; | ||
| 193 | for (1..$levels_down) | ||
| 194 | { | ||
| 195 | pop @cur_nr; | ||
| 196 | $toc .= '</ul>'; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | $cur_nr[-1]++; | ||
| 200 | my $tnr = join ('.', @cur_nr); | ||
| 201 | $toc .= "<li class='toclevel-$cur_level'><a href=\"#$link\"><span class='tocnumber'>$tnr</span> <span class='toctext'>$txt</span></a></li>\n"; | ||
| 202 | $level = $cur_level; | ||
| 203 | } | ||
| 204 | |||
| 205 | $toc .= "</ul></td></tr></table>\n"; | ||
| 206 | $toc .= '<script type="text/javascript">' . "\n" . | ||
| 207 | 'if (window.showTocToggle) {var tocShowText="show";var tocHideText="hide";showTocToggle();}' . | ||
| 208 | "\n</script>\n"; | ||
| 209 | |||
| 210 | $toc =~ s/[\n\r\t]/ /g if $self->{remove_newlines}; | ||
| 211 | $toc; | ||
| 212 | } | ||
| 213 | |||
| 214 | 1; | ||
| 215 | |||
| 216 | __END__ | ||