Skip to content

Commit

Permalink
Redesign layouts support - add isHtml to ResolvedPage
Browse files Browse the repository at this point in the history
  • Loading branch information
mkondratek committed Aug 17, 2020
1 parent c0082c1 commit 902319f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
17 changes: 7 additions & 10 deletions src/main/kotlin/com/virtuslab/dokka/site/processors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -195,28 +195,25 @@ class SitePagesCreator(cxt: DokkaContext) : BaseStaticSiteProcessor(cxt) {
} else from
)


val resolvedPage = try {
val context = RenderingContext(emptyMap(), layouts)
val context = RenderingContext(mapOf("content" to templateFile.rawCode), layouts)
if (from.isDirectory) {
children.find { it.isIndexPage }?.resolved ?: EmptyResolvedPage
} else if (templateFile.layout() != null ){
templateFile.layout().let { layouts[it] }
?. resolve(context.nest(templateFile.rawCode, from.absolutePath, emptyList()))
?: throw Exception("Failed to resolve (layout '${templateFile.layout()}' might not be defined")
} else { // no layout
} else {
templateFile.resolve(context)
}
} catch (e: Throwable) {
val msg = "Error rendering $from: ${e.message}"
println("ERROR: $msg") // TODO (#14): provide proper error handling
ResolvedPage(msg, emptyList())
ResolvedPage(msg)
}

val parser = HtmlParser()
val parser =
if (resolvedPage.isHtml) HtmlParser()
else MarkdownParser(logger = DokkaConsoleLogger)

val docTag = try {
parser.parseStringToDocNode(resolvedPage.html)
parser.parseStringToDocNode(resolvedPage.code)
} catch (e: Throwable) {
val msg = "Error rendering $from: ${e.message}"
println("ERROR: $msg") // TODO (#14): provide proper error handling
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/virtuslab/dokka/site/renderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ExternalDocsToolRenderer(context: DokkaContext) : org.jetbrains.dokka.base
script { unsafe { +"""var pathToRoot = "${locationProvider.resolveRoot(page)}";""" } }
}
body {
unsafe { +page.resolved.html }
unsafe { +page.resolved.code }
}
}
else ->
Expand Down
12 changes: 7 additions & 5 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,7 @@ import com.vladsch.flexmark.parser.ParserEmulationProfile
import com.vladsch.flexmark.util.options.DataHolder
import com.vladsch.flexmark.util.options.MutableDataSet
import liqp.Template
import org.jetbrains.dokka.model.doc.DocTag
import java.io.File
import java.util.HashMap

Expand Down Expand Up @@ -43,7 +44,7 @@ data class RenderingContext(
val markdownOptions: DataHolder = defaultMardownOptions,
val resources: List<String> = emptyList()
) {
fun nest(code: String, path: String, resources: List<String>) =
internal fun nest(code: String, path: String, resources: List<String>) =
copy(
resolving = resolving + path,
properties = properties + ("content" to code),
Expand All @@ -52,11 +53,12 @@ data class RenderingContext(
}

data class ResolvedPage(
val html: String,
val code: String,
val isHtml: Boolean = true,
val resources: List<String> = emptyList()
)

val EmptyResolvedPage = ResolvedPage("")
val EmptyResolvedPage = ResolvedPage(code = "")

data class TemplateFile(val file: File, val rawCode: String, private val settings: Map<String, List<String>>) {
private fun stringSetting(name: String): String? {
Expand All @@ -81,15 +83,15 @@ data class TemplateFile(val file: File, val rawCode: String, private val setting
throw java.lang.RuntimeException("Cycle in templates involving $file: ${ctx.resolving}")

val rendered = Template.parse(this.rawCode).render(HashMap(ctx.properties)) // Library requires mutable maps..
val code = if (isHtml) rendered else {
val code = if (!isHtml) rendered else {
val parser: Parser = Parser.builder().build()
HtmlRenderer.builder(ctx.markdownOptions).build().render(parser.parse(rendered))
}
val resources = listSetting("extraCSS") + listSetting("extraJS")
return layout()?.let {
val layoutTemplate = ctx.layouts[it] ?: throw RuntimeException("No layouts named $it in ${ctx.layouts}")
layoutTemplate.resolveInner(ctx.nest(code, file.absolutePath, resources))
} ?: ResolvedPage(code, resources + ctx.resources)
} ?: ResolvedPage(code, isHtml, resources + ctx.resources)
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/test/kotlin/headerTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TemplateFileTests {
) {
assertEquals(
"<p>Ala <p>ma kota w <strong>paski</strong></p>\n. Hej!</p>",
it.layouts["content"]!!.resolve(it).html.trim()
it.layouts["content"]!!.resolve(it).code.trim()
)
}
}
Expand Down Expand Up @@ -131,7 +131,7 @@ class TemplateFileTests {
) {
assertEquals(
expected,
it.layouts["content"]!!.resolve(it).html.trim()
it.layouts["content"]!!.resolve(it).code.trim()
)
}
}
Expand All @@ -144,7 +144,7 @@ class TemplateFileTests {
""".trimIndent(),
ext = "md"
) {
assertEquals("<h1>Hello there!</h1>", resolve(RenderingContext(mapOf("msg" to "there"))).html.trim())
assertEquals("<h1>Hello there!</h1>", resolve(RenderingContext(mapOf("msg" to "there"))).code.trim())
}
}

Expand All @@ -156,7 +156,7 @@ class TemplateFileTests {
""".trimIndent(),
ext = "md"
) {
assertEquals("<h1>Hello there!</h1>", resolve(RenderingContext(mapOf("msg" to "there"))).html.trim())
assertEquals("<h1>Hello there!</h1>", resolve(RenderingContext(mapOf("msg" to "there"))).code.trim())
}
}
}

0 comments on commit 902319f

Please sign in to comment.