5ccd4f6b41c6c358b03eef8f47d1a6549a809813
[xhtml-compiler.git] / XHTMLCompiler / DOMFilter / NewsLinker.php
blob5ccd4f6b41c6c358b03eef8f47d1a6549a809813
1 <?php
3 /**
4 * This filter, closely related with the News filter, generates links
5 * between news entries.
6 */
7 class XHTMLCompiler_DOMFilter_NewsLinker extends XHTMLCompiler_DOMFilter
9 protected $name = 'NewsLinker';
10 public function process(DOMDocument $dom, $page, $manager) {
11 if (!isset($page->attr['news'])) return;
13 // calculate forward and backward links
14 $fs = new FSTools();
15 // we assume that one directory up is the years directory. An alternate
16 // algorithm may be needed if we decide to allow further nesting.
17 // This code needs to be factored out
18 $result = $fs->globr(dirname($page->getDirname()), '*.xhtml');
19 // This doesn't work if there is more than one entry in a day
20 sort($result);
21 $prev = $next = null;
22 $found = false;
23 foreach ($result as $i => $entry) {
24 $base = basename($entry);
25 if (strlen($base) < 4 || !ctype_digit(substr($base, 0, 4))) {
26 continue;
28 if ($page->getSourcePath() == $entry) {
29 $found = true;
30 } elseif (!$found) {
31 $prev = $entry;
32 } else {
33 $next = $entry;
34 break;
38 $content = $dom->getElementById('content');
39 $body = $content->parentNode;
40 $nav = $dom->createElement('div');
41 $nav->setAttribute('id', 'news-navigation');
43 if ($prev) {
44 $prevPage = new XHTMLCompiler_Page($prev);
45 if (!$page->isCacheExistent()) {
46 // Force a new cache run
47 $prevPage->touch();
49 $prevDiv = $dom->createElement('div');
50 $prevDiv->setAttribute('class', 'prev');
51 $a = $dom->createElement('a', 'Previous');
52 $a->setAttribute('href', $prevPage->getAbsolutePath());
53 $prevDiv->appendChild($a);
54 $nav->appendchild($prevDiv);
57 $indexDiv = $dom->createElement('div');
58 $indexDiv->setAttribute('class', 'index');
59 $a = $dom->createElement('a', 'Index');
60 $indexDiv->appendChild($a);
61 //$nav->appendChild($indexDiv);
63 if ($next) {
64 $nextPage = new XHTMLCompiler_Page($next);
65 $nextDiv = $dom->createElement('div');
66 $nextDiv->setAttribute('class', 'next');
67 $a = $dom->createElement('a', 'Next');
68 $a->setAttribute('href', $nextPage->getAbsolutePath());
69 $nextDiv->appendChild($a);
70 $nav->appendChild($nextDiv);
73 $body->appendChild($nav);