Add pandoc conversion
This commit is contained in:
parent
3c1a1bd99d
commit
09da54f4f0
@ -3,6 +3,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
plugins {
|
||||
kotlin("jvm") version "1.8.22"
|
||||
application
|
||||
id("com.github.johnrengelman.shadow") version "8.1.1"
|
||||
}
|
||||
|
||||
group = "ru.mipt.npm"
|
||||
|
@ -2,13 +2,11 @@ package ru.mipt.npm.space.documentextractor
|
||||
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.StandardOpenOption
|
||||
import java.nio.file.attribute.FileAttribute
|
||||
import kotlin.io.path.*
|
||||
|
||||
@OptIn(ExperimentalPathApi::class)
|
||||
fun generateHtml(inputPath: Path, outputPath: Path) {
|
||||
internal fun prepareScripts(inputPath: Path): Path {
|
||||
val scriptPath = inputPath.resolveSibling("scripts").resolve("links-to-html.lua")
|
||||
if(!scriptPath.exists()) {
|
||||
if (!scriptPath.exists()) {
|
||||
scriptPath.parent.createDirectories()
|
||||
scriptPath.writeText(
|
||||
{}.javaClass.getResource("/links-to-html.lua")!!.readText(),
|
||||
@ -16,9 +14,31 @@ fun generateHtml(inputPath: Path, outputPath: Path) {
|
||||
StandardOpenOption.CREATE
|
||||
)
|
||||
}
|
||||
return scriptPath
|
||||
}
|
||||
|
||||
|
||||
@OptIn(ExperimentalPathApi::class)
|
||||
fun convert(inputPath: Path, outputPath: Path) {
|
||||
val scriptPath = prepareScripts(inputPath)
|
||||
inputPath.copyToRecursively(outputPath, followLinks = false) { source: Path, target: Path ->
|
||||
if (source.isRegularFile() && source.extension == "md") {
|
||||
val targetPath = outputPath.resolve(source.fileName.nameWithoutExtension + ".html")
|
||||
|
||||
val docxPath = target.parent.resolve(source.fileName.nameWithoutExtension + ".docx")
|
||||
|
||||
ProcessBuilder(
|
||||
"pandoc",
|
||||
"--from=markdown",
|
||||
"--to=docx",
|
||||
"--lua-filter=${scriptPath.absolute()}",
|
||||
"--output=${docxPath.absolute()}",
|
||||
"${source.absolute()}",
|
||||
|
||||
).also {
|
||||
logger.info("Running pandoc: ${it.command().joinToString(separator = " ")}")
|
||||
}.inheritIO().start().waitFor()
|
||||
|
||||
val htmlPath = target.parent.resolve(source.fileName.nameWithoutExtension + ".html")
|
||||
|
||||
ProcessBuilder(
|
||||
"pandoc",
|
||||
@ -28,17 +48,21 @@ fun generateHtml(inputPath: Path, outputPath: Path) {
|
||||
"--from=markdown",
|
||||
"--to=html5",
|
||||
"--lua-filter=${scriptPath.absolute()}",
|
||||
"--output=${targetPath.absolute()}",
|
||||
"--output=${htmlPath.absolute()}",
|
||||
"${source.absolute()}",
|
||||
|
||||
).also {
|
||||
).also {
|
||||
logger.info("Running pandoc: ${it.command().joinToString(separator = " ")}")
|
||||
}.inheritIO().start().waitFor()
|
||||
|
||||
|
||||
CopyActionResult.CONTINUE
|
||||
} else {
|
||||
source.copyToIgnoringExistingDirectory(target, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ZipOutputStream(zipFileName.outputStream().buffered()).use { zipStream ->
|
||||
// outputPath.walk().forEach { file ->
|
||||
@ -50,4 +74,3 @@ fun generateHtml(inputPath: Path, outputPath: Path) {
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
@ -40,15 +40,15 @@ suspend fun main(args: Array<String>) {
|
||||
description = "FolderId for the folder to export. By default uses project root."
|
||||
)
|
||||
|
||||
val generateHtml by parser.option(
|
||||
val convert by parser.option(
|
||||
ArgType.Boolean,
|
||||
description = "If defined, generate HTML directory and zip"
|
||||
description = "If defined, convert result to HTML and DOCX on download"
|
||||
).default(false)
|
||||
|
||||
val htmlOutputPath by parser.option(
|
||||
val convertOutputPath by parser.option(
|
||||
ArgType.String,
|
||||
description = "Path for html output directory sibling to 'output' directory"
|
||||
).default("html")
|
||||
description = "Path for html and docx output directory sibling to 'output' directory"
|
||||
).default("converted")
|
||||
|
||||
val clientId by parser.option(
|
||||
ArgType.String,
|
||||
@ -83,8 +83,8 @@ suspend fun main(args: Array<String>) {
|
||||
ProjectIdentifier.Key(project),
|
||||
folderId?.let { FolderIdentifier.Id(it) } ?: FolderIdentifier.Root
|
||||
)
|
||||
if (generateHtml) {
|
||||
generateHtml(target, target.resolveSibling(htmlOutputPath))
|
||||
if (convert) {
|
||||
convert(target, target.resolveSibling(convertOutputPath))
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user