Skip to content

Commit

Permalink
limited parameters to toc tag
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippMundhenk committed Mar 4, 2016
1 parent b7fdb3c commit 66e9f26
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 28 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ Headline 5: (l5: Headline5)<br/>
Headline 6: (l6: Headline6)

##Additional Options
The following are additional options for formatting the table of contents and the contained links
The following are additional options for formatting the table of contents and the contained links. All parameters go into the (toc) tag.
- levelchar: Allows to change the character used to separate different levels in the table of contents, usage: *levelchar: >* or *levelchar: space*

Note that the following options change the link behavior and thus need to be added to all tags that are supposed to work together, so the (toc), as well as all (l1), (l2), etc.
- 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*

###Example<br/>
(toc:6 split: - lowercase: 1 levelchar: >)<br/>
(l2: Test testä? split: - lowercase: 1)<br/>
(l3: Test2 testä? split: - lowercase: 1)<br/>
(l2: Test testä?)<br/>
(l3: Test2 testä?)<br/>

##Installation
copy to your kirby/site/tags folder
Expand Down
217 changes: 194 additions & 23 deletions site/tags/sections.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,42 @@

kirbytext::$tags['l1'] = array(
'attr' => array(
'split',
'lowercase'
),
'html' => function($tag) {
$link = $tag->attr('l1');

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;
}
}
}
}

$link = $tag->attr('l1');
$link = preg_replace("/[^ \w]+/", "", $link);
$splitchar = $tag->attr('split', "%20");
$link = str_replace(" ", $splitchar, $link);
if($tag->attr('lowercase')==1) {
if($lowercase==1) {
$link = strtolower($link);
}
return '<h1><a name="' . $link . '">'. $tag->attr('l1') . '</a></h1>';
Expand All @@ -23,11 +50,40 @@
'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;
}
}
}
}

$link = $tag->attr('l2');
$link = preg_replace("/[^ \w]+/", "", $link);
$splitchar = $tag->attr('split', "%20");
$link = str_replace(" ", $splitchar, $link);
if($tag->attr('lowercase')==1) {
if($lowercase==1) {
$link = strtolower($link);
}
return '<h2><a name="' . $link . '">'. $tag->attr('l2') . '</a></h2>';
Expand All @@ -40,11 +96,40 @@
'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;
}
}
}
}

$link = $tag->attr('l3');
$link = preg_replace("/[^ \w]+/", "", $link);
$splitchar = $tag->attr('split', "%20");
$link = str_replace(" ", $splitchar, $link);
if($tag->attr('lowercase')==1) {
if($lowercase==1) {
$link = strtolower($link);
}
return '<h3><a name="' . $link . '">'. $tag->attr('l3') . '</a></h3>';
Expand All @@ -57,11 +142,40 @@
'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;
}
}
}
}

$link = $tag->attr('l4');
$link = preg_replace("/[^ \w]+/", "", $link);
$splitchar = $tag->attr('split', "%20");
$link = str_replace(" ", $splitchar, $link);
if($tag->attr('lowercase')==1) {
if($lowercase==1) {
$link = strtolower($link);
}
return '<h4><a name="' . $link . '">'. $tag->attr('l4') . '</a></h4>';
Expand All @@ -74,11 +188,40 @@
'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;
}
}
}
}

$link = $tag->attr('l5');
$link = preg_replace("/[^ \w]+/", "", $link);
$splitchar = $tag->attr('split', "%20");
$link = str_replace(" ", $splitchar, $link);
if($tag->attr('lowercase')==1) {
if($lowercase==1) {
$link = strtolower($link);
}
return '<h5><a name="' . $link . '">'. $tag->attr('l5') . '</a></h5>';
Expand All @@ -91,11 +234,40 @@
'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;
}
}
}
}

$link = $tag->attr('l6');
$link = preg_replace("/[^ \w]+/", "", $link);
$splitchar = $tag->attr('split', "%20");
$link = str_replace(" ", $splitchar, $link);
if($tag->attr('lowercase')==1) {
if($lowercase==1) {
$link = strtolower($link);
}
return '<h6><a name="' . $link . '">'. $tag->attr('l6') . '</a></h6>';
Expand Down Expand Up @@ -125,28 +297,27 @@
$name = preg_split("/\(l[1-6]?: /",$heading);
$name = preg_split("/\)/",$name[1]);
$link = $name[0];
$link = preg_replace("/\ split: ./", "", $link);
$link = preg_replace("/\ lowercase: \d/", "", $link);
// $link = normalizer_normalize( $name[0], Normalizer::FORM_C );

$link = preg_replace("/[^ \w]+/", "", $link);
$link = str_replace(" ", $splitchar, $link);
$link = htmlentities($link);
if($tag->attr('lowercase')==1) {
$link = strtolower($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);
$name = preg_replace("/\ split:(\ )?./", "", $name[0]);
$name = preg_replace("/\ lowercase:(\ )?\d/", "", $name);

$html .= '<a href="#';
$html .= $link;
Expand Down

0 comments on commit 66e9f26

Please sign in to comment.