Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added option to choose navigation root #107

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions modules/lib/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,14 @@
"example": "docbook.odd"
}
},
{
"name": "nav-root",
"in": "query",
"schema": {
"type": "string",
"example": "1.4"
}
},
{
"name": "images-collection",
"in": "query",
Expand Down
5 changes: 4 additions & 1 deletion modules/lib/api/document.xql
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,15 @@ declare function dapi:epub($request as map(*)) {
};

declare %private function dapi:work2epub($request as map(*), $id as xs:string, $work as document-node(), $lang as xs:string?) {
let $navRootParam := $request?parameters?nav-root
let $navRoot := if ($navRootParam) then util:node-by-id($work, $navRootParam) else ()
let $imagesCollection := $request?parameters?images-collection
let $coverImage := $request?parameters?cover-image
let $config := map:merge(($config:epub-config($work, $lang), map {
'imagesCollection': $imagesCollection,
'skipTitle': $request?parameters?skip-title,
'coverImage': $coverImage
'coverImage': $coverImage,
'navRoot': $navRoot
}))
let $odd := head(($request?parameters?odd, $config:default-odd))
let $oddName := replace($odd, "^([^/\.]+).*$", "$1")
Expand Down
2 changes: 1 addition & 1 deletion modules/lib/epub.xql
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ declare function epub:nav-entry($config, $text) {
<body>
<nav epub:type="toc">
{
epub:toc-nav-div($config, nav:get-section($config?docConfig, $text)/..)
epub:toc-nav-div($config, if ($config?navRoot) then $config?navRoot else nav:get-section($config?docConfig, $text)/..)
}
</nav>
</body>
Expand Down
94 changes: 94 additions & 0 deletions test/navigation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const util = require('./util.js');
const path = require('path');
const FormData = require('form-data');
const chai = require('chai');
const chaiXML = require('chai-xml');
const expect = chai.expect;
const chaiResponseValidator = require('chai-openapi-response-validator');
const jsdom = require("jsdom");
const zip = require('adm-zip');
const { JSDOM } = jsdom;

const spec = path.resolve("./modules/lib/api.json");
chai.use(chaiResponseValidator(spec));
chai.use(chaiXML);

const testXml = `<TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader>
<fileDesc>
<titleStmt>
<title>EPUB Navigation Test</title>
</titleStmt>
<publicationStmt>
<p />
</publicationStmt>
<sourceDesc>
<p />
</sourceDesc>
</fileDesc>
</teiHeader>
<text>
<front>
<div type="document" n="1" xml:id="d1" subtype="pre-document">
<head>Pre-Document 1</head>
<p>Pre-Document 1 body</p>
</div>
<div type="document" n="2" xml:id="d2" subtype="pre-document">
<head>Pre-Document 2</head>
<p>Pre-Document 2 body</p>
</div>
<div type="document" n="3" xml:id="d3" subtype="pre-document">
<head>Pre-Document 3</head>
<p>Pre-Document 3 body</p>
</div>
</front>
<body>
<div type="document" n="4" xml:id="d4" subtype="document">
<head>Document 4</head>
<p>Document 4 body</p>
</div>
<div type="document" n="5" xml:id="d5" subtype="document">
<head>Document 5</head>
<p>Document 5 body</p>
</div>
<div type="document" n="6" xml:id="d6" subtype="document">
<head>Document 6</head>
<p>Document 6 body</p>
</div>
</body>
</text>
</TEI>`;

function getNav(data) {
return new zip(Buffer.from(data)).getEntries().find(({ entryName }) => entryName === 'OEBPS/nav.xhtml')?.getData().toString('utf-8');
}

describe('/api/document/{document}}/epub?nav-root=', function() {
before(async () => {
await util.login();
const formData = new FormData()
formData.append('files[]', testXml, "nav.xml");
const res = await util.axios.post('upload/playground', formData, {
headers: formData.getHeaders()
});
expect(res.data).to.have.length(1);
expect(res.data[0].name).to.equal('/db/apps/tei-publisher/data/playground/nav.xml');
expect(res).to.satisfyApiSpec;
});

it('let tei-publisher determine the navigation root', async () => {
const res = await util.axios.get('document/playground%2Fnav.xml/epub', { responseType: 'arraybuffer' });
expect(res.status).to.equal(200);
const document = new JSDOM(getNav(res.data), { contentType: "application/xml" }).window.document;
expect(document.querySelectorAll('li').length).to.equal(3);
});

it('define navigation root', async () => {
const res = await util.axios.get('document/playground%2Fnav.xml/epub?nav-root=1.4', { responseType: 'arraybuffer' });
expect(res.status).to.equal(200);
const document = new JSDOM(getNav(res.data), { contentType: "application/xml" }).window.document;
expect(document.querySelectorAll('li').length).to.equal(6);
});

after(util.logout);
});