Skip to content

Commit

Permalink
Create a version of SVGs that contains the required CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
thvitt committed Dec 12, 2016
1 parent ed5db3e commit 9eca2c1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/main/java/net/faustedition/gen/DiplomaticConversion.java
Expand Up @@ -31,6 +31,7 @@

import javax.xml.stream.XMLStreamException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamSource;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
Expand All @@ -41,6 +42,12 @@

import de.faustedition.transcript.simple.SimpleTransform;
import fi.iki.elonen.SimpleWebServer;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmAtomicValue;
import net.sf.saxon.s9api.XsltExecutable;
import net.sf.saxon.s9api.XsltTransformer;

public class DiplomaticConversion {

Expand Down Expand Up @@ -230,6 +237,8 @@ public static void main(final String[] args) throws IOException {
.parallel()
.map(page -> page.writeTranscriptJson())
.collect(Collectors.toList());
ImmutableList<TranscriptPage> allPages = ImmutableList.copyOf(transcriptPages);



int nThreads = Integer.valueOf(System.getProperty("faust.diplo.threads", "0"));
Expand All @@ -255,6 +264,8 @@ public static void main(final String[] args) throws IOException {
transcriptPages = failedConversions;
} while (failedPages > 0 && failedPages < totalPages);

addCssToSvgs(allPages);


if (!failedConversions.isEmpty()) {
logger.log(Level.SEVERE, MessageFormat.format("Conversion of the following {0} pages failed after {1} tries:\n {2}",
Expand Down Expand Up @@ -309,7 +320,30 @@ private static List<TranscriptPage> runSVGconversion(List<TranscriptPage> pages,
logger.log(Level.INFO, MessageFormat.format("... rendering failed for {0} pages:\n\t{1}", Joiner.on("\n\t").join(failedConversions)));
return failedConversions;
}

private static void addCssToSvgs(final List<TranscriptPage> transcripts) throws InterruptedException {
logger.info("Creating SVGs with CSS ...");
final Processor processor = new Processor(false);
try {
final XsltExecutable xslt = processor.newXsltCompiler().compile(new StreamSource(new File("src/main/resources/add-svg-stylesheet.xsl")));
final URI cssURI = Paths.get("src", "main", "web", "css", "document-transcript.css").toAbsolutePath().toUri();

for (final TranscriptPage page : transcripts)
try {
XsltTransformer transformer = xslt.load();
transformer.setParameter(new QName("css"), new XdmAtomicValue(cssURI));
transformer.setSource(new StreamSource(diplomatic_path.resolve(page.getPagePath("svg")).toFile()));
transformer.setDestination(processor.newSerializer(target.resolve("prepared-svg").resolve(page.getNewPagePath("svg")).toFile()));
transformer.transform();
} catch (SaxonApiException e) {
logger.log(Level.WARNING, e, () -> MessageFormat.format("Failed to add CSS to {0}: {1}", page, e.getMessage()));
}
} catch (SaxonApiException e) {
logger.log(Level.SEVERE, e, () -> MessageFormat.format("Failed to configure CSS inclusion: {0}", e.getMessage()));
}
}


public static Path resolveFaustUri(final URI uri) {
return root.resolve(uri.getPath().substring(1));
}
Expand Down
31 changes: 31 additions & 0 deletions src/main/resources/add-svg-stylesheet.xsl
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.w3.org/2000/svg"
xpath-default-namespace="http://www.w3.org/2000/svg"
exclude-result-prefixes="xs"
version="2.0">

<xsl:param name="css" required="yes"/>
<xsl:param name="css-content" select="unparsed-text($css)"/>
<xsl:output method="xml" cdata-section-elements="style"/>

<xsl:template match="/svg">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<defs>
<style type="text/css">
<xsl:copy-of select="$css-content"/>
</style>
</defs>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*, node()"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

0 comments on commit 9eca2c1

Please sign in to comment.