| 1 | package HTML::WikiConverter::DokuWikiFCKN; | ||
| 2 | |||
| 3 | # | ||
| 4 | # | ||
| 5 | # DokuWikFCK - A WikiCoverter Dialect for interfacing DokuWiki | ||
| 6 | # and the FCKEditor (http://www.fckeditor.net) | ||
| 7 | # which seeks to implement the graphic features of FCKEditor | ||
| 8 | # | ||
| 9 | # Myron Turner <turnermm02@shaw.ca> | ||
| 10 | # | ||
| 11 | # GNU General Public License Version 2 or later (the "GPL") | ||
| 12 | # http://www.gnu.org/licenses/gpl.html | ||
| 13 | # | ||
| 14 | # | ||
| 15 | # | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | |||
| 19 | use base 'HTML::WikiConverter::DokuWiki'; | ||
| 20 | use HTML::Element; | ||
| 21 | use HTML::Entities; | ||
| 22 | use Params::Validate ':types'; | ||
| 23 | |||
| 24 | |||
| 25 | our $VERSION = '0.28'; | ||
| 26 | |||
| 27 | my $SPACEBAR_NUDGING = 1; | ||
| 28 | my $color_pattern = qr/ | ||
| 29 | ([a-zA-z]+)| #colorname | ||
| 30 | (\#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}))| #colorvalue | ||
| 31 | (rgb\(([0-9]{1,3}%?,){2}[0-9]{1,3}%?\)) #rgb triplet | ||
| 32 | /x; | ||
| 33 | |||
| 34 | my $font_pattern = qr//; | ||
| 35 | my %style_patterns = ( 'color' => \$color_pattern, 'font' => \$font_pattern ); | ||
| 36 | |||
| 37 | my $nudge_char = '·'; | ||
| 38 | |||
| 39 | |||
| 40 | my $NL_marker = '~~~'; | ||
| 41 | my $EOL = '=~='; | ||
| 42 | |||
| 43 | my %_formats = ( 'b' => '**', | ||
| 44 | 'em' => '//', | ||
| 45 | 'i' => '//', | ||
| 46 | 'u' => '__', | ||
| 47 | 'ins' => '__' | ||
| 48 | ); | ||
| 49 | |||
| 50 | my %_format_regex = ( 'b' => qr/\*\*/, | ||
| 51 | 'em' => qr/\/\//, | ||
| 52 | 'i' => qr/\/\//, | ||
| 53 | 'u' => qr/__/, | ||
| 54 | 'ins' => qr/__/ | ||
| 55 | ); | ||
| 56 | |||
| 57 | |||
| 58 | sub attributes { | ||
| 59 | return { | ||
| 60 | browser => { default => 'IE5', type => SCALAR }, | ||
| 61 | group => { default => 'ANY', type => SCALAR }, | ||
| 62 | }; | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | my $kbd_start = '<font _dummy_/AmerType Md BT,American Typewriter,courier New>'; | ||
| 67 | my $kbd_end = '</font>'; | ||
| 68 | |||
| 69 | sub new { | ||
| 70 | my $class = shift; | ||
| 71 | my $self = $class->SUPER::new(@_); | ||
| 72 | $self->{'strike_out'} = 0; # this prevents deletions from being paragraphed | ||
| 73 | $self->{'list_type'} = ""; | ||
| 74 | $self->{'list_output'} = 0; # tells postprocess_output to clean up lists, if 1 | ||
| 75 | $self->{'in_table'} = 0; | ||
| 76 | $self->{'colspan'} = ""; | ||
| 77 | $self->{'code'} = 0; | ||
| 78 | $self->{'do_nudge'} = $SPACEBAR_NUDGING; | ||
| 79 | if(!$self->{'do_nudge'}) { | ||
| 80 | $nudge_char = ' '; | ||
| 81 | } | ||
| 82 | $self->{'err'} = "NOERR\n"; | ||
| 83 | $self->{'_fh'} = 0; # turn off debugging | ||
| 84 | return $self; | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | |||
| 89 | |||
| 90 | sub getFH { | ||
| 91 | my($self) = @_; | ||
| 92 | local *FH; | ||
| 93 | if(open(FH, ">> /var/tmp/fckw.log")) { | ||
| 94 | return *FH; | ||
| 95 | } | ||
| 96 | $self->{'err'} = "$! \n"; | ||
| 97 | return 0; | ||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | |||
| 102 | sub rules { | ||
| 103 | my $self = shift; | ||
| 104 | my $rules = $self->SUPER::rules(); | ||
| 105 | |||
| 106 | $rules->{ 'span' } = { replace => \&_span_contents }; | ||
| 107 | $rules->{ 'p' } = {replace => \&_p_alignment }; | ||
| 108 | $rules->{ 'div' } = {replace => \&_p_alignment }; | ||
| 109 | $rules->{ 'img' } = {replace => \&_image }; | ||
| 110 | $rules->{ 'a' } = { replace => \&_link }; | ||
| 111 | $rules->{ 'blockquote' } = { replace => \&_block }; | ||
| 112 | $rules->{ 'pre' } = { replace => \&_code_types }; | ||
| 113 | $rules->{ 'var' } = { start => '//', end => '//' }; | ||
| 114 | $rules->{ 'address' } = { start => '//', end => '//' }; | ||
| 115 | $rules->{ 'strike' } = { start => '<del>', end => '</del>' }; | ||
| 116 | $rules->{ 'cite' } = { start => '//', end => '//' }; | ||
| 117 | $rules->{ 'del' } = { alias => 'strike' }; | ||
| 118 | $rules->{ 'code' } = { replace => \&_code_types }; | ||
| 119 | $rules->{ 'kbd' } = { start => $kbd_start, end => $kbd_end }; | ||
| 120 | $rules->{ 'tt' } = { alias => 'kbd' }; | ||
| 121 | $rules->{ 'samp' } = { start => '<font _dummy_/courier New>', end => '</font>' }; | ||
| 122 | $rules->{ 'q' } = { start => '"', end => '"' }; | ||
| 123 | $rules->{ 'li' } = { replace => \&_li_start }; | ||
| 124 | $rules->{ 'ul' } = { line_format => 'multi', block => 1, line_prefix => ' ', | ||
| 125 | end => "\n<align left></align>\n" }, | ||
| 126 | $rules->{ 'ol' } = { alias => 'ul' }; | ||
| 127 | $rules->{ 'hr' } = { replace => "$NL_marker\n----${NL_marker}\n" }; | ||
| 128 | $rules->{ 'indent' } = { replace => \&_indent }; | ||
| 129 | $rules->{ 'header' } = { preserve => 1 }; | ||
| 130 | $rules->{ 'td' } = { replace => \&_td_start }; | ||
| 131 | $rules->{ 'th' } = { alias => 'td' }; | ||
| 132 | $rules->{ 'tr' } = { start => "$NL_marker\n", line_format => 'single', end => \&_row_end }; | ||
| 133 | for( 1..5 ) { | ||
| 134 | $rules->{"h$_"} = { replace => \&_header }; | ||
| 135 | } | ||
| 136 | $rules->{'plugin'} = { replace => \&_plugin}; | ||
| 137 | $rules->{ 'table' } = { start =>"<align left></align>", end => "<align left></align>" }; | ||
| 138 | |||
| 139 | $rules->{'ins'} = { alias => 'u'}; | ||
| 140 | $rules->{'b'} = { replace => \&_formats }; | ||
| 141 | $rules->{'i'} = { replace => \&_formats }; | ||
| 142 | $rules->{'u'} = { replace => \&_formats }; | ||
| 143 | return $rules; | ||
| 144 | |||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | sub _formats { | ||
| 149 | my($self, $node, $rules ) = @_; | ||
| 150 | |||
| 151 | my $text = $self->get_elem_contents($node); | ||
| 152 | |||
| 153 | $text = $self->trim($text); | ||
| 154 | return "" if ! $text; | ||
| 155 | return "" if $text !~ /[\p{IsDigit}\p{IsAlpha}\p{IsXDigit}]/; | ||
| 156 | |||
| 157 | $text =~ s/^$_format_regex{$node->tag}//; | ||
| 158 | $text =~ s/$_format_regex{$node->tag}$//; | ||
| 159 | |||
| 160 | return $_formats{$node->tag} . $text . $_formats{$node->tag}; | ||
| 161 | |||
| 162 | } | ||
| 163 | |||
| 164 | sub _plugin { | ||
| 165 | my($self, $node, $rules ) = @_; | ||
| 166 | my $text = $self->get_elem_contents($node); # text is the plugin pattern | ||
| 167 | |||
| 168 | $text = $self->trim($text); | ||
| 169 | return "" if !$text; | ||
| 170 | |||
| 171 | my $title = $node->attr('title'); | ||
| 172 | $title=$self->trim($title); | ||
| 173 | if(!$title) { | ||
| 174 | return ""; | ||
| 175 | } | ||
| 176 | |||
| 177 | $text =~ s/((<)+)/~$1~/g if $text !~ /[~]</; | ||
| 178 | $text =~ s/((>)+)/~$1~/g if $text !~ />[~]/; | ||
| 179 | |||
| 180 | return '<plugin title="' . $title . '">' . "$text</plugin>"; | ||
| 181 | } | ||
| 182 | |||
| 183 | |||
| 184 | sub _row_end { | ||
| 185 | my($self, $node, $rules ) = @_; | ||
| 186 | |||
| 187 | if($self->{'colspan'}) { | ||
| 188 | return $self->{'colspan'}; | ||
| 189 | } | ||
| 190 | $self->{'colspan'} = "" | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | sub _td_start { | ||
| 195 | my($self, $node, $rules ) = @_; | ||
| 196 | my $text = $self->get_elem_contents($node); | ||
| 197 | |||
| 198 | $text =~ s/\<br\>\s*$//m; # for Word tables pasted into editor | ||
| 199 | $text =~ s/\<br\>\s*/<br \/>/gm; | ||
| 200 | $self->{'colspan'} = ""; | ||
| 201 | my $prefix = $self->SUPER::_td_start($node, $rules); | ||
| 202 | |||
| 203 | $self->{'in_table'} = 1; | ||
| 204 | $self->{'colspan'} = $node->attr('colspan') || ""; | ||
| 205 | |||
| 206 | my $td_backcolor = ""; | ||
| 207 | my %table_header = $self->_get_type($node, ['th', 'th'], 'font'); | ||
| 208 | |||
| 209 | my $align = $node->attr('align') || 'left'; | ||
| 210 | $align =~ /^(\w)\w+/; | ||
| 211 | $align = uc($1); | ||
| 212 | |||
| 213 | |||
| 214 | |||
| 215 | if(!%table_header && $prefix !~ /\s*\^\s/) { | ||
| 216 | my $style = $node->attr('style') || ''; | ||
| 217 | if($style) { | ||
| 218 | my @styles = split ';', $style; | ||
| 219 | my $td_w; my $td_bg; my $back_color = ""; my $td_width = ""; | ||
| 220 | foreach my $s (@styles) { | ||
| 221 | if($s =~ /background-color/) { | ||
| 222 | $td_bg = $s; | ||
| 223 | } | ||
| 224 | elsif($s =~ /width/) { | ||
| 225 | $td_w = $s; | ||
| 226 | } | ||
| 227 | } | ||
| 228 | $back_color = $self->_extract_style_value($td_bg, 'background-color') if $td_bg; | ||
| 229 | |||
| 230 | $td_width = $self->_extract_style_value($td_w, 'width') if $td_w; | ||
| 231 | |||
| 232 | if($back_color || $td_width) { | ||
| 233 | $td_backcolor = " #$align" . $back_color . $td_width . '# '; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | else { | ||
| 237 | $align = "#$align#"; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | else { | ||
| 241 | $align = ""; $td_backcolor = ""; | ||
| 242 | } | ||
| 243 | |||
| 244 | |||
| 245 | |||
| 246 | if(%table_header) { | ||
| 247 | |||
| 248 | if($table_header{'th'} =~ 'th') { | ||
| 249 | $prefix = ' ^ ' ; | ||
| 250 | |||
| 251 | } | ||
| 252 | else { | ||
| 253 | $prefix = ' | ' ; | ||
| 254 | } | ||
| 255 | |||
| 256 | $text = $self->trim($text); | ||
| 257 | } | ||
| 258 | |||
| 259 | |||
| 260 | my $atts =$self->_get_basic_attributes($node); | ||
| 261 | |||
| 262 | if($atts->{'background'}) { | ||
| 263 | $text=~ s/(<indent style=['"]color:.*?['"]>)*(\s{3,})(?=.*?(<\/indent>)*)/$self->_td_indent($1,$2, $atts->{'background'})/ge; | ||
| 264 | } | ||
| 265 | else { | ||
| 266 | $text = $self->fix_td_color($atts,$text); | ||
| 267 | if(!$atts->{'background'}) { | ||
| 268 | if($text =~/<color\s+(.*?)\/(.*?)>/) { | ||
| 269 | if($2) { | ||
| 270 | $atts->{'background'} = $2; | ||
| 271 | } | ||
| 272 | elsif($1) { | ||
| 273 | $atts->{'background'} = $1; | ||
| 274 | } | ||
| 275 | } | ||
| 276 | } | ||
| 277 | if($atts->{'background'}) { | ||
| 278 | $text=~ s/(<indent style=['"]color:.*?['"]>)*(\s{3,})(?=.*?(<\/indent>)*)/$self->_td_indent($1,$2, $atts->{'background'})/ge; | ||
| 279 | } | ||
| 280 | } | ||
| 281 | |||
| 282 | |||
| 283 | my $suffix = $self->_td_end($node,$rules); | ||
| 284 | |||
| 285 | if($self->{'colspan'}) { | ||
| 286 | $self->{'colspan'} = chop $suffix; # save suffix marker for _row_end | ||
| 287 | |||
| 288 | } | ||
| 289 | |||
| 290 | $text =~ s/\n/ /gm; | ||
| 291 | $td_backcolor = $align if !$td_backcolor; | ||
| 292 | |||
| 293 | |||
| 294 | return $prefix . $td_backcolor . $text . $suffix; | ||
| 295 | |||
| 296 | } | ||
| 297 | |||
| 298 | |||
| 299 | sub _td_indent { | ||
| 300 | my ($self, $indent, $text, $color) = @_; | ||
| 301 | |||
| 302 | my $indent_tag = ""; | ||
| 303 | my $close_tag = ""; | ||
| 304 | |||
| 305 | if($color) { | ||
| 306 | $indent_tag = '<indent style="color:' . $color . ';" background-color:' . $color . ';">'; | ||
| 307 | $close_tag = '</indent>' if(!$indent); | ||
| 308 | } | ||
| 309 | |||
| 310 | |||
| 311 | return $indent_tag . $text . $close_tag; | ||
| 312 | } | ||
| 313 | |||
| 314 | sub fix_td_color { | ||
| 315 | my($self,$atts,$text) = @_; | ||
| 316 | |||
| 317 | |||
| 318 | my @colors = (); | ||
| 319 | |||
| 320 | if($text =~ /<color.*?<color/ms) { | ||
| 321 | |||
| 322 | while($text =~/<color(.*?)>/gms) { | ||
| 323 | my $color_str = $1; | ||
| 324 | $color_str =~ s/, /,/g; | ||
| 325 | my @elems = split '/', $color_str; | ||
| 326 | push @colors, {fg=>$elems[0],bg=>$elems[1]}; | ||
| 327 | } | ||
| 328 | |||
| 329 | my $fg = ""; my $bg = ""; | ||
| 330 | my $dummy_fg = ""; my $dummy_bg = ""; | ||
| 331 | my $dummy_set = 0; | ||
| 332 | foreach my $color_h(@colors) { | ||
| 333 | if($color_h->{'fg'} ne $color_h->{'bg'}) { | ||
| 334 | if ($color_h->{'fg'} =~ /_dummy_/ || $color_h->{'bg'} =~ /_dummy_/) { | ||
| 335 | if(!$dummy_set) { | ||
| 336 | $dummy_fg = $color_h->{'fg'}; | ||
| 337 | $dummy_bg = $color_h->{'bg'}; | ||
| 338 | } | ||
| 339 | } | ||
| 340 | else { | ||
| 341 | $fg = $color_h->{'fg'}; | ||
| 342 | $bg = $color_h->{'bg'}; | ||
| 343 | last; | ||
| 344 | } | ||
| 345 | } | ||
| 346 | |||
| 347 | |||
| 348 | } | ||
| 349 | |||
| 350 | if(!$fg) { | ||
| 351 | $fg = $dummy_fg ? $dummy_fg : '_dummy_'; | ||
| 352 | } | ||
| 353 | if(!$bg) { | ||
| 354 | $bg = $dummy_bg ? $dummy_bg : '_dummy_'; | ||
| 355 | } | ||
| 356 | |||
| 357 | $text=~ s/<color.*?>/ /gms; | ||
| 358 | $text=~ s/<\/color>/ /gms; | ||
| 359 | |||
| 360 | $atts->{'background'} = $bg; | ||
| 361 | |||
| 362 | return "<color $fg/$bg>$text</color>"; | ||
| 363 | |||
| 364 | } | ||
| 365 | |||
| 366 | return $text; | ||
| 367 | } | ||
| 368 | |||
| 369 | |||
| 370 | sub _extract_values { | ||
| 371 | my ($self, $attr, $values) = @_; | ||
| 372 | my $HTML_Elements = scalar @$values; | ||
| 373 | |||
| 374 | return $values->[0]->{$attr} if exists $values->[0]->{$attr}; | ||
| 375 | |||
| 376 | $HTML_Elements--; | ||
| 377 | if($HTML_Elements) { | ||
| 378 | return $values->[$HTML_Elements]->{$attr} if exists $values->[$HTML_Elements]->{$attr}; | ||
| 379 | } | ||
| 380 | |||
| 381 | return ""; | ||
| 382 | } | ||
| 383 | |||
| 384 | sub _extract_style_value { | ||
| 385 | my($self, $at, $search_term) = @_; | ||
| 386 | |||
| 387 | my($attribute, $value) = split /:/, $at; | ||
| 388 | |||
| 389 | $attribute =~ s/^\s+//; | ||
| 390 | $attribute =~ s/\s+$//; | ||
| 391 | |||
| 392 | $value =~ s/^\s+//; | ||
| 393 | $value =~ s/\s+$//; | ||
| 394 | |||
| 395 | return $value if $search_term && $attribute eq $search_term; | ||
| 396 | return 0; | ||
| 397 | } | ||
| 398 | |||
| 399 | |||
| 400 | sub _get_type { | ||
| 401 | my ($self, $node, $attrs,$type) = @_; | ||
| 402 | |||
| 403 | my $valuepat = ${$style_patterns{$type}}; | ||
| 404 | my %ret_values=(); | ||
| 405 | |||
| 406 | |||
| 407 | my @values_1 = $node->look_down($attrs->[0], $valuepat); | ||
| 408 | if(@values_1) { | ||
| 409 | my $retv = $self->_extract_values($attrs->[0],\@values_1); | ||
| 410 | if($retv) { | ||
| 411 | $ret_values{$attrs->[0]} = $retv; | ||
| 412 | } | ||
| 413 | } | ||
| 414 | |||
| 415 | |||
| 416 | my @values_2 = $node->look_down($attrs->[1], $valuepat) if scalar @$attrs == 2; | ||
| 417 | |||
| 418 | if(@values_2) { | ||
| 419 | my $retv = $self->_extract_values($attrs->[1],\@values_2); | ||
| 420 | if($retv) { | ||
| 421 | $ret_values{$attrs->[1]} = $retv; | ||
| 422 | } | ||
| 423 | } | ||
| 424 | |||
| 425 | if(!exists $ret_values{$attrs->[1]} || !exists $ret_values{$attrs->[0]}) { | ||
| 426 | my @style_values = $node->look_down('style',$font_pattern); | ||
| 427 | if(@style_values) { | ||
| 428 | my $retv = $self->_extract_values('style',\@style_values); | ||
| 429 | |||
| 430 | if(!exists $ret_values{$attrs->[0]}) { | ||
| 431 | my $attr_val =$self->_extract_style_value($retv, $attrs->[0]); | ||
| 432 | if($attr_val) { | ||
| 433 | $ret_values{$attrs->[0]} = $attr_val; | ||
| 434 | } | ||
| 435 | } | ||
| 436 | if($attrs->[1] && !exists $ret_values{$attrs->[1]}) { | ||
| 437 | my $attr_val =$self->_extract_style_value($retv, $attrs->[1]); | ||
| 438 | if($attr_val) { | ||
| 439 | $ret_values{$attrs->[1]} = $attr_val; | ||
| 440 | } | ||
| 441 | } | ||
| 442 | |||
| 443 | } | ||
| 444 | } | ||
| 445 | |||
| 446 | |||
| 447 | |||
| 448 | if(!exists $ret_values{$attrs->[0]}) { | ||
| 449 | my @values_1a = $node->look_up($attrs->[0], $valuepat); | ||
| 450 | if(@values_1a) { | ||
| 451 | my $retv = $self->_extract_values($attrs->[0],\@values_1a); | ||
| 452 | if($retv) { | ||
| 453 | $ret_values{$attrs->[0]} = $retv; | ||
| 454 | } | ||
| 455 | } | ||
| 456 | } | ||
| 457 | |||
| 458 | if($attrs->[1] && !exists $ret_values{$attrs->[1]}) { | ||
| 459 | my @values_2a = $node->look_up($attrs->[1], $valuepat); | ||
| 460 | if(@values_2a) { | ||
| 461 | my $retv = $self->_extract_values($attrs->[1],\@values_2a); | ||
| 462 | if($retv) { | ||
| 463 | $ret_values{$attrs->[1]} = $retv; | ||
| 464 | } | ||
| 465 | } | ||
| 466 | } | ||
| 467 | |||
| 468 | if(!exists $ret_values{$attrs->[0]}) { | ||
| 469 | my @values_3 = $node->attr_get_i($attrs->[0]); | ||
| 470 | foreach my $val(@values_3) { | ||
| 471 | $ret_values{$attrs->[0]} = $val; # if there is a hit, take the first one, there | ||
| 472 | last; # shouldn't be more | ||
| 473 | } | ||
| 474 | } | ||
| 475 | |||
| 476 | if($attrs->[1] && !exists $ret_values{$attrs->[1]}) { | ||
| 477 | my @values_4 = $node->attr_get_i($attrs->[1]); | ||
| 478 | foreach my $val(@values_4) { | ||
| 479 | |||
| 480 | $ret_values{$attrs->[1]} = $val; | ||
| 481 | last; # ditto to above | ||
| 482 | } | ||
| 483 | } | ||
| 484 | |||
| 485 | |||
| 486 | if(!exists $ret_values{$attrs->[1]} || !exists $ret_values{$attrs->[0]}) { | ||
| 487 | my @values_5 = $node->attr_get_i("style"); | ||
| 488 | foreach my $at(@values_5) { | ||
| 489 | |||
| 490 | if(!exists $ret_values{$attrs->[0]}) { | ||
| 491 | if($at =~ /$attrs->[0]/) { | ||
| 492 | my $attr_val =$self->_extract_style_value($at,$attrs->[0]); | ||
| 493 | if($attr_val) { | ||
| 494 | $ret_values{$attrs->[0]} = $attr_val; | ||
| 495 | last; | ||
| 496 | } | ||
| 497 | } | ||
| 498 | } | ||
| 499 | |||
| 500 | if($attrs->[1] && !exists $ret_values{$attrs->[1]}) { | ||
| 501 | if($at =~ /$attrs->[1]/) { | ||
| 502 | my $attr_val =$self->_extract_style_value($at, $attrs->[1]); | ||
| 503 | if($attr_val) { | ||
| 504 | $ret_values{$attrs->[1]} = $attr_val; | ||
| 505 | last; | ||
| 506 | } | ||
| 507 | } | ||
| 508 | } | ||
| 509 | |||
| 510 | } | ||
| 511 | } | ||
| 512 | |||
| 513 | |||
| 514 | return %ret_values; | ||
| 515 | } | ||
| 516 | |||
| 517 | |||
| 518 | |||
| 519 | |||
| 520 | sub _span_contents { | ||
| 521 | my($self, $node, $rules ) = @_; | ||
| 522 | |||
| 523 | my $text = $self->get_elem_contents($node); | ||
| 524 | my $current_text = ""; # used where more than one span occurs in the markup retrieved as $text | ||
| 525 | |||
| 526 | if($text =~ /^\s*<(color|font).*?\/(color|font)/) { | ||
| 527 | $text =~ s/<color\s+[r#].*?\/[r#].*?>\s+<\/color>//g; # remove empty color/font tags | ||
| 528 | return $text; | ||
| 529 | } | ||
| 530 | |||
| 531 | elsif($text =~ /(.*?)<(color|font).*?\/(color|font)/) { | ||
| 532 | $current_text = $1; | ||
| 533 | my $tmp = $current_text; | ||
| 534 | $tmp =~ s/([*\/\-'"{\[\]\(\)])/\\$1/gms; # escape regex pattern characters | ||
| 535 | $text =~ s/^$tmp//; | ||
| 536 | $current_text = $self->trim($current_text); | ||
| 537 | } | ||
| 538 | |||
| 539 | |||
| 540 | my %color_atts = $self->_get_type($node, ['color','background-color'], 'color'); | ||
| 541 | if(%color_atts) { | ||
| 542 | my $fg = (exists $color_atts{'color'}) ? ($color_atts{'color'}) : ""; | ||
| 543 | my $bg = (exists $color_atts{'background-color'}) ? ($color_atts{'background-color'}) : ""; | ||
| 544 | |||
| 545 | $fg = 'black' if($fg eq 'white' && !$bg); | ||
| 546 | |||
| 547 | if($fg eq $bg && $text =~ /<indent/) { | ||
| 548 | $fg = '_dummy_'; | ||
| 549 | } | ||
| 550 | if($fg eq $bg && $text =~ /<redact/) { | ||
| 551 | $fg = '_dummy_'; | ||
| 552 | } | ||
| 553 | if($current_text) { | ||
| 554 | $current_text = "<color $fg/$bg>$current_text</color>"; | ||
| 555 | } | ||
| 556 | $text = "$current_text<color $fg/$bg>$text</color>"; | ||
| 557 | } | ||
| 558 | |||
| 559 | elsif($current_text) { | ||
| 560 | |||
| 561 | } | ||
| 562 | |||
| 563 | my $pat = qr/<color\s+rgb\(\d+, \d+, \d+\)\/rgb\(\d+, \d+, \d+\)>/; | ||
| 564 | $text =~ s/($pat)\s*$pat(.*?)<\/color>/$1$2/; | ||
| 565 | |||
| 566 | |||
| 567 | my %font_atts = $self->_get_type($node, ['size', 'face'], 'font'); | ||
| 568 | if(%font_atts) { | ||
| 569 | my $size = (exists $font_atts{'size'}) ? ($font_atts{'size'}) : "_dummy_"; | ||
| 570 | my $face = (exists $font_atts{'face'}) ? ($font_atts{'face'}) : "_dummy_"; | ||
| 571 | if($current_text) { | ||
| 572 | $text = "<font $size/$face>$current_text</font><font $size/$face>$text</font>"; | ||
| 573 | } | ||
| 574 | else { | ||
| 575 | $text = "<font $size/$face>$text</font>"; | ||
| 576 | } | ||
| 577 | } | ||
| 578 | |||
| 579 | if(!%font_atts && !%color_atts && $current_text) { | ||
| 580 | $text = "$current_text$text"; | ||
| 581 | } | ||
| 582 | |||
| 583 | return $text; | ||
| 584 | } | ||
| 585 | |||
| 586 | sub clean_text { | ||
| 587 | my($self, $text) = @_; | ||
| 588 | $text =~ s/<.*?>/ /gs; | ||
| 589 | $text =~ s/\s+/ /gs; | ||
| 590 | |||
| 591 | return $text; | ||
| 592 | } | ||
| 593 | |||
| 594 | sub _code_types { | ||
| 595 | my($self, $node, $rules ) = @_; | ||
| 596 | my $text = $self->get_elem_contents($node) || ""; | ||
| 597 | |||
| 598 | $text = $self->trim($text); | ||
| 599 | $text =~ s/[\\]{2}/\n/g; # required for IE which places <br> at end of each line | ||
| 600 | $text =~ s/\n/$NL_marker\n/gms; | ||
| 601 | $text =~ s/<.*?>/ /gs; | ||
| 602 | |||
| 603 | |||
| 604 | $text =~ s/(?<![\w[:punct:]:])[\s](?![\w[:punct:]:])/x\00/gms; | ||
| 605 | return "" if ! $text; | ||
| 606 | $self->{'code'} = 1; | ||
| 607 | |||
| 608 | return "$NL_marker\n<code>$NL_marker\n $text $NL_marker\n</code>\n"; | ||
| 609 | |||
| 610 | } | ||
| 611 | |||
| 612 | |||
| 613 | |||
| 614 | |||
| 615 | sub _li_start { | ||
| 616 | my($self, $node, $rules ) = @_; | ||
| 617 | my $text = $self->get_elem_contents($node) || ""; | ||
| 618 | $text =~ s/<align\s*\w*>//gm; | ||
| 619 | $text =~ s/<\/align>//gm; | ||
| 620 | $text =~ s/^\s*//m; | ||
| 621 | $text =~ s/\n{2,}/\n/gm; | ||
| 622 | my $type = $self->SUPER::_li_start($node, $rules); | ||
| 623 | $self->{'list_output'} = 1; # signal postprocess_output to clean up lists | ||
| 624 | |||
| 625 | return "$NL_marker\n$type" . $text . $EOL; | ||
| 626 | } | ||
| 627 | |||
| 628 | sub _li__start { | ||
| 629 | my($self, $node, $rules ) = @_; | ||
| 630 | |||
| 631 | my $text = $self->get_elem_contents($node) || ""; | ||
| 632 | if($text !~ /<indent/) { | ||
| 633 | $text =~ s/([\s\x{a0}]{2,})/" <indent style='color:white'>" . $self->_space_convert($1) . "<\/indent>"/msge; | ||
| 634 | } | ||
| 635 | else { | ||
| 636 | $text =~ s/(\x{b7}|$nudge_char)([\s\x{a0}]+)/$1 . $self->_space_convert($2)/gme; | ||
| 637 | } | ||
| 638 | |||
| 639 | my $type = $self->SUPER::_li_start($node, $rules); | ||
| 640 | $self->{'list_output'} = 1; # signal postprocess_output to clean up lists | ||
| 641 | return "$NL_marker$type" . $text . $EOL; | ||
| 642 | |||
| 643 | } | ||
| 644 | |||
| 645 | sub _p_alignment { | ||
| 646 | my($self, $node, $rules ) = @_; | ||
| 647 | |||
| 648 | my $output = $self->get_elem_contents($node) || ""; | ||
| 649 | |||
| 650 | if($output =~ /^\s*[*\-]{1}\s+/gms) { | ||
| 651 | |||
| 652 | $self->{'list_output'} = 1; | ||
| 653 | return "\n $output$EOL"; | ||
| 654 | } | ||
| 655 | elsif($output =~/\<code/) { | ||
| 656 | return $output; | ||
| 657 | } | ||
| 658 | |||
| 659 | if($output =~ /<align/gms || $output =~ /<\/align/gms || $output =~ /\{\{.*?\}\}/gms) { | ||
| 660 | |||
| 661 | return $output; | ||
| 662 | } | ||
| 663 | |||
| 664 | |||
| 665 | if($node->parent) { | ||
| 666 | my $tag = $node->parent->tag(); | ||
| 667 | if($tag eq 'td') { | ||
| 668 | return $output . "<br>"; ## use <br>, not <br /> so it doesn't getlopped of in postprocess | ||
| 669 | } | ||
| 670 | } | ||
| 671 | |||
| 672 | |||
| 673 | my $newline = ""; | ||
| 674 | if($self->{'do_nudge'} && $output =~ /^\s{3,}/) { | ||
| 675 | $newline = "<align 1px></align>"; | ||
| 676 | } | ||
| 677 | if($self->{'do_nudge'}) { | ||
| 678 | $output =~ s/(?=<color\s+rgb.*?\/(rgb.*?)>)(.*?)(?=\<)/$self->_spaces_to_strikeout($2,$1)/gmse; | ||
| 679 | |||
| 680 | $output =~ s/(\x{b7}|$nudge_char)([\s\x{a0}]+)/$1 . $self->_space_convert($2)/gme; | ||
| 681 | } | ||
| 682 | if($output =~ /^(\s*|([\\][\\]))\s*<indent.*?>.*?<\/indent>\s*$/) { | ||
| 683 | return ""; | ||
| 684 | } | ||
| 685 | |||
| 686 | if($self->{'strike_out'}) { | ||
| 687 | $self->{'strike_out'} = 0; | ||
| 688 | return $output; | ||
| 689 | } | ||
| 690 | |||
| 691 | my $align = $node->{'style'} if exists $node->{'style'}; | ||
| 692 | |||
| 693 | my $align_tag = ""; | ||
| 694 | |||
| 695 | my $aligns_cnt = 0; | ||
| 696 | if($align) { # there have been some styles with multiple attributes, hence this code | ||
| 697 | my @styles = split ';', $align if($align); | ||
| 698 | foreach my $style(@styles) { | ||
| 699 | my ($att, $val) = split ':', $style; | ||
| 700 | |||
| 701 | if ($att && $val) | ||
| 702 | { | ||
| 703 | $att =~ s/^\s//; | ||
| 704 | $att =~ s/\s$//; | ||
| 705 | if($att =~ /(text\-align|margin\-left)/) { | ||
| 706 | $val =~ s/^\s//; | ||
| 707 | $val =~ s/\s$//; | ||
| 708 | $align_tag .= " <align $val>"; | ||
| 709 | $aligns_cnt++; | ||
| 710 | } | ||
| 711 | } | ||
| 712 | } | ||
| 713 | } | ||
| 714 | |||
| 715 | if(!$align_tag) { | ||
| 716 | $align_tag = "<align>"; | ||
| 717 | } | ||
| 718 | |||
| 719 | |||
| 720 | |||
| 721 | $output = "${align_tag}\n${output}\n</align>"; | ||
| 722 | |||
| 723 | $aligns_cnt--; | ||
| 724 | if($aligns_cnt) { | ||
| 725 | for(1...$aligns_cnt) { | ||
| 726 | $output .= " </align> "; | ||
| 727 | } | ||
| 728 | } | ||
| 729 | |||
| 730 | |||
| 731 | $output=~s/http\:(?!\/\/)/http:\/\//gsm; | ||
| 732 | $output =~ s/\/{3,}/\/\//g; # removes extra newline markers at start and end of images | ||
| 733 | |||
| 734 | |||
| 735 | if($output =~ s/<align left>[\x{a0}\x{b7}]+<\/align>/<align left><\/align>/gms) { | ||
| 736 | return $output; | ||
| 737 | } | ||
| 738 | |||
| 739 | $output =~ s/<align center>[\s\n\x{a0}\x{b7}]+<\/align>//gms; | ||
| 740 | $output =~ s/<align right>[\s\n\x{a0}\x{b7}]+<\/align>//gms; | ||
| 741 | $output =~ s/<align \d+px>[\s\n\x{a0}\x{b7}]+<\/align>//gms; | ||
| 742 | return "" if(!$output); | ||
| 743 | |||
| 744 | if($output =~/<align>[\s\n]*<\/align>/gms) { | ||
| 745 | $output = '<br />'; | ||
| 746 | } | ||
| 747 | return $newline . $output; | ||
| 748 | } | ||
| 749 | |||
| 750 | |||
| 751 | |||
| 752 | sub _dwimage_markup { | ||
| 753 | my ($self, $src, $align) = @_; | ||
| 754 | if($src !~ /^http:/) { | ||
| 755 | $src =~ s/\//:/g; | ||
| 756 | if($src !~ /:/) { | ||
| 757 | $src = ":$src"; | ||
| 758 | } | ||
| 759 | } | ||
| 760 | |||
| 761 | |||
| 762 | if($align eq 'center') { | ||
| 763 | return "\n<align center>\n{{$src}}\n</align>\n"; | ||
| 764 | } | ||
| 765 | if($align eq 'right') { | ||
| 766 | return "<align right>\n{{$src}} </align>"; | ||
| 767 | } | ||
| 768 | if($align eq 'left') { | ||
| 769 | return "<align left>\n{{$src}}</align>"; | ||
| 770 | } | ||
| 771 | if($align =~ /\d+px/) { | ||
| 772 | return "<align $align>\n{{$src}}\n</align>"; | ||
| 773 | } | ||
| 774 | |||
| 775 | return "{{$src}} "; | ||
| 776 | } | ||
| 777 | |||
| 778 | sub _image { | ||
| 779 | my($self, $node, $rules ) = @_; | ||
| 780 | |||
| 781 | my $src = $node->attr('src') || ''; | ||
| 782 | |||
| 783 | return "" if(!$src); | ||
| 784 | |||
| 785 | my $alignment = $self->_image_alignment($node); | ||
| 786 | if(!$alignment && $node->parent) { | ||
| 787 | $alignment = $self->_image_alignment($node->parent); | ||
| 788 | } | ||
| 789 | my $w = $node->attr('width') || 0; | ||
| 790 | my $h = $node->attr('height') || 0; | ||
| 791 | |||
| 792 | if(!$w) { | ||
| 793 | $w = $node->attr('w') || 0; | ||
| 794 | } | ||
| 795 | if(!$h) { | ||
| 796 | $h = $node->attr('h') || 0; | ||
| 797 | } | ||
| 798 | |||
| 799 | if( $w and $h ) { | ||
| 800 | $src .= "?${w}x${h}"; | ||
| 801 | } elsif( $w ) { | ||
| 802 | $src .= "?${w}"; | ||
| 803 | } | ||
| 804 | |||
| 805 | if($src =~ /editor\/images\/smiley\/msn/) { | ||
| 806 | if($src =~ /media=(http:.*?\.gif)/) { | ||
| 807 | $src = $1; | ||
| 808 | } | ||
| 809 | else { | ||
| 810 | my $HOST = $self->base_uri; | ||
| 811 | $src = 'http://' . $HOST . $src if($src !~ /$HOST/); | ||
| 812 | } | ||
| 813 | |||
| 814 | return "{{$src}} "; | ||
| 815 | } | ||
| 816 | |||
| 817 | |||
| 818 | if($src !~ /userfiles\/image/) { | ||
| 819 | my @elems = split /=/, $src; | ||
| 820 | $src = pop @elems; | ||
| 821 | return $self->_dwimage_markup($src,$alignment) if($src !~ /^http:/); | ||
| 822 | return $self->_dwimage_markup($src,$alignment); | ||
| 823 | } | ||
| 824 | |||
| 825 | |||
| 826 | if($src =~ s/^(.*?)\/userfiles\/image\///) { | ||
| 827 | return $self->_dwimage_markup($src,$alignment); | ||
| 828 | } | ||
| 829 | |||
| 830 | my $img_url = $self->SUPER::_image($node, $rules); | ||
| 831 | |||
| 832 | $img_url =~ s/%25/%/g; | ||
| 833 | $img_url =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; | ||
| 834 | $img_url =~ s/%25/%/g; | ||
| 835 | $img_url =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; | ||
| 836 | |||
| 837 | |||
| 838 | my @elems = split /media/, $img_url; | ||
| 839 | if (scalar @elems > 2) { | ||
| 840 | my $last_el = pop @elems; | ||
| 841 | my $dw_markup = $last_el; | ||
| 842 | if($dw_markup =~ s/^(.*?)userfiles\/image\///) { | ||
| 843 | return $self->_dwimage_markup($dw_markup, $alignment); | ||
| 844 | } | ||
| 845 | $img_url = $elems[0] . 'media' . $last_el; | ||
| 846 | } | ||
| 847 | |||
| 848 | |||
| 849 | return $img_url; | ||
| 850 | |||
| 851 | } | ||
| 852 | |||
| 853 | sub _image_alignment { | ||
| 854 | my ($self, $node) = @_; | ||
| 855 | if($node->parent) { | ||
| 856 | my $p = $node->parent; | ||
| 857 | my %atts = $p->all_external_attr(); | ||
| 858 | foreach my $at(keys %atts) { | ||
| 859 | if($at eq 'style') { | ||
| 860 | if($atts{$at} =~ /margin-left:\s+(\d+px)/) { | ||
| 861 | return $1; | ||
| 862 | } | ||
| 863 | elsif($atts{$at} =~ /text-align:\s+(\w+)/) { | ||
| 864 | return $1; | ||
| 865 | } | ||
| 866 | } | ||
| 867 | } | ||
| 868 | } | ||
| 869 | |||
| 870 | return ""; | ||
| 871 | } | ||
| 872 | |||
| 873 | sub _indent { | ||
| 874 | my($self, $node, $rules ) = @_; | ||
| 875 | |||
| 876 | my @list = $node->content_list(); | ||
| 877 | my $color = 'white'; | ||
| 878 | |||
| 879 | foreach my $n(@list) { | ||
| 880 | if(exists($n->{'color'})) { | ||
| 881 | $color = $n->{'color'}; | ||
| 882 | last; | ||
| 883 | } | ||
| 884 | } | ||
| 885 | |||
| 886 | my $text = $self->_as_text($node); | ||
| 887 | |||
| 888 | if($color eq 'white') { | ||
| 889 | my %color_atts = $self->_get_type($node, ['color','background-color'], 'color'); | ||
| 890 | if(%color_atts) { | ||
| 891 | my $bg = (exists $color_atts{'background-color'}) ? ($color_atts{'background-color'}) : ""; | ||
| 892 | $color=$bg, if $bg; | ||
| 893 | } | ||
| 894 | } | ||
| 895 | |||
| 896 | return "<indent style=\"color:$color; background-color:$color\">$text</indent>"; | ||
| 897 | } | ||
| 898 | |||
| 899 | sub format_InternalLink { | ||
| 900 | my($self, $node, $file) = @_; | ||
| 901 | |||
| 902 | my $inner_text = $self->get_elem_contents($node) || ""; | ||
| 903 | |||
| 904 | if($inner_text) { | ||
| 905 | return "$file|$inner_text"; | ||
| 906 | } | ||
| 907 | return $file; | ||
| 908 | } | ||
| 909 | |||
| 910 | sub _link { | ||
| 911 | my($self, $node, $rules ) = @_; | ||
| 912 | my $url = $node->attr('href') || ''; | ||
| 913 | my $internal_link = ""; | ||
| 914 | |||
| 915 | if ($url !~ /^\W*http:/ && $url =~ /\/(doku\.php\?id=)?:?((((\w)(\w|_)*)*:)*(\w(\w|_)*)*)$/) { | ||
| 916 | |||
| 917 | my $format = $self->format_InternalLink($node,$2); | ||
| 918 | $node->attr('href', $format); | ||
| 919 | $internal_link = "[[$format]]"; | ||
| 920 | } | ||
| 921 | elsif($url !~ /^\W*http:/ && $url =~ /\/(doku\.php\?id=)?:?(\w+\#[\-\.\w]+)$/) { | ||
| 922 | my $format = $self->format_InternalLink($node,$2); | ||
| 923 | $node->attr('href', $format); | ||
| 924 | $internal_link = "[[$format]]"; | ||
| 925 | |||
| 926 | } | ||
| 927 | elsif ($url =~ /^mailto:(.*)(\?.*)?/) { | ||
| 928 | return "<" . $1 . ">"; | ||
| 929 | } | ||
| 930 | elsif ($url =~ /\/lib\/exe\/fetch.php\?/) { | ||
| 931 | my $content = $self->get_elem_contents($node); | ||
| 932 | if ($content =~ /\{\{.*\}\}/) { | ||
| 933 | return $content; | ||
| 934 | } | ||
| 935 | if ($url =~ /media=(.*)(&.*)?/) { | ||
| 936 | return "{{" . $1 . "}}" if lc $1 eq lc $content; | ||
| 937 | return "{{" . $1 . "|" . $self->_as_text($node) . "}}"; | ||
| 938 | } | ||
| 939 | } | ||
| 940 | elsif ($url =~ /\/lib\/exe\/detail.php\?/) { | ||
| 941 | my $content = $self->get_elem_contents($node); | ||
| 942 | return $content; | ||
| 943 | } | ||
| 944 | |||
| 945 | my $output= $internal_link? $internal_link : $self->SUPER::_link($node, $rules); | ||
| 946 | my $text = $self->get_elem_contents($node) || ""; | ||
| 947 | my $left_alignment = ""; #actually any alignment, not just left | ||
| 948 | if($text =~ s/(\<align \d+px>)//) { | ||
| 949 | $left_alignment = $1; | ||
| 950 | $text =~ s/<\/align>//; | ||
| 951 | $output =~ s/\<align \d+px>//; | ||
| 952 | $output =~ s/<\/align>//; | ||
| 953 | } | ||
| 954 | |||
| 955 | elsif($text =~ s/(\<align \w+>)//) { | ||
| 956 | $left_alignment = $1; | ||
| 957 | $text =~ s/<\/align>//; | ||
| 958 | $output =~ s/\<align \w+>//; | ||
| 959 | $output =~ s/<\/align>//; | ||
| 960 | |||
| 961 | } | ||
| 962 | |||
| 963 | my $external_open; my $external_closed; | ||
| 964 | |||
| 965 | if($text =~ /(\<(font|color).*?\>)(.*?)(<\/\2>)/) { | ||
| 966 | $external_open = $1; | ||
| 967 | $external_closed = $4; | ||
| 968 | my $interior = $3; | ||
| 969 | $text = $3; | ||
| 970 | |||
| 971 | if($interior =~ /(\<(font|color).*?\>)(.*?)(<\/\2>)/) { | ||
| 972 | $text = $3; | ||
| 973 | $external_open = "${external_open}$1"; | ||
| 974 | $external_closed = "$4${external_closed}"; | ||
| 975 | } | ||
| 976 | |||
| 977 | } | ||
| 978 | |||
| 979 | if($text =~ /^\s*[\\]{2}s*/) { return ""; } | ||
| 980 | |||
| 981 | |||
| 982 | my $emphasis = ""; | ||
| 983 | if($text =~ /([\*\/_\"])\1/) { | ||
| 984 | $emphasis = "$1$1"; | ||
| 985 | } | ||
| 986 | |||
| 987 | if($text =~ /^(<.*?\w>).*?(<\/.*?>)$/) { | ||
| 988 | my $start = $1; | ||
| 989 | my $end = $2; | ||
| 990 | |||
| 991 | my $start_pat = $start; | ||
| 992 | my $end_pat = $end; | ||
| 993 | $start_pat =~ s/(\W)/\\$1/g; | ||
| 994 | $end_pat =~ s/(\W)/\\$1/g; | ||
| 995 | |||
| 996 | $text =~ /^$start_pat(.*?)$end_pat$/; | ||
| 997 | |||
| 998 | $text = $1; | ||
| 999 | if($text =~ /\W{2}(.*?)\W{2}/) { | ||
| 1000 | $text= $1; | ||
| 1001 | } | ||
| 1002 | $output =~ s/\|$start_pat.*?$end_pat/|$text/; | ||
| 1003 | $output = "$start${emphasis}${output}${emphasis}$end"; | ||
| 1004 | } | ||
| 1005 | elsif($emphasis) { | ||
| 1006 | my $pat =~ s/(\W)/\\$1/g; | ||
| 1007 | $output =~ s/$pat//g; | ||
| 1008 | $output = "${emphasis}${output}${emphasis}"; | ||
| 1009 | } | ||
| 1010 | |||
| 1011 | if($left_alignment) { | ||
| 1012 | $output = "$left_alignment${output}</align>"; | ||
| 1013 | } | ||
| 1014 | |||
| 1015 | if($external_open) { | ||
| 1016 | my($url, $name) = split /\|/, $output; | ||
| 1017 | |||
| 1018 | $name =~ s/<.*?>//g; | ||
| 1019 | |||
| 1020 | $output = $url . '|' . $name; | ||
| 1021 | $output = $external_open . $output . $external_closed; | ||
| 1022 | } | ||
| 1023 | |||
| 1024 | return $output; | ||
| 1025 | } | ||
| 1026 | |||
| 1027 | |||
| 1028 | sub _block { | ||
| 1029 | my($self, $node, $rules ) = @_; | ||
| 1030 | my $text = $self->get_elem_contents($node) || ""; | ||
| 1031 | |||
| 1032 | if($text =~ /<block/) { | ||
| 1033 | return $text; | ||
| 1034 | } | ||
| 1035 | |||
| 1036 | my $bg = ""; | ||
| 1037 | my $fg = ""; | ||
| 1038 | my $width = '80'; | ||
| 1039 | my $border = ""; | ||
| 1040 | my $font = ""; | ||
| 1041 | my $face = ""; | ||
| 1042 | my $size = ""; | ||
| 1043 | my $margin = "40"; | ||
| 1044 | |||
| 1045 | my $style = $node->attr('style'); | ||
| 1046 | |||
| 1047 | my @styles = split(';',$style); | ||
| 1048 | foreach my $at(@styles) { | ||
| 1049 | my $val = ""; | ||
| 1050 | if($val = $self->_extract_style_value($at,'width') ) { | ||
| 1051 | $val =~ s/\%//; | ||
| 1052 | $width = int($val); | ||
| 1053 | } | ||
| 1054 | elsif($val = $self->_extract_style_value($at,'background-color') ) { | ||
| 1055 | $bg = $val; | ||
| 1056 | } | ||
| 1057 | elsif($val = $self->_extract_style_value($at,'color') ) { | ||
| 1058 | $fg = $val; | ||
| 1059 | } | ||
| 1060 | elsif($val = $self->_extract_style_value($at,'border') ) { | ||
| 1061 | $border = "$val"; | ||
| 1062 | } | ||
| 1063 | |||
| 1064 | elsif($val = $self->_extract_style_value($at,'border-left') ) { | ||
| 1065 | $border = "$val"; | ||
| 1066 | } | ||
| 1067 | |||
| 1068 | elsif($val = $self->_extract_style_value($at,'margin-left') ) { | ||
| 1069 | if($val =~/(\d+)/) { | ||
| 1070 | $margin = $1; | ||
| 1071 | } | ||
| 1072 | } | ||
| 1073 | } | ||
| 1074 | my $basics = $self->_get_basic_attributes ($node); | ||
| 1075 | if(!$fg) { | ||
| 1076 | $fg = $basics->{'color'}; | ||
| 1077 | } | ||
| 1078 | $face = $basics->{'face'}; | ||
| 1079 | $size = $basics->{'size'}; | ||
| 1080 | if($face || $size) { | ||
| 1081 | $font = "$face/$size"; | ||
| 1082 | } | ||
| 1083 | |||
| 1084 | |||
| 1085 | $fg = 'black' if($fg eq 'white' && (!$bg || !$fg || $bg eq 'white')); | ||
| 1086 | |||
| 1087 | if(!$bg) { | ||
| 1088 | if($text =~ /<color.*?\/(.*?)>/) { | ||
| 1089 | $bg = $1; | ||
| 1090 | } | ||
| 1091 | } | ||
| 1092 | |||
| 1093 | |||
| 1094 | |||
| 1095 | my $color_regex = qr/ | ||
| 1096 | (rgb\(.*?\))|(\w+) | ||
| 1097 | /x; | ||
| 1098 | |||
| 1099 | if($text =~ s/(?<=\<indent)(\s+style=\"color:)($color_regex)(?=.*?>)/$1$bg/gms) { | ||
| 1100 | $text =~ s/(?<=background-color:)$color_regex/$bg/g; | ||
| 1101 | } | ||
| 1102 | else { | ||
| 1103 | $text =~ s/([\s\x{a0}]{2,})/$self->_spaces_to_strikeout($2,$bg)/ge; | ||
| 1104 | |||
| 1105 | } | ||
| 1106 | |||
| 1107 | my $block = "<block $width:$margin:${bg};$fg;$border;$font>"; | ||
| 1108 | $text =~ s/^\s+//; # trim | ||
| 1109 | $text =~ s/\s+$//; | ||
| 1110 | $text =~ s/\n{2,}/\n/g; # multi | ||
| 1111 | return $block . $text . '</block>'; | ||
| 1112 | |||
| 1113 | } | ||
| 1114 | |||
| 1115 | sub _spaces_to_strikeout { | ||
| 1116 | my($self, $text, $color) = @_; | ||
| 1117 | my $style=""; | ||
| 1118 | if($color) { | ||
| 1119 | $style = " style=\"color: $color\""; | ||
| 1120 | } | ||
| 1121 | return if ! $self->{'do_nudge'}; | ||
| 1122 | |||
| 1123 | $text =~ s/([\s\x{a0}]{2,})/" <indent${style}>" . $self->_space_convert($1) . "<\/indent> "/ge; | ||
| 1124 | return $text; | ||
| 1125 | } | ||
| 1126 | |||
| 1127 | sub _space_convert { | ||
| 1128 | my( $self, $spaces) = @_; | ||
| 1129 | |||
| 1130 | my $len = do { use bytes; length($spaces) }; | ||
| 1131 | return if !$len; | ||
| 1132 | |||
| 1133 | my $count = $spaces =~ s/[\s\x{a0}]/$nudge_char/gm; | ||
| 1134 | $spaces =~ s/[\s\x{a0}]//g; | ||
| 1135 | |||
| 1136 | return $spaces; | ||
| 1137 | } | ||
| 1138 | |||
| 1139 | sub _other_convert { | ||
| 1140 | my( $self, $spaces ) = @_; | ||
| 1141 | |||
| 1142 | my $count = $spaces =~ s/.{3}/$nudge_char/gms; | ||
| 1143 | |||
| 1144 | return $spaces; | ||
| 1145 | } | ||
| 1146 | |||
| 1147 | |||
| 1148 | sub postprocess_output { | ||
| 1149 | my($self, $outref ) = @_; | ||
| 1150 | $$outref =~ s/^[\s\xa0]+//; # trim | ||
| 1151 | $$outref =~ s/[\s\xa0]+$//; | ||
| 1152 | $$outref =~ s/\n{2,}/\n/g; # multi | ||
| 1153 | |||
| 1154 | $$outref =~ s/(?<=<\/align>)\s+(?=<align>)//gms; #### ???? #### | ||
| 1155 | $$outref =~ s/\s+$//gms; | ||
| 1156 | $$outref=~s/http\:(?!\/\/)/http:\/\//gsm; # replace missing forward slashes | ||
| 1157 | $$outref=~s/__(\/\/[\[\{])/$1/gsm; # remove underlining markup | ||
| 1158 | $$outref=~s/([\}\]]\/\/)__/$1/gsm; # ditto | ||
| 1159 | |||
| 1160 | |||
| 1161 | $$outref =~ s/\^<align 0px><\/align>//g; # remove aligns at top of file | ||
| 1162 | $$outref =~ s/[\s\n]*<align>[\s\n]*<\/align>[\s\n]*//gsm; # remove empty aligns | ||
| 1163 | |||
| 1164 | |||
| 1165 | if($self->{'do_nudge'}) { | ||
| 1166 | |||
| 1167 | $$outref =~ s/(<indent.*?>)(.*?)(?=\<\/indent>)/ $1 . $self->_other_convert($2)/msge; | ||
| 1168 | $$outref =~ s/(?!(${NL_marker}|${nudge_char}|\x{b7}))([\s\x{a0}]{3,})(?!${nudge_char}|[\x{b7}])/" <indent style='color:white'>" . $self->_space_convert($2) . "<\/indent> "/msge; | ||
| 1169 | $$outref =~ s/~{3,}(?!>)/\n<align left><\/align>/; | ||
| 1170 | $$outref =~ s/\|(.*?)<\/indent>\n(?=.*\|)/\|$1<\/indent>/mgs; | ||
| 1171 | } | ||
| 1172 | |||
| 1173 | |||
| 1174 | |||
| 1175 | $$outref =~ s/(?<!\w\>)(?<!$NL_marker)\n(?!\<\W\w)/\n<align left><\/align> /gms; | ||
| 1176 | |||
| 1177 | $$outref =~ s/$NL_marker/\n/gms; | ||
| 1178 | |||
| 1179 | $$outref =~ s/\n\s*(?=\|\n)//gms; | ||
| 1180 | |||
| 1181 | $$outref =~ s/^\s+//gms; # delete spaces at start of lines | ||
| 1182 | |||
| 1183 | $$outref =~ s/(<align 0px>[\n\s]*<\/align>[\n\s]*)+//gms; | ||
| 1184 | $$outref =~ s/(<align 1px>[\n\s]*<\/align>[\n\s]*)+//gms; | ||
| 1185 | |||
| 1186 | $$outref =~ s/\n[\\](2)s*/\n/gms; | ||
| 1187 | |||
| 1188 | |||
| 1189 | $$outref =~ s/(?<=<align>)[\n\s]+(?=<\/align>)//gms; | ||
| 1190 | $$outref =~ s/\n{3,}/\n/gms; | ||
| 1191 | |||
| 1192 | $$outref =~ s/<align left>[\n\s]+<\/align>[\\]{2}\s*//gms; | ||
| 1193 | |||
| 1194 | $$outref =~ s/([\s\n]*<align><\/align>[\s\n]*){2,}/<align><\/align>/gms; | ||
| 1195 | $$outref =~ s/([\s\n]*<align center><\/align>[\s\n]*){2,}/<align center><\/align>/gms; | ||
| 1196 | $$outref =~ s/([\s\n]*<align right><\/align>[\s\n]*){2,}/<align right><\/align>/gms; | ||
| 1197 | $$outref =~ s/([\s\n]*<align left><\/align>([\s\n])*){2,}/$2 ? "<align left><\/align>$2" : "<align left><\/align>"/gmse; | ||
| 1198 | |||
| 1199 | |||
| 1200 | if($self->{'list_output'}) { | ||
| 1201 | $$outref =~ s/(?<![\*\-])([\*\-])(?:\s)(.*?)($EOL[\s\n]*)/$self->_format_list($1,$2, $3)/gmse; | ||
| 1202 | } | ||
| 1203 | |||
| 1204 | $$outref =~ s/(?!\n)<align left>/\n<align left>/gms; | ||
| 1205 | |||
| 1206 | |||
| 1207 | $$outref =~ s/<font _dummy_\/courier New>\s*<\/font>//gms; | ||
| 1208 | |||
| 1209 | $$outref =~ s/([\/\{]{2})*(\s*)(?<!\[\[)\s*(http:\/\/[_\p{IsDigit}\p{IsAlpha}\p{IsXDigit};&?:.='"\/]{7,})(?!\|)/$self->_clean_url($3,$1, $2)/egms; | ||
| 1210 | |||
| 1211 | if($self->{'do_nudge'}) { | ||
| 1212 | $$outref =~ s/<indent style=[\'\"]color:white[\'\"]><\/indent>//gms; # remove blank indents | ||
| 1213 | $$outref =~ s/<indent style=['\"]color:white[\'\"]>(${nudge_char}){1,2}<\/indent>//msg; | ||
| 1214 | |||
| 1215 | $$outref =~s/(<indent style=[\'\"]color:rgb\(\d+,\s*\d+,\s*\d+\)\;?[\'\"]>)[\s\n]*<\/indent>[\s\n]*<indent\s+style=\"color:white\">(${nudge_char}|[\x{b7}\x{a0}]+<\/indent>)/$1\n$2/gms; | ||
| 1216 | $$outref =~s/(${nudge_char}|[\x{b7}\x{a0}])+<indent style=['\"]color:white[\'\"]>(${nudge_char}|[\n\s\x{b7}\x{a0}])*<\/indent>/$1$2/gms; | ||
| 1217 | |||
| 1218 | $$outref =~s/<indent style=['"]color:rgb\(.*?\)['"]><\/indent>//gms; | ||
| 1219 | } | ||
| 1220 | |||
| 1221 | |||
| 1222 | $$outref =~s/$EOL//g; | ||
| 1223 | |||
| 1224 | $$outref =~ s/<code><\/code>//gms; | ||
| 1225 | $$outref =~ s/<code>[\W]+<\/code>//gms; | ||
| 1226 | |||
| 1227 | $$outref =~ s/<align \w+>[\n\s]*(<align \w+>.*?<\/align>[\n\s]*)<\/align>/$1/gms; | ||
| 1228 | |||
| 1229 | $self->del_xtra_c_aligns($$outref); | ||
| 1230 | |||
| 1231 | $$outref =~ s/<\/align>\s*<br \/>/<\/align>/gms; | ||
| 1232 | $$outref =~ s/\n+/\n/gms; | ||
| 1233 | |||
| 1234 | |||
| 1235 | if($self->{'in_table'}) { # insure tables start with newline | ||
| 1236 | |||
| 1237 | $$outref =~ s/align>\s*(\||\^)/align>\n$1/gms; | ||
| 1238 | } | ||
| 1239 | |||
| 1240 | |||
| 1241 | $$outref =~ s/(?<=\<\/align>)(\s*[\\]{2}\s*)+//gms; | ||
| 1242 | $$outref =~ s/(?<=\<\/align>)\s*<br \/>\s*//gms; | ||
| 1243 | |||
| 1244 | |||
| 1245 | |||
| 1246 | |||
| 1247 | if($self->{'code'}) { | ||
| 1248 | $$outref =~ s/x\00/ /gms; | ||
| 1249 | $$outref =~ s/(?<=<code>)(.*?)(?=<\/code>)/$self->fix_code($1)/gmse; | ||
| 1250 | $$outref =~ s/<\/code>/<\/code><br \/>/gm; | ||
| 1251 | } | ||
| 1252 | |||
| 1253 | $$outref =~ s/<\/block>/<\/block><align left><\/align>/gm; | ||
| 1254 | $$outref .= "\n" unless $$outref =~ /\n\n$/m; | ||
| 1255 | } | ||
| 1256 | |||
| 1257 | |||
| 1258 | |||
| 1259 | sub del_xtra_c_aligns { | ||
| 1260 | my ($self, $text) = @_; | ||
| 1261 | |||
| 1262 | my @left = $text=~ /(<align)/gs; | ||
| 1263 | my @right = $text=~ /(<\/align)/gms; | ||
| 1264 | |||
| 1265 | |||
| 1266 | if(scalar @right > scalar @left) { | ||
| 1267 | my $oCount = 0; | ||
| 1268 | my $cCount = 0; | ||
| 1269 | |||
| 1270 | $text =~ s/((<align \w+>)|(<\/align>))/$self->fix_aligns($1, \$oCount, \$cCount)/egms; | ||
| 1271 | } | ||
| 1272 | } | ||
| 1273 | |||
| 1274 | |||
| 1275 | sub fix_aligns { | ||
| 1276 | my ($self, $align, $open, $close) = @_; | ||
| 1277 | $$close++ if $align =~ /<\/align/; | ||
| 1278 | $$open++ if $align =~ /<align \w+>/; | ||
| 1279 | |||
| 1280 | if ($$close > $$open) { | ||
| 1281 | $$close--; | ||
| 1282 | return "" | ||
| 1283 | } | ||
| 1284 | |||
| 1285 | return $align; | ||
| 1286 | } | ||
| 1287 | |||
| 1288 | sub _clean_url { | ||
| 1289 | my($self,$url, $markup, $spaces) = @_; | ||
| 1290 | |||
| 1291 | return $url if $url=~/editor\/images\/smiley\/msn/; | ||
| 1292 | |||
| 1293 | if($markup =~/\{\{/) { ## an external image, the first pair of brackets have been removed by regex | ||
| 1294 | |||
| 1295 | return $markup . $spaces . $url; | ||
| 1296 | } | ||
| 1297 | |||
| 1298 | my $italics=""; | ||
| 1299 | if($markup =~ /\//) { | ||
| 1300 | $italics='//';; | ||
| 1301 | } | ||
| 1302 | |||
| 1303 | $url =~ s/^[^h]+//ms; | ||
| 1304 | $url =~ s/['"\/*_]{2,}$//ms; | ||
| 1305 | |||
| 1306 | return $italics .'[[' . $url . ']]' . $italics; | ||
| 1307 | } | ||
| 1308 | |||
| 1309 | sub _format_list { | ||
| 1310 | my($self,$type, $item, $rest_of_sel) = @_; | ||
| 1311 | my $text = "${type}${item}${rest_of_sel}"; | ||
| 1312 | return $text if($text =~ /->/); | ||
| 1313 | my $prefix = ""; # any matter which precedes list | ||
| 1314 | |||
| 1315 | my $p = 0; | ||
| 1316 | pos($text) = 0; | ||
| 1317 | while($text =~ /(.*?)(?<!\-\-\-)(?=\s+[\*\-]\s+.*?$EOL)/gms) { | ||
| 1318 | $prefix .= $1; | ||
| 1319 | $p = pos($text); | ||
| 1320 | } | ||
| 1321 | pos($text) = $p; | ||
| 1322 | $text =~ /(.*?)$EOL/gms; | ||
| 1323 | $item = $self->trim($1) if $1; | ||
| 1324 | if($item eq '-' || $item eq '*') { | ||
| 1325 | $item = ""; #remove empty list items,they overlap previous line | ||
| 1326 | } | ||
| 1327 | $item =~ s/<indent\s+style=['"]color:white["']>($nudge_char)+<\/indent>\s*$//; | ||
| 1328 | $item =~ s/<align\s*\w*>\s*<\/align>\s*$//gm; | ||
| 1329 | return "$prefix\n $item"; | ||
| 1330 | |||
| 1331 | } | ||
| 1332 | |||
| 1333 | sub fix_code { | ||
| 1334 | my ($self, $text) = @_; | ||
| 1335 | $text =~s/^([\n\s])+$//m; | ||
| 1336 | $text =~s/([\n\s])+$//g; | ||
| 1337 | $text =~ s/<indent.*?>($nudge_char)*<\/indent>//gms; | ||
| 1338 | |||
| 1339 | $text =~ s/[\x{b7}\x{a0}]//gms; | ||
| 1340 | return $text; | ||
| 1341 | } | ||
| 1342 | sub trim { | ||
| 1343 | my($self,$text) = @_; | ||
| 1344 | $text =~ s/^\s+//; | ||
| 1345 | $text =~ s/\s+$//; | ||
| 1346 | return $text; | ||
| 1347 | } | ||
| 1348 | |||
| 1349 | sub log { | ||
| 1350 | my($self, $where, $data) = @_; | ||
| 1351 | my $fh = $self->{_fh}; | ||
| 1352 | $where = "" if ! $where; | ||
| 1353 | $data = "" if ! $data; | ||
| 1354 | if( $fh ) { | ||
| 1355 | print $fh "$where: $data\n"; | ||
| 1356 | } | ||
| 1357 | } | ||
| 1358 | |||
| 1359 | |||
| 1360 | |||
| 1361 | |||
| 1362 | sub _get_basic_attributes { | ||
| 1363 | my($self, $node) = @_; | ||
| 1364 | |||
| 1365 | my $fg = ''; | ||
| 1366 | my $bg = ''; | ||
| 1367 | my %color_atts = $self->_get_type($node, ['color','background-color'], 'color'); | ||
| 1368 | if(%color_atts) { | ||
| 1369 | $fg = (exists $color_atts{'color'}) ? ($color_atts{'color'}) : ""; | ||
| 1370 | $bg = (exists $color_atts{'background-color'}) ? ($color_atts{'background-color'}) : ""; | ||
| 1371 | } | ||
| 1372 | |||
| 1373 | |||
| 1374 | my $face = ''; | ||
| 1375 | my $size = ''; | ||
| 1376 | my %font_atts = $self->_get_type($node, ['size', 'face'], 'font'); | ||
| 1377 | if(%font_atts) { | ||
| 1378 | $face = (exists $font_atts{'face'}) ? ($font_atts{'face'}) : ''; | ||
| 1379 | $size = (exists $font_atts{'size'}) ? ($font_atts{'size'}) : ""; | ||
| 1380 | } | ||
| 1381 | return { 'face'=>$face, 'size'=>$size,'color'=>$fg, 'background'=>$bg }; | ||
| 1382 | } | ||
| 1383 | |||
| 1384 | sub _header { | ||
| 1385 | my($self, $node, $rules ) = @_; | ||
| 1386 | |||
| 1387 | my $text = $self->_as_text($node); | ||
| 1388 | |||
| 1389 | $node->tag =~ /(\d)/; | ||
| 1390 | |||
| 1391 | my $pre_and_post_fix = "=" x (7 - $1); | ||
| 1392 | |||
| 1393 | my $str = "\n" . "$NL_marker\n$pre_and_post_fix" . $text . "$pre_and_post_fix\n\n<align left></align>"; | ||
| 1394 | return $str; | ||
| 1395 | } | ||
| 1396 | |||
| 1397 | sub _as_text { | ||
| 1398 | my($self, $node) = @_; | ||
| 1399 | my $text = join '', map { $self->__get_text($_) } $node->content_list; | ||
| 1400 | return defined $text ? $text : ''; | ||
| 1401 | } | ||
| 1402 | |||
| 1403 | sub __get_text { | ||
| 1404 | my($self, $node) = @_; | ||
| 1405 | $node->normalize_content(); | ||
| 1406 | if( $node->tag eq '~text' ) { | ||
| 1407 | return $node->attr('text'); | ||
| 1408 | } elsif( $node->tag eq '~comment' ) { | ||
| 1409 | return '<!--' . $node->attr('text') . '-->'; | ||
| 1410 | } else { | ||
| 1411 | my $output = $self->_as_text($node)||''; | ||
| 1412 | return $output; | ||
| 1413 | } | ||
| 1414 | } | ||
| 1415 | |||
| 1416 | |||
| 1417 | |||
| 1418 | 1; | ||
| 1419 | |||
| 1420 | |||
| 1421 | |||
| 1422 |