Skip to content

Commit

Permalink
added option to choose navigation root
Browse files Browse the repository at this point in the history
  • Loading branch information
Charafeddine Cheraa committed Aug 12, 2022
1 parent 3d38420 commit 0b81ad8
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 2 deletions.
8 changes: 8 additions & 0 deletions modules/lib/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,14 @@
"type": "string",
"example": "docbook.odd"
}
},
{
"name": "nav-root",
"in": "query",
"schema": {
"type": "string",
"example": "/1/4"
}
}
],
"responses": {
Expand Down
3 changes: 2 additions & 1 deletion modules/lib/api/document.xql
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ 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 $config := $config:epub-config($work, $lang)
let $navRoot := fn:fold-left(tokenize($request?parameters?nav-root, '/'), (), function($a, $b) { (if (not(empty($a))) then $a else $work)/node()[position()=xs:integer($b)] })
let $config := map:merge(($config:epub-config($work, $lang), map { 'navRoot': $navRoot }))
let $odd := head(($request?parameters?odd, $config:default-odd))
let $oddName := replace($odd, "^([^/\.]+).*$", "$1")
let $cssDefault := util:binary-to-string(util:binary-doc($config:output-root || "/" || $oddName || ".css"))
Expand Down
2 changes: 1 addition & 1 deletion modules/lib/epub.xql
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,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
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@teipublisher/pb-components": "latest"
},
"devDependencies": {
"adm-zip": "^0.5.9",
"axios": "^0.21.2",
"chai": "^4.2.0",
"chai-openapi-response-validator": "^0.9.4",
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);
});

0 comments on commit 0b81ad8

Please sign in to comment.