[pmwiki-users] Table of content generation

Pierre Rouzeau pierre at rouzeau.net
Thu Feb 17 17:02:23 CST 2005


Hello,
I have chosen the second method, making the (:toc:) table of content
exchange after markup interpretation.

For the others points, i modidied the philosophy and created two
directives :
(:num:) which say to number headers and (:nonum:) stops it
(:indent:) which create indentations within the headers
(:noindent:) close existing indentation

With that i can use the ! markups to do everything and ° is no longer used.

Though, due to sequencing troubles, i have not overwritten the ^!
markup, i just created a new one which execute first.

As side effect, the SideBar is influenced, and the (:nonum:) directive
should be set atop the sidebar or in GroupFooter page. Not quite nice.

That give the below code, which can be improved to allow alphabetic
numbering.
There is no parameter and that does not take into account Q: markup,
which means this is not directly interchangeable with john Rankin recipe
(which shall be desactivated).

So, the code :

Markup('^!2','>include','/^(!{1,4})(.*)$/e',
        "MkNumTitle(strlen('$1'),PSS('$2'))");
Markup('num','directives','/\\(:(no)?num:\\)/e',
        "PZZ(\$GLOBALS['NumHdr']=('$1'!='no'))");
Markup('indent','directives','/\\(:(no)?indent:\\)/e',
        "RunIndent('$1')");

$MarkupFrameBase['posteval']['toc'] =
       "\$out = str_replace('(:toc:)', \$GLOBALS['Ntoc'], \$out); ";

function MkNumTitle ($level, $text) {
   global $TcNb, $TcNb2, $TcNb3, $CurLvl, $SepNum, $Ntoc,
          $NumHdr, $IndentHdr;
   static $tcount;
   SDV ($SepNum, '. ');
   if ($CurLvl>0 and $IndentHdr) $hdr = "</p></div>";
   else $hdr='';
   if (!isset($CurLvl)) $CurLvl=0;
   if ($CurLvl==0) {
     $CurLvl++;
     $TcNb = 0;
     $TcNb2 = 0;
     $TcNb3 = 0;
     $TcNb4 = 0;	
   }
   $delta = $CurLvl - $level;
   if ($NumHdr) {
     if ($level==1) {$num=++$TcNb; $TcNb2=0; $TcNb3=0;}
     else if ($level==2) {$TcNb2++; $num="$TcNb.$TcNb2"; $TcNb3=0;}
     else if ($level==3) {$TcNb3++; $num="$TcNb.$TcNb2.$TcNb3";}
     else if ($level==4) {$TcNb4++; $num="$TcNb.$TcNb2.$TcNb3.$TcNb4";}	
     $num.= $SepNum;
   }
   $CurLvl = $level++;
   $Ntoc.= "\n".str_repeat(" &nbsp; &nbsp;",$CurLvl);
   $Ntoc.= "$num<a href='#ntoc".++$tcount."'>$text</a><br />";
   if ($IndentHdr) {
     $classt = "class='numh$CurLvl'";
	$classbl =  "<div class='numlvl$CurLvl'>";
   }	
   $hdr .= "\n<p>$classbl<h$CurLvl $classt>";
   return ("$hdr\n[[#ntoc$tcount]]$num$text</h$CurLvl><p>");
}

function RunIndent ($noindent) {
   global $IndentHdr;
   $wasindent = $IndentHdr;
   if (!$IndentHdr =($noindent!='no') and $wasindent)
     return '</p></div>';
}

$HTMLStylesFmt['numlevel'] = "
   .numlvl1 { margin-left:1.25em; }
   .numlvl2 { margin-left:2.5em; }
   .numlvl3 { margin-left:3.75em; }
   .numlvl4 { margin-left:5em; }
   .numh1 { text-indent:-1em; font-size:132%;
            font-weight:bold; margin:20px 0px;}
   .numh2 { text-indent:-1em; font-size:124%;
            font-weight:bold; margin:16px 0px;}
   .numh3 { text-indent:-1em; font-size:116%;
            font-weight:bold; margin:12px 0px;}
   .numh4 { text-indent:-1em; font-size:108%;
            font-weight:bold; margin:12px 0px;}
";

Regards

Patrick R. Michaud a écrit :
> On Wed, Feb 16, 2005 at 11:27:14PM +0100, Pierre Rouzeau wrote:
> 
>>I am building the content of the table during interpretation of the 
>>title markups. Then, the interpretation is finished, and i want the 
>>table to be set 'before' the contents it is supposed to link to. 
> 
> 
> Also, John Rankin writes:
> 
>>Every time I go back to the table of contents script, I find myself
>>wondering how on earth I ever got it to work. ;-)
> 
> 
> I have a number of suggestions which may or may not be helpful here.
> First, I'll note that I've never looked in depth at the existing
> table of contents recipe, so I'm not sure how it works.  But if it's
> still based on the v1 recipe, then it may be a bit more complex than
> necessary.  (In fact, one of the reasons for v2 was to make it
> more possible to support things like table of contents.)
> 
> I can envision two approaches to the problem that Pierre is
> trying to resolve...
> 
> Approach #1:  Instead of building the table contents during interpretation
> of the heading markups, build the table when the entire markup text
> is available to be examined; i.e., *before* the split into separate
> lines occurs:
> 
>     Markup("toc", "<split" 
>       '/\\(:toc:\\)/e',
>       "TableOfContents(\$pagename, \$x)");
> 
> The "split" markup is the point at which PmWiki breaks the text into
> separate lines (i.e., after conditional markup and includes), thus by 
> performing the (:toc:) conversion before the split takes place the 
> $x variable will contain the *entire contents* of the markup text.  
> You can thereby scan the entire text for any heading markups needed to 
> build the table of contents, and replace the (:toc:) with an appropriately
> formatted table.
> 
> Approach #2:  You can accumulate table-of-contents data as the headings
> are processed as you are doing now, then place it into the markup output
> via the 'posteval' section of MarkupToHTML().  To do this, you add
> an entry to $MarkupFrameBase['posteval'] with the code to be executed
> after all of the markups have been processed (the current output is
> stored in $out):
> 
>     $MarkupFrameBase['posteval']['toc'] =
>       "\$out = str_replace('(:toc:)', \$_GLOBALS['TableContents'], \$out); ";
> 
> This would scan the HTML output for any instances of '(:toc:)' and 
> replace them with $TableContents.
> 
> Hope this helps.  
> 
> I'm interested in adding a numbered headings scheme (and table of
> contents) to PmWiki's distribution, but I'm not completely satisfied 
> with the markups involved.
> 
> Pm






More information about the pmwiki-users mailing list