Add support for long news entries that display less than they actually
[xhtml-compiler.git] / XHTMLCompiler / DOMFilter / News.php
blob478a4cd8a7044494593c2fdd951644402ac5599d
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';
41 $increase = $header[1] - 1; // amount increase sub-headings
43 // Recursively glob for source files
44 // :TODO: add DI for this
45 $fs = new FSTools();
46 $result = $fs->globr($source, '*.xhtml');
47 // This doesn't work if there is more than one entry in a day
48 rsort($result);
50 // Setup manager
51 $xc = XHTMLCompiler::getInstance();
52 $manager = $xc->getFilterManager();
54 for ($i = $d = 0, $c = count($result); $i < $c && $d != $limit; $i++) {
55 $entry = $result[$i];
57 // :TODO: Add support for nested directories. Also need to modify
58 // generateId when that happens.
59 $base = basename($entry);
60 if (strlen($base) < 4 || !ctype_digit(substr($base, 0, 4))) {
61 continue;
64 $entryFile = new XHTMLCompiler_Page($entry);
65 // This DOM has IDs setup, but no other processing setup.
66 $subdom = $manager->parse($entryFile->getSource(), $entryFile);
68 $entryNode = $dom->createElement('div');
69 $entryNode->setAttribute('class', 'item');
71 // Generate ID for this entry. The format is
72 // year-month-date-lc-title-with-dashes
73 $entryNode->setAttribute('id', $this->generateId($entryFile->getPathStem()));
75 // Grab the title
76 $node = $subdom->getElementsByTagName('h1')->item(0);
77 $h1 = $dom->importNode($node, true);
78 $hx = $dom->createElement($header);
79 $hx->setAttribute('class', 'title');
80 foreach ($h1->childNodes as $h1node) $hx->appendChild($h1node);
81 $entryNode->appendChild($hx);
83 // Possible place for factoring out. Also, we can also add
84 // a timezone to be more like ours.
85 $dateTime = $entryFile->getCreatedTime();
86 if ($dateTime) {
87 $time = $dateTime->format('g:i A T');
88 $date = $dateTime->format('l, F j, Y');
89 $dateNode = $dom->createElement('div');
90 $dateNode->setAttribute('class', 'date');
91 $abbrNode = $dom->createElement('abbr');
92 $abbrNode->setAttribute('class', 'at' . $dateTime->format('U'));
93 $dateText = $dom->createTextNode("Posted $time on $date");
94 $abbrNode->appendChild($dateText);
95 $dateNode->appendChild($abbrNode);
96 $entryNode->appendChild($dateNode);
99 // Grab the content
100 // :WARNING: This code apparently leaves behind an
101 // xmlns statement, although we're not quite sure why.
102 $node = $subdom->getElementById('short-content');
103 $permText = 'Read more...';
104 if (!$node) {
105 $node = $subdom->getElementById('content');
106 $permText = 'Permalink';
108 $node = $dom->importNode($node, true);
109 $this->confiscateAttr($node, 'id');
110 $node->setAttribute('class', 'body');
111 $entryNode->appendChild($node);
113 // Make permalink
114 $permalink = $dom->createElement('div');
115 $permalink->setAttribute('class', 'permalink');
116 $a = $dom->createElement('a');
117 $a->setAttribute('href', $entryFile->getAbsolutePath());
118 $a->appendChild($dom->createTextNode($permText));
119 $permalink->appendChild($a);
120 $entryNode->appendChild($permalink);
122 $container->appendChild($entryNode);
124 $manager->addDependency($entry);
126 // increment one successful
127 $d++;
132 * Generates an ID based on the filename of a blog entry
134 public function generateId($entry) {
135 $parts = array_slice(explode('/', $entry), -2);
136 // A very specific format: year/monthday-entry-name, like 2008/0131-foobar
137 // Arbitrary folders before that is ok.
138 $entry = 'entry-' . $parts[0] .
139 '-' . substr($parts[1], 0, 2) .
140 '-' . substr($parts[1], 2, 2) .
141 '-' . substr($parts[1], 5);
142 return $entry;