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(' ', $listLevel);
110 if ($listLevel == -1) {
113 } else if ($reader->name == 'size:large') {
114 $store .= "\n" . str_repeat('-', $headingLength);
116 } else if ($reader->name == 'size:huge') {
117 $store .= "\n" . str_repeat('=', $headingLength);
119 } else if (substr($reader->name, 0, 5) == 'link:') {
122 $linkUrl = htmlspecialchars_decode(strip_tags($linkText));
123 if ($reader->name == 'link:internal') {
124 $linkUrl = call_user_func($this->internalLinkHandler, $linkUrl);
126 $linkUrl = $this->fixLinkUrl($linkUrl);
131 case XMLReader::TEXT:
132 case XMLReader::SIGNIFICANT_WHITESPACE:
134 $headingLength = strlen(trim($reader->value));
135 $store .= trim($reader->value);
137 $text = wordwrap($reader->value, 72 - 2 * $listLevel, "\n");
138 $parts = explode("\n", $text);
139 foreach ($parts as $k => $v) {
144 $parts[$k] = str_repeat(' ', $listLevel * 2 + 2) . $v;
147 $store .= implode("\n", $parts);
152 'Unsupported XML node type: ' . $reader->nodeType
161 * Fixes external URLs without a protocol
163 * @param string $linkUrl URL to fix
165 * @return string Fixed URL
167 protected function fixLinkUrl($linkUrl)
169 if ($linkUrl{0} == '/') {
171 $linkUrl = 'file://' . $linkUrl;
177 * Dummy internal link handler that simply adds ".htm" to the note title
179 * @param string $linkUrl Title of page that is linked
181 * @return string URL to link to
183 public function internalLinkHandler($linkUrl)
185 return $linkUrl . '.htm';