aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/main/kotlin/parsers
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/base/src/main/kotlin/parsers')
-rw-r--r--plugins/base/src/main/kotlin/parsers/ModuleAndPackageDocumentationParser.kt76
-rw-r--r--plugins/base/src/main/kotlin/parsers/moduleAndPackage/IllegalModuleAndPackageDocumentation.kt7
-rw-r--r--plugins/base/src/main/kotlin/parsers/moduleAndPackage/ModuleAndPackageDocumentation.kt11
-rw-r--r--plugins/base/src/main/kotlin/parsers/moduleAndPackage/ModuleAndPackageDocumentationFragment.kt10
-rw-r--r--plugins/base/src/main/kotlin/parsers/moduleAndPackage/ModuleAndPackageDocumentationParsingContext.kt36
-rw-r--r--plugins/base/src/main/kotlin/parsers/moduleAndPackage/ModuleAndPackageDocumentationSource.kt17
-rw-r--r--plugins/base/src/main/kotlin/parsers/moduleAndPackage/parseModuleAndPackageDocumentation.kt15
-rw-r--r--plugins/base/src/main/kotlin/parsers/moduleAndPackage/parseModuleAndPackageDocumentationFragments.kt53
8 files changed, 149 insertions, 76 deletions
diff --git a/plugins/base/src/main/kotlin/parsers/ModuleAndPackageDocumentationParser.kt b/plugins/base/src/main/kotlin/parsers/ModuleAndPackageDocumentationParser.kt
deleted file mode 100644
index 4b46537b..00000000
--- a/plugins/base/src/main/kotlin/parsers/ModuleAndPackageDocumentationParser.kt
+++ /dev/null
@@ -1,76 +0,0 @@
-package org.jetbrains.dokka.base.parsers
-
-import org.jetbrains.dokka.DokkaException
-import java.io.File
-
-internal class IllegalModuleAndPackageDocumentation(
- source: ModuleAndPackageDocumentationSource, message: String
-) : DokkaException("[$source] $message")
-
-data class ModuleAndPackageDocFragment(
- val classifier: Classifier,
- val name: String,
- val documentation: String
-) {
- enum class Classifier { Module, Package }
-}
-
-internal abstract class ModuleAndPackageDocumentationSource {
- abstract val sourceDescription: String
- abstract val documentation: String
-
- override fun toString(): String {
- return sourceDescription
- }
-}
-
-internal class ModuleAndPackageDocumentationFile(private val file: File) : ModuleAndPackageDocumentationSource() {
- override val sourceDescription: String = file.path
- override val documentation: String by lazy(LazyThreadSafetyMode.PUBLICATION) { file.readText() }
-}
-
-internal fun parseModuleAndPackageDocFragments(source: File): List<ModuleAndPackageDocFragment> {
- return parseModuleAndPackageDocFragments(ModuleAndPackageDocumentationFile(source))
-}
-
-internal fun parseModuleAndPackageDocFragments(source: ModuleAndPackageDocumentationSource): List<ModuleAndPackageDocFragment> {
- val fragmentStrings = source.documentation.split(Regex("(|^)#\\s*(?=(Module|Package))"))
- return fragmentStrings
- .filter(String::isNotBlank)
- .map { fragmentString -> parseModuleAndPackageDocFragment(source, fragmentString) }
-}
-
-private fun parseModuleAndPackageDocFragment(
- source: ModuleAndPackageDocumentationSource,
- fragment: String
-): ModuleAndPackageDocFragment {
- val firstLineAndDocumentation = fragment.split("\r\n", "\n", "\r", limit = 2)
- val firstLine = firstLineAndDocumentation[0]
-
- val classifierAndName = firstLine.split(Regex("\\s+"), limit = 2)
- if (classifierAndName.size != 2) {
- throw IllegalModuleAndPackageDocumentation(source, "Missing ${classifierAndName.first()} name")
- }
-
- val classifier = when (classifierAndName[0].trim()) {
- "Module" -> ModuleAndPackageDocFragment.Classifier.Module
- "Package" -> ModuleAndPackageDocFragment.Classifier.Package
- else -> throw IllegalStateException("Unexpected classifier ${classifierAndName[0]}")
- }
-
- val name = classifierAndName[1].trim()
- if (name.contains(Regex("\\s"))) {
- throw IllegalModuleAndPackageDocumentation(
- source, "Module/Package name cannot contain whitespace in '$firstLine'"
- )
- }
-
- return ModuleAndPackageDocFragment(
- classifier = classifier,
- name = name,
- documentation = firstLineAndDocumentation.getOrNull(1)?.trim().orEmpty()
- )
-}
-
-
-
diff --git a/plugins/base/src/main/kotlin/parsers/moduleAndPackage/IllegalModuleAndPackageDocumentation.kt b/plugins/base/src/main/kotlin/parsers/moduleAndPackage/IllegalModuleAndPackageDocumentation.kt
new file mode 100644
index 00000000..f642c374
--- /dev/null
+++ b/plugins/base/src/main/kotlin/parsers/moduleAndPackage/IllegalModuleAndPackageDocumentation.kt
@@ -0,0 +1,7 @@
+package org.jetbrains.dokka.base.parsers.moduleAndPackage
+
+import org.jetbrains.dokka.DokkaException
+
+internal class IllegalModuleAndPackageDocumentation(
+ source: ModuleAndPackageDocumentationSource, message: String
+) : DokkaException("[$source] $message")
diff --git a/plugins/base/src/main/kotlin/parsers/moduleAndPackage/ModuleAndPackageDocumentation.kt b/plugins/base/src/main/kotlin/parsers/moduleAndPackage/ModuleAndPackageDocumentation.kt
new file mode 100644
index 00000000..5139c872
--- /dev/null
+++ b/plugins/base/src/main/kotlin/parsers/moduleAndPackage/ModuleAndPackageDocumentation.kt
@@ -0,0 +1,11 @@
+package org.jetbrains.dokka.base.parsers.moduleAndPackage
+
+import org.jetbrains.dokka.model.doc.DocumentationNode
+
+internal data class ModuleAndPackageDocumentation(
+ val name: String,
+ val classifier: Classifier,
+ val documentation: DocumentationNode
+) {
+ enum class Classifier { Module, Package }
+}
diff --git a/plugins/base/src/main/kotlin/parsers/moduleAndPackage/ModuleAndPackageDocumentationFragment.kt b/plugins/base/src/main/kotlin/parsers/moduleAndPackage/ModuleAndPackageDocumentationFragment.kt
new file mode 100644
index 00000000..fa99a8e2
--- /dev/null
+++ b/plugins/base/src/main/kotlin/parsers/moduleAndPackage/ModuleAndPackageDocumentationFragment.kt
@@ -0,0 +1,10 @@
+package org.jetbrains.dokka.base.parsers.moduleAndPackage
+
+import org.jetbrains.dokka.base.parsers.moduleAndPackage.ModuleAndPackageDocumentation.*
+
+internal data class ModuleAndPackageDocumentationFragment(
+ val name: String,
+ val classifier: Classifier,
+ val documentation: String,
+ val source: ModuleAndPackageDocumentationSource
+)
diff --git a/plugins/base/src/main/kotlin/parsers/moduleAndPackage/ModuleAndPackageDocumentationParsingContext.kt b/plugins/base/src/main/kotlin/parsers/moduleAndPackage/ModuleAndPackageDocumentationParsingContext.kt
new file mode 100644
index 00000000..f11a5d13
--- /dev/null
+++ b/plugins/base/src/main/kotlin/parsers/moduleAndPackage/ModuleAndPackageDocumentationParsingContext.kt
@@ -0,0 +1,36 @@
+@file:Suppress("FunctionName")
+
+package org.jetbrains.dokka.base.parsers.moduleAndPackage
+
+import org.jetbrains.dokka.analysis.DokkaResolutionFacade
+import org.jetbrains.dokka.base.parsers.MarkdownParser
+import org.jetbrains.dokka.base.parsers.moduleAndPackage.ModuleAndPackageDocumentation.Classifier.*
+import org.jetbrains.dokka.model.doc.DocumentationNode
+import org.jetbrains.dokka.utilities.DokkaLogger
+import org.jetbrains.kotlin.name.FqName
+import org.jetbrains.kotlin.name.Name
+
+internal fun interface ModuleAndPackageDocumentationParsingContext {
+ fun markdownParserFor(fragment: ModuleAndPackageDocumentationFragment): MarkdownParser
+}
+
+internal fun ModuleAndPackageDocumentationParsingContext.parse(
+ fragment: ModuleAndPackageDocumentationFragment
+): DocumentationNode {
+ return markdownParserFor(fragment).parse(fragment.documentation)
+}
+
+internal fun ModuleAndPackageDocumentationParsingContext(
+ logger: DokkaLogger,
+ facade: DokkaResolutionFacade? = null,
+) = ModuleAndPackageDocumentationParsingContext { fragment ->
+ val descriptor = when (fragment.classifier) {
+ Module -> facade?.moduleDescriptor?.getPackage(FqName.topLevel(Name.identifier("")))
+ Package -> facade?.moduleDescriptor?.getPackage(FqName(fragment.name))
+ }
+ MarkdownParser(
+ resolutionFacade = facade,
+ declarationDescriptor = descriptor,
+ logger = logger
+ )
+}
diff --git a/plugins/base/src/main/kotlin/parsers/moduleAndPackage/ModuleAndPackageDocumentationSource.kt b/plugins/base/src/main/kotlin/parsers/moduleAndPackage/ModuleAndPackageDocumentationSource.kt
new file mode 100644
index 00000000..7c3f6d97
--- /dev/null
+++ b/plugins/base/src/main/kotlin/parsers/moduleAndPackage/ModuleAndPackageDocumentationSource.kt
@@ -0,0 +1,17 @@
+package org.jetbrains.dokka.base.parsers.moduleAndPackage
+
+import java.io.File
+
+internal abstract class ModuleAndPackageDocumentationSource {
+ abstract val sourceDescription: String
+ abstract val documentation: String
+
+ override fun toString(): String {
+ return sourceDescription
+ }
+}
+
+internal data class ModuleAndPackageDocumentationFile(private val file: File) : ModuleAndPackageDocumentationSource() {
+ override val sourceDescription: String = file.path
+ override val documentation: String by lazy(LazyThreadSafetyMode.PUBLICATION) { file.readText() }
+}
diff --git a/plugins/base/src/main/kotlin/parsers/moduleAndPackage/parseModuleAndPackageDocumentation.kt b/plugins/base/src/main/kotlin/parsers/moduleAndPackage/parseModuleAndPackageDocumentation.kt
new file mode 100644
index 00000000..a2876308
--- /dev/null
+++ b/plugins/base/src/main/kotlin/parsers/moduleAndPackage/parseModuleAndPackageDocumentation.kt
@@ -0,0 +1,15 @@
+@file:Suppress("FunctionName")
+
+package org.jetbrains.dokka.base.parsers.moduleAndPackage
+
+internal fun parseModuleAndPackageDocumentation(
+ context: ModuleAndPackageDocumentationParsingContext,
+ fragment: ModuleAndPackageDocumentationFragment
+): ModuleAndPackageDocumentation {
+ return ModuleAndPackageDocumentation(
+ name = fragment.name,
+ classifier = fragment.classifier,
+ documentation = context.parse(fragment)
+ )
+}
+
diff --git a/plugins/base/src/main/kotlin/parsers/moduleAndPackage/parseModuleAndPackageDocumentationFragments.kt b/plugins/base/src/main/kotlin/parsers/moduleAndPackage/parseModuleAndPackageDocumentationFragments.kt
new file mode 100644
index 00000000..2667681c
--- /dev/null
+++ b/plugins/base/src/main/kotlin/parsers/moduleAndPackage/parseModuleAndPackageDocumentationFragments.kt
@@ -0,0 +1,53 @@
+package org.jetbrains.dokka.base.parsers.moduleAndPackage
+
+import java.io.File
+
+
+internal fun parseModuleAndPackageDocumentationFragments(source: File): List<ModuleAndPackageDocumentationFragment> {
+ return parseModuleAndPackageDocumentationFragments(ModuleAndPackageDocumentationFile(source))
+}
+
+internal fun parseModuleAndPackageDocumentationFragments(
+ source: ModuleAndPackageDocumentationSource
+): List<ModuleAndPackageDocumentationFragment> {
+ val fragmentStrings = source.documentation.split(Regex("(|^)#\\s*(?=(Module|Package))"))
+ return fragmentStrings
+ .filter(String::isNotBlank)
+ .map { fragmentString -> parseModuleAndPackageDocFragment(source, fragmentString) }
+}
+
+private fun parseModuleAndPackageDocFragment(
+ source: ModuleAndPackageDocumentationSource,
+ fragment: String
+): ModuleAndPackageDocumentationFragment {
+ val firstLineAndDocumentation = fragment.split("\r\n", "\n", "\r", limit = 2)
+ val firstLine = firstLineAndDocumentation[0]
+
+ val classifierAndName = firstLine.split(Regex("\\s+"), limit = 2)
+ if (classifierAndName.size != 2) {
+ throw IllegalModuleAndPackageDocumentation(source, "Missing ${classifierAndName.first()} name")
+ }
+
+ val classifier = when (classifierAndName[0].trim()) {
+ "Module" -> ModuleAndPackageDocumentation.Classifier.Module
+ "Package" -> ModuleAndPackageDocumentation.Classifier.Package
+ else -> throw IllegalStateException("Unexpected classifier ${classifierAndName[0]}")
+ }
+
+ val name = classifierAndName[1].trim()
+ if (name.contains(Regex("\\s"))) {
+ throw IllegalModuleAndPackageDocumentation(
+ source, "Module/Package name cannot contain whitespace in '$firstLine'"
+ )
+ }
+
+ return ModuleAndPackageDocumentationFragment(
+ name = name,
+ classifier = classifier,
+ documentation = firstLineAndDocumentation.getOrNull(1)?.trim().orEmpty(),
+ source = source
+ )
+}
+
+
+