]> bicyclesonthemoon.info Git - ott/bsta/commitdiff
improve BB tree conversion
authorb <rowerynaksiezycu@gmail.com>
Sun, 1 Oct 2023 22:10:12 +0000 (22:10 +0000)
committerb <rowerynaksiezycu@gmail.com>
Sun, 1 Oct 2023 22:10:12 +0000 (22:10 +0000)
bsta_lib.1.pm

index 0473e237362ec6df1eeafca8a2e5338e6453af40..5697e6b30575a3c26d81954c7f1dee30a61a6ef3 100644 (file)
@@ -71,7 +71,7 @@ use constant TEXT_MODE => {
        'info'   => 2,
 };
 
-use constant tagsbb => {
+use constant tags_bbcode => {
        'ht'    => '',
        '/ht'   => '',
        'fq'    => '[quote]',
@@ -99,7 +99,7 @@ use constant tagsbb => {
        '?'     => '[unknown!]',
        '/?'    => '[/unknown!]',
 };
-use constant tagsht => {
+use constant tags_html => {
        'ht'     => '',
        '/ht'    => '',
        'fq'     => '<div class="fq">',
@@ -425,82 +425,60 @@ sub bbtree {
 
 #convert tag tree to final text
 sub convtree {
-       (my $printdebug, my $debug, my $lang, my %bbtree) = @_;
-       my $ht;
+       (my $printdebug, my $debug, my $lang, my $bbtree) = @_;
+       my $out;
        my $ind;
        my $indd;
-       my $level=0;
-       my $tagsr = ($lang eq 'html') ? tagsht : tagsbb;
-       my %tags = %$tagsr;
+       my $level = 0;
+       my $tags = ($lang eq 'html') ? tags_html : tags_bbcode;
        my $escape = ($lang eq 'html');
        
        # $debug .= debug($printdebug, "\n****\n");
        # foreach my $iiii (keys %tags) {
-               # $debug .= debug($printdebug, $iiii.'='.$tags{$iiii}."\n");
+               # $debug .= debug($printdebug, $iiii.'='.$tags->{$iiii}."\n");
        # }
        # $debug .= debug($printdebug, "****\n");
        
-       $level=0;
-       $ind='_';
-       $ht='';
+       $level = 0;
+       $ind = '_';
+       $out = '';
        $debug .= debug($printdebug, "\n<!--PROCESSING BBCODE TREE:\n");
        
-       while ($level >=0) {
-               $debug .= debug($printdebug, "[$level:$ind:".int($bbtree{$ind.'.count'})."]");
+       while ($level >= 0) {
+               my $goto_next = '';
+               $debug .= debug($printdebug, "[$level:$ind:".int($bbtree->{$ind.'.count'})."]");
                #normal text
-               if($bbtree{$ind.'.type'} eq 'text') {
-                       $debug .= debug($printdebug, "text: ".$bbtree{$ind.'.value'});
-                       $ht .= $escape ? (html_encode_line($bbtree{$ind.'.value'})) : ($bbtree{$ind.'.value'});
+               if ($bbtree->{$ind.'.type'} eq 'text') {
+                       my $text = $bbtree->{$ind.'.value'}
+                       $debug .= debug($printdebug, "text: ".$text);
+                       $out .= $escape ? html_encode_line($text) : $text;
                        
-                       {do{
-                               $ind =~ s/\.([0-9]+)$//;
-                               $indd = int($1)+1;
-                               if ($indd < $bbtree{$ind.'.count'}){
-                                       $ind = $ind.'.'.$indd;
-                                       last;
-                               }
-                               else {
-                                       #should not occur with a correct bbtree
-                                       $debug .= debug($printdebug, "[<tx]");
-                                       $level -= 1;
-                               }
-                       } while ($level>=0);}
+                       $goto_next = 'tx';
                }
                #tag
-               elsif($bbtree{$ind.'.type'} eq 'tag') {
+               elsif ($bbtree->{$ind.'.type'} eq 'tag') {
+                       my $name = $bbtree->{$ind.'.name'};
                        #endtag
-                       if($bbtree{$ind.'.name'} =~ /^\//) {
-                               $debug .= debug($printdebug, "tag: [".$bbtree{$ind.'.name'}."]");
+                       if ($name =~ /^\//) {
+                               $debug .= debug($printdebug, "tag: [$name]");
                                $indd = $ind;
                                $indd =~ s/\.([0-9]+)$//;
-                               if (exists($tags{$bbtree{$ind.'.name'}.'='}) && ($bbtree{$indd.'.value'} ne '')) {
-                                       $ht = $ht.$tags{$bbtree{$ind.'.name'}.'='};
+                               if (exists($tags->{$name.'='}) && ($bbtree->{$indd.'.value'} ne '')) {
+                                       $out .= $tags->{$name.'='};
                                }
-                               elsif (exists($tags{$bbtree{$ind.'.name'}})) {
-                                       $ht = $ht.$tags{$bbtree{$ind.'.name'}};
+                               elsif (exists($tags->{$name})) {
+                                       $out .= $tags->{$name};
                                }
                                else {
-                                       $ht = $ht.$tags{'/?'};
+                                       $out .= $tags->{'/?'};
                                        $debug .= debug($printdebug, "[unknown!]");
                                }
                                
                                $ind =~ s/\.([0-9]+)$//;
                                $level -= 1;
                                $debug .= debug($printdebug, "[<]");
-                               if($level>=0) {
-                                       {do{
-                                               $ind =~ s/\.([0-9]+)$//;
-                                               $indd = int($1)+1;
-                                               if ($indd < $bbtree{$ind.'.count'}){
-                                                       $ind = $ind.'.'.$indd;
-                                                       last;
-                                               }
-                                               else {
-                                                       #should not occur with a correct bbtree
-                                                       $debug .= debug($printdebug, "[<nd]");
-                                                       $level -= 1;
-                                               }
-                                       } while ($level>=0);}
+                               if ($level >= 0) {
+                                       $goto-next = 'nd';
                                }
                                else {
                                        # time to end this
@@ -509,84 +487,86 @@ sub convtree {
                        }
                        #starttag
                        else {
-                               if($bbtree{$ind.'.closed'} ne '') {
-                                       $debug .= debug($printdebug, "tag: [".$bbtree{$ind.'.name'}."]");
+                               my $value = $bbtree->{$ind.'.value'};
+                               if($bbtree->{$ind.'.closed'} ne '') {
+                                       $debug .= debug($printdebug, "tag: [$name]");
                                        
-                                       if (exists($tags{$bbtree{$ind.'.name'}.'='}) && ($bbtree{$ind.'.value'} ne '')) {
-                                               if (exists($tags{$bbtree{$ind.'.name'}.'='.$bbtree{$ind.'.value'}})) {
-                                                       $ht = $ht.$tags{$bbtree{$ind.'.name'}.'='}.$tags{$bbtree{$ind.'.name'}.'='.$bbtree{$ind.'.value'}}.$tags{$bbtree{$ind.'.name'}.'/='};
+                                       if (exists($tags->{$name.'='}) && ($value ne '')) {
+                                               if (exists($tags->{$name.'='.$value})) {
+                                                       $out .=
+                                                               $tags->{$name.'='} .
+                                                               $tags->{$name.'='.$value'} .
+                                                               $tags->{$name.'/='};
                                                }
                                                else {
-                                                       $ht = $ht.$tags{$bbtree{$ind.'.name'}.'='}.($escape?entityencode($bbtree{$ind.'.value'}):$bbtree{$ind.'.value'}).$tags{$bbtree{$ind.'.name'}.'/='};
+                                                       $out .=
+                                                               $tags->{$name.'='} .
+                                                               ($escape ? html_entity_encode_dec($value, 1) : $value) .
+                                                               $tags->{$name.'/='};
                                                }
                                        }
-                                       elsif (exists($tags{$bbtree{$ind.'.name'}})) {
-                                               $ht = $ht.$tags{$bbtree{$ind.'.name'}};
+                                       elsif (exists($tags->{$name})) {
+                                               $out .= $tags->{$name};
                                        }
                                        else {
-                                               $ht = $ht.$tags{'?'};
+                                               $out .= $out.$tags->{'?'};
                                                $debug .= debug($printdebug, "[unknown!]");
                                        }
                                }
                                else {
-                                       $debug .= debug($printdebug, "unclosed tag: [".$bbtree{$ind.'.name'}."]");
-                                       $ht .= '['.($escape?html_encode_line($bbtree{$ind.'.name'}):$bbtree{$ind.'.name'}).']';
+                                       $debug .= debug($printdebug, "unclosed tag: [$name]");
+                                       my $text = $name . (($value ne '') ? ('='.$value) : '');
+                                       $out .= '['.($escape ? html_encode_line($text) : $text).']';
                                }
-                               if($bbtree{$ind.'.count'}>0) {
+                               if ($bbtree->{$ind.'.count'} > 0) {
                                        $ind = $ind.'.0';
                                        $level += 1;
                                        $debug .= debug($printdebug, "[>]");
                                }
                                else {
-                                       {do{
-                                               $ind =~ s/\.([0-9]+)$//;
-                                               $indd = int($1)+1;
-                                               if ($indd < $bbtree{$ind.'.count'}){
-                                                       $ind = $ind.'.'.$indd;
-                                                       last;
-                                               }
-                                               else {
-                                                       #should not occur with a correct bbtree
-                                                       $debug .= debug($printdebug, "[<st]");
-                                                       $level -= 1;
-                                               }
-                                       } while ($level>=0);}
+                                       $goto_next = 'st';
                                }
                        }
                }
-               #what is this
+               # what is this
                else {
-                       $debug .= debug($printdebug, "unknown thing: ".$bbtree{$ind.'.type'});
+                       $debug .= debug($printdebug, "unknown thing: ".$bbtree->{$ind.'.type'});
                        #should not occur with a correct bbtree
                        #unless unimplemented
                        $ind =~ s/\.([0-9]+)$//;
                        $level -= 1;
                        $debug .= debug($printdebug, "[<ui]");
-                       if($level>0) {
-                               {do{
-                                       $ind =~ s/\.([0-9]+)$//;
-                                       $indd = int($1)+1;
-                                       if ($indd < $bbtree{$ind.'.count'}){
-                                               $ind = $ind.'.'.$indd;
-                                               last;
-                                       }
-                                       else {
-                                               #should not occur with a correct bbtree
-                                               $debug .= debug($printdebug, "[<un]");
-                                               $level -= 1;
-                                       }
-                               } while ($level>=0);}
+                       if ($level > 0) {
+                               $goto_next = 'un';
                        }
                        else {
                                # time to end this
                                $level = -1;
                        }
                }
+               if ($goto_next ne '') {
+                       {do{
+                               $ind =~ s/\.([0-9]+)$//;
+                               my $i = int($1) + 1;
+                               if ($i < $bbtree->{$ind.'.count'}){
+                                       # goto next
+                                       $ind = $ind.'.'.$i;
+                                       last;
+                               }
+                               else {
+                                       # step out
+                                       # should not occur with a correct bbtree
+                                       $debug .= debug($printdebug, "[<$goto_next]");
+                                       $level -= 1;
+                               }
+                       } while ($level >= 0);}
+               }
+               
                $debug .= debug($printdebug, "[>$level:$ind]\n");
        }
        
        $debug .= debug($printdebug, "-->\n");
-       return ($debug, $ht);
+       return ($debug, $out);
 }
 
 #bbcode to html, TBD
@@ -596,7 +576,7 @@ sub bb_to_html {
        my %bbtree;
        my $debug;
        
-       ($debug, %bbtree) = bbtree($bb,$printdebug);
+       ($debug, %bbtree) = bbtree($bb, $printdebug);
        ($debug, $ht) = convtree ($printdebug, $debug, 'html', %bbtree);
        
        return $ht;
@@ -609,7 +589,7 @@ sub bb_to_bbcode {
        my %bbtree;
        my $debug;
        
-       ($debug, %bbtree) = bbtree($bb,$printdebug);
+       ($debug, %bbtree) = bbtree($bb, $printdebug);
        ($debug, $ht) = convtree ($printdebug, $debug, 'bb', %bbtree);
        
        return $ht;