Article pagination

2012-10-28 19:43

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 />?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

The Plan

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 />?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.

File(s)