4f246bc602495aa3a911c309d239a9676b39a4c9
[xhtml-compiler.git] / XHTMLCompiler / DOMFilter / Quoter.php
blob4f246bc602495aa3a911c309d239a9676b39a4c9
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 // first handle single-quotes
32 $nodes = $this->query("//html:q//html:q");
33 foreach ($nodes as $node) {
34 $this->quotify($dom, $node, $this->leftNestedQuote, $this->rightNestedQuote);
37 // now handle double-quotes
38 $nodes = $this->query("//html:q");
39 foreach ($nodes as $node) {
40 $this->quotify($dom, $node, $this->leftQuote, $this->rightQuote);
45 /**
46 * Takes a node and unpacks its contents, surrounding it with
47 * text instead.
48 * @param $node Node to be unpacked
49 * @param $left New text wrapper on left
50 * @param $right New text wrapper on right
52 protected function quotify($dom, $node, $left, $right) {
53 $parent = $node->parentNode;
54 $parent->insertBefore($dom->createTextNode($left), $node);
55 foreach ($node->childNodes as $child) {
56 $parent->insertBefore($child->cloneNode(true), $node);
58 $parent->insertBefore($dom->createTextNode($right), $node);
59 $parent->removeChild($node);