Finalize table of contents feature, also for confluence upload

This commit is contained in:
Stéphane Goetz 2016-04-14 12:02:31 +02:00
parent fb7343c109
commit d3f8d19f88
13 changed files with 89 additions and 33 deletions

View File

@ -1,7 +1,15 @@
Adding a table of contents becomes very easy with Daux.io
## Manual
Add `[TOC]` anywhere in your document and it will be replaced by a table of contents.
You can add it more than once in a page.
## Automatic
> Works only for html mode
A table of contents can be added automatically to all pages.
If `[TOC]` isn't present it will add it at the beginning of the page.
@ -10,10 +18,10 @@ You can enable this feature in your configuration
```json
{
"auto_toc": true
"html": {
"auto_toc": true
}
}
```
## Manual
Add `[TOC]` anywhere in your document and it will be replaced by a table of contents

View File

@ -18,8 +18,6 @@
"timezone": "America/Los_Angeles",
"auto_toc": false,
"live": {
"inherit_index": false,
"clean_urls": false
@ -33,6 +31,7 @@
"float": false,
"auto_landing": true,
"search": true,
"auto_toc": false,
"repo": "",
"twitter": [],

View File

@ -3,7 +3,9 @@
use League\CommonMark\DocParser;
use League\CommonMark\Environment;
use League\CommonMark\HtmlRenderer;
use Todaymade\Daux\Config;
use Todaymade\Daux\ContentTypes\Markdown\TOC\Parser;
use Todaymade\Daux\ContentTypes\Markdown\TOC\Renderer;
use Todaymade\Daux\ContentTypes\Markdown\TOC\TOCProcessor;
use Webuni\CommonMark\TableExtension\TableExtension;
@ -21,10 +23,9 @@ class CommonMarkConverter extends \League\CommonMark\CommonMarkConverter
$environment->addExtension(new TableExtension());
// Table of Contents
$environment->addBlockParser(new Parser());
$environment->addDocumentProcessor(new TOCProcessor($config['daux']));
$environment->addBlockParser(new TableOfContentsParser());
$this->extendEnvironment($environment);
$this->extendEnvironment($environment, $config['daux']);
if (array_key_exists('processor_instance', $config['daux'])) {
$config['daux']['processor_instance']->extendCommonMarkEnvironment($environment);
@ -39,7 +40,7 @@ class CommonMarkConverter extends \League\CommonMark\CommonMarkConverter
return new LinkRenderer($environment->getConfig('daux'));
}
protected function extendEnvironment(Environment $environment)
protected function extendEnvironment(Environment $environment, Config $config)
{
$environment->addInlineRenderer('Link', $this->getLinkRenderer($environment));
}

View File

@ -1,9 +1,9 @@
<?php namespace Todaymade\Daux\ContentTypes\Markdown\TOC;
<?php namespace Todaymade\Daux\ContentTypes\Markdown;
use League\CommonMark\Block\Element\AbstractBlock;
use League\CommonMark\Cursor;
class Element extends AbstractBlock
class TableOfContents extends AbstractBlock
{
/**

View File

@ -1,19 +1,10 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: onigoetz
* Date: 09/04/16
* Time: 23:03
*/
namespace Todaymade\Daux\ContentTypes\Markdown\TOC;
<?php namespace Todaymade\Daux\ContentTypes\Markdown;
use League\CommonMark\Block\Parser\AbstractBlockParser;
use League\CommonMark\ContextInterface;
use League\CommonMark\Cursor;
class Parser extends AbstractBlockParser
class TableOfContentsParser extends AbstractBlockParser
{
/**
@ -37,7 +28,7 @@ class Parser extends AbstractBlockParser
return false;
}
$context->addBlock(new Element());
$context->addBlock(new TableOfContents());
return true;
}

View File

@ -1,6 +1,7 @@
<?php namespace Todaymade\Daux\Format\Confluence\ContentTypes\Markdown;
use League\CommonMark\Environment;
use Todaymade\Daux\Config;
class CommonMarkConverter extends \Todaymade\Daux\ContentTypes\Markdown\CommonMarkConverter
{
@ -9,9 +10,11 @@ class CommonMarkConverter extends \Todaymade\Daux\ContentTypes\Markdown\CommonMa
return new LinkRenderer($environment->getConfig('daux'));
}
protected function extendEnvironment(Environment $environment)
protected function extendEnvironment(Environment $environment, Config $config)
{
parent::extendEnvironment($environment);
parent::extendEnvironment($environment, $config);
$environment->addBlockRenderer('Todaymade\Daux\ContentTypes\Markdown\TableOfContents', new TOCRenderer());
//Add code renderer
$environment->addBlockRenderer('FencedCode', new FencedCodeRenderer());

View File

@ -0,0 +1,13 @@
<?php namespace Todaymade\Daux\Format\Confluence\ContentTypes\Markdown;
use League\CommonMark\Block\Element\AbstractBlock;
use League\CommonMark\Block\Renderer\BlockRendererInterface;
use League\CommonMark\ElementRendererInterface;
class TOCRenderer implements BlockRendererInterface
{
public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false)
{
return '<ac:structured-macro ac:name="toc"></ac:structured-macro>';
}
}

View File

@ -0,0 +1,15 @@
<?php namespace Todaymade\Daux\Format\HTML\ContentTypes\Markdown;
use League\CommonMark\Environment;
use Todaymade\Daux\Config;
class CommonMarkConverter extends \Todaymade\Daux\ContentTypes\Markdown\CommonMarkConverter
{
protected function extendEnvironment(Environment $environment, Config $config)
{
parent::extendEnvironment($environment, $config);
$environment->addDocumentProcessor(new TOC\Processor($config));
$environment->addBlockRenderer('Todaymade\Daux\ContentTypes\Markdown\TableOfContents', new TOC\Renderer());
}
}

View File

@ -0,0 +1,12 @@
<?php namespace Todaymade\Daux\Format\HTML\ContentTypes\Markdown;
use Todaymade\Daux\Config;
class ContentType extends \Todaymade\Daux\ContentTypes\Markdown\ContentType
{
public function __construct(Config $config)
{
$this->config = $config;
$this->converter = new CommonMarkConverter(['daux' => $config]);
}
}

View File

@ -1,4 +1,4 @@
<?php namespace Todaymade\Daux\ContentTypes\Markdown\TOC;
<?php namespace Todaymade\Daux\Format\HTML\ContentTypes\Markdown\TOC;
use League\CommonMark\Block\Element\Heading;

View File

@ -1,4 +1,4 @@
<?php namespace Todaymade\Daux\ContentTypes\Markdown\TOC;
<?php namespace Todaymade\Daux\Format\HTML\ContentTypes\Markdown\TOC;
use DeepCopy\DeepCopy;
use League\CommonMark\Block\Element\Document;
@ -13,9 +13,10 @@ use League\CommonMark\Inline\Element\Text;
use League\CommonMark\Node\Node;
use ReflectionMethod;
use Todaymade\Daux\Config;
use Todaymade\Daux\ContentTypes\Markdown\TableOfContents;
use Todaymade\Daux\DauxHelper;
class TOCProcessor implements DocumentProcessorInterface
class Processor implements DocumentProcessorInterface
{
protected $config;
@ -26,7 +27,7 @@ class TOCProcessor implements DocumentProcessorInterface
public function hasAutoTOC()
{
return array_key_exists('auto_toc', $this->config) && $this->config['auto_toc'];
return array_key_exists('html', $this->config) && array_key_exists('auto_toc', $this->config['html']) && $this->config['html']['auto_toc'];
}
/**
@ -36,7 +37,7 @@ class TOCProcessor implements DocumentProcessorInterface
*/
public function processDocument(Document $document)
{
/** @var Element[] $tocs */
/** @var TableOfContents[] $tocs */
$tocs = [];
$headings = [];
@ -45,7 +46,7 @@ class TOCProcessor implements DocumentProcessorInterface
while ($event = $walker->next()) {
$node = $event->getNode();
if ($node instanceof Element && !$event->isEntering()) {
if ($node instanceof TableOfContents && !$event->isEntering()) {
$tocs[] = $node;
continue;
}
@ -64,7 +65,7 @@ class TOCProcessor implements DocumentProcessorInterface
if (count($tocs)) {
foreach ($tocs as $toc) {
$toc->replaceWith($this->render($generated->getChildren()));
$toc->appendChild($this->render($generated->getChildren()));
}
} else {
$document->prependChild($this->render($generated->getChildren()));

View File

@ -0,0 +1,13 @@
<?php namespace Todaymade\Daux\Format\HTML\ContentTypes\Markdown\TOC;
use League\CommonMark\Block\Element\AbstractBlock;
use League\CommonMark\Block\Renderer\BlockRendererInterface;
use League\CommonMark\ElementRendererInterface;
class Renderer implements BlockRendererInterface
{
public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false)
{
return $htmlRenderer->renderBlocks($block->children());
}
}

View File

@ -1,4 +1,4 @@
<?php namespace Todaymade\Daux\ContentTypes\Markdown\TOC;
<?php namespace Todaymade\Daux\Format\HTML\ContentTypes\Markdown\TOC;
class RootEntry extends Entry
{