use ILIKE for case insensitive searches
[grauphel.git] / lib / converter / restructuredtext.php
1 <?php
2 /**
3  * Part of grauphel
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Grauphel
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
13  */
14 namespace OCA\Grauphel\Converter;
15 use \XMLReader;
16
17 /**
18  * Convert Tomboy note XML to reStructuredText.
19  * Mainly used to paste the content of a note into an e-mail
20  *
21  * @category  Tools
22  * @package   Grauphel
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
28  */
29 class ReStructuredText extends Base
30 {
31     protected static $simpleMap = array(
32         'bold'      => '**',
33         'italic'    => '*',
34         'monospace' => '``',
35         'strikethrough' => '-',
36         'highlight'  => '**',
37     );
38
39     public $internalLinkHandler;
40
41
42
43     public function __construct()
44     {
45         $this->internalLinkHandler = array($this, 'internalLinkHandler');
46     }
47
48     /**
49      * Converts the tomboy note XML into reStructuredText
50      *
51      * @param string $xmlContent Tomboy note content
52      *
53      * @return string Plain text
54      */
55     public function convert($xmlContent)
56     {
57         if (strpos($xmlContent, '</link:internal><link:internal>') !== false) {
58             $xmlContent = $this->fixNastyLinks($xmlContent);
59         }
60
61         $rst = '';
62         $reader = new XMLReader();
63         $reader->xml(
64             '<?xml version="1.0" encoding="utf-8"?>' . "\n"
65             . '<content xmlns:size="size" xmlns:link="link">'
66             . $xmlContent
67             . '</content>'
68         );
69
70         $withinLink = false;
71         $store = &$rst;
72         $listLevel  = -1;
73         $listPrefix = '';
74         $listItemCount = 0;
75         $heading = false;
76         $headingLength = 0;
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') {
84                     ++$listLevel;
85                     $listItemCount = 0;
86                     $listPrefix = str_repeat('  ', $listLevel);
87                 } else if ($reader->name == 'list-item') {
88                     ++$listItemCount;
89                     if ($listItemCount == 1) {
90                         $store .= "\n";
91                     }
92                     $store .= $listPrefix . '- ';
93                 } else if ($reader->name == 'size:large'
94                     || $reader->name == 'size:huge'
95                 ) {
96                     $store .= "\n";
97                     $heading = true;
98                 } else if (substr($reader->name, 0, 5) == 'link:') {
99                     $withinLink = true;
100                     $linkText    = '';
101                     $store       = &$linkText;
102                 }
103                 break;
104             case XMLReader::END_ELEMENT:
105                 if (isset(static::$simpleMap[$reader->name])) {
106                     $store .= static::$simpleMap[$reader->name];
107                 } else if ($reader->name == 'list') {
108                     --$listLevel;
109                     $listPrefix = str_repeat('  ', $listLevel);
110                     if ($listLevel == -1) {
111                         $store .= "\n";
112                     }
113                 } else if ($reader->name == 'size:large') {
114                     $store .= "\n" . str_repeat('-', $headingLength);
115                     $heading = false;
116                 } else if ($reader->name == 'size:huge') {
117                     $store .= "\n" . str_repeat('=', $headingLength);
118                     $heading = false;
119                 } else if (substr($reader->name, 0, 5) == 'link:') {
120                     $withinLink = false;
121                     $store      = &$rst;
122                     $linkUrl = htmlspecialchars_decode(strip_tags($linkText));
123                     if ($reader->name == 'link:internal') {
124                         $linkUrl = call_user_func($this->internalLinkHandler, $linkUrl);
125                     } else {
126                         $linkUrl = $this->fixLinkUrl($linkUrl);
127                     }
128                     $store .= $linkUrl;
129                 }
130                 break;
131             case XMLReader::TEXT:
132             case XMLReader::SIGNIFICANT_WHITESPACE:
133                 if ($heading) {
134                     $headingLength = strlen(trim($reader->value));
135                     $store .= trim($reader->value);
136                 } else {
137                     $text = wordwrap($reader->value, 72 - 2 * $listLevel, "\n");
138                     $parts = explode("\n", $text);
139                     foreach ($parts as $k => $v) {
140                         if ($k == 0) {
141                             continue;
142                         }
143                         if ($v != '') {
144                             $parts[$k] = str_repeat(' ', $listLevel * 2 + 2) . $v;
145                         }
146                     }
147                     $store .= implode("\n", $parts);
148                 }
149                 break;
150             default:
151                 throw new Exception(
152                     'Unsupported XML node type: ' . $reader->nodeType
153                 );
154             }
155         }
156
157         return $rst;
158     }
159
160     /**
161      * Fixes external URLs without a protocol
162      *
163      * @param string $linkUrl URL to fix
164      *
165      * @return string Fixed URL
166      */
167     protected function fixLinkUrl($linkUrl)
168     {
169         if ($linkUrl{0} == '/') {
170             //Unix file path
171             $linkUrl = 'file://' . $linkUrl;
172         }
173         return $linkUrl;
174     }
175
176     /**
177      * Dummy internal link handler that simply adds ".htm" to the note title
178      *
179      * @param string $linkUrl Title of page that is linked
180      *
181      * @return string URL to link to
182      */
183     public function internalLinkHandler($linkUrl)
184     {
185         return $linkUrl . '.htm';
186     }
187 }
188 ?>