Phile Table Of Contents

Plugin Name:phileTableOfContents
Version:0.5
Description:Generate a table of contents from page headers
Last Update:2013/04/26
Author:jacmgr
Comments: See Comment Section Tags:

Introduction

I installed the phileTableOfContents plugin by pschmitt. See his page for instructions and usage.

Installing

I ran into some issues installing. I recieved 2 errors that I fixed this way.

Parse error: syntax error, unexpected '[' in C:\xampp\htdocs\phile10\philesystem\plugins\pschmitt\tableOfContents\Classes\Plugin.php on line 148

I modified the Plugin.php this way:

//jacmgr gets error on this line
//$occurences = array_count_values($titles)[$id];
//try this.
$occurences = array_count_values($titles); //[$id];

This error

[8] Undefined property: Phile\Plugin\Pschmitt\TableOfContents\Plugin::$currentPageUrl in C:\xampp\htdocs\phile10\philesystem\plugins\pschmitt\tableOfContents\Classes\Plugin.php on line 159

I modified the plugin this way

// see this page for solution
// http://stackoverflow.com/questions/8218230/php-domdocument-loadhtml-not-encoding-utf-8-correctly
$domdoc = new \DOMDocument();
libxml_use_internal_errors(true);  //jacmgr
$domdoc->loadHTML('<?xml encoding="utf-8" ?>' . $content);
libxml_use_internal_errors(false); //jacmgr
$xp = new \DOMXPath($domdoc);

URLS

I also had a problem that the generated link urls were not properly referencing the current page. (This is related to how I baseurl works I think.) I modified the plugin to add the page url into each link.

Added 1 line to get the page url

case 'after_parse_content':
$this->currentPageUrl = basename($data['page']->getUrl()); //jacmgr
$this->after_parse_content($data['content']);

Then later when building the links used

<a href="'.$this->currentPageUrl.'#'

instead of

<a href="#'

and later

$a->setAttribute('href', $this->currentPageUrl.'#top');

instead of

$a->setAttribute('href', '#top');

Putting the tags in your template.

Simply put these tags in your layout/template that is being use for the current page. Since you probably don't want the menu in every page, you may have different templates and specify the template to use in the pages meta

What I did

I use my plugin Jaccms Meta Data Plugin. I am only showing this on the docs hierarchy in my site. Those pages already use a separate template that is defined in the docs/index.md meta. So I just added the twig tags in that template.

Future

Tweak the CSS and location of menu on the page.

Although the plugin only prints the menu in templates where the tag is shown, IT DOES PUT THE TOP LINK ON EVERY HEADER regardless of template tags. Not sure how to stop that? I don't really want those top links. I think i will approach it with another meta tag.