Article pagination

28 October 2012

Keywords: split, paginate, chapter, part

The Problem

Suppose you’ve got a really long text that would better be split in chapters. Rather than writing an article by chapter (and loosing the possibility to comment the whole text), you can structure an article semantically and serve only one part at a time. No need to use HTML5 semantics (though this would be better), the good old header tags will suffice.

The Plan

So, suppose you have split the article by h4. Our plan will be to output everything between two h4 headers, where the header position will be given by some url parameter, say part. Additionally, we will create a linked table of contents.

The Solution

All we have to do is replace <txp:body /> in the article form by

<txp:etc_query data='<txp:body />'
  markup="html" globals="_GET"
  query="//h4[{?part|1|intval}]"
  replace='//h4=<a href="<txp:permlink />&amp;part={1+count(preceding-sibling::h4)}">{?}</a>'
  separator="%">
	{preceding-sibling::h4}
	{.|following-sibling::*
		[name()!='h4']
		[count(preceding-sibling::h4)={?part|1|intval}]
	}
	{following-sibling::h4}
</txp:etc_query>

and here we are.

Paginated article

The Problem

Suppose you’ve got a really long text that would better be split in chapters. Rather than writing an article by chapter (and loosing the possibility to comment the whole text), you can structure an article semantically and serve only one part at a time. No need to use HTML5 semantics (though this would be better), the good old header tags will suffice.

The Plan

The Solution

File(s)