From 92cd2f30b4d37cf2af9ab55f12d14a5780a75398 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Sat, 14 Dec 2024 20:25:32 +0100 Subject: [PATCH 1/2] feat(renderer): Render constructors and properties --- renderer/src/main/kotlin/renderer/MkDocsRenderer2.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/renderer/src/main/kotlin/renderer/MkDocsRenderer2.kt b/renderer/src/main/kotlin/renderer/MkDocsRenderer2.kt index 8ad2969..d82ad45 100644 --- a/renderer/src/main/kotlin/renderer/MkDocsRenderer2.kt +++ b/renderer/src/main/kotlin/renderer/MkDocsRenderer2.kt @@ -79,7 +79,7 @@ open class MkDocsRenderer2( buildComment { "TABLE NODE $node" } when (node.dci.kind) { - ContentKind.Packages, ContentKind.Classlikes, ContentKind.Functions -> { + ContentKind.Packages, ContentKind.Classlikes, ContentKind.Functions, ContentKind.Constructors, ContentKind.Properties -> { for (pkg in node.children) { for (child in pkg.children) { when (child) { -- 2.51.2 From f84849fa389e038182c07f46b25a3fe0ab757099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Sun, 15 Dec 2024 15:40:21 +0100 Subject: [PATCH 2/2] feat(dokka-mkdocs): Place top-level factories right after their class --- .../src/commonMain/kotlin/Example.kt | 7 ++++ .../src/main/kotlin/DokkaMkDocsPlugin.kt | 36 +++++++++++++++---- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/docs/example/example-core/src/commonMain/kotlin/Example.kt b/docs/example/example-core/src/commonMain/kotlin/Example.kt index d7f9518..a2067dd 100644 --- a/docs/example/example-core/src/commonMain/kotlin/Example.kt +++ b/docs/example/example-core/src/commonMain/kotlin/Example.kt @@ -58,6 +58,13 @@ class Example() { } } +/** + * Top-level function masquerading as a constructor. + */ +fun Example(id: Int): Example { + TODO() +} + /** * This is a top-level function. */ diff --git a/dokka-mkdocs/src/main/kotlin/DokkaMkDocsPlugin.kt b/dokka-mkdocs/src/main/kotlin/DokkaMkDocsPlugin.kt index 20dfb0b..d592f32 100644 --- a/dokka-mkdocs/src/main/kotlin/DokkaMkDocsPlugin.kt +++ b/dokka-mkdocs/src/main/kotlin/DokkaMkDocsPlugin.kt @@ -58,20 +58,32 @@ abstract class DokkaMkDocsPlugin : DokkaFormatPlugin(formatName = "mkdocs") { doLast { val root = siteOutput.asFile val builder = StringBuilder() - var depth = 1 - root.walkTopDown() + root.walkBottomUp() .onEnter { file -> // Ignore directories that only contain other directories file.walkBottomUp().any { it.isFile } } - .onLeave { - if (it.isDirectory) - depth-- + .sortedWith { a, b -> + // Very ugly code :/ Our goal: + // ①. A directory should be immediately before its contents + // ②. If a file has the same name as a directory, it should be just after the directory's contents + // ③. Everything else should be placed alphabetically. + + val aR = a.relativeTo(root) + val bR = b.relativeTo(root) + + when { + aR.isDirectory && aR in bR -> -1 + bR.isDirectory && bR in aR -> 1 + else -> aR.path.replace(File.separatorChar, '\u0001').decodeAsDokkaUrl().compareTo(bR.path.replace(File.separatorChar, '\u0001').decodeAsDokkaUrl()) + }.also { + println("$it — $a \t $b") + } } .forEach { file -> val relative = file.relativeTo(root) - + val depth = relative.path.count { it == File.separatorChar } + 1 val indent = " ".repeat(depth) + " " when { @@ -81,7 +93,6 @@ abstract class DokkaMkDocsPlugin : DokkaFormatPlugin(formatName = "mkdocs") { file.isDirectory -> { builder.appendLine("$indent- ${relative.name.decodeAsDokkaUrl()}:") - depth++ } file.isFile && file.name.endsWith(".md") -> { @@ -135,3 +146,14 @@ fun String.decodeAsDokkaUrl(): String { } return result } + +operator fun File.contains(child: File): Boolean { + val childPath = child.path.split(File.separatorChar) + val parentPath = this.path.split(File.separatorChar) + + for ((i, it) in parentPath.withIndex()) { + if (childPath.getOrNull(i) != it) return false + } + + return true +} -- 2.51.2