Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Includes support #36

Draft
wants to merge 2 commits into
base: parser
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions documentation/_includes/html_include_from_includes_dir.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h3>File, I am your HTML include</h3>

html html html html html html html

html html html html html html html

<hr>
7 changes: 7 additions & 0 deletions documentation/_includes/md_include_from_includes_dir.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### File, I am your MD include

md md md md md md md md md md md md

md md md md md md md md md md md md

---
7 changes: 7 additions & 0 deletions documentation/docs/static-page/includes_html.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: Includes HTML
---

{% myinclude md_include_from_includes_dir.md %}

{% myinclude html_include_from_includes_dir.html %}
7 changes: 7 additions & 0 deletions documentation/docs/static-page/includes_md.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: Includes MD
---

{% myinclude md_include_from_includes_dir.md %}

{% 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
""
}
}