From d3f8d19f88a9033004e029bb1cd07d19497b633f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ste=CC=81phane=20Goetz?= Date: Thu, 14 Apr 2016 12:02:31 +0200 Subject: [PATCH] Finalize table of contents feature, also for confluence upload --- docs/01_Features/Table_of_contents.md | 14 +++++++++++--- global.json | 3 +-- .../ContentTypes/Markdown/CommonMarkConverter.php | 9 +++++---- .../{TOC/Element.php => TableOfContents.php} | 4 ++-- .../{TOC/Parser.php => TableOfContentsParser.php} | 15 +++------------ .../ContentTypes/Markdown/CommonMarkConverter.php | 7 +++++-- .../ContentTypes/Markdown/TOCRenderer.php | 13 +++++++++++++ .../ContentTypes/Markdown/CommonMarkConverter.php | 15 +++++++++++++++ .../HTML/ContentTypes/Markdown/ContentType.php | 12 ++++++++++++ .../HTML}/ContentTypes/Markdown/TOC/Entry.php | 2 +- .../HTML/ContentTypes/Markdown/TOC/Processor.php} | 13 +++++++------ .../HTML/ContentTypes/Markdown/TOC/Renderer.php | 13 +++++++++++++ .../HTML}/ContentTypes/Markdown/TOC/RootEntry.php | 2 +- 13 files changed, 89 insertions(+), 33 deletions(-) rename libs/ContentTypes/Markdown/{TOC/Element.php => TableOfContents.php} (89%) rename libs/ContentTypes/Markdown/{TOC/Parser.php => TableOfContentsParser.php} (74%) create mode 100644 libs/Format/Confluence/ContentTypes/Markdown/TOCRenderer.php create mode 100644 libs/Format/HTML/ContentTypes/Markdown/CommonMarkConverter.php create mode 100644 libs/Format/HTML/ContentTypes/Markdown/ContentType.php rename libs/{ => Format/HTML}/ContentTypes/Markdown/TOC/Entry.php (95%) rename libs/{ContentTypes/Markdown/TOC/TOCProcessor.php => Format/HTML/ContentTypes/Markdown/TOC/Processor.php} (90%) create mode 100644 libs/Format/HTML/ContentTypes/Markdown/TOC/Renderer.php rename libs/{ => Format/HTML}/ContentTypes/Markdown/TOC/RootEntry.php (80%) diff --git a/docs/01_Features/Table_of_contents.md b/docs/01_Features/Table_of_contents.md index 682f27f..3136058 100644 --- a/docs/01_Features/Table_of_contents.md +++ b/docs/01_Features/Table_of_contents.md @@ -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 diff --git a/global.json b/global.json index 78b1df6..6c93a79 100644 --- a/global.json +++ b/global.json @@ -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": [], diff --git a/libs/ContentTypes/Markdown/CommonMarkConverter.php b/libs/ContentTypes/Markdown/CommonMarkConverter.php index 395326f..7cd7b54 100644 --- a/libs/ContentTypes/Markdown/CommonMarkConverter.php +++ b/libs/ContentTypes/Markdown/CommonMarkConverter.php @@ -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)); } diff --git a/libs/ContentTypes/Markdown/TOC/Element.php b/libs/ContentTypes/Markdown/TableOfContents.php similarity index 89% rename from libs/ContentTypes/Markdown/TOC/Element.php rename to libs/ContentTypes/Markdown/TableOfContents.php index b8467b4..c8dd5b3 100644 --- a/libs/ContentTypes/Markdown/TOC/Element.php +++ b/libs/ContentTypes/Markdown/TableOfContents.php @@ -1,9 +1,9 @@ -addBlock(new Element()); + $context->addBlock(new TableOfContents()); return true; } diff --git a/libs/Format/Confluence/ContentTypes/Markdown/CommonMarkConverter.php b/libs/Format/Confluence/ContentTypes/Markdown/CommonMarkConverter.php index 1655237..39ae5f1 100644 --- a/libs/Format/Confluence/ContentTypes/Markdown/CommonMarkConverter.php +++ b/libs/Format/Confluence/ContentTypes/Markdown/CommonMarkConverter.php @@ -1,6 +1,7 @@ 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()); diff --git a/libs/Format/Confluence/ContentTypes/Markdown/TOCRenderer.php b/libs/Format/Confluence/ContentTypes/Markdown/TOCRenderer.php new file mode 100644 index 0000000..9e5ee90 --- /dev/null +++ b/libs/Format/Confluence/ContentTypes/Markdown/TOCRenderer.php @@ -0,0 +1,13 @@ +'; + } +} diff --git a/libs/Format/HTML/ContentTypes/Markdown/CommonMarkConverter.php b/libs/Format/HTML/ContentTypes/Markdown/CommonMarkConverter.php new file mode 100644 index 0000000..8eb1220 --- /dev/null +++ b/libs/Format/HTML/ContentTypes/Markdown/CommonMarkConverter.php @@ -0,0 +1,15 @@ +addDocumentProcessor(new TOC\Processor($config)); + $environment->addBlockRenderer('Todaymade\Daux\ContentTypes\Markdown\TableOfContents', new TOC\Renderer()); + } +} diff --git a/libs/Format/HTML/ContentTypes/Markdown/ContentType.php b/libs/Format/HTML/ContentTypes/Markdown/ContentType.php new file mode 100644 index 0000000..96930eb --- /dev/null +++ b/libs/Format/HTML/ContentTypes/Markdown/ContentType.php @@ -0,0 +1,12 @@ +config = $config; + $this->converter = new CommonMarkConverter(['daux' => $config]); + } +} diff --git a/libs/ContentTypes/Markdown/TOC/Entry.php b/libs/Format/HTML/ContentTypes/Markdown/TOC/Entry.php similarity index 95% rename from libs/ContentTypes/Markdown/TOC/Entry.php rename to libs/Format/HTML/ContentTypes/Markdown/TOC/Entry.php index 5f1a6bc..561ee29 100644 --- a/libs/ContentTypes/Markdown/TOC/Entry.php +++ b/libs/Format/HTML/ContentTypes/Markdown/TOC/Entry.php @@ -1,4 +1,4 @@ -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())); diff --git a/libs/Format/HTML/ContentTypes/Markdown/TOC/Renderer.php b/libs/Format/HTML/ContentTypes/Markdown/TOC/Renderer.php new file mode 100644 index 0000000..4d91638 --- /dev/null +++ b/libs/Format/HTML/ContentTypes/Markdown/TOC/Renderer.php @@ -0,0 +1,13 @@ +renderBlocks($block->children()); + } +} diff --git a/libs/ContentTypes/Markdown/TOC/RootEntry.php b/libs/Format/HTML/ContentTypes/Markdown/TOC/RootEntry.php similarity index 80% rename from libs/ContentTypes/Markdown/TOC/RootEntry.php rename to libs/Format/HTML/ContentTypes/Markdown/TOC/RootEntry.php index cd2c4b5..c942ca2 100644 --- a/libs/ContentTypes/Markdown/TOC/RootEntry.php +++ b/libs/Format/HTML/ContentTypes/Markdown/TOC/RootEntry.php @@ -1,4 +1,4 @@ -