]> bicyclesonthemoon.info Git - ott/bsta/commitdiff
bbcode converter more readable
authorb <rowerynaksiezycu@gmail.com>
Sat, 30 Sep 2023 21:37:24 +0000 (21:37 +0000)
committerb <rowerynaksiezycu@gmail.com>
Sat, 30 Sep 2023 21:37:24 +0000 (21:37 +0000)
botm-common
bsta_lib.1.pm

index bd6107e790c6666a87a0e9f2bec9370868bf531b..c88ca49b964a50d81875ab64ccaf2f25c64b89f3 160000 (submodule)
@@ -1 +1 @@
-Subproject commit bd6107e790c6666a87a0e9f2bec9370868bf531b
+Subproject commit c88ca49b964a50d81875ab64ccaf2f25c64b89f3
index 048c0830a102fc1582ddff9a1012119a3e79dd93..0473e237362ec6df1eeafca8a2e5338e6453af40 100644 (file)
@@ -308,96 +308,117 @@ sub urlencode {
        return url_encode($t, '', $all);
 }
 
+# BB code stuff
+# different & simpler implementation than in post library
+# to consider:
+# a BBcode library?
+
 #analyse bbcode text to build tag tree #TODO make [/*] optional!
 sub bbtree {
        (my $bb, my $printdebug) = @_;
        my %bbtree;
        my $ind;
        my $tag;
-       my $tagname;
-       my $tagvalue;
-       my $tagend;
+       my $tag_name;
+       my $tag_value;
+       my $tag_end;
        my $level=0;
-       my $pretext;
+       my $pre_text;
        my $debug;
        
        $ind="_";
        $level=0;
-       $bbtree{"_.n"}="ht";
-       $bbtree{"_.v"}='';
-       $bbtree{"_.t"}="tg";
-       $bbtree{"_.e"}=0;
-       $bbtree{"_.c"}='';
-       $debug .= debug($printdebug, "\n<!--GENERATING BBCODE TREE:\n".'[_]automatic tag: [ht]'."\n");
+       $bbtree{"_.name" }  = "ht";
+       $bbtree{"_.value" } = '';
+       $bbtree{"_.type"  } = "tag";
+       $bbtree{"_.count" } = 0;
+       $bbtree{"_.closed"} = 0;
+       $debug .= debug($printdebug,
+               "\n".
+               "<!--GENERATING BBCODE TREE:\n".
+               '[_]automatic tag: [ht]'."\n"
+       );
        
        while ($bb ne '') {
-               if($bb =~ m/(\[(\/?)([a-z]+|\*)(=([^\[\]]*))?\])/g) {
-                       $tag = $1;
-                       $tagend = $2;
-                       $tagname = $3;
-                       $tagvalue = $5;
-                       $pretext = substr($bb,0,pos ($bb)-length($tag));
-                       $bb = substr ($bb,pos ($bb));
+               my $new_ind = $ind.'.'.$bbtree{$ind.'.count'};
+               
+               if($bb =~ m/\[(\/?)([a-z]+|\*)(=([^\[\]]*))?\]/g) {
+                       $pre_text = $`;
+                       $tag = $&;
+                       $tag_end = $1;
+                       $tag_name = $2;
+                       $tag_value = $4;
+                       $bb = $';
                        
-                       if ($pretext ne '') {
-                               $debug .= debug($printdebug, '['.$ind.'.'.$bbtree{$ind.'.e'}.']text: '.$pretext."\n");
-                               $bbtree{$ind.'.'.$bbtree{$ind.'.e'}.'.t'}='tx';
-                               $bbtree{$ind.'.'.$bbtree{$ind.'.e'}.'.v'}=$pretext;
-                               $bbtree{$ind.'.e'} += 1;
+                       if ($pre_text ne '') {
+                               $debug .= debug($printdebug, "[$new_ind]text: $pre_text\n");
+                               $bbtree{$new_ind.'.type' } = 'text';
+                               $bbtree{$new_ind.'.value'} = $pre_text;
+                               $bbtree{    $ind.'.count'}+= 1;
+                               $new_ind = $ind.'.'.$bbtree{$ind.'.count'};
                        }
                        
-                       if($tagname =~ /^(fq|tq|br|ni|po|url|i|list|\*)$/) {
-                               if ($tagend ne '') {
-                                       if(($tagname ne $bbtree{$ind.'.n'}) || ($level <= 0)) {
-                                               $debug .= debug($printdebug, '['.$ind.'.'.$bbtree{$ind.'.e'}.']text: '.$tag."\n");
-                                               $bbtree{$ind.'.'.$bbtree{$ind.'.e'}.'.t'}='tx';
-                                               $bbtree{$ind.'.'.$bbtree{$ind.'.e'}.'.v'}=$tag;
-                                               $bbtree{$ind.'.e'} += 1;
+                       if($tag_name =~ /^(fq|tq|br|ni|po|url|i|list|\*)$/) {
+                               if ($tag_end ne '') {
+                                       if (
+                                               ($tag_name ne $bbtree{$ind.'.name'}) ||
+                                               ($level <= 0)
+                                       ) {
+                                               $debug .= debug($printdebug, "[$new_ind]text: $tag\n");
+                                               $bbtree{$new_ind.'.type' } = 'text';
+                                               $bbtree{$new_ind.'.value'} = $tag;
+                                               $bbtree{    $ind.'.count'}+= 1;
+                                               # $new_ind = $ind.'.'.$bbtree{$ind.'.count'};
                                        }
                                        else {
-                                               $debug .= debug($printdebug, '['.$ind.'.'.$bbtree{$ind.'.e'}.']tag: '.$tag."\n");
-                                               $bbtree{$ind.'.'.$bbtree{$ind.'.e'}.'.t'}='tg';
-                                               $bbtree{$ind.'.'.$bbtree{$ind.'.e'}.'.n'}='/'.$tagname;
-                                               $bbtree{$ind.'.'.$bbtree{$ind.'.e'}.'.v'}=$tagvalue;
-                                               $bbtree{$ind.'.e'} += 1;
-                                               $bbtree{$ind.'.c'}=1;
+                                               $debug .= debug($printdebug, "[$new_$ind]tag: $tag\n");
+                                               $bbtree{$new_ind.'.type'  } = 'tag';
+                                               $bbtree{$new_ind.'.name'  } = '/'.$tag_name;
+                                               $bbtree{$new_ind.'.value' } = $tag_value;
+                                               $bbtree{    $ind.'.count' }+= 1;
+                                               $bbtree{    $ind.'.closed'} = 1;
                                                $level -= 1;
                                                $ind =~ s/\.[0-9]+$//;
+                                               # $new_ind = $ind.'.'.$bbtree{$ind.'.count'};
                                        }
                                }
                                else
                                {
-                                       $debug .= debug($printdebug, '['.$ind.'.'.$bbtree{$ind.'.e'}.']tag: '.$tag."\n");
-                                       $bbtree{$ind.'.'.$bbtree{$ind.'.e'}.'.t'}='tg';
-                                       $bbtree{$ind.'.'.$bbtree{$ind.'.e'}.'.n'}=$tagname;
-                                       $bbtree{$ind.'.'.$bbtree{$ind.'.e'}.'.v'}=$tagvalue;
-                                       $bbtree{$ind.'.'.$bbtree{$ind.'.e'}.'.e'}=0;
-                                       $bbtree{$ind.'.'.$bbtree{$ind.'.e'}.'.c'}='';
-                                       $bbtree{$ind.'.e'} += 1;
+                                       $debug .= debug($printdebug, "[$new_ind]tag: $tag\n");
+                                       $bbtree{$new_ind.'.type'  } = 'tag';
+                                       $bbtree{$new_ind.'.name'  } = $tag_name;
+                                       $bbtree{$new_ind.'.value' } = $tag_value;
+                                       $bbtree{$new_ind.'.count' } = 0;
+                                       $bbtree{$new_ind.'.closed'} = 0;
+                                       $bbtree{    $ind.'.count' }+= 1;
                                        $level += 1;
-                                       $ind = $ind.'.'.($bbtree{$ind.'.e'}-1);
+                                       $ind = $new_ind;
+                                       # $new_ind = $ind.'.'.$bbtree{$ind.'.count'};
                                }
                        }
                        else {
-                               $debug .= debug($printdebug, '['.$ind.'.'.$bbtree{$ind.'.e'}.']text: '.$tag."\n");
-                               $bbtree{$ind.'.'.$bbtree{$ind.'.e'}.'.t'}='tx';
-                               $bbtree{$ind.'.'.$bbtree{$ind.'.e'}.'.v'}=$tag;
-                               $bbtree{$ind.'.e'} += 1;
+                               $debug .= debug($printdebug, "[$new_ind]text: $tag\n");
+                               $bbtree{$new_ind.'.type' } = 'text';
+                               $bbtree{$new_ind.'.value'} = $tag;
+                               $bbtree{    $ind.'.count'}+= 1;
+                               # $new_ind = $ind.'.'.$bbtree{$ind.'.count'};
                        }
                }
                else {
-                       $debug .= debug($printdebug, '['.$ind.'.'.$bbtree{$ind.'.e'}.']text: '.$bb."\n");
-                       $bbtree{$ind.'.'.$bbtree{$ind.'.e'}.'.t'}='tx';
-                       $bbtree{$ind.'.'.$bbtree{$ind.'.e'}.'.v'}=$bb;
-                       $bbtree{$ind.'.e'} += 1;
-                       $bb='';
+                       $debug .= debug($printdebug, "[$new_ind]text: $bb\n");
+                       $bbtree{$new_ind.'.type' } = 'text';
+                       $bbtree{$new_ind.'.value'} = $bb;
+                       $bbtree{    $ind.'.count'}+= 1;
+                       # $new_ind = $ind.'.'.$bbtree{$ind.'.count'};
+                       $bb = '';
                }
        }
-       $debug .= debug($printdebug, '[_.'.$bbtree{'_.e'}.']automatic tag: [/ht]'."\n -->\n");
-       $bbtree{'_.'.$bbtree{"_.e"}.'.t'}="tg";
-       $bbtree{'_.'.$bbtree{"_.e"}.'.n'}='/ht';
-       $bbtree{"_.e"}+=1;
-       $bbtree{"_.c"}=1;
+       my $final_ind = '_.'.$bbtree{"_.count"}
+       $debug .= debug($printdebug, "[$final_ind]automatic tag: [/ht]\n -->\n");
+       $bbtree{$final_ind.'.type' } = "tag";
+       $bbtree{$final_ind.'.name' } = '/ht';
+       $bbtree{         '_.count' }+= 1;
+       $bbtree{         '_.closed'} = 1;
        
        return ($debug, %bbtree);
 }
@@ -425,16 +446,16 @@ sub convtree {
        $debug .= debug($printdebug, "\n<!--PROCESSING BBCODE TREE:\n");
        
        while ($level >=0) {
-               $debug .= debug($printdebug, "[$level:$ind:".int($bbtree{$ind.'.e'})."]");
+               $debug .= debug($printdebug, "[$level:$ind:".int($bbtree{$ind.'.count'})."]");
                #normal text
-               if($bbtree{$ind.'.t'} eq 'tx') {
-                       $debug .= debug($printdebug, "text: ".$bbtree{$ind.'.v'});
-                       $ht .= $escape ? (html_encode_line($bbtree{$ind.'.v'})) : ($bbtree{$ind.'.v'});
+               if($bbtree{$ind.'.type'} eq 'text') {
+                       $debug .= debug($printdebug, "text: ".$bbtree{$ind.'.value'});
+                       $ht .= $escape ? (html_encode_line($bbtree{$ind.'.value'})) : ($bbtree{$ind.'.value'});
                        
                        {do{
                                $ind =~ s/\.([0-9]+)$//;
                                $indd = int($1)+1;
-                               if ($indd < $bbtree{$ind.'.e'}){
+                               if ($indd < $bbtree{$ind.'.count'}){
                                        $ind = $ind.'.'.$indd;
                                        last;
                                }
@@ -446,17 +467,17 @@ sub convtree {
                        } while ($level>=0);}
                }
                #tag
-               elsif($bbtree{$ind.'.t'} eq 'tg') {
+               elsif($bbtree{$ind.'.type'} eq 'tag') {
                        #endtag
-                       if($bbtree{$ind.'.n'} =~ /^\//) {
-                               $debug .= debug($printdebug, "tag: [".$bbtree{$ind.'.n'}."]");
+                       if($bbtree{$ind.'.name'} =~ /^\//) {
+                               $debug .= debug($printdebug, "tag: [".$bbtree{$ind.'.name'}."]");
                                $indd = $ind;
                                $indd =~ s/\.([0-9]+)$//;
-                               if (exists($tags{$bbtree{$ind.'.n'}.'='}) && ($bbtree{$indd.'.v'} ne '')) {
-                                       $ht = $ht.$tags{$bbtree{$ind.'.n'}.'='};
+                               if (exists($tags{$bbtree{$ind.'.name'}.'='}) && ($bbtree{$indd.'.value'} ne '')) {
+                                       $ht = $ht.$tags{$bbtree{$ind.'.name'}.'='};
                                }
-                               elsif (exists($tags{$bbtree{$ind.'.n'}})) {
-                                       $ht = $ht.$tags{$bbtree{$ind.'.n'}};
+                               elsif (exists($tags{$bbtree{$ind.'.name'}})) {
+                                       $ht = $ht.$tags{$bbtree{$ind.'.name'}};
                                }
                                else {
                                        $ht = $ht.$tags{'/?'};
@@ -470,7 +491,7 @@ sub convtree {
                                        {do{
                                                $ind =~ s/\.([0-9]+)$//;
                                                $indd = int($1)+1;
-                                               if ($indd < $bbtree{$ind.'.e'}){
+                                               if ($indd < $bbtree{$ind.'.count'}){
                                                        $ind = $ind.'.'.$indd;
                                                        last;
                                                }
@@ -488,19 +509,19 @@ sub convtree {
                        }
                        #starttag
                        else {
-                               if($bbtree{$ind.'.c'} ne '') {
-                                       $debug .= debug($printdebug, "tag: [".$bbtree{$ind.'.n'}."]");
+                               if($bbtree{$ind.'.closed'} ne '') {
+                                       $debug .= debug($printdebug, "tag: [".$bbtree{$ind.'.name'}."]");
                                        
-                                       if (exists($tags{$bbtree{$ind.'.n'}.'='}) && ($bbtree{$ind.'.v'} ne '')) {
-                                               if (exists($tags{$bbtree{$ind.'.n'}.'='.$bbtree{$ind.'.v'}})) {
-                                                       $ht = $ht.$tags{$bbtree{$ind.'.n'}.'='}.$tags{$bbtree{$ind.'.n'}.'='.$bbtree{$ind.'.v'}}.$tags{$bbtree{$ind.'.n'}.'/='};
+                                       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'}.'/='};
                                                }
                                                else {
-                                                       $ht = $ht.$tags{$bbtree{$ind.'.n'}.'='}.($escape?entityencode($bbtree{$ind.'.v'}):$bbtree{$ind.'.v'}).$tags{$bbtree{$ind.'.n'}.'/='};
+                                                       $ht = $ht.$tags{$bbtree{$ind.'.name'}.'='}.($escape?entityencode($bbtree{$ind.'.value'}):$bbtree{$ind.'.value'}).$tags{$bbtree{$ind.'.name'}.'/='};
                                                }
                                        }
-                                       elsif (exists($tags{$bbtree{$ind.'.n'}})) {
-                                               $ht = $ht.$tags{$bbtree{$ind.'.n'}};
+                                       elsif (exists($tags{$bbtree{$ind.'.name'}})) {
+                                               $ht = $ht.$tags{$bbtree{$ind.'.name'}};
                                        }
                                        else {
                                                $ht = $ht.$tags{'?'};
@@ -508,10 +529,10 @@ sub convtree {
                                        }
                                }
                                else {
-                                       $debug .= debug($printdebug, "unclosed tag: [".$bbtree{$ind.'.n'}."]");
-                                       $ht .= '['.($escape?html_encode_line($bbtree{$ind.'.n'}):$bbtree{$ind.'.n'}).']';
+                                       $debug .= debug($printdebug, "unclosed tag: [".$bbtree{$ind.'.name'}."]");
+                                       $ht .= '['.($escape?html_encode_line($bbtree{$ind.'.name'}):$bbtree{$ind.'.name'}).']';
                                }
-                               if($bbtree{$ind.'.e'}>0) {
+                               if($bbtree{$ind.'.count'}>0) {
                                        $ind = $ind.'.0';
                                        $level += 1;
                                        $debug .= debug($printdebug, "[>]");
@@ -520,7 +541,7 @@ sub convtree {
                                        {do{
                                                $ind =~ s/\.([0-9]+)$//;
                                                $indd = int($1)+1;
-                                               if ($indd < $bbtree{$ind.'.e'}){
+                                               if ($indd < $bbtree{$ind.'.count'}){
                                                        $ind = $ind.'.'.$indd;
                                                        last;
                                                }
@@ -535,7 +556,7 @@ sub convtree {
                }
                #what is this
                else {
-                       $debug .= debug($printdebug, "unknown thing: ".$bbtree{$ind.'.t'});
+                       $debug .= debug($printdebug, "unknown thing: ".$bbtree{$ind.'.type'});
                        #should not occur with a correct bbtree
                        #unless unimplemented
                        $ind =~ s/\.([0-9]+)$//;
@@ -545,7 +566,7 @@ sub convtree {
                                {do{
                                        $ind =~ s/\.([0-9]+)$//;
                                        $indd = int($1)+1;
-                                       if ($indd < $bbtree{$ind.'.e'}){
+                                       if ($indd < $bbtree{$ind.'.count'}){
                                                $ind = $ind.'.'.$indd;
                                                last;
                                        }