Improve coverage in News smoketest.
[xhtml-compiler.git] / XHTMLCompiler / DOMFilter / News.php
blobf07c807b19d982147d26c2caae6765f42cfa6ab9
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 $containers = $this->query("//html:div[@news:source]");
23 foreach ($containers as $container) {
24 $this->generateNews($container, $dom, $page, $manager);
29 /**
30 * Generates a particular news container.
32 public function generateNews($container, DOMDocument $dom, $page, $manager) {
34 // Grab variables
35 $source = $this->confiscateAttr($container, $this->ns, 'source');
36 $source = $page->normalizePath($source);
37 $limit = $this->confiscateAttr($container, $this->ns, 'limit');
38 if (!$limit) $limit = 5;
39 $header = $this->confiscateAttr($container, $this->ns, 'header');
40 if (!$header) $header = 'h2';
42 // Recursively glob for source files
43 // :TODO: add DI for this
44 $fs = new FSTools();
45 $result = $fs->globr($source, '*.xhtml');
46 // This doesn't work if there is more than one entry in a day
47 rsort($result);
49 // Setup manager
50 $xc = XHTMLCompiler::getInstance();
51 $manager = $xc->getFilterManager();
53 for ($i = $d = 0, $c = count($result); $i < $c && $d != $limit; $i++) {
54 $entry = $result[$i];
56 // :TODO: Add support for nested directories. Also need to modify
57 // generateId when that happens.
58 $base = basename($entry);
59 if (strlen($base) < 4 || !ctype_digit(substr($base, 0, 4))) {
60 continue;
63 $entryFile = new XHTMLCompiler_Page($entry);
64 // This DOM has IDs setup, but no other processing setup.
65 $subdom = $manager->parse($entryFile->getSource(), $entryFile);
67 $entryNode = $dom->createElement('div');
68 $entryNode->setAttribute('class', 'item');
70 // Generate ID for this entry. The format is
71 // year-month-date-lc-title-with-dashes
72 $entryNode->setAttribute('id', $this->generateId($entryFile->getPathStem()));
74 // Grab the title
75 $node = $subdom->getElementsByTagName('h1')->item(0);
76 $h1 = $dom->importNode($node, true);
77 $hx = $dom->createElement($header);
78 $hx->setAttribute('class', 'title');
79 foreach ($h1->childNodes as $h1node) $hx->appendChild($h1node);
80 $entryNode->appendChild($hx);
82 // Possible place for factoring out. Also, we can also add
83 // a timezone to be more like ours.
84 $dateTime = $entryFile->getCreatedTime();
85 if ($dateTime) {
86 $time = $dateTime->format('g:i A e');
87 $date = $dateTime->format('l, F j, Y');
88 $dateNode = $dom->createElement('div');
89 $dateNode->setAttribute('class', 'date');
90 $abbrNode = $dom->createElement('abbr');
91 $abbrNode->setAttribute('class', 'at' . $dateTime->format('U'));
92 $dateText = $dom->createTextNode("Posted $time on $date");
93 $abbrNode->appendChild($dateText);
94 $dateNode->appendChild($abbrNode);
95 $entryNode->appendChild($dateNode);
98 // Grab the content
99 // :WARNING: This code apparently leaves behind an
100 // xmlns statement, although we're not quite sure why.
101 $node = $subdom->getElementById('content');
102 $node = $dom->importNode($node, true);
103 $this->confiscateAttr($node, 'id');
104 $node->setAttribute('class', 'body');
105 $entryNode->appendChild($node);
107 // Make permalink
108 $permalink = $dom->createElement('div');
109 $permalink->setAttribute('class', 'permalink');
110 $a = $dom->createElement('a');
111 $a->setAttribute('href', $entryFile->getAbsolutePath());
112 $a->appendChild($dom->createTextNode('Permalink'));
113 $permalink->appendChild($a);
114 $entryNode->appendChild($permalink);
116 $container->appendChild($entryNode);
118 $manager->addDependency($entry);
120 // increment one successful
121 $d++;
126 * Generates an ID based on the filename of a blog entry
128 public function generateId($entry) {
129 $parts = array_slice(explode('/', $entry), -2);
130 // A very specific format: year/monthday-entry-name, like 2008/0131-foobar
131 // Arbitrary folders before that is ok.
132 $entry = 'entry-' . $parts[0] .
133 '-' . substr($parts[1], 0, 2) .
134 '-' . substr($parts[1], 2, 2) .
135 '-' . substr($parts[1], 5);
136 return $entry;