diff options
author | Felix Mulder <felix.mulder@gmail.com> | 2017-01-05 17:15:02 +0100 |
---|---|---|
committer | Felix Mulder <felix.mulder@gmail.com> | 2017-01-31 14:29:18 +0100 |
commit | 1dc446fc8973dce2da0bbb3c6eb97aa035d0d191 (patch) | |
tree | 8cb3c6bb35d5c2d82fdecc44c73d8fd77d47d817 /doc-tool/src/dotty/tools | |
parent | 56cec80db7ab46c69cbbc8551ddf3c9857f8a804 (diff) | |
download | dotty-1dc446fc8973dce2da0bbb3c6eb97aa035d0d191.tar.gz dotty-1dc446fc8973dce2da0bbb3c6eb97aa035d0d191.tar.bz2 dotty-1dc446fc8973dce2da0bbb3c6eb97aa035d0d191.zip |
Add initial page rendering using liquid and yaml front matter
Diffstat (limited to 'doc-tool/src/dotty/tools')
-rw-r--r-- | doc-tool/src/dotty/tools/dottydoc/core/ContextDottydoc.scala | 5 | ||||
-rw-r--r-- | doc-tool/src/dotty/tools/dottydoc/staticsite/Page.scala | 50 |
2 files changed, 54 insertions, 1 deletions
diff --git a/doc-tool/src/dotty/tools/dottydoc/core/ContextDottydoc.scala b/doc-tool/src/dotty/tools/dottydoc/core/ContextDottydoc.scala index 963c72bc9..c2dd3fdef 100644 --- a/doc-tool/src/dotty/tools/dottydoc/core/ContextDottydoc.scala +++ b/doc-tool/src/dotty/tools/dottydoc/core/ContextDottydoc.scala @@ -8,7 +8,9 @@ import model.Package import com.vladsch.flexmark.parser.ParserEmulationFamily import com.vladsch.flexmark.parser.Parser import com.vladsch.flexmark.ext.tables.TablesExtension +import com.vladsch.flexmark.ext.front.matter.YamlFrontMatterExtension import com.vladsch.flexmark.util.options.{ DataHolder, MutableDataSet } +import java.util.{ Collections => JCollections } class ContextDottydoc extends ContextDocstrings { import scala.collection.mutable @@ -28,5 +30,6 @@ class ContextDottydoc extends ContextDocstrings { val markdownOptions: DataHolder = new MutableDataSet() .setFrom(ParserEmulationFamily.KRAMDOWN.getOptions) - .set(Parser.EXTENSIONS, java.util.Collections.singleton(TablesExtension.create())) + .set(Parser.EXTENSIONS, JCollections.singleton(TablesExtension.create())) + .set(Parser.EXTENSIONS, JCollections.singleton(YamlFrontMatterExtension.create())) } diff --git a/doc-tool/src/dotty/tools/dottydoc/staticsite/Page.scala b/doc-tool/src/dotty/tools/dottydoc/staticsite/Page.scala new file mode 100644 index 000000000..7048109fd --- /dev/null +++ b/doc-tool/src/dotty/tools/dottydoc/staticsite/Page.scala @@ -0,0 +1,50 @@ +package dotty.tools +package dottydoc +package staticsite + +import dotc.core.Contexts.Context +import dotty.tools.dottydoc.util.syntax._ + +import com.vladsch.flexmark.html.HtmlRenderer +import com.vladsch.flexmark.parser.Parser +import com.vladsch.flexmark.ext.front.matter.AbstractYamlFrontMatterVisitor +import liqp.{ Template => LiquidTemplate } + +class MarkdownPage(fileContents: => String, params: Map[String, AnyRef]) { + import scala.collection.JavaConverters._ + + def yaml(implicit ctx: Context): Map[String, String] = { + if (_yaml eq null) initFields() + _yaml + } + + def html(implicit ctx: Context): String = { + if (_html eq null) initFields() + _html + } + + private[this] var _yaml: Map[String, String] = _ + private[this] var _html: String = _ + private[this] def initFields()(implicit ctx: Context) = { + val template = LiquidTemplate.parse(fileContents) + val yamlCollector = new AbstractYamlFrontMatterVisitor() + val mdParser = Parser.builder(ctx.docbase.markdownOptions).build + + yamlCollector.visit(mdParser.parse(fileContents)) + + _yaml = yamlCollector + .getData().asScala + .mapValues(_.asScala.headOption.getOrElse("")) + .toMap + + // make accessible via "{{ page.title }}" in templates + val page = Map("page" -> _yaml.asJava) + val renderedTemplate = template.render((page ++ params).asJava) + + _html = HtmlRenderer + .builder(ctx.docbase.markdownOptions) + .escapeHtml(false) + .build() + .render(mdParser.parse(renderedTemplate)) + } +} |