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 reStructuredText.
19 * Mainly used to paste the content of a note into an e-mail
23 * @author Christian Weiske <cweiske@cweiske.de>
24 * @copyright 2014 Christian Weiske
25 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
26 * @version Release: @package_version@
27 * @link http://cweiske.de/grauphel.htm
29 class ReStructuredText extends Base
31 protected static $simpleMap = array(
35 'strikethrough' => '-',
39 public $internalLinkHandler;
43 public function __construct()
45 $this->internalLinkHandler = array($this, 'internalLinkHandler');
49 * Converts the tomboy note XML into reStructuredText
51 * @param string $xmlContent Tomboy note content
53 * @return string Plain text
55 public function convert($xmlContent)
57 if (strpos($xmlContent, '</link:internal><link:internal>') !== false) {
58 $xmlContent = $this->fixNastyLinks($xmlContent);
62 $reader = new XMLReader();
64 '<?xml version="1.0" encoding="utf-8"?>' . "\n"
65 . '<content xmlns:size="size" xmlns:link="link">'
77 while ($reader->read()) {
78 switch ($reader->nodeType) {
79 case XMLReader::ELEMENT:
80 //echo $reader->name . "\n";
81 if (isset(static::$simpleMap[$reader->name])) {
82 $store .= static::$simpleMap[$reader->name];
83 } else if ($reader->name == 'list') {
86 $listPrefix = str_repeat(' ', $listLevel);
87 } else if ($reader->name == 'list-item') {
89 if ($listItemCount == 1) {
92 $store .= $listPrefix . '- ';
93 } else if ($reader->name == 'size:large'
94 || $reader->name == 'size:huge'
98 } else if (substr($reader->name, 0, 5) == 'link:') {
104 case XMLReader::END_ELEMENT:
105 if (isset(static::$simpleMap[$reader->name])) {
106 $store .= static::$simpleMap[$reader->name];
107 } else if ($reader->name == 'list') {
109 $listPrefix = str_repeat(
110 ' ', $listLevel < 0 ? 0 : $listLevel
112 if ($listLevel == -1) {
115 } else if ($reader->name == 'size:large') {
116 $store .= "\n" . str_repeat('-', $headingLength);
118 } else if ($reader->name == 'size:huge') {
119 $store .= "\n" . str_repeat('=', $headingLength);
121 } else if (substr($reader->name, 0, 5) == 'link:') {
124 $linkUrl = htmlspecialchars_decode(strip_tags($linkText));
125 if ($reader->name == 'link:internal') {
126 $linkUrl = call_user_func($this->internalLinkHandler, $linkUrl);
128 $linkUrl = $this->fixLinkUrl($linkUrl);
133 case XMLReader::TEXT:
134 case XMLReader::SIGNIFICANT_WHITESPACE:
136 $headingLength = strlen(trim($reader->value));
137 $store .= trim($reader->value);
139 $text = wordwrap($reader->value, 72 - 2 * $listLevel, "\n");
140 $parts = explode("\n", $text);
141 foreach ($parts as $k => $v) {
146 $parts[$k] = str_repeat(' ', $listLevel * 2 + 2) . $v;
149 $store .= implode("\n", $parts);
154 'Unsupported XML node type: ' . $reader->nodeType
163 * Fixes external URLs without a protocol
165 * @param string $linkUrl URL to fix
167 * @return string Fixed URL
169 protected function fixLinkUrl($linkUrl)
171 if ($linkUrl{0} == '/') {
173 $linkUrl = 'file://' . $linkUrl;
179 * Dummy internal link handler that simply adds ".htm" to the note title
181 * @param string $linkUrl Title of page that is linked
183 * @return string URL to link to
185 public function internalLinkHandler($linkUrl)
187 return $linkUrl . '.htm';