Skip to content

Commit

Permalink
Add includes support
Browse files Browse the repository at this point in the history
  • Loading branch information
mkondratek committed Oct 28, 2020
1 parent c76b54c commit cd58666
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
4 changes: 2 additions & 2 deletions documentation/docs/static-page/includes_html.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
title: Includes HTML
---

{% include md_include_from_includes_dir.md %}
{% myinclude md_include_from_includes_dir.md %}

{% include html_include_from_includes_dir.html %}
{% myinclude html_include_from_includes_dir.html %}
4 changes: 2 additions & 2 deletions documentation/docs/static-page/includes_md.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
title: Includes MD
---

{% include md_include_from_includes_dir.md %}
{% myinclude md_include_from_includes_dir.md %}

{% include html_include_from_includes_dir.html %}
{% myinclude html_include_from_includes_dir.html %}
13 changes: 12 additions & 1 deletion src/main/kotlin/com/virtuslab/dokka/site/StaticSiteContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class StaticSiteContext(val root: File, cxt: DokkaContext) {
dirs.map { loadTemplateFile(it) }.map { it.name() to it }.toMap()
}

private val includes: Map<String, String> by lazy {
val includeRoot = File(root, "_includes")
val dirs: Array<File> = includeRoot.listFiles() ?: emptyArray()
dirs.map { loadTemplateFile(it) }.map { it.file.name to it.rawCode }.toMap()
}

private fun isValidTemplate(file: File): Boolean =
(file.isDirectory && !file.name.startsWith("_")) ||
Expand Down Expand Up @@ -124,7 +129,13 @@ class StaticSiteContext(val root: File, cxt: DokkaContext) {
val properties = myTemplate.templateFile.layout()
?.let { mapOf("content" to myTemplate.templateFile.rawCode) } ?: emptyMap()

myTemplate.templateFile.resolveMarkdown(RenderingContext(properties, layouts))
val ctx = RenderingContext(
properties = properties,
layouts = layouts,
includes = includes
)

myTemplate.templateFile.resolveMarkdown(ctx)
} catch (e: Throwable) {
val msg = "Error rendering $myTemplate: ${e.message}"
println("ERROR: $msg") // TODO (#14): provide proper error handling
Expand Down
30 changes: 26 additions & 4 deletions src/main/kotlin/com/virtuslab/dokka/site/templates.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import com.vladsch.flexmark.parser.ParserEmulationProfile
import com.vladsch.flexmark.util.options.DataHolder
import com.vladsch.flexmark.util.options.MutableDataSet
import liqp.Template
import liqp.TemplateContext
import liqp.nodes.LNode
import liqp.tags.Tag
import java.io.File
import java.util.*

Expand All @@ -38,6 +41,7 @@ val defaultMarkdownOptions: DataHolder =

data class RenderingContext(
val properties: Map<String, Any>,
val includes: Map<String, String> = emptyMap(),
val layouts: Map<String, TemplateFile> = emptyMap(),
val resolving: Set<String> = emptySet(),
val markdownOptions: DataHolder = defaultMarkdownOptions,
Expand Down Expand Up @@ -105,12 +109,14 @@ data class TemplateFile(
fun hasFrame(): Boolean = stringSetting("hasFrame") != "false"


fun resolveMarkdown(ctx: RenderingContext): PreResolvedPage =
resolveInner(
ctx = ctx.copy(properties = HashMap(ctx.properties) + ("page" to mapOf("title" to title()))),
fun resolveMarkdown(ctx: RenderingContext): PreResolvedPage {
val properties = HashMap(ctx.properties) + ("page" to mapOf("title" to title())) + HashMap(ctx.includes)
return resolveInner(
ctx = ctx.copy(properties = properties),
stopAtHtml = true,
!isHtml // This is toplevel template
)
}

fun resolveToHtml(ctx: RenderingContext, hasMarkdown: Boolean): PreResolvedPage =
resolveInner(ctx, stopAtHtml = false, hasMarkdown)
Expand All @@ -130,6 +136,7 @@ data class TemplateFile(
return if (stopAtHtml && isHtml) {
PreResolvedPage(ctx.properties["content"].toString(), LayoutInfo(this, ctx), hasMarkdown)
} else {
Tag.registerTag(MyInclude())
val rendered =
Template.parse(this.rawCode).render(HashMap(ctx.properties)) // Library requires mutable maps..
val code = if (!isHtml) rendered else {
Expand Down Expand Up @@ -166,8 +173,23 @@ fun loadTemplateFile(file: File): TemplateFile {

return TemplateFile(
file = file,
file.name.endsWith(".html"),
isHtml = file.name.endsWith(".html"),
rawCode = content.joinToString(LineSeparator),
settings = yamlCollector.data
)
}


class MyInclude : Tag() {
override fun render(context: TemplateContext, vararg nodes: LNode): Any = try {
if (nodes.size > 1) {
println("ERROR: Multiple include nodes") // TODO (#14): provide proper error handling
}
nodes[0].render(context)
val includeResource = super.asString(context.get(nodes[0].toString()))
Template.parse(includeResource, context.flavor).render(context.variables)
} catch (e: Exception) {
println("ERROR: include rendering failure: $e") // TODO (#14): provide proper error handling
""
}
}

0 comments on commit cd58666

Please sign in to comment.