for (DocElement element : elements) {
if (element instanceof DocMenu) {
DocMenu menu = (DocMenu) element;
for (int i = 0, maxIndex = menu.elements.size() - 1; i <= maxIndex; i++) {
DocElement subElement = menu.elements.get(i);
if (subElement instanceof DocPage) {
prepareTemplatePage((DocPage) subElement);
if (menu.showPager) {
// link to previous page
DocPage page = (DocPage) subElement;
DocElement prev = i == 0 ? null : menu.elements.get(i - 1);
if (prev != null && prev instanceof DocPage) {
page.prevPage = (DocPage) prev;
}
// link to next page
DocElement next = i == maxIndex ? null : menu.elements.get(i + 1);
if (next != null && next instanceof DocPage) {
page.nextPage = (DocPage) next;
}
// show pager is dependent on having at least a prev or next