Skip to content

Commit

Permalink
Allow adding extra resources to EPUB files
Browse files Browse the repository at this point in the history
This adds a few extra `MetaProperty` definitions that allow for adding
resources to EPUB files. It also introduces Tika to guess content types
for file suffixes instead of the hardcoded table in previous versions.

Fix: #19
  • Loading branch information
io7m committed Sep 20, 2022
1 parent c653e41 commit a7bc3fa
Show file tree
Hide file tree
Showing 13 changed files with 332 additions and 26 deletions.
Expand Up @@ -57,6 +57,40 @@
inserted into the <Term type="term">colophon</Term> page of the generated EPUB.
</Paragraph>
</Subsection>
<Subsection title="com.io7m.xstructural.epub.resource">
<Paragraph>
The contents of this property add an extra file to the manifest. The property can be specified multiple times to
add multiple resources. The resources are resolved relative to the source directory of the document.
</Paragraph>
</Subsection>
<Subsection title="com.io7m.xstructural.epub.resource-list">
<Paragraph>
The contents of this property denote a file that contains a list of extra resources to add to the manifest. The
file name is resolved relative to the source directory of the document. The file must contain one resource file
per line. For example, if a file named
<Term type="file">extra-files.txt</Term>
contains the following text:
</Paragraph>
<FormalItem title="Example File List">
<Verbatim><![CDATA[file0.txt
file1.txt
file2.txt
]]></Verbatim>
</FormalItem>
<Paragraph>
Then the following <Term type="expression">MetaProperty</Term> declarations are effectively equivalent:
</Paragraph>
<FormalItem title="Example File List">
<Verbatim>
<![CDATA[<MetaProperty name="com.io7m.xstructural.epub.resource-list">extra-files.txt</MetaProperty>]]></Verbatim>
</FormalItem>
<FormalItem title="Example Files">
<Verbatim><![CDATA[<MetaProperty name="com.io7m.xstructural.epub.resource">file0.txt</MetaProperty>
<MetaProperty name="com.io7m.xstructural.epub.resource">file1.txt</MetaProperty>
<MetaProperty name="com.io7m.xstructural.epub.resource">file2.txt</MetaProperty>
]]></Verbatim>
</FormalItem>
</Subsection>
<Subsection title="Examples">
<FormalItem title="EPUB Example">
<Verbatim><![CDATA[
Expand All @@ -66,6 +100,10 @@
<dc:title>Example Document</dc:title>
<MetaProperty name="com.io7m.xstructural.epub.colophon">colophon_extra.xml</MetaProperty>
<MetaProperty name="com.io7m.xstructural.epub.cover">cover.jpg</MetaProperty>
<MetaProperty name="com.io7m.xstructural.epub.resource">file0.txt</MetaProperty>
<MetaProperty name="com.io7m.xstructural.epub.resource">file1.txt</MetaProperty>
<MetaProperty name="com.io7m.xstructural.epub.resource">file2.txt</MetaProperty>
<MetaProperty name="com.io7m.xstructural.epub.resource-list">extra-files.txt</MetaProperty>
</Metadata>
...
]]></Verbatim>
Expand Down
Expand Up @@ -23,6 +23,7 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -1062,4 +1063,104 @@ public void testTransformXHTMLEPUBOKExampleTwice_80()
Assertions.assertEquals(0, main.exitCode());
}
}

@Test
public void testTransformEPUB4_80()
throws Exception
{
final var file0 =
Files.writeString(
this.sourceDirectory.resolve("file0.txt"),
"Text 0."
);
final var file1 =
Files.writeString(
this.sourceDirectory.resolve("file1.txt"),
"Text 1."
);
final var file2 =
Files.writeString(
this.sourceDirectory.resolve("file2.txt"),
"Text 2."
);

final var main = new Main(new String[]{
"epub",
"--sourceFile",
XSTestDirectories.resourceOf(
XSCommandLineTest.class,
this.sourceDirectory,
"example4_80.xml")
.toString(),
"--outputDirectory",
this.outputDirectory.toString(),
"--traceFile",
this.directory.resolve("trace.xml").toString(),
"--messagesFile",
this.directory.resolve("messages.log").toString(),
"--verbose",
"trace"
});

final var capture =
XSOutputCaptured.capture(main::run);

Assertions.assertEquals(0, main.exitCode());
}

@Test
public void testTransformEPUB5_80()
throws Exception
{
final var file =
this.sourceDirectory.resolve("extra-resources.txt");

try (var writer = Files.newBufferedWriter(file, UTF_8)) {
writer.append("file0.txt");
writer.newLine();
writer.append("file1.txt");
writer.newLine();
writer.append("file2.txt");
writer.newLine();
}

final var file0 =
Files.writeString(
this.sourceDirectory.resolve("file0.txt"),
"Text 0."
);
final var file1 =
Files.writeString(
this.sourceDirectory.resolve("file1.txt"),
"Text 1."
);
final var file2 =
Files.writeString(
this.sourceDirectory.resolve("file2.txt"),
"Text 2."
);

final var main = new Main(new String[]{
"epub",
"--sourceFile",
XSTestDirectories.resourceOf(
XSCommandLineTest.class,
this.sourceDirectory,
"example5_80.xml")
.toString(),
"--outputDirectory",
this.outputDirectory.toString(),
"--traceFile",
this.directory.resolve("trace.xml").toString(),
"--messagesFile",
this.directory.resolve("messages.log").toString(),
"--verbose",
"trace"
});

final var capture =
XSOutputCaptured.capture(main::run);

Assertions.assertEquals(0, main.exitCode());
}
}
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8" ?>

<!-- Comments! -->

