Skip to content

Commit

Permalink
Merge branch 'release/1.3.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sommerregen committed Oct 24, 2015
2 parents 60f9d41 + 1ecae9a commit dbaef77
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 37 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# v1.3.1
## 10/24/2015

2. [](#improved)
* Do not render TOC if it is empty. [#6](https://github.com/Sommerregen/grav-plugin-toc/issues/6) & [#7](https://github.com/Sommerregen/grav-plugin-toc/pull/7)
3. [](#bugfix)
* Fixed [#5](https://github.com/Sommerregen/grav-plugin-toc/pull/5) (Fix typo in `README.md`)

# v1.3.0
## 09/24/2015

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* [Twig Filter](#twig-filter)
* [CSS Stylesheet Override](#css-stylesheet-override)
* [Contributing](#contributing)
* [Licencse](#license)
* [License](#license)

## About

Expand Down
2 changes: 1 addition & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "Toc"
version: 1.3.0
version: 1.3.1
description: "This plugin automagically generates a (minified) Table of Contents based on special markers in the document and adds it into the resulting HTML document."
icon: language
author:
Expand Down
73 changes: 40 additions & 33 deletions classes/Toc.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,48 +246,55 @@ public function render($content, $options = [], $page = null)
$replacements = [];
// Find all occurrences of TOC and MINITOC in content
$regex = '~(<p>)?\s*\[(?P<type>(?:MINI)?TOC)\]\s*(?(1)</p>)~i';
if (preg_match_all($regex, $content, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
// Generate TOC
$toc = $this->createToc($content);
foreach ($matches as $match) {
$offset = $match[0][1];
$type = strtolower($match['type'][0]);
if (preg_match_all($regex, $content, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER) === false) {
return $content;
}

// Initialize variables
$current = -1;
$minitoc = [];
// Generate TOC
$toc = $this->createToc($content);
if (empty($toc)) {
// Hide (mini-)toc marker
return preg_replace($regex, '', $content);
}

if ($type == 'toc') {
$minitoc = $toc;
} else {
// Get current (sub-)heading
foreach ($toc as $index => $heading) {
if ($index < $offset) {
$current = $index;
foreach ($matches as $match) {
$offset = $match[0][1];
$type = strtolower($match['type'][0]);

// Initialize variables
$current = -1;
$minitoc = [];

if ($type == 'toc') {
$minitoc = $toc;
} else {
// Get current (sub-)heading
foreach ($toc as $index => $heading) {
if ($index < $offset) {
$current = $index;
} else {
$level = $toc[$current]['level'];
if ($heading['level'] > $level) {
$minitoc[$index] = $heading;
} else {
$level = $toc[$current]['level'];
if ($heading['level'] > $level) {
$minitoc[$index] = $heading;
} else {
break;
}
break;
}
}
}
}

// Render TOC
$vars['toc'] = [
'list' => $minitoc,
'type' => $type,
'heading' => ($current > -1) ? $toc[$current] : null,
] + $options->toArray();
// Render TOC
$vars['toc'] = [
'list' => $minitoc,
'type' => $type,
'heading' => ($current > -1) ? $toc[$current] : null,
] + $options->toArray();

$template = 'partials/toc' . TEMPLATE_EXT;
$minitoc = $twig->processTemplate($template, $vars);
$template = 'partials/toc' . TEMPLATE_EXT;
$minitoc = $twig->processTemplate($template, $vars);

// Save rendered TOC for later replacement
$replacements[] = $minitoc;
}
// Save rendered TOC for later replacement
$replacements[] = $minitoc;
}

// Tocify content
Expand Down
4 changes: 2 additions & 2 deletions toc.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Toc v1.3.0
* Toc v1.3.1
*
* This plugin automagically generates a (minified) Table of Contents
* based on special markers in the document and adds it into the
Expand All @@ -10,7 +10,7 @@
* http://benjamin-regler.de/license/
*
* @package Toc
* @version 1.3.0
* @version 1.3.1
* @link <https://github.com/sommerregen/grav-plugin-external-links>
* @author Benjamin Regler <[email protected]>
* @copyright 2015, Benjamin Regler
Expand Down

0 comments on commit dbaef77

Please sign in to comment.