Skip to content

Commit

Permalink
Add initial support for built-in default theme
Browse files Browse the repository at this point in the history
  • Loading branch information
jelovirt committed Nov 10, 2024
1 parent 3e51275 commit 07009fe
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dependencies {
sourceSets {
main {
resources {
srcDirs = ["src/generator"]
srcDirs = ["src/generator", "src/main/resources"]
include "**/*.xsl"
include "**/*.json"
}
Expand Down
3 changes: 3 additions & 0 deletions src/generator/com/elovirta/pdf/merge.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

<xsl:variable name="current" select="x:normalize(x:flatten($base), (), $url)"/>
<xsl:choose>
<xsl:when test="map:contains($base, 'extends') and $base ?extends eq 'default'">
<xsl:sequence select="json-doc(xs:anyURI('classpath:/com/elovirta/pdf/default.json'))"/>
</xsl:when>
<xsl:when test="map:contains($base, 'extends')">
<xsl:variable name="extends-url" select="resolve-uri($base ?extends, $url)"/>
<xsl:variable name="extends" select="x:extends(json-doc($extends-url), $extends-url)"/>
Expand Down
32 changes: 16 additions & 16 deletions src/generator/com/elovirta/pdf/toc.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -573,23 +573,23 @@
<xsl:with-param name="properties" select="$allProperties[. ne 'start-indent']"/>
</xsl:call-template>
</axsl:attribute-set>

<axsl:attribute-set name="e:part_toc">
<xsl:call-template name="generate-attribute-set">
<xsl:with-param name="prefix" select="'style-part-toc'"/>
</xsl:call-template>
</axsl:attribute-set>
<axsl:attribute-set name="e:chapter_toc">
<xsl:call-template name="generate-attribute-set">
<xsl:with-param name="prefix" select="'style-chapter-toc'"/>
</xsl:call-template>
</axsl:attribute-set>
<axsl:attribute-set name="e:appendix_toc">
<xsl:call-template name="generate-attribute-set">
<xsl:with-param name="prefix" select="'style-chapter-appendix'"/>
</xsl:call-template>
</axsl:attribute-set>
</xsl:if>

<axsl:attribute-set name="e:part_toc">
<xsl:call-template name="generate-attribute-set">
<xsl:with-param name="prefix" select="'style-part-toc'"/>
</xsl:call-template>
</axsl:attribute-set>
<axsl:attribute-set name="e:chapter_toc">
<xsl:call-template name="generate-attribute-set">
<xsl:with-param name="prefix" select="'style-chapter-toc'"/>
</xsl:call-template>
</axsl:attribute-set>
<axsl:attribute-set name="e:appendix_toc">
<xsl:call-template name="generate-attribute-set">
<xsl:with-param name="prefix" select="'style-chapter-appendix'"/>
</xsl:call-template>
</axsl:attribute-set>
</axsl:stylesheet>
</xsl:template>

Expand Down
19 changes: 16 additions & 3 deletions src/main/java/com/elovirta/pdf/ant/StylesheetGeneratorTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
});
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/com/elovirta/pdf/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"style": {
"toc-1": {
"color": "pink"
}
}
}

0 comments on commit 07009fe

Please sign in to comment.