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 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 ## Automatic
> Works only for html mode
A table of contents can be added automatically to all pages. 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. 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 ```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", "timezone": "America/Los_Angeles",
"auto_toc": false,
"live": { "live": {
"inherit_index": false, "inherit_index": false,
"clean_urls": false "clean_urls": false
@ -33,6 +31,7 @@
"float": false, "float": false,
"auto_landing": true, "auto_landing": true,
"search": true, "search": true,
"auto_toc": false,
"repo": "", "repo": "",
"twitter": [], "twitter": [],

View File

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

View File

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

View File

@ -1,6 +1,7 @@
<?php namespace Todaymade\Daux\Format\Confluence\ContentTypes\Markdown; <?php namespace Todaymade\Daux\Format\Confluence\ContentTypes\Markdown;
use League\CommonMark\Environment; use League\CommonMark\Environment;
use Todaymade\Daux\Config;
class CommonMarkConverter extends \Todaymade\Daux\ContentTypes\Markdown\CommonMarkConverter 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')); 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 //Add code renderer
$environment->addBlockRenderer('FencedCode', new FencedCodeRenderer()); $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; 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 DeepCopy\DeepCopy;
use League\CommonMark\Block\Element\Document; use League\CommonMark\Block\Element\Document;
@ -13,9 +13,10 @@ use League\CommonMark\Inline\Element\Text;
use League\CommonMark\Node\Node; use League\CommonMark\Node\Node;
use ReflectionMethod; use ReflectionMethod;
use Todaymade\Daux\Config; use Todaymade\Daux\Config;
use Todaymade\Daux\ContentTypes\Markdown\TableOfContents;
use Todaymade\Daux\DauxHelper; use Todaymade\Daux\DauxHelper;
class TOCProcessor implements DocumentProcessorInterface class Processor implements DocumentProcessorInterface
{ {
protected $config; protected $config;
@ -26,7 +27,7 @@ class TOCProcessor implements DocumentProcessorInterface
public function hasAutoTOC() 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) public function processDocument(Document $document)
{ {
/** @var Element[] $tocs */ /** @var TableOfContents[] $tocs */
$tocs = []; $tocs = [];
$headings = []; $headings = [];
@ -45,7 +46,7 @@ class TOCProcessor implements DocumentProcessorInterface
while ($event = $walker->next()) { while ($event = $walker->next()) {
$node = $event->getNode(); $node = $event->getNode();
if ($node instanceof Element && !$event->isEntering()) { if ($node instanceof TableOfContents && !$event->isEntering()) {
$tocs[] = $node; $tocs[] = $node;
continue; continue;
} }
@ -64,7 +65,7 @@ class TOCProcessor implements DocumentProcessorInterface
if (count($tocs)) { if (count($tocs)) {
foreach ($tocs as $toc) { foreach ($tocs as $toc) {
$toc->replaceWith($this->render($generated->getChildren())); $toc->appendChild($this->render($generated->getChildren()));
} }
} else { } else {
$document->prependChild($this->render($generated->getChildren())); $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 class RootEntry extends Entry
{ {