Implement Newslinker, also fix some bugs.
[xhtml-compiler.git] / XHTMLCompiler / DOMFilter / News.php
blobd6aaa0c90e188b4a7c23916c000c0c5c55d891e5
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 protected $xcAttr = array('news');
15 public function process(DOMDocument $dom, $page, $manager) {
16 // Remove the semaphore xc:news attribute, and assign to $page
17 // appropriately
18 if ($this->confiscateAttr($dom->documentElement, 'xc', 'news')) {
19 $page->attr['news'] = true;
22 $container = $this->query("//html:div[@news:source]")->item(0);
23 if (!$container) return;
25 // Grab variables
26 $source = $this->confiscateAttr($container, $this->ns, 'source');
27 $source = $page->normalizePath($source);
28 $limit = $this->confiscateAttr($container, $this->ns, 'limit');
29 if (!$limit) $limit = 5;
30 $header = $this->confiscateAttr($container, $this->ns, 'header');
31 if (!$header) $header = 'h2';
33 // Recursively glob for source files
34 // :TODO: add DI for this
35 $fs = new FSTools();
36 $result = $fs->globr($source, '*.xhtml');
37 rsort($result);
39 // Setup manager
40 $xc = XHTMLCompiler::getInstance();
41 $manager = $xc->getFilterManager();
43 for ($i = $d = 0, $c = count($result); $i < $c && $d < $limit; $i++) {
44 $entry = $result[$i];
46 // :TODO: Add support for nested directories. Also need to modify
47 // generateId when that happens.
48 $base = basename($entry);
49 if (strlen($base) < 4 || !ctype_digit(substr($base, 0, 4))) {
50 continue;
53 $entryFile = new XHTMLCompiler_Page($entry);
54 // This DOM has IDs setup, but no other processing setup.
55 $subdom = $manager->parse($entryFile->getSource(), $entryFile);
57 $entryNode = $dom->createElement('div');
58 $entryNode->setAttribute('class', 'item');
60 // Generate ID for this entry. The format is
61 // year-month-date-lc-title-with-dashes
62 $entryNode->setAttribute('id', $this->generateId($entryFile->getPathStem()));
64 // Grab the title
65 $node = $subdom->getElementsByTagName('h1')->item(0);
66 $h1 = $dom->importNode($node, true);
67 $hx = $dom->createElement($header);
68 $hx->setAttribute('class', 'title');
69 foreach ($h1->childNodes as $h1node) $hx->appendChild($h1node);
70 $entryNode->appendChild($hx);
72 // Possible place for factoring out. Also, we can also add
73 // a timezone to be more like ours.
74 $dateTime = $entryFile->getCreatedTime();
75 if ($dateTime) {
76 $time = $dateTime->format('g:i A e');
77 $date = $dateTime->format('l, F j, Y');
78 $dateNode = $dom->createElement('div');
79 $dateNode->setAttribute('class', 'date');
80 $abbrNode = $dom->createElement('abbr');
81 $abbrNode->setAttribute('class', 'at' . $dateTime->format('U'));
82 $dateText = $dom->createTextNode("Posted $time on $date");
83 $abbrNode->appendChild($dateText);
84 $dateNode->appendChild($abbrNode);
85 $entryNode->appendChild($dateNode);
88 // Grab the content
89 // :WARNING: This code apparently leaves behind an
90 // xmlns statement, although we're not quite sure why.
91 $node = $subdom->getElementById('content');
92 $node = $dom->importNode($node, true);
93 $this->confiscateAttr($node, 'id');
94 $node->setAttribute('class', 'body');
95 $entryNode->appendChild($node);
97 // Make permalink
98 $permalink = $dom->createElement('div');
99 $permalink->setAttribute('class', 'permalink');
100 $a = $dom->createElement('a');
101 $a->setAttribute('href', $entryFile->getAbsolutePath());
102 $a->appendChild($dom->createTextNode('Permalink'));
103 $permalink->appendChild($a);
104 $entryNode->appendChild($permalink);
106 $container->appendChild($entryNode);
108 // increment one successful
109 $d++;
114 * Generates an ID based on the filename of a blog entry
116 public function generateId($entry) {
117 $parts = array_slice(explode('/', $entry), -2);
118 // A very specific format: year/monthday-entry-name, like 2008/0131-foobar
119 // Arbitrary folders before that is ok.
120 $entry = 'entry-' . $parts[0] .
121 '-' . substr($parts[1], 0, 2) .
122 '-' . substr($parts[1], 2, 2) .
123 '-' . substr($parts[1], 5);
124 return $entry;