9 * @author Christian Weiske <cweiske@cweiske.de>
10 * @copyright 2014 Christian Weiske
11 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12 * @link http://cweiske.de/grauphel.htm
14 namespace OCA\Grauphel\Converter;
18 * Convert Tomboy note XML to HTML
20 * Tomboy already ships with a converter:
21 * https://git.gnome.org/browse/tomboy/tree/Tomboy/Addins/ExportToHtml/ExportToHtml.xsl
22 * We cannot use it since we want nice callbacks, and we do not want to rely
23 * on the PHP XSL extension, and we have to fix the links.
27 * @author Christian Weiske <cweiske@cweiske.de>
28 * @copyright 2014 Christian Weiske
29 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
30 * @version Release: @package_version@
31 * @link http://cweiske.de/grauphel.htm
33 class Html extends Base
35 protected static $tagMap = array(
42 protected static $styleClassMap = array(
43 'strikethrough' => 'strikethrough',
44 'highlight' => 'highlight',
45 'size:small' => 'small',
46 'size:large' => 'large',
47 'size:huge' => 'huge',
50 protected static $styleMap = array(
51 'monospace' => 'font-family:monospace; white-space: pre-wrap'
54 public $internalLinkHandler;
58 public function __construct()
60 $this->internalLinkHandler = array($this, 'internalLinkHandler');
64 * Converts the tomboy note XML into HTML
66 * @param string $xmlContent Tomboy note content
70 public function convert($xmlContent)
72 if (strpos($xmlContent, '</link:internal><link:internal>') !== false) {
73 $xmlContent = $this->fixNastyLinks($xmlContent);
77 $reader = new XMLReader();
79 '<?xml version="1.0" encoding="utf-8"?>' . "\n"
80 . '<content xmlns:size="size" xmlns:link="link">'
88 while ($reader->read()) {
89 switch ($reader->nodeType) {
90 case XMLReader::ELEMENT:
91 //echo $reader->name . "\n";
92 array_unshift($nesting, $reader->name);
93 if (isset(static::$tagMap[$reader->name])) {
94 $store .= '<' . static::$tagMap[$reader->name] . '>';
95 } else if (isset(static::$styleClassMap[$reader->name])) {
96 $store .= '<span class="'
97 . static::$styleClassMap[$reader->name]
99 } else if (isset(static::$styleMap[$reader->name])) {
100 $store .= '<span style="'
101 . static::$styleMap[$reader->name]
103 } else if (substr($reader->name, 0, 5) == 'link:') {
109 case XMLReader::END_ELEMENT:
110 array_shift($nesting);
111 if (isset(static::$tagMap[$reader->name])) {
112 $store .= '</' . static::$tagMap[$reader->name] . '>';
113 } else if (isset(static::$styleClassMap[$reader->name])) {
115 } else if (isset(static::$styleMap[$reader->name])) {
117 } else if (substr($reader->name, 0, 5) == 'link:') {
120 $linkUrl = htmlspecialchars_decode(strip_tags($linkText));
121 if ($reader->name == 'link:internal') {
122 $linkUrl = call_user_func($this->internalLinkHandler, $linkUrl);
124 $linkUrl = $this->fixLinkUrl($linkUrl);
126 $store .= '<a href="' . htmlspecialchars($linkUrl) . '">'
131 case XMLReader::TEXT:
132 case XMLReader::SIGNIFICANT_WHITESPACE:
133 $text = htmlspecialchars($reader->value);
134 if ($nesting[0] != 'monospace') {
135 $text = nl2br($text);
141 'Unsupported XML node type: ' . $reader->nodeType
146 $html = str_replace("</ul><br />\n", "</ul>\n", $html);
152 * Fixes external URLs without a protocol
154 * @param string $linkUrl URL to fix
156 * @return string Fixed URL
158 protected function fixLinkUrl($linkUrl)
160 if ($linkUrl{0} == '/') {
162 $linkUrl = 'file://' . $linkUrl;
168 * Dummy internal link handler that simply adds ".htm" to the note title
170 * @param string $linkUrl Title of page that is linked
172 * @return string URL to link to
174 public function internalLinkHandler($linkUrl)
176 return $linkUrl . '.htm';