From 225b7346a8c5076ee49e0deefebd0546af5a9d65 Mon Sep 17 00:00:00 2001 From: "liubar.pa" Date: Fri, 5 May 2023 23:39:51 +0300 Subject: [PATCH] fillDependencies is implemented --- .../src/main/kotlin/DocumentBuilder.kt | 8 ++-- .../src/main/kotlin/MdParser.kt | 38 ++++++++++++------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/snark-document-builder/src/main/kotlin/DocumentBuilder.kt b/snark-document-builder/src/main/kotlin/DocumentBuilder.kt index c2d76c8..6650197 100644 --- a/snark-document-builder/src/main/kotlin/DocumentBuilder.kt +++ b/snark-document-builder/src/main/kotlin/DocumentBuilder.kt @@ -13,7 +13,7 @@ public suspend fun buildDocument(documentDirectory: Directory) { TODO() /*resolving of dependencies*/ } -public suspend fun buildDependencyGraph(root: Directory): DependencyGraph { +public suspend fun buildDependencyGraph(root: Directory): DependencyGraph { val nodes = HashMap() buildNodes(root, nodes) @@ -23,11 +23,11 @@ public suspend fun buildDependencyGraph(root: Directory): DependencyGraph { private suspend fun buildNodes(folder: Directory, nodes: HashMap) { val pathString = folder.path.toString() - + assert(!nodes.containsKey(pathString)) val rootDcoument = folder.get(DEFAULT_DOCUMENT_ROOT) - nodes.put(pathString, buildDependencyGraphNode(rootDcoument.readAll())) + nodes.put(pathString, buildDependencyGraphNode(rootDcoument.readAll(), folder.path)) val dependencies = getDependencies(nodes.getValue(pathString)) @@ -39,7 +39,7 @@ private suspend fun buildNodes(folder: Directory, nodes: HashMap { val dependencies = mutableListOf() - + for (dependency in node.dependencies) { when (dependency) { is IncludeDependency -> dependencies.addAll(dependency.includeList) diff --git a/snark-document-builder/src/main/kotlin/MdParser.kt b/snark-document-builder/src/main/kotlin/MdParser.kt index 20243b0..bf4bc83 100644 --- a/snark-document-builder/src/main/kotlin/MdParser.kt +++ b/snark-document-builder/src/main/kotlin/MdParser.kt @@ -2,6 +2,7 @@ package documentBuilder import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue +import java.nio.file.Path private val MARKDOWN_PARSER = "../nodejs/MarkdownParser.js" @@ -13,30 +14,39 @@ public suspend fun parseMd(mdFile: ByteArray): MdAstRoot { .start().inputStream.bufferedReader().readText()) } -public suspend fun buildDependencyGraphNode(mdFile: ByteArray): DependencyGraphNode { +public suspend fun buildDependencyGraphNode(mdFile: ByteArray, path: Path): DependencyGraphNode { val treeRoot = parseMd(mdFile) val dependencies = mutableListOf() - fillDependencies(treeRoot, dependencies) + fillDependencies(treeRoot, dependencies, path) return DependencyGraphNode(treeRoot, dependencies) } -private suspend fun fillDependencies( +internal suspend fun fillDependencies( currentNode: MdAstElement, - dependencies: MutableList) { - // when (currentNode) { - // is MdAstParent -> { - // val iterator = currentNode.children.listIterator() + dependencies: MutableList, + path: Path) { + when (currentNode) { + is MdAstParent -> { + for (child in currentNode.children) { + if (child is MdAstText) { + val includeList = getIncludeFiles(child.value).toMutableList() - // while (iterator.hasNext()) { + if (includeList.size > 0) { + includeList.replaceAll { path.toString() + "/" + it } + dependencies += IncludeDependency(currentNode, child, includeList) + } + } else { + fillDependencies(child, dependencies, path) + } + } + } + else -> {} + } +} - // iterator.next() - // } - - // } - // else -> {} - // } +public suspend fun getIncludeFiles(string: String): List { TODO() } \ No newline at end of file