Forum How do I...?

Table of contents, multiple files & JavaScript

Infarinato
On Håkon’s suggestion, here I am asking about an issue I’m having with JavaScript-generated table of contents.

Basically, I’m trying to reproduce the Oliver Twist example by splitting it up into different chunks (my ultimate aim is to replace these chunks with URLs and be able to “print on demand” portions of a website with an automatically generated TOC).

I use XInclude directives in a wrapper XML file to include the [UTF-8/XHTML version of the] different chunks (say, oliver-toc.html, oliver-1.html, oliver-2.html [first two chapters only, to reduce processing time]) after putting the CSS and JS into two separate files (of course, I need to remove the onload attribute from the body tag(s) and add window.onload = maketoc at the end of the JS file). I then call Prince like so:
prince -s oliver.css --script=oliver.js oliver-wrapper.xml -o oliver-twist.pdf
(Test files attached below).

To my greatest amazement, this almost works! :D The TOC gets generated with all the correct link texts and page references, but inactive hyperlinks.

Any advice on this would be greatly appreciated. :mrgreen:
  1. oliver.zip121.2 kB
    Oliver Twist in chunks
Johann
The elements span and a that you have created are not in the XHTML namespace!
Add/change the following lines and it will work:

var XHTML_NS = "http://www.w3.org/1999/xhtml";
...
var span = document.createElementNS(XHTML_NS,"span");
...
var link = document.createElementNS(XHTML_NS,"a");
...

- - -
Johann

Infarinato
Johann wrote:
The elements span and a that you have created are not in the XHTML namespace!
Add/change the following lines and it will work: […]

Thank you very much, Johann: it did! :D (I knew it ought to be something “XMLly” —which I admittedly know very little about. :oops:)

Now back to me :roll: and on with the creation of a full-fledged TOC (with h1, h2, etc.)! :)

Thanks.
Infarinato
Infarinato wrote:
Now back to me… :roll:

Quite! :? Alas, I seem to have stumbled upon another problem I’m unable to solve :oops:: it might still be something to do with my poor knowledge of XML (in which case feel free to “RTFM me”), but could also be due to the order in which Prince processes things, hence my asking.

Thanks to Johann’s advice, my test now works perfectly if all the files are local, but if I just change my oliver-wrapper.xml like this:
<?xml version="1.0" encoding="utf-8"?>
<xi:part xmlns:xi="http://www.w3.org/2001/XInclude">
	<xi:include href="oliver-toc.html"/>
	<xi:include href="http://www.mydomain.com/prince/test/oliver-1.html"/>
	<xi:include href="http://www.mydomain.com/prince/test/oliver-2.html"/>
</xi:part>
where oliver-1.html and oliver-2.html are now remote and to be requested via HTTP (remember my ultimate goal?), hyperlinks once again disappear and, what’s more, although link texts are correctly retrieved, now also page numbers all show as 0s on the table of contents. :(

Is this a real limitation (and hence I should give up trying to get JavaScript to automatically generate a TOC for a set of random web pages, and resort to a “more server-side” approach, instead) or am I doing once again something silly?

Please do forgive my pestering…
Infarinato
Infarinato wrote:
Is this a real limitation (and hence I should give up trying to get JavaScript to automatically generate a TOC for a set of random web pages, and resort to a “more server-side” approach, instead) or am I doing once again something silly?

Fortunately (and probably not surprisingly) the latter. :) The wrapper file needs to be changed to something like this:
<?xml version="1.0" encoding="utf-8"?>
<xi:part xmlns:xi="http://www.w3.org/2001/XInclude" xml:base="./">
	<xi:include href="oliver-toc.html"/>
	<xi:include href="http://www.mydomain.com/prince/test/oliver-1.html" xml:base="./"/>
	<xi:include href="http://www.mydomain.com/prince/test/oliver-2.html" xml:base="./"/>
</xi:part>
(…or [very possibly (*)] more elegant variations thereof —and probably the inclusion should also be refined by extracting only the body elements of the pages with XPointer, but that’s for another day!).

__________
(*) Using "./" as the base URI actually prevents remote files referenced from within the included pages to be retrieved, but hopefully it just a matter of finding the “right setting”, and certainly not a Prince limitation.
Infarinato
Infarinato wrote:
…and probably the inclusion should also be refined by extracting only the body elements of the pages with XPointer…

[An aside:] Am I right in thinking that Prince’s XPointer implementation only supports ID searching such as
xpointer="myid"
and not full element traversing like
xpointer="xpointer(/html/body/1)"
?
Infarinato
Infarinato wrote:
[An aside:] Am I right in thinking that Prince’s XPointer implementation only supports ID searching… and not full element traversing…

My bad: forgot about the namespace declaration. :oops: Sorry about the noise.