From 07009fee4c36df85d8cc184c0c6313d8bb46b2d8 Mon Sep 17 00:00:00 2001 From: Jarno Elovirta Date: Fri, 8 Nov 2024 14:58:59 +0200 Subject: [PATCH] Add initial support for built-in default theme --- build.gradle | 2 +- src/generator/com/elovirta/pdf/merge.xsl | 3 ++ src/generator/com/elovirta/pdf/toc.xsl | 32 +++++++++---------- .../pdf/ant/StylesheetGeneratorTask.java | 19 +++++++++-- .../resources/com/elovirta/pdf/default.json | 7 ++++ 5 files changed, 43 insertions(+), 20 deletions(-) create mode 100644 src/main/resources/com/elovirta/pdf/default.json diff --git a/build.gradle b/build.gradle index 85e38a9..2fbabe1 100644 --- a/build.gradle +++ b/build.gradle @@ -37,7 +37,7 @@ dependencies { sourceSets { main { resources { - srcDirs = ["src/generator"] + srcDirs = ["src/generator", "src/main/resources"] include "**/*.xsl" include "**/*.json" } diff --git a/src/generator/com/elovirta/pdf/merge.xsl b/src/generator/com/elovirta/pdf/merge.xsl index b11a928..348e77e 100644 --- a/src/generator/com/elovirta/pdf/merge.xsl +++ b/src/generator/com/elovirta/pdf/merge.xsl @@ -21,6 +21,9 @@ + + + diff --git a/src/generator/com/elovirta/pdf/toc.xsl b/src/generator/com/elovirta/pdf/toc.xsl index f36409c..23c4de3 100644 --- a/src/generator/com/elovirta/pdf/toc.xsl +++ b/src/generator/com/elovirta/pdf/toc.xsl @@ -573,23 +573,23 @@ - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + diff --git a/src/main/java/com/elovirta/pdf/ant/StylesheetGeneratorTask.java b/src/main/java/com/elovirta/pdf/ant/StylesheetGeneratorTask.java index 609f343..abd4c5d 100644 --- a/src/main/java/com/elovirta/pdf/ant/StylesheetGeneratorTask.java +++ b/src/main/java/com/elovirta/pdf/ant/StylesheetGeneratorTask.java @@ -12,8 +12,7 @@ import javax.xml.transform.TransformerException; import javax.xml.transform.URIResolver; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.net.URI; import java.nio.file.Files; import java.util.HashMap; @@ -29,6 +28,9 @@ public class StylesheetGeneratorTask extends Task { private static final QName ATTR = QName.fromClarkName("{}attr"); private static final QName VERSION = QName.fromClarkName("{}version"); private static final QName FORMATTER = QName.fromClarkName("{}formatter"); + /** Magic extends for build-in theme. */ + private static final String DEFAULT_EXTENDS = "default"; + private static final URI DEFAULT_EXTENDS_URI = URI.create("classpath:/com/elovirta/pdf/default.json"); private File template; private File dstDir; @@ -198,9 +200,20 @@ private XdmItem parseYamlTemplate(final XsltExecutable executable, final XdmItem try { final Xslt30Transformer transformer = executable.load30(); final XdmItem extendsValue = xpathCompiler.evaluateSingle(". ?extends", base); - if (extendsValue != null) { + if (extendsValue != null && extendsValue.getStringValue().equals(DEFAULT_EXTENDS)) { + final XdmItem extendsRes = xpathCompiler.evaluateSingle("json-doc(.)", new XdmAtomicValue(DEFAULT_EXTENDS_URI)); + + final XdmValue flattened = transformer.callFunction(QName.fromClarkName("{x}flatten"), new XdmValue[]{ + base + }); + final XdmValue normalized = transformer.callFunction(QName.fromClarkName("{x}normalize"), new XdmValue[]{ + flattened, XdmEmptySequence.getInstance(), new XdmAtomicValue(DEFAULT_EXTENDS_URI) + }); + return normalized.itemAt(0); + } else if (extendsValue != null) { final URI extendsUri = url.resolve(extendsValue.getStringValue()); final XdmItem extendsRes = parseYamlTemplate(executable, parseYaml(extendsUri), url); + final XdmValue flattened = transformer.callFunction(QName.fromClarkName("{x}flatten"), new XdmValue[]{ base }); diff --git a/src/main/resources/com/elovirta/pdf/default.json b/src/main/resources/com/elovirta/pdf/default.json new file mode 100644 index 0000000..9d3b971 --- /dev/null +++ b/src/main/resources/com/elovirta/pdf/default.json @@ -0,0 +1,7 @@ +{ + "style": { + "toc-1": { + "color": "pink" + } + } +}