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