Implement RSSFromGit, a substitute for RSSFromSVN.
[xhtml-compiler.git] / XHTMLCompiler / DOMFilter / NewsLinker.php
blob01f6c6cf61e279d3ee8e1886030da3547ad842ab
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 $body = $dom->getElementsByTagName('body')->item(0);
39 $nav = $dom->createElement('div');
40 $nav->setAttribute('id', 'news-navigation');
42 if ($prev) {
43 $prevPage = new XHTMLCompiler_Page($prev);
44 if (!$page->isCacheExistent()) {
45 // Force a new cache run
46 $prevPage->touch();
48 $prevDiv = $dom->createElement('div');
49 $prevDiv->setAttribute('class', 'prev');
50 $a = $dom->createElement('a', 'Previous');
51 $a->setAttribute('href', $prevPage->getAbsolutePath());
52 $prevDiv->appendChild($a);
53 $nav->appendchild($prevDiv);
56 $indexDiv = $dom->createElement('div');
57 $indexDiv->setAttribute('class', 'index');
58 $a = $dom->createElement('a', 'Index');
59 $indexDiv->appendChild($a);
60 //$nav->appendChild($indexDiv);
62 if ($next) {
63 $nextPage = new XHTMLCompiler_Page($next);
64 $nextDiv = $dom->createElement('div');
65 $nextDiv->setAttribute('class', 'next');
66 $a = $dom->createElement('a', 'Next');
67 $a->setAttribute('href', $nextPage->getAbsolutePath());
68 $nextDiv->appendChild($a);
69 $nav->appendChild($nextDiv);
72 $body->appendChild($nav);