<Document xmlns="urn:com.io7m.structural:8:0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
tableOfContentsDepth="3"
tableOfContents="true">

<!-- Comments! -->

<Metadata>
<dc:contributor>A Contributor</dc:contributor>
<dc:creator>A Creator</dc:creator>
<dc:description>An example article.</dc:description>
<dc:identifier>7486b85a-30de-4007-8b32-679b8a851b91</dc:identifier>
<dc:language>English</dc:language>
<dc:publisher>A Publisher</dc:publisher>
<dc:relation>A Relation</dc:relation>
<dc:rights>CC-0</dc:rights>
<dc:source>A Source</dc:source>
<dc:title>Example Document</dc:title>

<MetaProperty name="com.io7m.xstructural.epub.resource">file0.txt</MetaProperty>
<MetaProperty name="com.io7m.xstructural.epub.resource">file1.txt</MetaProperty>
<MetaProperty name="com.io7m.xstructural.epub.resource">file2.txt</MetaProperty>
</Metadata>

<Section title="08eb2001-58ef-44be-acce-5eecee051515">
<Paragraph>
No bagel.
</Paragraph>
<Paragraph>
No bagel.
</Paragraph>
<Paragraph>
No bagel.
</Paragraph>
<Paragraph>
No bagel.
</Paragraph>
</Section>

</Document>
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8" ?>

<!-- Comments! -->

<Document xmlns="urn:com.io7m.structural:8:0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
tableOfContentsDepth="3"
tableOfContents="true">

<!-- Comments! -->

<Metadata>
<dc:contributor>A Contributor</dc:contributor>
<dc:creator>A Creator</dc:creator>
<dc:description>An example article.</dc:description>
<dc:identifier>7486b85a-30de-4007-8b32-679b8a851b91</dc:identifier>
<dc:language>English</dc:language>
<dc:publisher>A Publisher</dc:publisher>
<dc:relation>A Relation</dc:relation>
<dc:rights>CC-0</dc:rights>
<dc:source>A Source</dc:source>
<dc:title>Example Document</dc:title>

<MetaProperty name="com.io7m.xstructural.epub.resource-list">extra-resources.txt</MetaProperty>
</Metadata>

<Section title="08eb2001-58ef-44be-acce-5eecee051515">
<Paragraph>
No bagel.
</Paragraph>
<Paragraph>
No bagel.
</Paragraph>
<Paragraph>
No bagel.
</Paragraph>
<Paragraph>
No bagel.
</Paragraph>
</Section>

</Document>
4 changes: 4 additions & 0 deletions com.io7m.xstructural.vanilla/pom.xml
Expand Up @@ -70,6 +70,10 @@
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
</dependency>
</dependencies>

</project>
Expand Up @@ -144,6 +144,11 @@ private void executeTransform()
QName.fromEQName("outputFile"),
XdmValue.makeValue(outputFile.toUri().toString())
);
transformer.setParameter(
QName.fromEQName("sourceDirectory"),
XdmValue.makeValue(
this.request.sourceFile().getParent().toUri().toString())
);

LOG.debug("output file: {}", outputFile);
LOG.debug("executing stylesheet");
Expand Down
Expand Up @@ -162,6 +162,12 @@ private void executeTransform()
XdmValue.makeValue(outputPath.toUri().toString())
);

transformer.setParameter(
QName.fromEQName("xstructural.sourceDirectory"),
XdmValue.makeValue(
this.request.sourceFile().getParent().toUri().toString())
);

this.request.brandingFile()
.ifPresent(path -> {
LOG.debug("branding file: {}", path);
Expand Down
Expand Up @@ -21,11 +21,10 @@
import net.sf.saxon.s9api.SequenceType;
import net.sf.saxon.s9api.XdmAtomicValue;
import net.sf.saxon.s9api.XdmValue;
import org.apache.tika.Tika;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Locale;

import static net.sf.saxon.s9api.ItemType.STRING;
import static net.sf.saxon.s9api.OccurrenceIndicator.ONE;

Expand All @@ -39,13 +38,15 @@ public final class XSMIMEExtensionFunction
private static final Logger LOG =
LoggerFactory.getLogger(XSMIMEExtensionFunction.class);

private final Tika tika;

/**
* The {@code mimeOf} extension function.
*/

public XSMIMEExtensionFunction()
{

this.tika = new Tika();
}

@Override
Expand All @@ -70,21 +71,9 @@ public SequenceType[] getArgumentTypes()
public XdmValue call(
final XdmValue[] arguments)
{
final String arg = arguments[0].itemAt(0).getStringValue();
LOG.trace("mimeOf: {}", arg);

final var argUpper = arg.toUpperCase(Locale.ROOT);
if (argUpper.endsWith(".PNG")) {
return new XdmAtomicValue("image/png");
}
if (argUpper.endsWith(".JPG")) {
return new XdmAtomicValue("image/jpeg");
}
if (argUpper.endsWith(".SVG")) {
return new XdmAtomicValue("image/svg+xml");
}
throw new IllegalArgumentException(
String.format("Could not determine the type of file '%s'", arg)
);
final var arg = arguments[0].itemAt(0).getStringValue();
final var detected = this.tika.detect(arg);
LOG.trace("mimeOf: {} -> {}", arg, detected);
return new XdmAtomicValue(detected);
}
}
Expand Up @@ -33,6 +33,7 @@
requires java.xml;
requires org.apache.commons.compress;
requires org.apache.commons.io;
requires org.apache.tika.core;
requires org.slf4j;
requires org.w3c.epubcheck;

Expand Down

0 comments on commit a7bc3fa

Please sign in to comment.