/** * Handle a node * * Handle / transform a given node, and return the result of the * conversion. * * @param ezcDocumentElementVisitorConverter $converter * @param DOMElement $node * @param mixed $root * @return mixed */ public function handle(ezcDocumentElementVisitorConverter $converter, DOMElement $node, $root) { // Locate optional attribution elements, and transform them below the // recursive quote visiting. $xpath = new DOMXPath($node->ownerDocument); $attributionNodes = $xpath->query('*[local-name() = "attribution"]', $node); $attributions = array(); foreach ($attributionNodes as $attribution) { $attributions[] = $attribution->cloneNode(true); $attribution->parentNode->removeChild($attribution); } // Recursively decorate blockquote, after all attribution nodes are // removed ezcDocumentDocbookToRstConverter::$indentation += 4; $root = $converter->visitChildren($node, $root); // Append attribution nodes, if any foreach ($attributions as $attribution) { $converter->setSkipPostDecoration(true); $attributionLine = '-- ' . trim($converter->visitChildren($attribution, '')); $converter->setSkipPostDecoration(false); $root .= ezcDocumentDocbookToRstConverter::wordWrap($attributionLine) . "\n\n"; } ezcDocumentDocbookToRstConverter::$indentation -= 4; return $root; }
/** * Handle a node * * Handle / transform a given node, and return the result of the * conversion. * * @param ezcDocumentElementVisitorConverter $converter * @param DOMElement $node * @param mixed $root * @return mixed */ public function handle(ezcDocumentElementVisitorConverter $converter, DOMElement $node, $root) { $converter->setSkipPostDecoration(true); $comment = $converter->visitChildren($node, ''); $converter->setSkipPostDecoration(false); $root .= '.. ' . trim(ezcDocumentDocbookToRstConverter::wordWrap($comment, 3)) . "\n\n"; return $root; }
/** * Handle a node * * Handle / transform a given node, and return the result of the * conversion. * * @param ezcDocumentElementVisitorConverter $converter * @param DOMElement $node * @param mixed $root * @return mixed */ public function handle(ezcDocumentElementVisitorConverter $converter, DOMElement $node, $root) { foreach ($this->headerMapping as $tagName => $metaName) { if (($nodes = $node->getElementsBytagName($tagName)) && $nodes->length > 0) { foreach ($nodes as $child) { $root .= ":{$metaName}:\n"; $root .= ezcDocumentDocbookToRstConverter::wordWrap(trim($converter->visitChildren($child, '')), 2); $root .= "\n"; } } } return $root; }
/** * Handle a node. * * Handle / transform a given node, and return the result of the * conversion. * * @param ezcDocumentElementVisitorConverter $converter * @param DOMElement $node * @param mixed $root * @return mixed */ public function handle(ezcDocumentElementVisitorConverter $converter, DOMElement $node, $root) { foreach ($node->childNodes as $child) { if ($child->nodeType === XML_ELEMENT_NODE && $child->tagName === 'varlistentry') { $term = $child->getElementsByTagName('term')->item(0); $root .= ezcDocumentDocbookToRstConverter::wordWrap(trim($converter->visitChildren($term, ''))) . "\n"; ezcDocumentDocbookToRstConverter::$indentation += 4; foreach ($child->childNodes as $subChild) { if ($subChild->nodeType === XML_ELEMENT_NODE && $subChild->tagName === 'listitem') { $root = $converter->visitChildren($subChild, $root); } } ezcDocumentDocbookToRstConverter::$indentation -= 4; } } return $root; }
/** * Render a directive * * Render a directive with the given paramters. * * @param string $name * @param string $parameter * @param array $options * @param string $content * @return string */ protected function renderDirective($name, $parameter, array $options, $content = null) { $indentation = str_repeat(' ', ezcDocumentDocbookToRstConverter::$indentation); // Show directive with given parameters $directive = sprintf("\n%s.. %s:: %s\n", $indentation, $name, $parameter); // Append options foreach ($options as $key => $value) { $directive .= sprintf("%s :%s: %s\n", $indentation, ezcDocumentDocbookToRstConverter::escapeRstText($key), ezcDocumentDocbookToRstConverter::escapeRstText($value)); } // Append content, if given if ($content !== null) { $directive .= "\n" . str_repeat(' ', ezcDocumentDocbookToRstConverter::$indentation + 3) . trim(ezcDocumentDocbookToRstConverter::wordWrap($content, 3)) . "\n"; } // Append an additional newline after the directive contents $directive .= "\n"; return $directive; }
/** * Handle a node * * Handle / transform a given node, and return the result of the * conversion. * * @param ezcDocumentElementVisitorConverter $converter * @param DOMElement $node * @param mixed $root * @return mixed */ public function handle(ezcDocumentElementVisitorConverter $converter, DOMElement $node, $root) { // Find all anachors in paragraph, create pre paragraph RST anchors out // of them and remove them from the paragraph. $anchors = $node->getElementsByTagName('anchor'); $foundAnchors = false; foreach ($anchors as $anchor) { $root .= '.. _' . $anchor->getAttribute('ID') . ":\n"; $anchor->parentNode->removeChild($anchor); $foundAnchors = true; } $root .= $foundAnchors ? "\n" : ''; // Visit paragraph contents $contents = $converter->visitChildren($node, ''); // Remove all line breaks inside the paragraph. $contents = trim(preg_replace('(\\s+)', ' ', $contents)); $root .= ezcDocumentDocbookToRstConverter::wordWrap($contents) . "\n\n"; $root = $converter->finishParagraph($root); return $root; }
/** * Handle a node * * Handle / transform a given node, and return the result of the * conversion. * * @param ezcDocumentElementVisitorConverter $converter * @param DOMElement $node * @param mixed $root * @return mixed */ public function handle(ezcDocumentElementVisitorConverter $converter, DOMElement $node, $root) { $columns = $this->estimateColumnWidths($converter, $node); $rows = $node->getElementsByTagName('row'); $table = array(); $rowLines = array(); $rowNr = 0; $oldWidth = $converter->options->wordWrap; // Create contents from tables cells recursively and calculate their // content width, extending the column width, if necessary. foreach ($rows as $row) { $cellNr = 0; $rowLines[$rowNr] = 1; foreach ($row->childNodes as $cell) { if ($cell->nodeType === XML_ELEMENT_NODE && $cell->tagName === 'entry') { ezcDocumentDocbookToRstConverter::$wordWrap = $columns[$cellNr]; $table[$rowNr][$cellNr] = $cellContent = explode("\n", trim($converter->visitChildren($cell, ''))); $rowLines[$rowNr] = max($rowLines[$rowNr], count($cellContent)); $columns[$cellNr] = max($columns[$cellNr], $this->getMaxLineLength($cellContent)); ++$cellNr; } } ++$rowNr; } ezcDocumentDocbookToRstConverter::$wordWrap = $converter->options->wordWrap; // Build table row seperator $separator = ''; foreach ($columns as $width) { $separator .= str_repeat('-', $width) . ' '; } $separator = rtrim($separator) . "\n"; // Check if table has a header. RST does only support the foirst row as // a header row, so we will only check for this, and render all // subsequent header lines as plain contents. $hasHeader = (bool) $node->getElementsByTagName('thead')->length; // Draw table $cellCount = count($columns); $root .= str_replace('-', '=', $separator); foreach ($table as $rowNr => $row) { for ($line = 0; $line < $rowLines[$rowNr]; ++$line) { for ($cellNr = 0; $cellNr < $cellCount; ++$cellNr) { $last = $cellNr >= $cellCount - 1; $width = $columns[$cellNr] + ($last ? 0 : 2); $lineContent = isset($table[$rowNr][$cellNr][$line]) ? $table[$rowNr][$cellNr][$line] : ''; $root .= $last ? $lineContent . "\n" : str_pad(rtrim($lineContent), $width, ' '); } } // Always add row seperator if ($hasHeader && $rowNr === 0 || $rowNr >= count($table) - 1) { $root .= str_replace('-', '=', $separator); } else { $root .= $separator; } } $root .= "\n"; return $root; }
/** * Initialize destination document * * Initialize the structure which the destination document could be build * with. This may be an initial DOMDocument with some default elements, or * a string, or something else. * * @return mixed */ protected function initializeDocument() { self::$indentation = 0; self::$wordWrap = $this->options->wordWrap; return ''; }