From db823d80fd425779519a955634baf073dc82e444 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Mon, 27 Apr 2009 17:34:26 -0400 Subject: [PATCH] Have filters add self as dependencies, rm whitespace. Signed-off-by: Edward Z. Yang --- TODO.txt | 1 - XHTMLCompiler/DOMFilter/AbsolutePath.php | 17 +++++---- XHTMLCompiler/DOMFilter/Acronymizer.php | 16 ++++---- XHTMLCompiler/DOMFilter/AutoStyle.php | 18 +++++---- XHTMLCompiler/DOMFilter/ConfigLinker.php | 11 +++--- .../DOMFilter/GenerateTableOfContents.php | 29 +++++++------- XHTMLCompiler/DOMFilter/IEConditionalComments.php | 13 ++++--- XHTMLCompiler/DOMFilter/MarkLeadParagraphs.php | 9 +++-- XHTMLCompiler/DOMFilter/Metadata.php | 13 ++++--- XHTMLCompiler/DOMFilter/News.php | 44 ++++++++++++---------- XHTMLCompiler/DOMFilter/NewsDate.php | 1 + XHTMLCompiler/DOMFilter/NewsLinker.php | 13 ++++--- XHTMLCompiler/DOMFilter/Quoter.php | 24 +++++++----- XHTMLCompiler/DOMFilter/RSSFromGit.php | 29 +++++++------- XHTMLCompiler/DOMFilter/RSSGenerator.php | 39 +++++++++---------- 15 files changed, 148 insertions(+), 129 deletions(-) diff --git a/TODO.txt b/TODO.txt index 4df8161..567f994 100644 --- a/TODO.txt +++ b/TODO.txt @@ -23,7 +23,6 @@ Maintenance - Single-file update using update.php - Incremental HTML page updates - Enable page retrieval via portable library (include it, call function) -- Have filters also add themselves as dependencies. Sugar - Prettier dashboard using a Circular Menu diff --git a/XHTMLCompiler/DOMFilter/AbsolutePath.php b/XHTMLCompiler/DOMFilter/AbsolutePath.php index 3082afa..c247e3b 100644 --- a/XHTMLCompiler/DOMFilter/AbsolutePath.php +++ b/XHTMLCompiler/DOMFilter/AbsolutePath.php @@ -8,21 +8,22 @@ */ class XHTMLCompiler_DOMFilter_AbsolutePath extends XHTMLCompiler_DOMFilter { - + protected $name = 'AbsolutePath'; protected $xcAttr = array('absolute'); - + public function process(DOMDocument $dom, $page, $manager) { - + // this is the absolute path implementation of the functionality $xc = XHTMLCompiler::getInstance(); $prefix = $xc->getConf('web_path') . '/'; - + // this is the relative path implementation of the functionality //$prefix = str_repeat('../', $page->getDepth()); - + $nodes = $this->query("//*[@xc:absolute]"); - + if ($nodes) $manager->addDependency(__FILE__); + foreach ($nodes as $node) { $attribute = $this->confiscateAttr($node, $this->ns, 'absolute'); if ($attribute) { @@ -35,7 +36,7 @@ class XHTMLCompiler_DOMFilter_AbsolutePath extends XHTMLCompiler_DOMFilter $node->setAttribute($attribute, $prefix . $uri); } } - + } - + } diff --git a/XHTMLCompiler/DOMFilter/Acronymizer.php b/XHTMLCompiler/DOMFilter/Acronymizer.php index 6858c9b..79c2f44 100644 --- a/XHTMLCompiler/DOMFilter/Acronymizer.php +++ b/XHTMLCompiler/DOMFilter/Acronymizer.php @@ -7,9 +7,9 @@ */ class XHTMLCompiler_DOMFilter_Acronymizer extends XHTMLCompiler_DOMFilter { - + protected $name = 'Acronymizer'; - + /** * Associative array of recognized abbreviations/initialisms * @note To be used when each letter should be spelled out, such @@ -47,7 +47,7 @@ class XHTMLCompiler_DOMFilter_Acronymizer extends XHTMLCompiler_DOMFilter 'FTP' => 'File Transfer Protocol', 'URI' => 'Uniform Resource Identifier', ); - + /** * Array of recognized acronyms. * @note Acronyms can be spoken literally, if in doubt, make it @@ -66,18 +66,18 @@ class XHTMLCompiler_DOMFilter_Acronymizer extends XHTMLCompiler_DOMFilter 'WYSIWYG' => 'What You See Is What You Get', 'WYSIWYM' => 'What You See Is What You Mean', ); - + public function process(DOMDocument $dom, $page, $manager) { $nodes = $this->query("//html:acronym[not(@title)]"); foreach ($nodes as $node) $this->addAdvisoryTitle($node, $this->acronyms); - + $nodes = $this->query("//html:abbr[not(@title)]"); foreach ($nodes as $node) $this->addAdvisoryTitle($node, $this->abbreviations); - + // add self as dependency; when acronym lists change, so does the page $manager->addDependency(__FILE__); } - + protected function addAdvisoryTitle($node, $lookup) { $key = $node->textContent; if (!isset($lookup[$key])) { @@ -88,5 +88,5 @@ class XHTMLCompiler_DOMFilter_Acronymizer extends XHTMLCompiler_DOMFilter } $node->setAttribute('title', $lookup[$key]); } - + } diff --git a/XHTMLCompiler/DOMFilter/AutoStyle.php b/XHTMLCompiler/DOMFilter/AutoStyle.php index 659a769..5fce17d 100644 --- a/XHTMLCompiler/DOMFilter/AutoStyle.php +++ b/XHTMLCompiler/DOMFilter/AutoStyle.php @@ -8,14 +8,16 @@ */ class XHTMLCompiler_DOMFilter_AutoStyle extends XHTMLCompiler_DOMFilter { - + protected $name = 'AutoStyle'; - + public function process(DOMDocument $dom, $page, $manager) { - + $head = $this->query("//html:head")->item(0); $links = $this->query('link', $head); - + + $manager->addDependency(__FILE__); + $preloaded = array(); $prefix = $page->getDirSName(); foreach ($links as $link) { @@ -29,11 +31,11 @@ class XHTMLCompiler_DOMFilter_AutoStyle extends XHTMLCompiler_DOMFilter } $preloaded[$prefix . $path] = true; } - + $dir = $page->getDir(); $css_files = $dir->scanFlat('.css'); $base = $page->getPathStem(); - + foreach ($css_files as $file) { // determine whether or not it should be included // TODO: extend this algorithm to allow different media @@ -51,7 +53,7 @@ class XHTMLCompiler_DOMFilter_AutoStyle extends XHTMLCompiler_DOMFilter $head->appendChild($link); } } - + } - + } diff --git a/XHTMLCompiler/DOMFilter/ConfigLinker.php b/XHTMLCompiler/DOMFilter/ConfigLinker.php index ecc8e15..34c0165 100644 --- a/XHTMLCompiler/DOMFilter/ConfigLinker.php +++ b/XHTMLCompiler/DOMFilter/ConfigLinker.php @@ -8,16 +8,17 @@ */ class XHTMLCompiler_DOMFilter_ConfigLinker extends XHTMLCompiler_DOMFilter { - + protected $name = 'ConfigLinker'; protected $xcAttr = array('configdoc'); - + public function process(DOMDocument $dom, $page, $manager) { - + // this is the absolute path implementation of the functionality $xc = XHTMLCompiler::getInstance(); $configdoc = $xc->getConf('configdoc'); $nodes = $this->query("//html:a[@xc:configdoc]"); + if ($nodes) $manager->addDependency(__FILE__); foreach ($nodes as $node) { $directive = $this->confiscateAttr($node, $this->ns, 'configdoc'); if (!$configdoc) continue; @@ -30,7 +31,7 @@ class XHTMLCompiler_DOMFilter_ConfigLinker extends XHTMLCompiler_DOMFilter } } } - + } - + } diff --git a/XHTMLCompiler/DOMFilter/GenerateTableOfContents.php b/XHTMLCompiler/DOMFilter/GenerateTableOfContents.php index 5817215..457d088 100644 --- a/XHTMLCompiler/DOMFilter/GenerateTableOfContents.php +++ b/XHTMLCompiler/DOMFilter/GenerateTableOfContents.php @@ -6,30 +6,31 @@ */ class XHTMLCompiler_DOMFilter_GenerateTableOfContents extends XHTMLCompiler_DOMFilter { - + protected $name = 'GenerateTableOfContents'; - + public function process(DOMDocument $dom, $page, $manager) { - + // test for ToC container, if not present don't bother // currently, only id="toc" is supported, which means there can // only be one table of contents per page $container = $this->query("//html:div[@id='toc']")->item(0); if (!$container) return; - + $manager->addDependency(__FILE__); + // grab all headings h2 and down from the document $headings = array('h2', 'h3', 'h4', 'h5', 'h6'); foreach ($headings as $k => $v) $headings[$k] = "self::html:$v"; $query_headings = implode(' or ', $headings); $query = "//*[$query_headings]"; // looks like "//*[self::html:h2 or ...]" $headings = $this->query($query); - + // setup the table of contents element $toc = $dom->createElement('ul'); $toc->setAttribute('class', 'toc-base'); $container->appendChild($dom->createElement('h2', 'Table of Contents')); $container->appendChild($toc); - + // iterate through headings and build the table of contents $current_level = 2; $parents = array(false, $toc); @@ -38,7 +39,7 @@ class XHTMLCompiler_DOMFilter_GenerateTableOfContents extends XHTMLCompiler_DOMF foreach ($headings as $node) { $level = (int) $node->tagName[1]; $name = $node->textContent; // no support for formatting - + while ($level > $current_level) { if (!$parents[$current_level-1]->lastChild) { $parents[$current_level-1]->appendChild( @@ -51,15 +52,15 @@ class XHTMLCompiler_DOMFilter_GenerateTableOfContents extends XHTMLCompiler_DOMF $current_level++; $indexes[$current_level - 2] = 0; } - + while ($level < $current_level) { unset($indexes[$current_level - 2]); $current_level--; } - + $indexes[$current_level - 2]++; - - + + $line = $dom->createElement('li'); $label = $dom->createElement('span', implode('.', $indexes) . '.'); $label->setAttribute('class', 'toc-label'); @@ -67,7 +68,7 @@ class XHTMLCompiler_DOMFilter_GenerateTableOfContents extends XHTMLCompiler_DOMF $link = $dom->createElement('a', htmlspecialchars($name)); $line->appendChild($link); $parents[$current_level-1]->appendChild($line); - + // setup the anchors $header_id = $node->getAttribute('id'); if (!$header_id) { @@ -80,8 +81,8 @@ class XHTMLCompiler_DOMFilter_GenerateTableOfContents extends XHTMLCompiler_DOMF $i++; } $link->setAttribute('href', '#' . $header_id); - + } } - + } diff --git a/XHTMLCompiler/DOMFilter/IEConditionalComments.php b/XHTMLCompiler/DOMFilter/IEConditionalComments.php index 1309537..83c2dbe 100644 --- a/XHTMLCompiler/DOMFilter/IEConditionalComments.php +++ b/XHTMLCompiler/DOMFilter/IEConditionalComments.php @@ -7,14 +7,15 @@ */ class XHTMLCompiler_DOMFilter_IEConditionalComments extends XHTMLCompiler_DOMFilter { - + protected $name = 'IEConditionalComments'; protected $xcAttr = array('ie-condition'); - + public function process(DOMDocument $dom, $page, $manager) { - + $nodes = $this->query( "//*[@xc:ie-condition]" ); - + + if ($nodes) $manager->addDependency(__FILE__); foreach ($nodes as $node) { $condition = $this->confiscateAttr($node, $this->ns, 'ie-condition'); if ($condition) { @@ -25,7 +26,7 @@ class XHTMLCompiler_DOMFilter_IEConditionalComments extends XHTMLCompiler_DOMFil $parent->replaceChild($dom->createComment($comment), $node); } } - + } - + } diff --git a/XHTMLCompiler/DOMFilter/MarkLeadParagraphs.php b/XHTMLCompiler/DOMFilter/MarkLeadParagraphs.php index 16b0343..bb34323 100644 --- a/XHTMLCompiler/DOMFilter/MarkLeadParagraphs.php +++ b/XHTMLCompiler/DOMFilter/MarkLeadParagraphs.php @@ -5,21 +5,22 @@ */ class XHTMLCompiler_DOMFilter_MarkLeadParagraphs extends XHTMLCompiler_DOMFilter { - + protected $name = 'MarkLeadParagraphs'; protected $className = 'lead'; - + public function __construct($class = false, $name = false) { parent::__construct($name); if ($class) $this->className = $class; } - + public function process(DOMDocument $dom, $page, $manager) { // possibly add some attribute which disables this behavior $nodes = $this->query("//html:p[local-name(preceding-sibling::*[1])!='p']"); + if ($nodes) $manager->addDependency(__FILE__); foreach ($nodes as $node) { $node->setAttribute('class', $this->className); } } - + } diff --git a/XHTMLCompiler/DOMFilter/Metadata.php b/XHTMLCompiler/DOMFilter/Metadata.php index 8b955b4..f7855ad 100644 --- a/XHTMLCompiler/DOMFilter/Metadata.php +++ b/XHTMLCompiler/DOMFilter/Metadata.php @@ -5,14 +5,15 @@ * control system) and inserts it in the HTML page. */ class XHTMLCompiler_DOMFilter_Metadata extends XHTMLCompiler_DOMFilter { - + protected $name = 'Metadata'; - + public function process(DOMDocument $dom, $page, $manager) { - + $head = $dom->getElementsByTagName('head')->item(0); if (!$head) return; - + $manager->addDependency(__FILE__); + $date = $page->getCreatedTime(); // check if the meta element already exists $meta = $dom->createElement('meta'); @@ -36,7 +37,7 @@ class XHTMLCompiler_DOMFilter_Metadata extends XHTMLCompiler_DOMFilter { $meta->setAttribute('content', $date->format('c')); if ($append) $head->appendChild($meta); } - + } - + } diff --git a/XHTMLCompiler/DOMFilter/News.php b/XHTMLCompiler/DOMFilter/News.php index 45489fd..c2bf01e 100644 --- a/XHTMLCompiler/DOMFilter/News.php +++ b/XHTMLCompiler/DOMFilter/News.php @@ -15,22 +15,26 @@ class XHTMLCompiler_DOMFilter_News extends XHTMLCompiler_DOMFilter_NewsBase public function process(DOMDocument $dom, $page, $manager) { // Remove the semaphore xc:news attribute, and assign to $page // appropriately + $dep = false; if ($this->confiscateAttr($dom->documentElement, 'xc', 'news')) { + $dep = true; $page->attr['news'] = true; } - + $containers = $this->query("//html:div[@news:source]"); foreach ($containers as $container) { + $dep = true; $this->generateNews($container, $dom, $page, $manager); } - + if ($dep) $manager->addDependency(__FILE__); + } - + /** * Generates a particular news container. */ public function generateNews($container, DOMDocument $dom, $page, $manager) { - + // Grab variables $source = $this->confiscateAttr($container, $this->ns, 'source'); $source = $page->normalizePath($source); @@ -41,39 +45,39 @@ class XHTMLCompiler_DOMFilter_News extends XHTMLCompiler_DOMFilter_NewsBase $idPrefix = $this->confiscateAttr($container, $this->ns, 'idPrefix'); if (!$idPrefix) $idPrefix = ''; $increase = $header[1] - 1; // amount increase sub-headings - + // Recursively glob for source files // :TODO: add DI for this $fs = new FSTools(); $result = $fs->globr($source, '*.xhtml'); // This doesn't work if there is more than one entry in a day rsort($result); - + // Setup manager $xc = XHTMLCompiler::getInstance(); $manager = $xc->getFilterManager(); - + for ($i = $d = 0, $c = count($result); $i < $c && $d != $limit; $i++) { $entry = $result[$i]; - + // :TODO: Add support for nested directories. Also need to modify // generateId when that happens. $base = basename($entry); if (strlen($base) < 4 || !ctype_digit(substr($base, 0, 4))) { continue; } - + $entryFile = new XHTMLCompiler_Page($entry); // This DOM has IDs setup, but no other processing setup. $subdom = $manager->parse($entryFile->getSource(), $entryFile); - + $entryNode = $dom->createElement('div'); $entryNode->setAttribute('class', 'item'); - + // Generate ID for this entry. The format is // year-month-date-lc-title-with-dashes $entryNode->setAttribute('id', $idPrefix . $this->generateId($entryFile->getPathStem())); - + // Grab the title $node = $subdom->getElementsByTagName('h1')->item(0); $h1 = $dom->importNode($node, true); @@ -81,14 +85,14 @@ class XHTMLCompiler_DOMFilter_News extends XHTMLCompiler_DOMFilter_NewsBase $hx->setAttribute('class', 'title'); foreach ($h1->childNodes as $h1node) $hx->appendChild($h1node); $entryNode->appendChild($hx); - + // Possible place for factoring out. Also, we can also add // a timezone to be more like ours. $dateTime = $entryFile->getCreatedTime(); if ($dateTime) { $entryNode->appendChild($this->makeDateNode($dom, $dateTime)); } - + // Grab the content // :WARNING: This code apparently leaves behind an // xmlns statement, although we're not quite sure why. @@ -102,7 +106,7 @@ class XHTMLCompiler_DOMFilter_News extends XHTMLCompiler_DOMFilter_NewsBase $this->confiscateAttr($node, 'id'); $node->setAttribute('class', 'body'); $entryNode->appendChild($node); - + // Make permalink $permalink = $dom->createElement('div'); $permalink->setAttribute('class', 'permalink'); @@ -111,16 +115,16 @@ class XHTMLCompiler_DOMFilter_News extends XHTMLCompiler_DOMFilter_NewsBase $a->appendChild($dom->createTextNode($permText)); $permalink->appendChild($a); $entryNode->appendChild($permalink); - + $container->appendChild($entryNode); - + $manager->addDependency($entry); - + // increment one successful $d++; } } - + /** * Generates an ID based on the filename of a blog entry */ @@ -134,5 +138,5 @@ class XHTMLCompiler_DOMFilter_News extends XHTMLCompiler_DOMFilter_NewsBase '-' . substr($parts[1], 5); return $entry; } - + } diff --git a/XHTMLCompiler/DOMFilter/NewsDate.php b/XHTMLCompiler/DOMFilter/NewsDate.php index aba331f..c4d3739 100644 --- a/XHTMLCompiler/DOMFilter/NewsDate.php +++ b/XHTMLCompiler/DOMFilter/NewsDate.php @@ -11,6 +11,7 @@ class XHTMLCompiler_DOMFilter_NewsDate extends XHTMLCompiler_DOMFilter_NewsBase if (!isset($page->attr['news'])) return; $dateTime = $page->getCreatedTime(); if (!$dateTime) return; + $manager->addDependency(__FILE__); $content = $dom->getElementById('content'); $body = $content->parentNode; $date = $this->makeDateNode($dom, $dateTime); diff --git a/XHTMLCompiler/DOMFilter/NewsLinker.php b/XHTMLCompiler/DOMFilter/NewsLinker.php index 5ccd4f6..96a26c1 100644 --- a/XHTMLCompiler/DOMFilter/NewsLinker.php +++ b/XHTMLCompiler/DOMFilter/NewsLinker.php @@ -9,7 +9,8 @@ class XHTMLCompiler_DOMFilter_NewsLinker extends XHTMLCompiler_DOMFilter protected $name = 'NewsLinker'; public function process(DOMDocument $dom, $page, $manager) { if (!isset($page->attr['news'])) return; - + $manager->addDependency(__FILE__); + // calculate forward and backward links $fs = new FSTools(); // we assume that one directory up is the years directory. An alternate @@ -34,12 +35,12 @@ class XHTMLCompiler_DOMFilter_NewsLinker extends XHTMLCompiler_DOMFilter break; } } - + $content = $dom->getElementById('content'); $body = $content->parentNode; $nav = $dom->createElement('div'); $nav->setAttribute('id', 'news-navigation'); - + if ($prev) { $prevPage = new XHTMLCompiler_Page($prev); if (!$page->isCacheExistent()) { @@ -53,13 +54,13 @@ class XHTMLCompiler_DOMFilter_NewsLinker extends XHTMLCompiler_DOMFilter $prevDiv->appendChild($a); $nav->appendchild($prevDiv); } - + $indexDiv = $dom->createElement('div'); $indexDiv->setAttribute('class', 'index'); $a = $dom->createElement('a', 'Index'); $indexDiv->appendChild($a); //$nav->appendChild($indexDiv); - + if ($next) { $nextPage = new XHTMLCompiler_Page($next); $nextDiv = $dom->createElement('div'); @@ -69,7 +70,7 @@ class XHTMLCompiler_DOMFilter_NewsLinker extends XHTMLCompiler_DOMFilter $nextDiv->appendChild($a); $nav->appendChild($nextDiv); } - + $body->appendChild($nav); } } diff --git a/XHTMLCompiler/DOMFilter/Quoter.php b/XHTMLCompiler/DOMFilter/Quoter.php index 4f246bc..052b4d5 100644 --- a/XHTMLCompiler/DOMFilter/Quoter.php +++ b/XHTMLCompiler/DOMFilter/Quoter.php @@ -5,15 +5,15 @@ */ class XHTMLCompiler_DOMFilter_Quoter extends XHTMLCompiler_DOMFilter { - + protected $name = 'Quoter'; - + protected $leftQuote = "\xE2\x80\x9C"; protected $rightQuote = "\xE2\x80\x9D"; - + protected $leftNestedQuote = "\xE2\x80\x98"; protected $rightNestedQuote = "\xE2\x80\x99"; - + public function __construct( $left = null, $right = null, $left_nest = null, $right_nest = null, @@ -25,23 +25,27 @@ class XHTMLCompiler_DOMFilter_Quoter extends XHTMLCompiler_DOMFilter if ($left_nest) $this->leftNestedQuote = $left_nest; if ($right_nest) $this->rightNestedQuote = $right_nest; } - + public function process(DOMDocument $dom, $page, $manager) { - + + $dep = false; // first handle single-quotes $nodes = $this->query("//html:q//html:q"); foreach ($nodes as $node) { + $dep = true; $this->quotify($dom, $node, $this->leftNestedQuote, $this->rightNestedQuote); } - + // now handle double-quotes $nodes = $this->query("//html:q"); foreach ($nodes as $node) { + $dep = true; $this->quotify($dom, $node, $this->leftQuote, $this->rightQuote); } - + if ($dep) $manager->addDependency(__FILE__); + } - + /** * Takes a node and unpacks its contents, surrounding it with * text instead. @@ -58,5 +62,5 @@ class XHTMLCompiler_DOMFilter_Quoter extends XHTMLCompiler_DOMFilter $parent->insertBefore($dom->createTextNode($right), $node); $parent->removeChild($node); } - + } diff --git a/XHTMLCompiler/DOMFilter/RSSFromGit.php b/XHTMLCompiler/DOMFilter/RSSFromGit.php index 227e56a..2640e98 100644 --- a/XHTMLCompiler/DOMFilter/RSSFromGit.php +++ b/XHTMLCompiler/DOMFilter/RSSFromGit.php @@ -1,7 +1,7 @@ and other places */ protected $title = 'Log for %s'; protected $xcAttr = array('rss-from-git'); - + public function process(DOMDocument $dom, $page, $manager) { $xc = XHTMLCompiler::getInstance(); // implement gitweb/repo.or.cz support - + $logs = $page->getLog(); - + // see if we need to make the link ourself $trigger = $this->confiscateAttr($dom->documentElement, $this->ns, 'rss-from-git'); if ($trigger == 'yes') { @@ -39,29 +39,30 @@ class XHTMLCompiler_DOMFilter_RSSFromGit extends XHTMLCompiler_DOMFilter // only one allowed $link = $this->query('//html:link[@xc:rss-from-svn]')->item(0); if (!$link) return; // nothing to do - + $trigger = $this->confiscateAttr($link, $this->ns, 'rss-from-svn'); if ($trigger != 'yes') return; if (!$logs) return; } - + $manager->addDependency(__FILE__); + $path = $page->getWebPath(); - + $rss = new XHTMLCompiler_RSSFeed( $link->getAttribute('title'), $path, str_replace('%s', $page->getCachePath(), $this->description), $dom->documentElement->getAttribute('xml:lang') ); - + for ($i = 0; $i < $this->limit && isset($logs[$i]); $i++) { $commit = $logs[$i]; - + // :TODO: link to gitweb commitdiff $item_link = $path . '#git_' . $commit->id; - + // generate short message (first line) for title\ - + $rss->addItem( $item_link, htmlspecialchars($commit->message), @@ -70,13 +71,13 @@ class XHTMLCompiler_DOMFilter_RSSFromGit extends XHTMLCompiler_DOMFilter htmlspecialchars($commit->message) ); } - + $rss->save( $page->normalizePath( $link->getAttribute('href') ) ); - - + + } } diff --git a/XHTMLCompiler/DOMFilter/RSSGenerator.php b/XHTMLCompiler/DOMFilter/RSSGenerator.php index 422747f..0d99c99 100644 --- a/XHTMLCompiler/DOMFilter/RSSGenerator.php +++ b/XHTMLCompiler/DOMFilter/RSSGenerator.php @@ -6,43 +6,44 @@ */ class XHTMLCompiler_DOMFilter_RSSGenerator extends XHTMLCompiler_DOMFilter { - + protected $name = 'RSSGenerator'; protected $prefix = 'rss'; - + // new namespace defines the following attributes: // for - IDREF to an element where we are to get the RSS info from - + public function process(DOMDocument $dom, $page, $manager) { - + // attempt to find declarations of the namespace $nodes = $this->query( "//attribute::*[namespace-uri() = '".$this->ns."']" ); if (!$nodes->length) return; - + $manager->addDependency(__FILE__); + // grab the document's links to RSS feeds // we require that the link have a href, a title and a type // as well as an rss:for attribute specifying where to grab data $links = $this->query('//html:link[@rss:for]'); - + foreach ($links as $link) { $this->generateRSS($dom, $link, $page); } - + } - + /** * Generates the RSS feed for a specific link in a document * @param $link DOMElement we're generating feed for * @param $page Page we're generating for */ protected function generateRSS($dom, $link, $page) { - + // retrieve web-path of the page $path = $page->getWebPath(); $xc = XHTMLCompiler::getInstance(); - + // generate RSS template $rss = new XHTMLCompiler_RSSFeed( $link->getAttribute('title'), @@ -50,23 +51,23 @@ class XHTMLCompiler_DOMFilter_RSSGenerator extends XHTMLCompiler_DOMFilter $this->confiscateAttr($link, $this->ns, 'description'), $lang = $dom->documentElement->getAttribute('xml:lang') ); - + // retrieve data source $id = $this->confiscateAttr($link, $this->ns, 'for'); $data_source = $dom->getElementById($id); - + // parse data source, add news items foreach ($data_source->childNodes as $src_item) { if (! $src_item instanceof DOMElement) continue; if ($src_item->getAttribute('class') !== 'item') continue; - + $title = $date = $body = ''; foreach ($src_item->childNodes as $element) { if (! $element instanceof DOMElement) continue; $var = $element->getAttribute('class'); if ($var == 'date') { if (($node = $element->childNodes->item(0)) instanceof DOMElement) { - // If we are using human readable dates but + // If we are using human readable dates but // non-parseable machine dates, an abbr // element should be inside the element with class="date" // with a class atNNN where NNN is the Unix timestamp @@ -82,7 +83,7 @@ class XHTMLCompiler_DOMFilter_RSSGenerator extends XHTMLCompiler_DOMFilter $$var = $element->textContent; } } - + if (isset($permalink)) { // assume that the permalink is an absolute path if ($permalink[0] !== '/') { @@ -100,16 +101,16 @@ class XHTMLCompiler_DOMFilter_RSSGenerator extends XHTMLCompiler_DOMFilter $article_link = $path . '#' . $item_id; } $rss->addItem($article_link, $title, $date, $body); - + } - + // save the feed $rss->save( $page->normalizePath( $link->getAttribute('href') ) ); - + } - + } -- 2.11.4.GIT