From fec72485b844c1f1017b171bd3d9b9c9ba7485d1 Mon Sep 17 00:00:00 2001 From: James Date: Sun, 5 May 2019 17:08:48 +0100 Subject: [PATCH] Move TeX header outside of user input, get and use that resource Add output directory creation to pre-processing stage Idiomatic PrintWriter section --- .../kotlin/org/idiosapps/BuilderPipeline.kt | 18 +++++++----------- .../kotlin/org/idiosapps/FXMLController.kt | 2 ++ src/main/kotlin/org/idiosapps/Filenames.kt | 4 ++-- src/main/kotlin/org/idiosapps/TexUtils.kt | 11 +++++++++++ .../main/resources/org.idiosapps/texHeader | 2 +- 5 files changed, 23 insertions(+), 14 deletions(-) rename input/header => src/main/resources/org.idiosapps/texHeader (96%) diff --git a/src/main/kotlin/org/idiosapps/BuilderPipeline.kt b/src/main/kotlin/org/idiosapps/BuilderPipeline.kt index 6e7f0f9..0949954 100644 --- a/src/main/kotlin/org/idiosapps/BuilderPipeline.kt +++ b/src/main/kotlin/org/idiosapps/BuilderPipeline.kt @@ -17,22 +17,18 @@ class BuilderPipeline : Task() { var languageUsed = "mandarin" - OSUtils.tryMakeOutputDir() // Java will only make a new file if the parent folder exists (on Windows anyway) - val outputStoryTeXWriter = PrintWriter(Filenames.outputTexFilename, "UTF-8") - val vocab = VocabUtils.splitIntoParts(Filenames.inputVocabFilename) val names = VocabUtils.splitIntoParts(Filenames.inputKeyNamesFilename) + PrintWriter(Filenames.outputTexFilename, "UTF-8").use { texWriter -> + TexUtils.copyToTex(texWriter, Filenames.inputHeaderResource) + TexUtils.copyToTex(texWriter, Filenames.inputTitleFilename) + TexUtils.copyToTex(texWriter, Filenames.inputStoryFilename) - TexUtils.copyToTex(outputStoryTeXWriter, Filenames.inputHeaderFilename) - TexUtils.copyToTex(outputStoryTeXWriter, Filenames.inputTitleFilename) - TexUtils.copyToTex(outputStoryTeXWriter, Filenames.inputStoryFilename) - - SummaryPageWriter.writeVocabSection(outputStoryTeXWriter, vocab) - // todo WriteSummaryPage.writeTexGrammar + SummaryPageWriter.writeVocabSection(texWriter, vocab) // TODO add summary / grammar pages too - outputStoryTeXWriter.append("\\end{document}") - outputStoryTeXWriter.close() + texWriter.append("\\end{document}") + } PDFUtils.xelatexToPDF() diff --git a/src/main/kotlin/org/idiosapps/FXMLController.kt b/src/main/kotlin/org/idiosapps/FXMLController.kt index 0d71b27..e26b54f 100644 --- a/src/main/kotlin/org/idiosapps/FXMLController.kt +++ b/src/main/kotlin/org/idiosapps/FXMLController.kt @@ -35,6 +35,7 @@ class FXMLController { try { // first check dependencies & inputs - have a good idea of what to expect OSUtils.hasProgram(PDFTEX) OSUtils.hasProgram(XETEX) + OSUtils.tryMakeOutputDir() // Java will only make a new file if the parent folder exists (on Windows anyway) Filenames.checkInputs() // just check files exist clearInputStateLabels() @@ -51,6 +52,7 @@ class FXMLController { } } + // Start another thread with the real processing, so the UI doesn't freeze private fun startBookBuildPipeline() { val task: Task = BuilderPipeline() progressBar.progressProperty().bind(task.progressProperty()) diff --git a/src/main/kotlin/org/idiosapps/Filenames.kt b/src/main/kotlin/org/idiosapps/Filenames.kt index 9e7eb0e..34cfa7d 100644 --- a/src/main/kotlin/org/idiosapps/Filenames.kt +++ b/src/main/kotlin/org/idiosapps/Filenames.kt @@ -1,13 +1,14 @@ package org.idiosapps import java.io.File +import java.net.URL class Filenames { companion object { val inputPrefix: String = "./input/" val outputPrefix: String = "./output/" - val inputHeaderFilename: String = inputPrefix + "header" + val inputHeaderResource: URL = this::class.java.classLoader.getResource("org.idiosapps/texHeader") val inputTitleFilename: String = inputPrefix + "title" val inputStoryFilename: String = inputPrefix + "story" @@ -19,7 +20,6 @@ class Filenames { fun checkInputs() { val inputArray = arrayListOf( - inputHeaderFilename, inputTitleFilename, inputStoryFilename, inputVocabFilename, diff --git a/src/main/kotlin/org/idiosapps/TexUtils.kt b/src/main/kotlin/org/idiosapps/TexUtils.kt index 2a203d6..abba6ac 100644 --- a/src/main/kotlin/org/idiosapps/TexUtils.kt +++ b/src/main/kotlin/org/idiosapps/TexUtils.kt @@ -2,6 +2,7 @@ package org.idiosapps import java.io.File import java.io.PrintWriter +import java.net.URL import java.util.* class TexUtils { @@ -47,5 +48,15 @@ class TexUtils { } scanner.close() } + + fun copyToTex(outputStoryWriter: PrintWriter, inputResource: URL) { + val resourceFile = File(inputResource.toURI()) + + Scanner(resourceFile, "UTF-8").use { scanner -> + while (scanner.hasNextLine()) { + outputStoryWriter.println(scanner.nextLine()) + } + } + } } } \ No newline at end of file diff --git a/input/header b/src/main/resources/org.idiosapps/texHeader similarity index 96% rename from input/header rename to src/main/resources/org.idiosapps/texHeader index d5c65cb..f9b5159 100644 --- a/input/header +++ b/src/main/resources/org.idiosapps/texHeader @@ -5,7 +5,7 @@ % Add the ability to write pinyin with tones \usepackage{xeCJK} \usepackage{xpinyin} -% Add the ability to create fancy headers and footers +% Add the ability to create fancy headers and pageFooters \usepackage{fancyhdr} % Add the ability to use Lorem Ipsum placeholder text \usepackage{lipsum}