From 22b67bf2bd34cd787cb2276c498198e2b3a87b26 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sun, 27 Jul 2008 15:03:02 -0600 Subject: [PATCH] Implement Newslinker, also fix some bugs. * Added xc:news property, for indicating if a page is a news item. * Fixed bug in Page getAbsolutePath Signed-off-by: Edward Z. Yang --- XHTMLCompiler/DOMFilter/News.php | 7 ++++ XHTMLCompiler/DOMFilter/NewsLinker.php | 69 ++++++++++++++++++++++++++++++++++ XHTMLCompiler/Page.php | 6 ++- config.filters.php | 1 + 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 XHTMLCompiler/DOMFilter/NewsLinker.php diff --git a/XHTMLCompiler/DOMFilter/News.php b/XHTMLCompiler/DOMFilter/News.php index 311c1e9..d6aaa0c 100644 --- a/XHTMLCompiler/DOMFilter/News.php +++ b/XHTMLCompiler/DOMFilter/News.php @@ -11,7 +11,14 @@ class XHTMLCompiler_DOMFilter_News extends XHTMLCompiler_DOMFilter { protected $name = 'News'; protected $prefix = 'news'; + protected $xcAttr = array('news'); public function process(DOMDocument $dom, $page, $manager) { + // Remove the semaphore xc:news attribute, and assign to $page + // appropriately + if ($this->confiscateAttr($dom->documentElement, 'xc', 'news')) { + $page->attr['news'] = true; + } + $container = $this->query("//html:div[@news:source]")->item(0); if (!$container) return; diff --git a/XHTMLCompiler/DOMFilter/NewsLinker.php b/XHTMLCompiler/DOMFilter/NewsLinker.php new file mode 100644 index 0000000..6113c16 --- /dev/null +++ b/XHTMLCompiler/DOMFilter/NewsLinker.php @@ -0,0 +1,69 @@ +attr['news'])) return; + + // calculate forward and backward links + $fs = new FSTools(); + // we assume that one directory up is the years directory. An alternate + // algorithm may be needed if we decide to allow further nesting. + // This code needs to be factored out + $result = $fs->globr(dirname($page->getDirname()), '*.xhtml'); + sort($result); + $prev = $next = null; + $found = false; + foreach ($result as $i => $entry) { + $base = basename($entry); + if (strlen($base) < 4 || !ctype_digit(substr($base, 0, 4))) { + continue; + } + if ($page->getSourcePath() == $entry) { + $found = true; + } elseif (!$found) { + $prev = $entry; + } else { + $next = $entry; + break; + } + } + + $body = $dom->getElementsByTagName('body')->item(0); + $nav = $dom->createElement('div'); + $nav->setAttribute('id', 'news-navigation'); + + if ($prev) { + $page = new XHTMLCompiler_Page($prev); + $prevDiv = $dom->createElement('div'); + $prevDiv->setAttribute('class', 'prev'); + $a = $dom->createElement('a', 'Previous'); + $a->setAttribute('href', $page->getAbsolutePath()); + $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) { + $page = new XHTMLCompiler_Page($next); + $nextDiv = $dom->createElement('div'); + $nextDiv->setAttribute('class', 'next'); + $a = $dom->createElement('a', 'Next'); + $a->setAttribute('href', $page->getAbsolutePath()); + $nextDiv->appendChild($a); + $nav->appendChild($nextDiv); + } + + $body->appendChild($nav); + } +} diff --git a/XHTMLCompiler/Page.php b/XHTMLCompiler/Page.php index ba16978..ac030a7 100644 --- a/XHTMLCompiler/Page.php +++ b/XHTMLCompiler/Page.php @@ -30,6 +30,9 @@ class XHTMLCompiler_Page /** Instance of XHTMLCompiler_Directory for all of the above files*/ protected $dir; + /** Array of attributes about this file. Currently used by News/NewsLinker */ + public $attr = array(); + /** * Constructs a page object, validates filename for correctness * @param $path String path filename, can be from untrusted source @@ -152,7 +155,8 @@ class XHTMLCompiler_Page $xc = XHTMLCompiler::getInstance(); $name = $this->cache->getName(); // a little icky - if ($name[0] == '.') $name = substr($name, 1); + if ($name[0] !== '/') $name = "/$name"; + if (strncmp($name, './', 2) === 0) $name = substr($name, 1); return $xc->getConf('web_path') . $name; } diff --git a/config.filters.php b/config.filters.php index 4006ad0..30a07ae 100644 --- a/config.filters.php +++ b/config.filters.php @@ -4,6 +4,7 @@ $filters->addDOMFilter('Metadata'); $filters->addDOMFilter('News'); +$filters->addDOMFilter('NewsLinker'); $filters->addDOMFilter('GenerateTableOfContents'); $filters->addDOMFilter('Acronymizer'); $filters->addDOMFilter('Quoter'); -- 2.11.4.GIT