From 154631ce96516148ba5dbeaa8fdb6739c2d84bbc Mon Sep 17 00:00:00 2001 From: b Date: Sun, 20 Aug 2023 20:33:08 +0000 Subject: [PATCH] preview tool now works but wrong --- post_common.1.pm | 126 +++++++++++++++++++++++++++++++++++++++++------ preview.1.pl | 10 +++- 2 files changed, 121 insertions(+), 15 deletions(-) diff --git a/post_common.1.pm b/post_common.1.pm index 2db88fb..7f529bf 100644 --- a/post_common.1.pm +++ b/post_common.1.pm @@ -31,6 +31,7 @@ our @EXPORT_OK = ( 'prepare_post', 'wget', 'CODE', + 'bbcode_to_html' ); ###PERL_LIB: use lib '/botm/lib/post'; @@ -183,6 +184,58 @@ sub prepare_post { # BBCODE # ########## +use constant BBCODE_SMILEY_FILE => { + ':D' => 'icon_biggrin.gif', + ':)' => 'icon_smile.gif', + ':(' => 'icon_sad.gif', + ':o' => 'icon_surprised.gif', + ':shock:' => 'icon_eek.gif', + ':?' => 'icon_confused.gif', + '8-)' => 'icon_cool.gif', + ':lol' => 'icon_lol.gif', + ':x' => 'icon_mad.gif', + ':P' => 'icon_razz.gif', + ':oops:' => 'icon_redface.gif', + ':cry:' => 'icon_cry.gif', + ':evil:' => 'icon_evil.gif', + ':twisted:'=> 'icon_twisted.gif', + ':roll:' => 'icon_rolleyes.gif', + ':wink:' => 'icon_wink.gif', + ';)' => 'icon_wink.gif', + ':!:' => 'icon_exclaim.gif', + ':?:' => 'icon_question.gif', + ':idea:' => 'icon_idea.gif', + ':arrow:' => 'icon_arrow.gif', + ':|' => 'icon_neutral.gif', + ':mrgreen:'=> 'icon_mrgreen.gif' +}; + +use constant BBCODE_SMILEY_TITLE => { + ':D' => 'Very Happy', + ':)' => 'Smile', + ':(' => 'Sad', + ':o' => 'Surprised', + ':shock:' => 'Shocked', + ':?' => 'Confused', + '8-)' => 'Cool', + ':lol' => 'Laughing', + ':x' => 'Mad', + ':P' => 'Razz', + ':oops:' => 'Embarassed', + ':cry:' => 'Crying or Very Sad', + ':evil:' => 'Evil or Very Mad', + ':twisted:'=> 'Twisted Evil', + ':roll:' => 'Rolling Eyes', + ':wink:' => 'Wink', + ';)' => 'Wink', + ':!:' => 'Exclamation', + ':?:' => 'Question', + ':idea:' => 'Idea', + ':arrow:' => 'Arrow', + ':|' => 'Neutral', + ':mrgreen:'=> 'Mr. Green' +}; + # bbtree is the representation of a BBcode tag tree as a Perl hash # each element can be a tag or text. # text only has value @@ -256,7 +309,7 @@ sub bbtree_add_tag { $bbtree->{$new_ind.'.endtext'} = ''; if ($print) { - print bbtree_debug(%bbtree, $new_ind); + print bbtree_debug($bbtree, $new_ind); } # enter NEW tag @@ -293,7 +346,7 @@ sub bbtree_add_text { } if ($print){ - print bbtree_debug(%bbtree, $ind); + print bbtree_debug($bbtree, $ind); } return ($level, $ind); } @@ -307,7 +360,7 @@ sub bbtree_close_tag { $bbtree->{$ind.'closed'} = 1; if ($print){ - print bbtree_debug(%bbtree, $ind); + print bbtree_debug($bbtree, $ind); } # return to PARENT tag @@ -329,7 +382,7 @@ sub bbtree_drop_tag { $bbtree->{$ind.'closed'} = 0; if ($print){ - print bbtree_debug(%bbtree, $ind); + print bbtree_debug($bbtree, $ind); } # return to PARENT tag @@ -381,7 +434,7 @@ sub bbtree_debug { # element is text, just print elsif ($bbtree->{$ind.'.type'} eq 'text') { - $debug .= 'text: '.$bbtree->{$ind.'.value'}; + $debug .= 'text: '.$bbtree->{$ind.'.text'}; } # unknown type, no idea what to print @@ -492,13 +545,13 @@ sub bbcode_to_html { my $in_code = 0; # if (and how deep) in [code] tag my $in_spoiler = 0; # if (and how deep) in [spoiler] tag my $in_size = 0; # if (and how deep) in [size] tag - my $print = $cmd_options->{'verbose'} ne '' # allowed to print + my $print = ($cmd_options->{'verbose'} ne ''); # allowed to print # initialise BB tree with top level if ($print) { print "Build BBcode tree:\n"; } - ($ind, $level) = bbtree_init(\%bbtree, $print); + ($level, $ind) = bbtree_init(\%bbtree, $print); while ($bbcode ne '') { @@ -555,7 +608,8 @@ sub bbcode_to_html { next; } - # opening tag if ($tag_end eq '') { + # opening tag + if ($tag_end eq '') { # additional reasons for rejecting tag if ( @@ -634,7 +688,7 @@ sub bbcode_to_html { print "implicit [/*]\n"; } # add [/*] to BB tree - ($level, $ind) = bbtree_close_tag(\%bbtree, $level, $ind, '[/*]' $print); + ($level, $ind) = bbtree_close_tag(\%bbtree, $level, $ind, '[/*]', $print); } # handle special case tags @@ -699,7 +753,7 @@ sub bbcode_to_html { else { $tag_value = 'QUOTE MARKS MISSING; '.html_entity_encode_dec($tag_value) } - $html .= '
'.tagvalue.' wrote:'; + $html .= '
'.html_entity_encode_dec($tag_value).' wrote:'; # $html .= '
'.tagvalue.' wrote:
'; } } @@ -813,7 +867,7 @@ sub bbcode_to_html { } elsif ($tag_name eq 'spoiler') { - $html .= '
Spoiler:
'; + $html .= '
Spoiler:
'; # $html .= '
Spoiler:
'; if ($tag_value ne '') { $html .= 'INVALID VALUE; '; @@ -844,7 +898,7 @@ sub bbcode_to_html { $html .= 'INVALID VALUE; '; $tag_value = 'decimal'; } - $html .= '
    '; + $html .= '
      '; } } @@ -941,14 +995,58 @@ sub bbcode_to_html { # text else { - # TODO + $text = $bbtree{$ind.'.'.$sub_ind.'.text'}; + + # This here is kind of a hack. + # normally I should first detect URLs and smilies + # and convert them to HTML tags. + # And then do escapng of special characters in the parts + # of the text which were not converted. + # Instead I do it in reverse order. + # First I do the escaping and afterwards I'm detecting. + # This will not be messed up because the only escaped characters + # are "&<=> and none of these can be foung in existing smilies + # and this will not interfere with detecting the start and end + # of an URL which needs to be escaped anyway. + + $text = html_entity_encode_dec($text); + + # preserve indentation in [code] + if ($in_code > 0) { + $text =~ s/ / /gs; + $text =~ s/\t/  /gs; + } + + # convert URLs + if (($post->{'urls'}) and ($tag_name !~ /^((code)|(img)|(url))$/)) { + $text =~ s/https?:\/\/[^\n\r\t<> ]+/$&<\/a>/gs; + } + + # 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; + } + + # convert NL + $text =~ s/\r?\n/\n/gs; + # $text =~ s/\r?\n/
      \n/gs; + + $html .= $text; } - ($level, $ind, $sub_ind) = bbtree_next(\%bbtree, $level, $ind, $sub_ind); + ($level, $ind, $sub_ind) = bbtree_next(\%bbtree, $level, $ind, $sub_ind, $print); } + if ($print) { + print "\n"; + } + + return $html; } + + ######## # WGET # ######## diff --git a/preview.1.pl b/preview.1.pl index dfcde13..8a0b0bc 100644 --- a/preview.1.pl +++ b/preview.1.pl @@ -27,7 +27,8 @@ use botm_common ( ); use post_common ( 'prepare_post', - 'CODE' + 'CODE', + 'bbcode_to_html' ); ###PERL_VERSION: use constant VERSION => 'x.x.x'; @@ -185,6 +186,13 @@ foreach my $arg (@ARGV) { } # now generate preview + my $html = bbcode_to_html(\%options, \%post); + + if ($options{'verbose'} ne '') { + print "HTML\n"; + print $html."\n"; + print "\n"; + } } exit $code; -- 2.30.2