From 0e7ff369c7cdb573400c491ce2337d405f924633 Mon Sep 17 00:00:00 2001 From: b Date: Tue, 22 Aug 2023 22:11:37 +0000 Subject: [PATCH] core functionality of bbcode conversion now works --- post_common.1.pm | 202 ++++++++++++++++++++++++++++++----------------- preview.1.pl | 18 +++++ 2 files changed, 148 insertions(+), 72 deletions(-) diff --git a/post_common.1.pm b/post_common.1.pm index 7f529bf..e0c78dc 100644 --- a/post_common.1.pm +++ b/post_common.1.pm @@ -37,6 +37,7 @@ our @EXPORT_OK = ( ###PERL_LIB: use lib '/botm/lib/post'; use botm_common ( 'system_encoded', + 'write_data_file', 'write_postdata_file', 'html_entity_encode_dec' ); @@ -192,7 +193,7 @@ use constant BBCODE_SMILEY_FILE => { ':shock:' => 'icon_eek.gif', ':?' => 'icon_confused.gif', '8-)' => 'icon_cool.gif', - ':lol' => 'icon_lol.gif', + ':lol:' => 'icon_lol.gif', ':x' => 'icon_mad.gif', ':P' => 'icon_razz.gif', ':oops:' => 'icon_redface.gif', @@ -218,7 +219,7 @@ use constant BBCODE_SMILEY_TITLE => { ':shock:' => 'Shocked', ':?' => 'Confused', '8-)' => 'Cool', - ':lol' => 'Laughing', + ':lol:' => 'Laughing', ':x' => 'Mad', ':P' => 'Razz', ':oops:' => 'Embarassed', @@ -328,7 +329,7 @@ sub bbtree_add_text { if ($count >= 0) { my $ind_last = $ind.'.'.($count-1); if ($bbtree->{$ind_last.'.type'} eq 'text') { - $bbtree->{$ind_last.'.text'} += $text; + $bbtree->{$ind_last.'.text'} .= $text; $text = ''; } } @@ -355,9 +356,9 @@ sub bbtree_add_text { sub bbtree_close_tag { (my $bbtree, my $level, my $ind, my $text, my $print) = @_; - $bbtree->{$ind.'endtext'} = $text; + $bbtree->{$ind.'.endtext'} = $text; # mark CURRENT tag as closed - $bbtree->{$ind.'closed'} = 1; + $bbtree->{$ind.'.closed'} = 1; if ($print){ print bbtree_debug($bbtree, $ind); @@ -377,9 +378,9 @@ sub bbtree_close_tag { sub bbtree_drop_tag { (my $bbtree, my $level, my $ind, my $text, my $print) = @_; - $bbtree->{$ind.'endtext'} = $text; + $bbtree->{$ind.'.endtext'} = $text; # mark CURRENT tag as NOT closed - $bbtree->{$ind.'closed'} = 0; + $bbtree->{$ind.'.closed'} = 0; if ($print){ print bbtree_debug($bbtree, $ind); @@ -455,6 +456,7 @@ sub bbtree_start { my $level = 0; if ($print) { + # print 'GOTO ['.$ind."]\n"; print bbtree_info($bbtree, $ind); } return ($level, $ind, $sub_ind); @@ -483,13 +485,15 @@ sub bbtree_next { } my $full_ind = $ind.'.'.$sub_ind; - if ($print) { - print '['.$full_ind."]\n"; - } + # if ($print) { + # print 'GOTO ['.$full_ind."]\n"; + # } + # next element is a tag; enter it - if ($bbtree->{'.type'} eq 'tag') { + if ($bbtree->{$full_ind.'.type'} eq 'tag') { $ind = $full_ind; $sub_ind = ''; + $level += 1; } if ($print) { @@ -499,6 +503,16 @@ sub bbtree_next { return ($level, $ind, $sub_ind); } +sub bbtree_finish { + (my $bbtree, my $print) = @_; + my $level; + my $ind; + my $sub_ind; + + ($level, $ind, $sub_ind) = bbtree_start($bbtree, 0); + ($level, $ind) = bbtree_close_tag($bbtree, $level, $ind, '', $print); +} + # full info about bbtree element sub bbtree_info { (my $bbtree, my $ind) = @_; @@ -681,6 +695,21 @@ sub bbcode_to_html { next; } + # additional reasons for rejecting tag + if ( + # no [/*] not in [list] or [*] + (($tag_name eq '*') and ($bbtree{$ind.'.name'} !~ /^((list)|\*)$/)) or + # no tags in [img] + (($tag_name ne 'img') and ($bbtree{$ind.'.name'} eq 'img')) + ) { + if ($print) { + print "this tag forbidden here\n"; + } + bbtree_add_text(\%bbtree, $level, $ind, $tag, $print); + # skip + next; + } + # unclosed [*] before [/list] # add implicit [/*] if (($tag_name eq 'list' ) and ($bbtree{$ind.'.name'} eq '*')) { @@ -709,27 +738,37 @@ sub bbcode_to_html { } # add bad tag to BB tree as text and close current tag ($level, $ind) = bbtree_drop_tag(\%bbtree, $level, $ind, $tag, $print); + #skip + next; } # close current tag ($level, $ind) = bbtree_close_tag(\%bbtree, $level, $ind, $tag, $print); } } + bbtree_finish(\%bbtree, $print); $in_code = 0; if ($print) { + print "BBTREE:\n"; + write_data_file(\*STDOUT, 'console_out', 0, \%bbtree); print "\nGenerate HTML from BBcode tree:\n"; } ($level, $ind, $sub_ind) = bbtree_start(\%bbtree, $print); while ($level >= 0) { + # if ($print) { + # print "[$level][$ind][$sub_ind]\n"; + # } + $tag = $bbtree{$ind.'.text'}; $tag_end = $bbtree{$ind.'.endtext'}; $tag_name = $bbtree{$ind.'.name'}; $tag_value = $bbtree{$ind.'.value'}; $tag_count = $bbtree{$ind.'.count'}; $tag_closed = $bbtree{$ind.'.closed'}; + my $add_html = ''; # tag start if ($sub_ind eq '') { @@ -738,13 +777,17 @@ sub bbcode_to_html { if ($print) { print "unmatched tag\n"; } - $html .= html_entity_encode_dec($tag); + $add_html .= html_entity_encode_dec($tag); + } + + elsif ($tag_name eq '_') { + # post start here } elsif ($tag_name eq 'quote') { if ($tag_value eq '') { - $html .= '
'; - #$html .= '
'; + $add_html .= '
'; + #$add_html .= '
'; } else { if ($tag_value =~ m/^"(.*)"$/gs) { @@ -753,29 +796,29 @@ sub bbcode_to_html { else { $tag_value = 'QUOTE MARKS MISSING; '.html_entity_encode_dec($tag_value) } - $html .= '
'.html_entity_encode_dec($tag_value).' wrote:'; - # $html .= '
'.tagvalue.' wrote:
'; + $add_html .= '
'.$tag_value.' wrote:'; + # $add_html .= '
'.$tag_value.' wrote:
'; } } elsif ($tag_name eq 'b') { - $html .= ''; + $add_html .= ''; if ($tag_value ne '') { - $html .= 'INVALID VALUE; '; + $add_html .= 'INVALID VALUE; '; } } elsif ($tag_name eq 'i') { - $html .= ''; + $add_html .= ''; if ($tag_value ne '') { - $html .= 'INVALID VALUE; '; + $add_html .= 'INVALID VALUE; '; } } elsif ($tag_name eq 'u') { - $html .= ''; + $add_html .= ''; if ($tag_value ne '') { - $html .= 'INVALID VALUE; '; + $add_html .= 'INVALID VALUE; '; } } @@ -784,23 +827,20 @@ sub bbcode_to_html { if ($print) { print "not a tag, actually\n"; } - $html .= html_entity_encode_dec($tag); + $add_html .= html_entity_encode_dec($tag); } else { - $html .= '
Code: Select all
'; - # $html .= '
Code: Select all
'; + $add_html .= '
Code: Select all
'; + # $add_html .= '
Code: Select all
'; } if ($tag_value ne '') { - $html .= 'INVALID VALUE; '; + $add_html .= 'INVALID VALUE; '; } $in_code += 1; } elsif ($tag_name eq 'img') { - if ($tag_value ne '') { - $html .= 'INVALID VALUE; '; - } - $html .= ''; + $add_html .= ''; } elsif ($tag_name eq 'size') { if (($tag_value =~ m/^([0-9]+)$/gs) and (int($tag_value) <= 200)) { - $html .= ''; + $add_html .= ''; } else { - $html .= ' INVALID VALUE; '; + $add_html .= ' INVALID VALUE; '; } } elsif ($tag_name eq 'color') { if ($tag_value =~ m/^(#[0-9A-F]{6})$/gs) { - $html .= ''; + $add_html .= ''; } else { - $html .= ' INVALID VALUE; '; + $add_html .= ' INVALID VALUE; '; } } elsif ($tag_name eq 'center') { - $html .= '
'; + $add_html .= '
'; if ($tag_value ne '') { - $html .= 'INVALID VALUE; '; + $add_html .= 'INVALID VALUE; '; } } elsif ($tag_name eq 'right') { - $html .= '
'; + $add_html .= '
'; if ($tag_value ne '') { - $html .= 'INVALID VALUE; '; + $add_html .= 'INVALID VALUE; '; } } elsif ($tag_name eq 's') { - $html .= ''; + $add_html .= ''; if ($tag_value ne '') { - $html .= 'INVALID VALUE; '; + $add_html .= 'INVALID VALUE; '; } } elsif ($tag_name eq 'sub') { - $html .= ''; + $add_html .= ''; if ($tag_value ne '') { - $html .= 'INVALID VALUE; '; + $add_html .= 'INVALID VALUE; '; } } elsif ($tag_name eq 'sup') { - $html .= ''; + $add_html .= ''; if ($tag_value ne '') { - $html .= 'INVALID VALUE; '; + $add_html .= 'INVALID VALUE; '; } } elsif ($tag_name eq 'spoiler') { - $html .= '
Spoiler:
'; - # $html .= '
Spoiler:
'; + $add_html .= '
Spoiler:
'; + # $add_html .= '
Spoiler:
'; if ($tag_value ne '') { - $html .= 'INVALID VALUE; '; + $add_html .= 'INVALID VALUE; '; } } elsif ($tag_name eq 'list') { if ($tag_value eq '') { - $html .= '
    '; + $add_html .= '
      '; } else { if ($tag_value eq '1') { @@ -895,17 +935,17 @@ sub bbcode_to_html { $tag_value = 'upper-alpha'; } else { - $html .= 'INVALID VALUE; '; + $add_html .= 'INVALID VALUE; '; $tag_value = 'decimal'; } - $html .= '
        '; + $add_html .= '
          '; } } elsif ($tag_name eq '*') { - $html .= '
        1. '; + $add_html .= '
        2. '; if ($tag_value ne '') { - $html .= 'INVALID VALUE; '; + $add_html .= 'INVALID VALUE; '; } } @@ -913,7 +953,7 @@ sub bbcode_to_html { if ($print) { print "unknown tag\n" } - $html .= html_entity_encode_dec($tag); + $add_html .= html_entity_encode_dec($tag); } } @@ -924,15 +964,19 @@ sub bbcode_to_html { if ($print) { print "unmatched tag\n"; } - $html .= html_entity_encode_dec($tag_end); + $add_html .= html_entity_encode_dec($tag_end); + } + + elsif ($tag_name eq '_') { + # post end here } elsif ($tag_name eq 'quote') { - $html .= '
'; + $add_html .= '
'; } elsif ($tag_name =~ m/^(b|i|u|(size)|(color))$/gs) { - $html .= ''; + $add_html .= ''; } elsif ($tag_name eq 'code') { @@ -941,55 +985,64 @@ sub bbcode_to_html { if ($print) { print "not a tag, actually\n"; } - $html .= html_entity_encode_dec($tag_end); + $add_html .= html_entity_encode_dec($tag_end); } else { - $html .= ''; + $add_html .= ''; } } + elsif ($tag_name eq 'img') { + $add_html .= '" alt="Image" />'; + # $add_html .= '" alt="Image">'; + if ($tag_value ne '') { + $add_html .= 'INVALID VALUE; '; + } + + } + elsif ($tag_name eq 'url') { - $html .= ''; + $add_html .= ''; } elsif ($tag_name =~ /^((center)|(right))$/) { - $html .= '
'; + $add_html .= '
'; } elsif ($tag_name eq 's') { - $html .= ''; + $add_html .= ''; } elsif ($tag_name eq 'sub') { - $html .= ''; + $add_html .= ''; } elsif ($tag_name eq 'sup') { - $html .= ''; + $add_html .= ''; } elsif ($tag_name eq 'spoiler') { - $html .= '
'; + $add_html .= '
'; } elsif ($tag_name eq 'list') { if ($tag_value eq '') { - $html .= ''; + $add_html .= ''; } else { - $html .= ''; + $add_html .= ''; } } elsif ($tag_name eq '*') { - $html .= ''; + $add_html .= ''; } else { if ($print) { print "unknown tag\n"; } - $html .= html_entity_encode_dec($tag_end); + $add_html .= html_entity_encode_dec($tag_end); } } @@ -1024,16 +1077,21 @@ sub bbcode_to_html { # convert smilies if (($post->{'smilies'}) and ($tag_name !~ /^((code)|(img))$/)) { - $text =~ s/(^|[ \t])((:([D\(\)o\?xP\|]|((shock)|(lol)|(oops)|(cry)|(evil)|(twisted)|(roll)|(wink)|(idea)|(arrow)|(mrgreen)|[!\?]:)))|(8-\))|(;\)))($|[ \t])/' '/ge; - # $text =~ s/(^|[ \t])((:([D\(\)o\?xP\|]|((shock)|(lol)|(oops)|(cry)|(evil)|(twisted)|(roll)|(wink)|(idea)|(arrow)|(mrgreen)|[!\?]:)))|(8-\))|(;\)))($|[ \t])/' '/ge; + $text =~ s/(^|[ \t])((:([D\(\)o\?xP\|]|(((shock)|(lol)|(oops)|(cry)|(evil)|(twisted)|(roll)|(wink)|(idea)|(arrow)|(mrgreen)|[!\?]):)))|(8-\))|(;\)))($|[ \t])/' '.$2.' '/gme; + # $text =~ s/(^|[ \t])((:([D\(\)o\?xP\|]|(((shock)|(lol)|(oops)|(cry)|(evil)|(twisted)|(roll)|(wink)|(idea)|(arrow)|(mrgreen)|[!\?]):)))|(8-\))|(;\)))($|[ \t])/' '.$2.' '/gme; } # convert NL $text =~ s/\r?\n/\n/gs; # $text =~ s/\r?\n/
\n/gs; - $html .= $text; + $add_html .= $text; + } + + if ($print) { + print "HTML=$add_html\n"; } + $html .= $add_html; ($level, $ind, $sub_ind) = bbtree_next(\%bbtree, $level, $ind, $sub_ind, $print); } diff --git a/preview.1.pl b/preview.1.pl index 8a0b0bc..e1489aa 100644 --- a/preview.1.pl +++ b/preview.1.pl @@ -141,6 +141,20 @@ if ((scalar @ARGV) == 0) { @ARGV = (\*STDIN); } +my $fh; +if ($options{'output-file'} ne '') { + unless (open ($fh, '>:encoding(UTF-8)', encode('locale_fs', $options{'output-file'}))) { + unless ($options{'quiet'} ne '') { + print 'FAIL open '.$options{'output-file'}."\n"; + } + print STDERR 'Failed to open/create file '.$options{'output-file'}.".\n"; + return CODE->{'POST_FAILED'}; + } +} +else { + $fh = \*STDOUT; +} + my $code = CODE->{'OK'}; foreach my $arg (@ARGV) { my $r; @@ -193,6 +207,10 @@ foreach my $arg (@ARGV) { print $html."\n"; print "\n"; } + + print $fh $html."\n"; } +close ($fh); + exit $code; -- 2.30.2