Have filters add self as dependencies, rm whitespace.
[xhtml-compiler.git] / XHTMLCompiler / DOMFilter / Metadata.php
blobf7855ad177ab0141d309bd64930a888739742e87
1 <?php
3 /**
4 * Extracts metadata from XHTMLCompiler_Page (which comes the version
5 * control system) and inserts it in the HTML page.
6 */
7 class XHTMLCompiler_DOMFilter_Metadata extends XHTMLCompiler_DOMFilter {
9 protected $name = 'Metadata';
11 public function process(DOMDocument $dom, $page, $manager) {
13 $head = $dom->getElementsByTagName('head')->item(0);
14 if (!$head) return;
15 $manager->addDependency(__FILE__);
17 $date = $page->getCreatedTime();
18 // check if the meta element already exists
19 $meta = $dom->createElement('meta');
20 $append = true;
21 foreach ($head->getElementsByTagName('meta') as $node) {
22 if ($node->getAttribute('name') == 'Date') {
23 // it exists: use this date as the "real" date.
24 $meta = $node;
25 if ($t = $meta->getAttribute('content')) {
26 // this code is slightly redundant since getCreatedTime()
27 // will have already accounted for this. Still, can't hurt.
28 $date = new DateTime($t);
30 $append = false;
31 break;
34 // only write if we have a valid date
35 if ($date) {
36 $meta->setAttribute('name', 'Date');
37 $meta->setAttribute('content', $date->format('c'));
38 if ($append) $head->appendChild($meta);