]> bicyclesonthemoon.info Git - ott/post/commitdiff
preview tool now works but wrong
authorb <rowerynaksiezycu@gmail.com>
Sun, 20 Aug 2023 20:33:08 +0000 (20:33 +0000)
committerb <rowerynaksiezycu@gmail.com>
Sun, 20 Aug 2023 20:33:08 +0000 (20:33 +0000)
post_common.1.pm
preview.1.pl

index 2db88fb39d5653f523fd7ac17f1c741f77fea514..7f529bfb80638535156fb19b16a98d1226cc77ae 100644 (file)
@@ -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\r          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 .= '<blockquote><div><cite>'.tagvalue.' wrote:</cite>';
+                                       $html .= '<blockquote><div><cite>'.html_entity_encode_dec($tag_value).' wrote:</cite>';
                                        # $html .= '<blockquote class="pq"><div><cite class="pq"><b class="br">'.tagvalue.'</b> wrote:</cite><br>';
                                }
                        }
@@ -813,7 +867,7 @@ sub bbcode_to_html {
                        }
                        
                        elsif ($tag_name eq 'spoiler') {
-                               $html .= '<div style="margin:20px; margin-top:5px"><div class="quotetitle"><b>Spoiler:</b> <input type="button" value="Show" style="width:45px;font-size:10px;margin:0px;padding:0px;" onclick="if (this.parentNode.parentNode.getElementsByTagName(\'div\')[1].getElementsByTagName(\'div\')[0].style.display != \'\') { this.parentNode.parentNode.getElementsByTagName(\'div\')[1].getElementsByTagName(\'div\')[0].style.display = \'\';        this.innerText = \\''; this.value = \'Hide\'; } else { this.parentNode.parentNode.getElementsByTagName(\'div\')[1].getElementsByTagName(\'div\')[0].style.display = \'none\'; this.innerText = \'\'; this.value = \'Show\'; }" /></div><div class="quotecontent"><div style="display: none;">';
+                               $html .= '<div style="margin:20px; margin-top:5px"><div class="quotetitle"><b>Spoiler:</b> <input type="button" value="Show" style="width:45px;font-size:10px;margin:0px;padding:0px;" onclick="if (this.parentNode.parentNode.getElementsByTagName(\'div\')[1].getElementsByTagName(\'div\')[0].style.display != \'\') { this.parentNode.parentNode.getElementsByTagName(\'div\')[1].getElementsByTagName(\'div\')[0].style.display = \'\';        this.innerText = \'\'; this.value = \'Hide\'; } else { this.parentNode.parentNode.getElementsByTagName(\'div\')[1].getElementsByTagName(\'div\')[0].style.display = \'none\'; this.innerText = \'\'; this.value = \'Show\'; }" /></div><div class="quotecontent"><div style="display: none;">';
                                # $html .= '<div style="margin:20px; margin-top:5px"><div class="quotetitle"><b>Spoiler:</b> <input type="button" class="pk" value="Show" style="width:45px;font-size:10px;margin:0px;padding:0px;" onclick="if (this.parentNode.parentNode.getElementsByTagName(\'div\')[1].getElementsByTagName(\'div\')[0].style.display != \'\') { this.parentNode.parentNode.getElementsByTagName(\'div\')[1].getElementsByTagName(\'div\')[0].style.display = \'\';        this.innerText = \'\'; this.value = \'Hide\'; } else { this.parentNode.parentNode.getElementsByTagName(\'div\')[1].getElementsByTagName(\'div\')[0].style.display = \'none\'; this.innerText = \'\'; this.value = \'Show\'; }" /></div><div class="quotecontent"><div style="display: none;">';
                                if ($tag_value ne '') {
                                        $html .= 'INVALID VALUE; ';
@@ -844,7 +898,7 @@ sub bbcode_to_html {
                                                $html .= 'INVALID VALUE; ';
                                                $tag_value = 'decimal';
                                        }
-                                       $html .= '<ol style="list-style-type: '.tag_value.'">';
+                                       $html .= '<ol style="list-style-type: '.$tag_value.'">';
                                }
                        }
                        
@@ -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/ /&nbsp;/gs;
+                               $text =~ s/\t/&nbsp;&nbsp;/gs;
+                       }
+                       
+                       # convert URLs
+                       if (($post->{'urls'}) and ($tag_name !~ /^((code)|(img)|(url))$/)) {
+                               $text =~ s/https?:\/\/[^\n\r\t<> ]+/<a href="$&" class="postlink">$&<\/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])/' <img title="'.BBCODE_SMILEY_TITLE->{$2}.'alt="'.$2.'" src="'.BBCODE_SMILEY_FILE->{$2}.'" \/> '/ge;
+                               # $text =~ s/(^|[ \t])((:([D\(\)o\?xP\|]|((shock)|(lol)|(oops)|(cry)|(evil)|(twisted)|(roll)|(wink)|(idea)|(arrow)|(mrgreen)|[!\?]:)))|(8-\))|(;\)))($|[ \t])/' <img title="'.BBCODE_SMILEY_TITLE->{$2}.'alt="'.$2.'" src="'.BBCODE_SMILEY_FILE->{$2}.'"> '/ge;
+                       }
+                       
+                       # convert NL
+                       $text =~ s/\r?\n/<br\/>\n/gs;
+                       # $text =~ s/\r?\n/<br>\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 #
 ########
index dfcde1353a29778a8e97cf09da94361d4dbd7b5f..8a0b0bcedd92675a15b35635f8189230516fa372 100644 (file)
@@ -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;