Have filters add self as dependencies, rm whitespace.
[xhtml-compiler.git] / XHTMLCompiler / DOMFilter / Quoter.php
blob052b4d5d22e16d7f633fadf41ec6a3219e76b22b
1 <?php
3 /**
4 * Converts <q> tags into quotation marks.
5 */
6 class XHTMLCompiler_DOMFilter_Quoter extends XHTMLCompiler_DOMFilter
9 protected $name = 'Quoter';
11 protected $leftQuote = "\xE2\x80\x9C";
12 protected $rightQuote = "\xE2\x80\x9D";
14 protected $leftNestedQuote = "\xE2\x80\x98";
15 protected $rightNestedQuote = "\xE2\x80\x99";
17 public function __construct(
18 $left = null, $right = null,
19 $left_nest = null, $right_nest = null,
20 $name = false
21 ) {
22 parent::__construct($name);
23 if ($left) $this->leftQuote = $left;
24 if ($right) $this->rightQuote = $right;
25 if ($left_nest) $this->leftNestedQuote = $left_nest;
26 if ($right_nest) $this->rightNestedQuote = $right_nest;
29 public function process(DOMDocument $dom, $page, $manager) {
31 $dep = false;
32 // first handle single-quotes
33 $nodes = $this->query("//html:q//html:q");
34 foreach ($nodes as $node) {
35 $dep = true;
36 $this->quotify($dom, $node, $this->leftNestedQuote, $this->rightNestedQuote);
39 // now handle double-quotes
40 $nodes = $this->query("//html:q");
41 foreach ($nodes as $node) {
42 $dep = true;
43 $this->quotify($dom, $node, $this->leftQuote, $this->rightQuote);
45 if ($dep) $manager->addDependency(__FILE__);
49 /**
50 * Takes a node and unpacks its contents, surrounding it with
51 * text instead.
52 * @param $node Node to be unpacked
53 * @param $left New text wrapper on left
54 * @param $right New text wrapper on right
56 protected function quotify($dom, $node, $left, $right) {
57 $parent = $node->parentNode;
58 $parent->insertBefore($dom->createTextNode($left), $node);
59 foreach ($node->childNodes as $child) {
60 $parent->insertBefore($child->cloneNode(true), $node);
62 $parent->insertBefore($dom->createTextNode($right), $node);
63 $parent->removeChild($node);