From e582507cf1f23152389cb7e8479a28934b61dabc Mon Sep 17 00:00:00 2001 From: Benjamin Wolf Date: Fri, 17 Nov 2023 16:05:45 +0100 Subject: [PATCH] Fix index file generation --- src/main/java/org/isaqb/asciidoc/Main.java | 287 +++++++++++---------- 1 file changed, 145 insertions(+), 142 deletions(-) diff --git a/src/main/java/org/isaqb/asciidoc/Main.java b/src/main/java/org/isaqb/asciidoc/Main.java index 698a467..636f3db 100644 --- a/src/main/java/org/isaqb/asciidoc/Main.java +++ b/src/main/java/org/isaqb/asciidoc/Main.java @@ -16,151 +16,154 @@ public class Main { - private static final String PROJECT_VERSION = "projectVersion"; - private static final String CURRICULUM_FILE_NAME = "curriculumFileName"; - private static final String CRITERIA_FILE_NAME = "criteriaFileName"; - private static final String INDEX_FILE_NAME = "index"; - private static final String VERSION_DATE = "versionDate"; - private static final String LANGUAGES = "languages"; - private static final String[] FORMATS = {"html", "pdf"}; - - private static final String LANGUAGE_SEPERATOR = ","; - - private static final String SOURCE_DIR = "./docs/"; - private static final String BASE_DIR = SOURCE_DIR; - private static final String OUTPUT_DIR = "./build/"; - - private static final String ADOC = "adoc"; - private static final String HTML = "html"; - private static final String HTML5 = "html5"; - private static final String PDF = "pdf"; - private static final String ENGLISH = "EN"; - - public static void main(final String[] args) { - Objects.requireNonNull(System.getProperty(PROJECT_VERSION)); - Objects.requireNonNull(System.getProperty(CURRICULUM_FILE_NAME)); - Objects.requireNonNull(System.getProperty(VERSION_DATE)); - Objects.requireNonNull(System.getProperty(LANGUAGES)); - - final String projectVersion = System.getProperty(PROJECT_VERSION); - final String curriculumFileName = System.getProperty(CURRICULUM_FILE_NAME); - final String criteriaFileName = System.getProperty(CRITERIA_FILE_NAME); - final String versionDate = System.getProperty(VERSION_DATE); - final String[] languages = System.getProperty(LANGUAGES).split(LANGUAGE_SEPERATOR); - - Stream.of(languages).forEach(language -> convertInLanguage( - language, - projectVersion, - curriculumFileName, - versionDate - )); - - Stream.of(languages).forEach(language -> convertInLanguage( - language, - projectVersion, - criteriaFileName, - versionDate - )); - - convertInLanguage( - ENGLISH, - projectVersion, - INDEX_FILE_NAME, - versionDate); + private static final String PROJECT_VERSION = "projectVersion"; + private static final String CURRICULUM_FILE_NAME = "curriculumFileName"; + private static final String CRITERIA_FILE_NAME = "criteriaFileName"; + private static final String INDEX_FILE_NAME = "index"; + private static final String VERSION_DATE = "versionDate"; + private static final String LANGUAGES = "languages"; + private static final String[] FORMATS = {"html", "pdf"}; + + private static final String LANGUAGE_SEPERATOR = ","; + + private static final String SOURCE_DIR = "./docs/"; + private static final String BASE_DIR = SOURCE_DIR; + private static final String OUTPUT_DIR = "./build/"; + + private static final String ADOC = "adoc"; + private static final String HTML = "html"; + private static final String HTML5 = "html5"; + private static final String PDF = "pdf"; + private static final String ENGLISH = "EN"; + + public static void main(final String[] args) { + Objects.requireNonNull(System.getProperty(PROJECT_VERSION)); + Objects.requireNonNull(System.getProperty(CURRICULUM_FILE_NAME)); + Objects.requireNonNull(System.getProperty(VERSION_DATE)); + Objects.requireNonNull(System.getProperty(LANGUAGES)); + + final String projectVersion = System.getProperty(PROJECT_VERSION); + final String curriculumFileName = System.getProperty(CURRICULUM_FILE_NAME); + final String criteriaFileName = System.getProperty(CRITERIA_FILE_NAME); + final String versionDate = System.getProperty(VERSION_DATE); + final String[] languages = System.getProperty(LANGUAGES).split(LANGUAGE_SEPERATOR); + + Stream.of(languages).forEach(language -> convertInLanguage( + language, + projectVersion, + curriculumFileName, + versionDate + )); + + Stream.of(languages).forEach(language -> convertInLanguage( + language, + projectVersion, + criteriaFileName, + versionDate + )); + + convertInLanguage( + ENGLISH, + projectVersion, + INDEX_FILE_NAME, + versionDate); + } + + private static void convertInLanguage( + final String language, + final String projectVersion, + final String curriculumFileName, + final String versionDate) { + Stream.of(FORMATS).forEach(format -> convertInFormat( + format, projectVersion, + curriculumFileName, + versionDate, + language + )); + } + + private static void convertInFormat( + final String format, + final String projectVersion, + final String curriculumFileName, + final String versionDate, + final String language) { + try (final Asciidoctor asciidoctor = create()) { + final Attributes attributes = toAttributes( + projectVersion, + curriculumFileName, + versionDate, + language); + asciidoctor.convertDirectory( + List.of(new File("%s%s.%s".formatted( + SOURCE_DIR, + curriculumFileName, + ADOC))), + Options.builder() + .baseDir(new File(BASE_DIR)) + .backend(toBackend(format)) + .mkDirs(true) + .attributes(attributes) + .standalone(true) + .toDir(new File(OUTPUT_DIR)) + .safe(SafeMode.UNSAFE) + .build()); + + if (!INDEX_FILE_NAME.equals(curriculumFileName)) { + renameResultAccordingToLanguage(curriculumFileName, format, language); + } } - - private static void convertInLanguage( - final String language, - final String projectVersion, - final String curriculumFileName, - final String versionDate) { - Stream.of(FORMATS).forEach(format -> convertInFormat( - format, projectVersion, - curriculumFileName, - versionDate, - language - )); - } - - private static void convertInFormat( - final String format, - final String projectVersion, - final String curriculumFileName, - final String versionDate, - final String language) { - try (final Asciidoctor asciidoctor = create()) { - final Attributes attributes = toAttributes( - projectVersion, - curriculumFileName, - versionDate, - language); - asciidoctor.convertDirectory( - List.of(new File("%s%s.%s".formatted( - SOURCE_DIR, - curriculumFileName, - ADOC))), - Options.builder() - .baseDir(new File(BASE_DIR)) - .backend(toBackend(format)) - .mkDirs(true) - .attributes(attributes) - .standalone(true) - .toDir(new File(OUTPUT_DIR)) - .safe(SafeMode.UNSAFE) - .build()); - renameResultAccordingToLanguage(curriculumFileName, format, language); - } - } - - private static Attributes toAttributes( - final String projectVersion, - final String curriculumFileName, - final String versionDate, - final String language) { - final String fileVersion = "%s - %s".formatted(projectVersion, language); - final String documentVersion = "%s-%s".formatted(fileVersion, versionDate); - - final Map attributes = new HashMap<>() {{ - put("icons" , "font"); - put("version-label" , ""); - put("revnumber" , fileVersion); - put("revdate" , versionDate); - put("document-version" , documentVersion); - put("currentDate" , versionDate); - put("language" , language); - put("curriculumFileName", curriculumFileName); - put("debug_adoc" , false); - put("pdf-themesdir" , "../pdf-theme/themes"); - put("pdf-fontsdir" , "../pdf-theme/fonts"); - put("pdf-theme" , "isaqb"); - put("stylesheet" , "../html-theme/adoc-github.css"); - put("stylesheet-dir" , "../html-theme"); - put("data-uri" , true); - put("allow-uri-read" , true); - }}; - - return Attributes.builder().attributes(attributes).build(); - } - - private static String toBackend(final String format) { - return switch (format) { - case HTML -> HTML5; - case PDF -> PDF; - default -> throw new IllegalArgumentException("Unknown target format %s".formatted(format)); - }; - } - - private static void renameResultAccordingToLanguage( - final String fileName, - final String format, - final String language) { - final File original = new File("%s%s.%s".formatted(OUTPUT_DIR, fileName, format)); + } + + private static Attributes toAttributes( + final String projectVersion, + final String curriculumFileName, + final String versionDate, + final String language) { + final String fileVersion = "%s - %s".formatted(projectVersion, language); + final String documentVersion = "%s-%s".formatted(fileVersion, versionDate); + + final Map attributes = new HashMap<>() {{ + put("icons", "font"); + put("version-label", ""); + put("revnumber", fileVersion); + put("revdate", versionDate); + put("document-version", documentVersion); + put("currentDate", versionDate); + put("language", language); + put("curriculumFileName", curriculumFileName); + put("debug_adoc", false); + put("pdf-themesdir", "../pdf-theme/themes"); + put("pdf-fontsdir", "../pdf-theme/fonts"); + put("pdf-theme", "isaqb"); + put("stylesheet", "../html-theme/adoc-github.css"); + put("stylesheet-dir", "../html-theme"); + put("data-uri", true); + put("allow-uri-read", true); + }}; + + return Attributes.builder().attributes(attributes).build(); + } + + private static String toBackend(final String format) { + return switch (format) { + case HTML -> HTML5; + case PDF -> PDF; + default -> throw new IllegalArgumentException("Unknown target format %s".formatted(format)); + }; + } + + private static void renameResultAccordingToLanguage( + final String fileName, + final String format, + final String language) { + final File original = new File("%s%s.%s".formatted(OUTPUT_DIR, fileName, format)); final File renamed = new File("%s%s-%s.%s".formatted(OUTPUT_DIR, fileName, language.toLowerCase(), format)); - if (!original.exists()) { + if (!original.exists()) { System.err.printf("Failed to rename result file %s as it does not exist", original.getAbsolutePath()); - } else if (!original.renameTo(renamed)) { + } else if (!original.renameTo(renamed)) { System.err.printf("Failed to rename result file %s to %s%n", original.getName(), renamed.getName()); - } - original.deleteOnExit(); } + original.deleteOnExit(); + } }