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 1 commit
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 @@ -884,6 +884,14 @@
"type": "string",
"example": "docbook.odd"
}
},
{
"name": "nav-root",
"in": "query",
"schema": {
"type": "string",
"example": "/1/4"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test below uses a value of 1/4, but this example has a leading slash - /1/4. Is the leading slash optional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, it should be /1/4 won't work, I will update it

}
}
],
"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)] })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TEI Publisher application makes extensive use of the functions util:node-id() and util:node-by-id(). I suspect they may exhibit better performance. Were you aware of these functions? If so, did you have a reason for opting not to use them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose to use the position of the node instead because the node to be the root for navigation will most likely be text or body, which don't always have an ID

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The util:node-id() and util:node-by-id() functions do use the node's numeric position in the node hierarchy. For their function documentation, see https://exist-db.org/exist/apps/fundocs/view.html?uri=http://exist-db.org/xquery/util#node-by-id.2.

For example, given the sample TEI file from your test saved to /db/test/test.xml, the query util:node-id(doc("/db/test/test.xml")/tei:TEI/tei:text) returns 1.4, and correspondingly, the query util:node-by-id(doc("/db/test/test.xml"), "1.4") returns the <text> element.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't thought of using that, I will test it and let you know.

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);
});