Add support for custom limits and headers in News.
[xhtml-compiler.git] / XHTMLCompiler / DOMFilter / News.php
blob311c1e9b4270e831b5176b9a75d568adebf0e917
1 <?php
3 /**
4 * Mini blog-style filter for globbing contents of another directory
5 * and inserting summaries into a main page.
6 * @note This filter should be run before other "cosmetic" filters, as
7 * it makes major changes to page content and may pull data from
8 * sources that have not been processed yet.
9 */
10 class XHTMLCompiler_DOMFilter_News extends XHTMLCompiler_DOMFilter
12 protected $name = 'News';
13 protected $prefix = 'news';
14 public function process(DOMDocument $dom, $page, $manager) {
15 $container = $this->query("//html:div[@news:source]")->item(0);
16 if (!$container) return;
18 // Grab variables
19 $source = $this->confiscateAttr($container, $this->ns, 'source');
20 $source = $page->normalizePath($source);
21 $limit = $this->confiscateAttr($container, $this->ns, 'limit');
22 if (!$limit) $limit = 5;
23 $header = $this->confiscateAttr($container, $this->ns, 'header');
24 if (!$header) $header = 'h2';
26 // Recursively glob for source files
27 // :TODO: add DI for this
28 $fs = new FSTools();
29 $result = $fs->globr($source, '*.xhtml');
30 rsort($result);
32 // Setup manager
33 $xc = XHTMLCompiler::getInstance();
34 $manager = $xc->getFilterManager();
36 for ($i = $d = 0, $c = count($result); $i < $c && $d < $limit; $i++) {
37 $entry = $result[$i];
39 // :TODO: Add support for nested directories. Also need to modify
40 // generateId when that happens.
41 $base = basename($entry);
42 if (strlen($base) < 4 || !ctype_digit(substr($base, 0, 4))) {
43 continue;
46 $entryFile = new XHTMLCompiler_Page($entry);
47 // This DOM has IDs setup, but no other processing setup.
48 $subdom = $manager->parse($entryFile->getSource(), $entryFile);
50 $entryNode = $dom->createElement('div');
51 $entryNode->setAttribute('class', 'item');
53 // Generate ID for this entry. The format is
54 // year-month-date-lc-title-with-dashes
55 $entryNode->setAttribute('id', $this->generateId($entryFile->getPathStem()));
57 // Grab the title
58 $node = $subdom->getElementsByTagName('h1')->item(0);
59 $h1 = $dom->importNode($node, true);
60 $hx = $dom->createElement($header);
61 $hx->setAttribute('class', 'title');
62 foreach ($h1->childNodes as $h1node) $hx->appendChild($h1node);
63 $entryNode->appendChild($hx);
65 // Possible place for factoring out. Also, we can also add
66 // a timezone to be more like ours.
67 $dateTime = $entryFile->getCreatedTime();
68 if ($dateTime) {
69 $time = $dateTime->format('g:i A e');
70 $date = $dateTime->format('l, F j, Y');
71 $dateNode = $dom->createElement('div');
72 $dateNode->setAttribute('class', 'date');
73 $abbrNode = $dom->createElement('abbr');
74 $abbrNode->setAttribute('class', 'at' . $dateTime->format('U'));
75 $dateText = $dom->createTextNode("Posted $time on $date");
76 $abbrNode->appendChild($dateText);
77 $dateNode->appendChild($abbrNode);
78 $entryNode->appendChild($dateNode);
81 // Grab the content
82 // :WARNING: This code apparently leaves behind an
83 // xmlns statement, although we're not quite sure why.
84 $node = $subdom->getElementById('content');
85 $node = $dom->importNode($node, true);
86 $this->confiscateAttr($node, 'id');
87 $node->setAttribute('class', 'body');
88 $entryNode->appendChild($node);
90 // Make permalink
91 $permalink = $dom->createElement('div');
92 $permalink->setAttribute('class', 'permalink');
93 $a = $dom->createElement('a');
94 $a->setAttribute('href', $entryFile->getAbsolutePath());
95 $a->appendChild($dom->createTextNode('Permalink'));
96 $permalink->appendChild($a);
97 $entryNode->appendChild($permalink);
99 $container->appendChild($entryNode);
101 // increment one successful
102 $d++;
107 * Generates an ID based on the filename of a blog entry
109 public function generateId($entry) {
110 $parts = array_slice(explode('/', $entry), -2);
111 // A very specific format: year/monthday-entry-name, like 2008/0131-foobar
112 // Arbitrary folders before that is ok.
113 $entry = 'entry-' . $parts[0] .
114 '-' . substr($parts[1], 0, 2) .
115 '-' . substr($parts[1], 2, 2) .
116 '-' . substr($parts[1], 5);
117 return $entry;