Skip to content

Commit

Permalink
Apply fix of index.html generation
Browse files Browse the repository at this point in the history
  • Loading branch information
programming-wolf committed Nov 17, 2023
1 parent b6fa743 commit 1e6acf4
Showing 1 changed file with 134 additions and 132 deletions.
266 changes: 134 additions & 132 deletions src/main/java/org/isaqb/asciidoc/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,141 +16,143 @@

public class Main {

private static final String PROJECT_VERSION = "projectVersion";
private static final String CURRICULUM_FILE_NAME = "curriculumFileName";
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 versionDate = System.getProperty(VERSION_DATE);
final String[] languages = System.getProperty(LANGUAGES).split(LANGUAGE_SEPERATOR);

Stream.of(languages).forEach(language -> convertInLanguage(
language,
projectVersion,
curriculumFileName,
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 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 versionDate = System.getProperty(VERSION_DATE);
final String[] languages = System.getProperty(LANGUAGES).split(LANGUAGE_SEPERATOR);

Stream.of(languages).forEach(language -> convertInLanguage(
language,
projectVersion,
curriculumFileName,
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<String, Object> 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<String, Object> 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();
}
}

0 comments on commit 1e6acf4

Please sign in to comment.