aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/pages
diff options
context:
space:
mode:
authorBłażej Kardyś <bkardys@virtuslab.com>2019-11-15 03:37:27 +0100
committerBłażej Kardyś <bkardys@virtuslab.com>2019-11-25 16:08:44 +0100
commitdef5b63ec4afa69d22f66bf1af76175fc1cdec3f (patch)
treed920fd0460506d75dac1aac279ac8f50e525f58a /core/src/main/kotlin/pages
parent1be2870b600cdbbe6aebb9f64c0226b26e42ca5a (diff)
downloaddokka-def5b63ec4afa69d22f66bf1af76175fc1cdec3f.tar.gz
dokka-def5b63ec4afa69d22f66bf1af76175fc1cdec3f.tar.bz2
dokka-def5b63ec4afa69d22f66bf1af76175fc1cdec3f.zip
Page generation changes
Diffstat (limited to 'core/src/main/kotlin/pages')
-rw-r--r--core/src/main/kotlin/pages/ContentNodes.kt10
-rw-r--r--core/src/main/kotlin/pages/MarkdownToContentConverter.kt118
-rw-r--r--core/src/main/kotlin/pages/PageNodes.kt89
3 files changed, 124 insertions, 93 deletions
diff --git a/core/src/main/kotlin/pages/ContentNodes.kt b/core/src/main/kotlin/pages/ContentNodes.kt
index a94a42c5..40b5e114 100644
--- a/core/src/main/kotlin/pages/ContentNodes.kt
+++ b/core/src/main/kotlin/pages/ContentNodes.kt
@@ -29,8 +29,10 @@ data class ContentHeader(
override val dci: DCI,
override val platforms: Set<PlatformData>,
override val style: Set<Style>,
- override val extras: Set<Extra>
-) : ContentComposite
+ override val extras: Set<Extra> = emptySet()
+) : ContentComposite {
+ constructor(level: Int, c: ContentComposite) : this(c.children, level, c.dci, c.platforms, c.style, c.extras)
+}
/** Code blocks */
data class ContentCode(
@@ -117,11 +119,11 @@ interface Style
interface Kind
enum class ContentKind : Kind {
- Comment, Functions, Parameters, Properties, Classes, Packages, Symbol, Sample
+ Comment, Functions, Parameters, Properties, Classes, Packages, Symbol, Sample, Main
}
enum class TextStyle : Style {
- Bold, Italic, Strong, Strikethrough
+ Bold, Italic, Strong, Strikethrough, Paragraph
}
interface HTMLMetadata: Extra {
diff --git a/core/src/main/kotlin/pages/MarkdownToContentConverter.kt b/core/src/main/kotlin/pages/MarkdownToContentConverter.kt
index b29614d7..8caddeb0 100644
--- a/core/src/main/kotlin/pages/MarkdownToContentConverter.kt
+++ b/core/src/main/kotlin/pages/MarkdownToContentConverter.kt
@@ -12,38 +12,55 @@ class MarkdownToContentConverter(
fun buildContent(
node: MarkdownNode,
dci: DCI,
- links: Map<String, DRI> = emptyMap()
+ platforms: Set<PlatformData>,
+ links: Map<String, DRI> = emptyMap(),
+ styles: Set<Style> = emptySet(),
+ extras: Set<Extra> = emptySet()
+
): List<ContentNode> {
// println(tree.toTestString())
- fun buildChildren(node: MarkdownNode) = node.children.flatMap {
- buildContent(it, dci, links)
- }.coalesceText()
+ fun buildChildren(node: MarkdownNode, newStyles: Set<Style> = emptySet(), newExtras: Set<Extra> = emptySet()) =
+ node.children.flatMap {
+ buildContent(it, dci, platforms, links, styles + newStyles, extras + newExtras)
+ }.coalesceText(platforms, styles + newStyles, extras + newExtras)
+
+ fun buildHeader(level: Int) =
+ ContentHeader(buildChildren(node), level, dci, platforms, styles)
return when (node.type) {
- MarkdownElementTypes.ATX_1 -> listOf(ContentHeader(buildChildren(node), 1, dci))
- MarkdownElementTypes.ATX_2 -> listOf(ContentHeader(buildChildren(node), 2, dci))
- MarkdownElementTypes.ATX_3 -> listOf(ContentHeader(buildChildren(node), 3, dci))
- MarkdownElementTypes.ATX_4 -> listOf(ContentHeader(buildChildren(node), 4, dci))
- MarkdownElementTypes.ATX_5 -> listOf(ContentHeader(buildChildren(node), 5, dci))
- MarkdownElementTypes.ATX_6 -> listOf(ContentHeader(buildChildren(node), 6, dci))
- MarkdownElementTypes.UNORDERED_LIST -> listOf(ContentList(buildChildren(node), false, dci))
- MarkdownElementTypes.ORDERED_LIST -> listOf(ContentList(buildChildren(node), true, dci))
- MarkdownElementTypes.LIST_ITEM -> TODO()
- MarkdownElementTypes.EMPH -> listOf(
- ContentStyle(
+ MarkdownElementTypes.ATX_1 -> listOf(buildHeader(1))
+ MarkdownElementTypes.ATX_2 -> listOf(buildHeader(2))
+ MarkdownElementTypes.ATX_3 -> listOf(buildHeader(3))
+ MarkdownElementTypes.ATX_4 -> listOf(buildHeader(4))
+ MarkdownElementTypes.ATX_5 -> listOf(buildHeader(5))
+ MarkdownElementTypes.ATX_6 -> listOf(buildHeader(6))
+ MarkdownElementTypes.UNORDERED_LIST -> listOf(
+ ContentList(
buildChildren(node),
- Style.Emphasis,
- dci
+ false,
+ dci,
+ platforms,
+ styles,
+ extras
)
- )// TODO
- MarkdownElementTypes.STRONG -> listOf(
- ContentStyle(
+ )
+ MarkdownElementTypes.ORDERED_LIST -> listOf(
+ ContentList(
buildChildren(node),
- Style.Strong,
- dci
+ true,
+ dci,
+ platforms,
+ styles,
+ extras
)
- ) // TODO
+ )
+ MarkdownElementTypes.LIST_ITEM -> TODO()
+ MarkdownElementTypes.STRONG,
+ MarkdownTokenTypes.EMPH,
+ MarkdownElementTypes.EMPH ->
+ buildChildren(node, setOf(TextStyle.Strong))
+ // TODO
MarkdownElementTypes.CODE_SPAN -> TODO()
// val startDelimiter = node.child(MarkdownTokenTypes.BACKTICK)?.text
// if (startDelimiter != null) {
@@ -55,15 +72,9 @@ class MarkdownToContentConverter(
MarkdownElementTypes.CODE_BLOCK,
MarkdownElementTypes.CODE_FENCE -> {
val language = node.child(MarkdownTokenTypes.FENCE_LANG)?.text?.trim() ?: ""
- listOf(ContentCode(buildChildren(node).toString(), language, dci)) // TODO
+ listOf(ContentCode(buildChildren(node), language, dci, platforms, styles, extras)) // TODO
}
- MarkdownElementTypes.PARAGRAPH -> listOf(
- ContentStyle(
- buildChildren(node),
- Style.Paragraph,
- dci
- )
- ) // TODO
+ MarkdownElementTypes.PARAGRAPH -> buildChildren(node, newStyles = setOf(TextStyle.Paragraph))
MarkdownElementTypes.INLINE_LINK -> {
// val linkTextNode = node.child(MarkdownElementTypes.LINK_TEXT)
@@ -90,7 +101,16 @@ class MarkdownToContentConverter(
val destination = destinationNode.children.find { it.type == MarkdownTokenTypes.TEXT }?.text
?: destinationNode.text
links[destination]?.let { dri ->
- listOf(ContentLink(destination, dri, dci))
+ listOf(
+ ContentResolvedLink(
+ buildChildren(node),
+ destination,
+ DCI(dri, ContentKind.Symbol),
+ platforms,
+ styles,
+ extras
+ )
+ )
} ?: let {
logger.error("Apparently there is no link resolved for $destination")
emptyList<ContentNode>()
@@ -103,7 +123,7 @@ class MarkdownToContentConverter(
// if (nodeStack.peek() !is ContentHeading || node.parent?.children?.first() != node) {
// parent.append(ContentText(node.text))
// }
- listOf(ContentText(" ", dci))
+ listOf(ContentText(" ", dci, platforms, styles, extras))
}
MarkdownTokenTypes.EOL -> {
// if ((keepEol(nodeStack.peek()) && node.parent?.children?.last() != node) ||
@@ -111,11 +131,11 @@ class MarkdownToContentConverter(
// (processingList(nodeStack.peek()) && node.previous?.type == MarkdownTokenTypes.EOL)) {
// parent.append(ContentText(node.text))
// }
- listOf(ContentText(" ", dci))
+ listOf(ContentText(" ", dci, platforms, styles, extras))
}
MarkdownTokenTypes.CODE_LINE -> {
- listOf(ContentText(node.text, dci)) // TODO check
+ listOf(ContentText(node.text, dci, platforms, styles, extras)) // TODO check
// if (parent is ContentBlockCode) {
// parent.append(content)
// } else {
@@ -139,15 +159,7 @@ class MarkdownToContentConverter(
// }
//
// parent.append(createEntityOrText(node.text))
- listOf(ContentText(node.text, dci)) // TODO
-
-
- MarkdownTokenTypes.EMPH ->
-// val parentNodeType = node.parent?.type
-// if (parentNodeType != MarkdownElementTypes.EMPH && parentNodeType != MarkdownElementTypes.STRONG) {
-// parent.append(ContentText(node.text))
-// }
- listOf(ContentStyle(buildChildren(node), Style.Emphasis, dci)) // TODO
+ listOf(ContentText(node.text, dci, platforms, styles, extras)) // TODO
MarkdownTokenTypes.COLON,
MarkdownTokenTypes.SINGLE_QUOTE,
@@ -161,19 +173,29 @@ class MarkdownToContentConverter(
MarkdownTokenTypes.EXCLAMATION_MARK,
MarkdownTokenTypes.BACKTICK,
MarkdownTokenTypes.CODE_FENCE_CONTENT -> {
- listOf(ContentText(node.text, dci))
+ listOf(ContentText(node.text, dci, platforms, styles, extras))
}
MarkdownElementTypes.LINK_DEFINITION -> TODO()
MarkdownTokenTypes.EMAIL_AUTOLINK ->
- listOf(ContentResolvedLink(node.text, "mailto:${node.text}", dci))
+ listOf(
+ ContentResolvedLink(
+ listOf(ContentText(node.text, dci, platforms, styles, extras)),
+ "mailto:${node.text}",
+ dci, platforms, styles, extras
+ )
+ )
else -> buildChildren(node)
}
}
- private fun Collection<ContentNode>.coalesceText() =
+ private fun Collection<ContentNode>.coalesceText(
+ platforms: Set<PlatformData>,
+ styles: Set<Style>,
+ extras: Set<Extra>
+ ) =
this
.sliceWhen { prev, next -> prev::class != next::class }
.flatMap { nodes ->
@@ -181,7 +203,7 @@ class MarkdownToContentConverter(
is ContentText -> listOf(
ContentText(
nodes.joinToString("") { (it as ContentText).text },
- nodes.first().dci
+ nodes.first().dci, platforms, styles, extras
)
)
else -> nodes
diff --git a/core/src/main/kotlin/pages/PageNodes.kt b/core/src/main/kotlin/pages/PageNodes.kt
index 41692256..0d6072f7 100644
--- a/core/src/main/kotlin/pages/PageNodes.kt
+++ b/core/src/main/kotlin/pages/PageNodes.kt
@@ -4,69 +4,76 @@ import org.jetbrains.dokka.Model.DocumentationNode
import org.jetbrains.dokka.Platform
import org.jetbrains.dokka.links.DRI
-abstract class PageNode(
- val name: String,
- val content: List<ContentNode>,
- val parent: PageNode?,
- val dri: DRI?,
+interface PageNode {
+ val name: String
+ val content: ContentNode
+ val parent: PageNode?
+ val dri: DRI
val documentationNode: DocumentationNode<*>?
-) {
val children: List<PageNode>
- get() = _children
+}
- private val _children: MutableList<PageNode> = mutableListOf()
+abstract class BasicPageNode(children: List<PageNode>): PageNode {
- fun appendChildren(children: List<PageNode>) = _children.addAll(children)
- fun appendChild(child: PageNode) = _children.add(child)
+ private lateinit var _parent: PageNode
+ override val parent: PageNode? by lazy { _parent }
+ override val children = children
override fun equals(other: Any?): Boolean =
if (other is PageNode) {
- dri?.equals(other.dri) ?: (other.dri == null && name == other.name)
+ dri == other.dri && name == other.name
}
else false
+
+ override fun hashCode(): Int =
+ (name + dri).hashCode()
+
+ init {
+ children.forEach { if (it is BasicPageNode) it._parent = this }
+ }
+
}
class ModulePageNode(
- name: String,
- content: List<ContentNode>,
- parent: PageNode? = null,
- documentationNode: DocumentationNode<*>?
-): PageNode(name, content, parent, null, documentationNode)
+ override val name: String,
+ override val content: ContentNode,
+ override val documentationNode: DocumentationNode<*>?,
+ children: List<PageNode>
+): BasicPageNode(children) {
+ override val parent: Nothing? = null
+ override val dri: DRI = DRI.topLevel
+}
class PackagePageNode(
- name: String,
- content: List<ContentNode>,
- parent: PageNode,
- dri: DRI,
- documentationNode: DocumentationNode<*>?
-): PageNode(name, content, parent, dri, documentationNode)
+ override val name: String,
+ override val content: ContentNode,
+ override val dri: DRI,
+ override val documentationNode: DocumentationNode<*>?,
+ children: List<PageNode>
+): BasicPageNode(children)
class ClassPageNode(
- name: String,
- content: List<ContentNode>,
- parent: PageNode,
- dri: DRI,
- documentationNode: DocumentationNode<*>?
-): PageNode(name, content, parent, dri, documentationNode) // class, companion object
+ override val name: String,
+ override val content: ContentNode,
+ override val dri: DRI,
+ override val documentationNode: DocumentationNode<*>?,
+ children: List<PageNode>
+): BasicPageNode(children)
class MemberPageNode(
- name: String,
- content: List<ContentNode>,
- parent: PageNode,
- dri: DRI,
- documentationNode: DocumentationNode<*>?
-): PageNode(name, content, parent, dri, documentationNode) // functions, extension functions, properties
-
-data class PlatformData(val platformName: String, val platformType: Platform) {
- override fun toString() = platformName
+ override val name: String,
+ override val content: ContentNode,
+ override val dri: DRI,
+ override val documentationNode: DocumentationNode<*>?,
+ children: List<PageNode> = emptyList()
+): BasicPageNode(children)
+
+data class PlatformData(val platformType: Platform, val targets: List<String>) {
+ override fun toString() = targets.toString()
}
-
-fun PageNode.platforms(): List<PlatformData> = this.content.flatMap { it.platforms }.distinct() // TODO: Override equals???
fun PageNode.dfs(predicate: (PageNode) -> Boolean): PageNode? = if (predicate(this)) { this } else { this.children.asSequence().mapNotNull { it.dfs(predicate) }.firstOrNull() }
-
-
// Navigation??
// content modifier?