<?php if (!defined('PmWiki')) exit();
## Module NumberedSections 
## Author:Andrei Radulescu-Banu - Version 1.2, from Dec 14 2012
## Cloned and modified after NumToc by Pierre Rouzeau
## Licence GPL
##
##

##
## Documentation:
## 
## To start section numbering, include the directive (:numbered-sections:)
## To stop section numbering, include (:nonumbered-sections:)
##
## The (:numbered-sections:) directive takes an optional format parameter
## (:numbered-sections format=format_string:)
## where format_string is of the following form:
## 
## a.b.c.d.e.f
## where a, b, c, d, e, f can be the digits 0 or 1 or the characters a or A. 
## 
## If a digit is set to 0 at level L, then level L section headers don't get 
## counted. 
##
## If a digit is set to 1, a or A at level L, then level L section headers 
## get counted starting respectively with 1, a or A.
##
## 
## The inner separator can be controlled as well:
## 1X1Y1Z1U1V1
## will result in X, Y, Z, ... being separator characters between numbers.
##
## For example, assume your sectioning is
## 
## h1
##  h2
##  h2
##  h2
##  h3
##   h4
##    h5
##     h6
##     h6
##     h6

## Then format 1.1.1.1.1.a yields the following section numbering:
##
## 1 h1
##  1.1 h2
##  1.2 h2
##  1.3 h2
##   1.3.1 h3
##    1.3.1.1 h4
##     1.3.1.1.1 h5
##      1.3.1.1.1.a h6
##      1.3.1.1.1.b h6
##      1.3.1.1.1.c h6
##
## Format 0.0.0.0.1-a yields the following section numbering:
##
## h1
##  h2
##  h2
##  h2
##   h3
##    h4
##     1 h5
##      1-a h6
##      1-b h6
##      1-c h6
##
## You may also define $DDNumFmt="a.b.c.d.e.f"; in your config.php, 
## to specify a site-specific default.
##
## The NumberedSections recipe can be combined with the 'numtoc', 'sectionedit'
## and other section-related recipes
## 
##

## Version date
$RecipeInfo['NumberedSections']['Version'] = '2011-12-09';

## Enable debug
$DDDebug = 0;

##
## Global variables that can be defined in config.php
##
SDV ($DDNumFmt, '1.1.1.1.1.1');

Markup('dd-num1','>include','/^(!{1,6})(?:\s*)(.*)$/e', "DDMkNumTitle(PSS('$1'),PSS('$2'))");
Markup('dd-num2','directives','/\\(:(no)?numbered-sections(?:\s+(.*?))?:\\)/e', "DDRunNum('$1',PSS('$2'))");

##
## If numbered sections are enabled in the group or site header or footer, ensure they are not inherited
## in the page.
##
$GroupHeaderFmt = $GroupHeaderFmt.'(:nonumbered-sections:)';
$GroupFooterFmt = '(:nonumbered-sections:)'.$GroupFooterFmt;

$SiteHeaderFmt = $SiteHeaderFmt.'(:nonumbered-sections:)';
$SiteFooterFmt = '(:nonumbered-sections:)'.$SiteFooterFmt;


function DDMkNumTitle ($prefix, $text) {
  global $DDNumFmt, $DDNumHdrEnable, $DDFmtSet, $DDDebug;
  static $FmtNb, $OrNb, $TcNb, $sp, $numoffset, $CurLvl;

  $level = strlen($prefix);
  if ($level > 6) return;

  if ($DDNumHdrEnable) {
        if (!$DDFmtSet) {
	  $sp[1] = '';	
          for ($i=1; $i<=6; $i++) {
	    $sp[$i+1] = $DDNumFmt[$i+$i-1];
            $FmtNb[$i] = $DDNumFmt[$i+$i-2];
	    $OrNb[$i] = DDDecNum($FmtNb[$i]); 
	    $TcNb[$i] = $OrNb[$i];
	  }  
          $DDFmtSet = 1; 
        }

	$TcNb[$level] = DDIncNum($TcNb[$level]);
        $space_needed = 0;
        if ($FmtNb[$level]) {
           for ($i=1; $i<=$level; $i++) {
               if ($FmtNb[$i]) {  
                  if ($space_needed) {          
                     $num .= $sp[$i];
                  }
                  $num .= $TcNb[$i];	
                  $space_needed = 1;
              }   
           }
           $num.= " ";
        }      
	for ($i=$level+1; $i<6; $i++) $TcNb[$i]= $OrNb[$i];
  }

  if ($DDDebug) printf("<!-- %s() prefix %s level %d num %s text %s fmt %s -->\n", __FUNCTION__, $prefix, $level, $num, $text, $DDNumFmt);


  return ("$prefix$num$text"); 
}

function DDIncNum ($num) {
  if (is_numeric($num)) 
    return ++$num; 
  else 
    return chr(ord($num)+1);
}

function DDDecNum ($num) {
  if (is_numeric($num)) 
    return --$num; 
  else 
    return chr(ord($num)-1);
}

function DDRunNum ($nonum, $argstr) {
  global $DDNumHdrEnable, $DDFmtSet, $DDNumFmt, $DDDebug;
 
  $args = ParseArgs($argstr);

  $DDNumFmt = ($args['format'] ? $args['format'] : '1.1.1.1.1.1');

  if ($nonum =='no') {
    $DDNumHdrEnable = 0;
    $DDFmtSet = 0;
  } else {
    $DDNumHdrEnable = 1;
  }


  if ($DDDebug) printf("<!-- %s() fmt %s DDNumHdrEnable %d DDFmtSet %d -->\n", __FUNCTION__, $DDNumFmt, $DDNumHdrEnable, $DDFmtSet);
}

?>