diff --git a/site/tags/sections.php b/site/tags/sections.php index adc1eb8..e9cad21 100644 --- a/site/tags/sections.php +++ b/site/tags/sections.php @@ -1,337 +1,241 @@ page()->text(); + + preg_match_all("/\(toc: .*\)/", $text, $tocs); + + foreach ($tocs[0] as $toc) { + // determine the lowercase parameter + preg_match_all("/lowercase:(\ )?[0,1]?/", $toc, $parameters); + foreach ($parameters[0] as $param) { + $param = preg_replace("/lowercase:/", "", $param); + $param = preg_replace("/\ /", "", $param); + + if($param==1) + $lowercase=1; + else + $lowercase=0; + } + + // determine the splitchar parameter + $splitchar = '%20'; + preg_match_all("/split:(\ )?./", $toc, $parameters); + foreach ($parameters[0] as $param) { + $param = preg_replace("/split:(\ )?/", "", $param); + $param = preg_replace("/\ ./", "", $param); + $splitchar = $param; + } + } + + $sc = $splitchar; + $lc = $lowercase; +} + +/********************************************************************************************/ +/* + The following functions replace the (lx: ) tags by a headline of the respective size, + containing also a named anchor. +*/ +/********************************************************************************************/ kirbytext::$tags['l1'] = array( 'attr' => array( ), 'html' => function($tag) { - foreach($tag->page()->content() as $link) { - $cnt+=1; - if($cnt==3) { - preg_match_all("/\(toc: .*\)/", $link, $tocs); - foreach ($tocs[0] as $toc) { - preg_match_all("/lowercase:(\ )?[0,1]?/", $toc, $parameters); - foreach ($parameters[0] as $param) { - $param = preg_replace("/lowercase:/", "", $param); - $param = preg_replace("/\ /", "", $param); - if($param==1) - { - $lowercase=1; - } - else - { - $lowercase=0; - } - } - $splitchar = '%20'; - preg_match_all("/split:(\ )?./", $toc, $parameters); - foreach ($parameters[0] as $param) { - $param = preg_replace("/split:(\ )?/", "", $param); - $param = preg_replace("/\ ./", "", $param); - $splitchar = $param; - } - } - } - } + getparams($tag, $splitchar, $lowercase); // Get splitchar and lowercase params - $link = $tag->attr('l1'); + $link = $tag->attr('l1'); // get the "l1:" tag attribute $link = preg_replace("/[^ \w]+/", "", $link); $link = str_replace(" ", $splitchar, $link); - if($lowercase==1) { + + if($lowercase==1) $link = strtolower($link); - } + return '

'. $tag->attr('l1') . '

'; } ); kirbytext::$tags['l2'] = array( 'attr' => array( - 'split', - 'lowercase' ), 'html' => function($tag) { - foreach($tag->page()->content() as $link) { - $cnt+=1; - if($cnt==3) { - preg_match_all("/\(toc: .*\)/", $link, $tocs); - foreach ($tocs[0] as $toc) { - preg_match_all("/lowercase:(\ )?[0,1]?/", $toc, $parameters); - foreach ($parameters[0] as $param) { - $param = preg_replace("/lowercase:/", "", $param); - $param = preg_replace("/\ /", "", $param); - if($param==1) - { - $lowercase=1; - } - else - { - $lowercase=0; - } - } - $splitchar = '%20'; - preg_match_all("/split:(\ )?./", $toc, $parameters); - foreach ($parameters[0] as $param) { - $param = preg_replace("/split:(\ )?/", "", $param); - $param = preg_replace("/\ ./", "", $param); - $splitchar = $param; - } - } - } - } + getparams($tag, $splitchar, $lowercase); // Get splitchar and lowercase params - $link = $tag->attr('l2'); - $link = preg_replace("/[^ \w]+/", "", $link); - $link = str_replace(" ", $splitchar, $link); - if($lowercase==1) { + $link = $tag->attr('l2'); + $link = preg_replace("/[^ \w]+/", "", $link); + $link = str_replace(" ", $splitchar, $link); + + if($lowercase==1) $link = strtolower($link); - } - return '

'. $tag->attr('l2') . '

'; + + return '

'. $tag->attr('l2') . '

'; } ); kirbytext::$tags['l3'] = array( 'attr' => array( - 'split', - 'lowercase' ), 'html' => function($tag) { - foreach($tag->page()->content() as $link) { - $cnt+=1; - if($cnt==3) { - preg_match_all("/\(toc: .*\)/", $link, $tocs); - foreach ($tocs[0] as $toc) { - preg_match_all("/lowercase:(\ )?[0,1]?/", $toc, $parameters); - foreach ($parameters[0] as $param) { - $param = preg_replace("/lowercase:/", "", $param); - $param = preg_replace("/\ /", "", $param); - if($param==1) - { - $lowercase=1; - } - else - { - $lowercase=0; - } - } - $splitchar = '%20'; - preg_match_all("/split:(\ )?./", $toc, $parameters); - foreach ($parameters[0] as $param) { - $param = preg_replace("/split:(\ )?/", "", $param); - $param = preg_replace("/\ ./", "", $param); - $splitchar = $param; - } - } - } - } + getparams($tag, $splitchar, $lowercase); // Get splitchar and lowercase params $link = $tag->attr('l3'); $link = preg_replace("/[^ \w]+/", "", $link); $link = str_replace(" ", $splitchar, $link); - if($lowercase==1) { + + if($lowercase==1) $link = strtolower($link); - } + return '

'. $tag->attr('l3') . '

'; } ); kirbytext::$tags['l4'] = array( 'attr' => array( - 'split', - 'lowercase' ), 'html' => function($tag) { - foreach($tag->page()->content() as $link) { - $cnt+=1; - if($cnt==3) { - preg_match_all("/\(toc: .*\)/", $link, $tocs); - foreach ($tocs[0] as $toc) { - preg_match_all("/lowercase:(\ )?[0,1]?/", $toc, $parameters); - foreach ($parameters[0] as $param) { - $param = preg_replace("/lowercase:/", "", $param); - $param = preg_replace("/\ /", "", $param); - if($param==1) - { - $lowercase=1; - } - else - { - $lowercase=0; - } - } - $splitchar = '%20'; - preg_match_all("/split:(\ )?./", $toc, $parameters); - foreach ($parameters[0] as $param) { - $param = preg_replace("/split:(\ )?/", "", $param); - $param = preg_replace("/\ ./", "", $param); - $splitchar = $param; - } - } - } - } + getparams($tag, $splitchar, $lowercase); // Get splitchar and lowercase params $link = $tag->attr('l4'); $link = preg_replace("/[^ \w]+/", "", $link); $link = str_replace(" ", $splitchar, $link); - if($lowercase==1) { + + if($lowercase==1) $link = strtolower($link); - } + return '

'. $tag->attr('l4') . '

'; } ); kirbytext::$tags['l5'] = array( 'attr' => array( - 'split', - 'lowercase' ), 'html' => function($tag) { - foreach($tag->page()->content() as $link) { - $cnt+=1; - if($cnt==3) { - preg_match_all("/\(toc: .*\)/", $link, $tocs); - foreach ($tocs[0] as $toc) { - preg_match_all("/lowercase:(\ )?[0,1]?/", $toc, $parameters); - foreach ($parameters[0] as $param) { - $param = preg_replace("/lowercase:/", "", $param); - $param = preg_replace("/\ /", "", $param); - if($param==1) - { - $lowercase=1; - } - else - { - $lowercase=0; - } - } - $splitchar = '%20'; - preg_match_all("/split:(\ )?./", $toc, $parameters); - foreach ($parameters[0] as $param) { - $param = preg_replace("/split:(\ )?/", "", $param); - $param = preg_replace("/\ ./", "", $param); - $splitchar = $param; - } - } - } - } + getparams($tag, $splitchar, $lowercase); // Get splitchar and lowercase params $link = $tag->attr('l5'); $link = preg_replace("/[^ \w]+/", "", $link); $link = str_replace(" ", $splitchar, $link); - if($lowercase==1) { + + if($lowercase==1) $link = strtolower($link); - } + return '
'. $tag->attr('l5') . '
'; } ); kirbytext::$tags['l6'] = array( 'attr' => array( - 'split', - 'lowercase' ), 'html' => function($tag) { - foreach($tag->page()->content() as $link) { - $cnt+=1; - if($cnt==3) { - preg_match_all("/\(toc: .*\)/", $link, $tocs); - foreach ($tocs[0] as $toc) { - preg_match_all("/lowercase:(\ )?[0,1]?/", $toc, $parameters); - foreach ($parameters[0] as $param) { - $param = preg_replace("/lowercase:/", "", $param); - $param = preg_replace("/\ /", "", $param); - if($param==1) - { - $lowercase=1; - } - else - { - $lowercase=0; - } - } - $splitchar = '%20'; - preg_match_all("/split:(\ )?./", $toc, $parameters); - foreach ($parameters[0] as $param) { - $param = preg_replace("/split:(\ )?/", "", $param); - $param = preg_replace("/\ ./", "", $param); - $splitchar = $param; - } - } - } - } - + getparams($tag, $splitchar, $lowercase); // Get splitchar and lowercase params + $link = $tag->attr('l6'); $link = preg_replace("/[^ \w]+/", "", $link); $link = str_replace(" ", $splitchar, $link); - if($lowercase==1) { + + if($lowercase==1) $link = strtolower($link); - } + return '
'. $tag->attr('l6') . '
'; } ); kirbytext::$tags['toc'] = array( - 'attr' => array( + 'attr' => array( // attributes, passed to the tag 'split', 'lowercase', 'levelchar' ), 'html' => function($tag) { +/* + This function replaces the (toc: ) tag by the TOC, containing links to the headline anchors. + + Supports the following params: + (toc:6 split: - lowercase: 1 levelchar: >) + + - split: Allows to change the replacement character for a space, example usage: split: - + - lowercase: Allows to change all letters in the link to lowercase, usage: lowercase: 1 + - levelchar: Allows to change the character used to separate different levels in the table of contents, + usage: levelchar: > or levelchar: space - $html = ""; - $cnt = 0; +*/ + $html = ""; // the return string + + // preparing the parameters. If not given, the default values will be used $splitchar = $tag->attr('split', "%20"); $levelchar = $tag->attr('levelchar', '|'); - if(strcmp($levelchar,"space")) { + $lowercase = $tag->attr('lowercase', '0'); + + if(!strcmp($levelchar, 'space')) $levelchar=" "; - } - foreach($tag->page()->content() as $link) { - $cnt+=1; - if($cnt==3) { - preg_match_all("/\(l[1-6]?: .*\)/", $link, $headings); - foreach ($headings[0] as $heading) { - $name = preg_split("/\(l[1-6]?: /",$heading); - $name = preg_split("/\)/",$name[1]); - $link = $name[0]; - - $link = preg_replace("/[^ \w]+/", "", $link); - $link = str_replace(" ", $splitchar, $link); - $link = htmlentities($link); - - $number = preg_split("/\(l/",$heading); - $number = preg_split("/: .*\)/",$number[1]); - $number = $number[0]; - - if($tag->attr('lowercase')==1) { - $link = strtolower($link); - } - if($number <= $tag->attr('toc')) - { - for ($x = 2; $x < $number; $x++) { - $html .= $levelchar.' '; - } - - $name = preg_replace("/\ split:(\ )?./", "", $name[0]); - $name = preg_replace("/\ lowercase:(\ )?\d/", "", $name); - - $html .= ''; - $html .= $name; - $html .= ''; - $html .= '
'; + // Scan the text field of the current page + $text = $tag->page()->text(); + + // Scan for all lines containing "(l[1-6]: ... )" + preg_match_all("/\(l[1-6]?: .*\)/", $text, $headings); + + // $headings[0] returns an array with the found lines + foreach ($headings[0] as $heading) { + // Process any headline + // remove "lx:" + $name = preg_split("/\(l[1-6]?: /",$heading); + $name = preg_split("/\)/",$name[1]); + $link = $name[0]; + + // replace whitespaces by splitchar + $link = preg_replace("/[^ \w]+/", "", $link); + $link = str_replace(" ", $splitchar, $link); + $link = htmlentities($link); // a PHP function + + // determine the headline depth + $number = preg_split("/\(l/",$heading); + $number = preg_split("/: .*\)/",$number[1]); + $number = $number[0]; + + // change to lowercase + if($lowercase == 1) + $link = strtolower($link); + + // we only return the headlines up to the given depth + if($number <= $tag->attr('toc')) { // the "toc:" parameter, e.g. 6 + + // output the levelchar as much as wanted + for ($x = 1; $x < $number; $x++) { + $html .= $levelchar . ' '; } - } - } - } - return $html; + $name = $name[0]; // link name from above + + // HTML Code erzeugen + $html .= ''; + $html .= $name; + $html .= ''; + $html .= '
'; + //$html .= "\n\r"; + } //_if + } //_foreach + + return $html; } );