|
| 1 | +package org.isaqb.asciidoc; |
| 2 | + |
| 3 | +import org.asciidoctor.Asciidoctor; |
| 4 | +import org.asciidoctor.Attributes; |
| 5 | +import org.asciidoctor.Options; |
| 6 | +import org.asciidoctor.SafeMode; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Objects; |
| 13 | +import java.util.stream.Stream; |
| 14 | + |
| 15 | +import static org.asciidoctor.Asciidoctor.Factory.create; |
| 16 | + |
| 17 | +public class Main { |
| 18 | + |
| 19 | + private static final String PROJECT_VERSION = "projectVersion"; |
| 20 | + private static final String CURRICULUM_FILE_NAME = "curriculumFileName"; |
| 21 | + private static final String INDEX_FILE_NAME = "index"; |
| 22 | + private static final String VERSION_DATE = "versionDate"; |
| 23 | + private static final String LANGUAGES = "languages"; |
| 24 | + private static final String[] FORMATS = {"html", "pdf"}; |
| 25 | + |
| 26 | + private static final String LANGUAGE_SEPERATOR = ","; |
| 27 | + |
| 28 | + private static final String SOURCE_DIR = "./docs/"; |
| 29 | + private static final String BASE_DIR = SOURCE_DIR; |
| 30 | + private static final String OUTPUT_DIR = "./build/"; |
| 31 | + |
| 32 | + private static final String ADOC = "adoc"; |
| 33 | + private static final String HTML = "html"; |
| 34 | + private static final String HTML5 = "html5"; |
| 35 | + private static final String PDF = "pdf"; |
| 36 | + private static final String ENGLISH = "EN"; |
| 37 | + |
| 38 | + public static void main(final String[] args) { |
| 39 | + Objects.requireNonNull(System.getProperty(PROJECT_VERSION)); |
| 40 | + Objects.requireNonNull(System.getProperty(CURRICULUM_FILE_NAME)); |
| 41 | + Objects.requireNonNull(System.getProperty(VERSION_DATE)); |
| 42 | + Objects.requireNonNull(System.getProperty(LANGUAGES)); |
| 43 | + |
| 44 | + final String projectVersion = System.getProperty(PROJECT_VERSION); |
| 45 | + final String curriculumFileName = System.getProperty(CURRICULUM_FILE_NAME); |
| 46 | + final String versionDate = System.getProperty(VERSION_DATE); |
| 47 | + final String[] languages = System.getProperty(LANGUAGES).split(LANGUAGE_SEPERATOR); |
| 48 | + |
| 49 | + Stream.of(languages).forEach(language -> convertInLanguage( |
| 50 | + language, |
| 51 | + projectVersion, |
| 52 | + curriculumFileName, |
| 53 | + versionDate |
| 54 | + )); |
| 55 | + convertInLanguage( |
| 56 | + ENGLISH, |
| 57 | + projectVersion, |
| 58 | + INDEX_FILE_NAME, |
| 59 | + versionDate); |
| 60 | + } |
| 61 | + |
| 62 | + private static void convertInLanguage( |
| 63 | + final String language, |
| 64 | + final String projectVersion, |
| 65 | + final String curriculumFileName, |
| 66 | + final String versionDate) { |
| 67 | + Stream.of(FORMATS).forEach(format -> convertInFormat( |
| 68 | + format, projectVersion, |
| 69 | + curriculumFileName, |
| 70 | + versionDate, |
| 71 | + language |
| 72 | + )); |
| 73 | + } |
| 74 | + |
| 75 | + private static void convertInFormat( |
| 76 | + final String format, |
| 77 | + final String projectVersion, |
| 78 | + final String curriculumFileName, |
| 79 | + final String versionDate, |
| 80 | + final String language) { |
| 81 | + try (final Asciidoctor asciidoctor = create()) { |
| 82 | + final Attributes attributes = toAttributes( |
| 83 | + projectVersion, |
| 84 | + curriculumFileName, |
| 85 | + versionDate, |
| 86 | + language); |
| 87 | + asciidoctor.convertDirectory( |
| 88 | + List.of(new File("%s%s.%s".formatted( |
| 89 | + SOURCE_DIR, |
| 90 | + curriculumFileName, |
| 91 | + ADOC))), |
| 92 | + Options.builder() |
| 93 | + .baseDir(new File(BASE_DIR)) |
| 94 | + .backend(toBackend(format)) |
| 95 | + .mkDirs(true) |
| 96 | + .attributes(attributes) |
| 97 | + .standalone(true) |
| 98 | + .toDir(new File(OUTPUT_DIR)) |
| 99 | + .safe(SafeMode.UNSAFE) |
| 100 | + .build()); |
| 101 | + renameResultAccordingToLanguage(curriculumFileName, format, language); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private static Attributes toAttributes( |
| 106 | + final String projectVersion, |
| 107 | + final String curriculumFileName, |
| 108 | + final String versionDate, |
| 109 | + final String language) { |
| 110 | + final String fileVersion = "%s - %s".formatted(projectVersion, language); |
| 111 | + final String documentVersion = "%s-%s".formatted(fileVersion, versionDate); |
| 112 | + |
| 113 | + final Map<String, Object> attributes = new HashMap<>() {{ |
| 114 | + put("icons" , "font"); |
| 115 | + put("version-label" , ""); |
| 116 | + put("revnumber" , fileVersion); |
| 117 | + put("revdate" , versionDate); |
| 118 | + put("document-version" , documentVersion); |
| 119 | + put("currentDate" , versionDate); |
| 120 | + put("language" , language); |
| 121 | + put("curriculumFileName", curriculumFileName); |
| 122 | + put("debug_adoc" , false); |
| 123 | + put("pdf-themesdir" , "../pdf-theme/themes"); |
| 124 | + put("pdf-fontsdir" , "../pdf-theme/fonts"); |
| 125 | + put("pdf-theme" , "isaqb"); |
| 126 | + put("stylesheet" , "../html-theme/adoc-github.css"); |
| 127 | + put("stylesheet-dir" , "../html-theme"); |
| 128 | + put("data-uri" , true); |
| 129 | + put("allow-uri-read" , true); |
| 130 | + }}; |
| 131 | + |
| 132 | + return Attributes.builder().attributes(attributes).build(); |
| 133 | + } |
| 134 | + |
| 135 | + private static String toBackend(final String format) { |
| 136 | + return switch (format) { |
| 137 | + case HTML -> HTML5; |
| 138 | + case PDF -> PDF; |
| 139 | + default -> throw new IllegalArgumentException("Unknown target format %s".formatted(format)); |
| 140 | + }; |
| 141 | + } |
| 142 | + |
| 143 | + private static void renameResultAccordingToLanguage( |
| 144 | + final String fileName, |
| 145 | + final String format, |
| 146 | + final String language) { |
| 147 | + final File original = new File("%s%s.%s".formatted(OUTPUT_DIR, fileName, format)); |
| 148 | + final File renamed = new File("%s%s-%s.%s".formatted(OUTPUT_DIR, fileName, language.toLowerCase(), format)); |
| 149 | + if (!original.exists()) { |
| 150 | + System.err.printf("Failed to rename result file %s as it does not exist", original.getAbsolutePath()); |
| 151 | + } else if (!original.renameTo(renamed)) { |
| 152 | + System.err.printf("Failed to rename result file %s to %s%n", original.getName(), renamed.getName()); |
| 153 | + } |
| 154 | + original.deleteOnExit(); |
| 155 | + } |
| 156 | +} |
0 commit comments