From 9031638910abb2c4222fb0691e3aa5004783ba5c Mon Sep 17 00:00:00 2001 From: Kirill Grachev Date: Thu, 18 May 2023 15:43:43 +0300 Subject: [PATCH] Pass directory and root separately --- .../src/main/kotlin/DocumentBuilder.kt | 23 +++++++++++-------- .../kscience/snark/main/ServerDataHolder.kt | 4 ++-- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/snark-document-builder/src/main/kotlin/DocumentBuilder.kt b/snark-document-builder/src/main/kotlin/DocumentBuilder.kt index 7da1d96..7c04acf 100644 --- a/snark-document-builder/src/main/kotlin/DocumentBuilder.kt +++ b/snark-document-builder/src/main/kotlin/DocumentBuilder.kt @@ -9,6 +9,7 @@ import kotlinx.html.dom.createHTMLDocument import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue import kotlinx.serialization.json.Json +import kotlin.io.path.* private val SNARK_HTML_RENDER = "snark-document-builder/src/main/nodejs/HtmlRenderer.js" fun getHtml(ast_string: String): String @@ -21,35 +22,37 @@ fun getHtml(ast_string: String): String private val DEFAULT_DOCUMENT_ROOT = "main.md" -public suspend fun buildDocument(documentDirectory: Directory): String { - val dependencyGraph = buildDependencyGraph(documentDirectory) +public suspend fun buildDocument(root: Directory, path: Path): String { + val dependencyGraph = buildDependencyGraph(root, path) - val root: MdAstRoot = dependencyGraph.nodes[documentDirectory.path.toString()]!!.mdAst + println("dependency graph nodes (size: ${dependencyGraph.nodes.size}):\n${dependencyGraph.nodes}") + + val root: MdAstRoot = dependencyGraph.nodes[path.toString()]!!.mdAst return getHtml(jacksonObjectMapper().writeValueAsString(root)) } -public suspend fun buildDependencyGraph(root: Directory): DependencyGraph { +public suspend fun buildDependencyGraph(root: Directory, path: Path): DependencyGraph { val nodes = HashMap() - buildNodes(root, nodes) + buildNodes(root, path, nodes) return DependencyGraph(nodes) } -private suspend fun buildNodes(folder: Directory, nodes: HashMap) { - val pathString = folder.path.toString() +private suspend fun buildNodes(root: Directory, path: Path, nodes: HashMap) { + val pathString = path.toString() assert(!nodes.containsKey(pathString)) - val rootDcoument = folder.get(DEFAULT_DOCUMENT_ROOT) - nodes.put(pathString, buildDependencyGraphNode(rootDcoument.readAll(), folder.path)) + val rootDcoument = (root / path).get(DEFAULT_DOCUMENT_ROOT) + nodes.put(pathString, buildDependencyGraphNode(rootDcoument.readAll(), path)) val dependencies = getDependencies(nodes.getValue(pathString)) for (dependency in dependencies) { if (!nodes.containsKey(dependency)) - buildNodes(folder.getSubdir(Paths.get(dependency)), nodes) + buildNodes(root, Path(dependency), nodes) } } diff --git a/snark-main/src/main/kotlin/space/kscience/snark/main/ServerDataHolder.kt b/snark-main/src/main/kotlin/space/kscience/snark/main/ServerDataHolder.kt index 1b7b4a7..2104fa3 100644 --- a/snark-main/src/main/kotlin/space/kscience/snark/main/ServerDataHolder.kt +++ b/snark-main/src/main/kotlin/space/kscience/snark/main/ServerDataHolder.kt @@ -7,10 +7,10 @@ import java.nio.file.Path internal class ServerDataHolder(private val directory: Directory): DataHolder { - override suspend fun init(relativePath: Path): Directory = directory.getSubdir(relativePath) + override suspend fun init(relativePath: Path): Directory = directory override suspend fun represent(relativePath: Path): String { - return buildDocument(init(relativePath)) + return buildDocument(directory, relativePath) } } \ No newline at end of file