Add command line tool to convert tombody notes to rST
[grauphel.git] / lib / converter / base.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
16 /**
17  * Base class to convert tomboy XML to some other format.
18  *
19  * @category  Tools
20  * @package   Grauphel
21  * @author    Christian Weiske <cweiske@cweiske.de>
22  * @copyright 2014 Christian Weiske
23  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
24  * @version   Release: @package_version@
25  * @link      http://cweiske.de/grauphel.htm
26  */
27 class Base
28 {
29     /**
30      * Re-arranges the XML of formatted links to that clean link tags can
31      * be generated.
32      *
33      * Tomboy 1.15.2 allows link formatting, and the resulting XML is a
34      * mess of multiple(!) link tags that are within or around other formatting
35      * tags.
36      *
37      * This method tries to re-arrange the links so that only a single link tag
38      * appears with all the formatting inside.
39      * 
40      * @param string $xmlContent Tomboy note content
41      *
42      * @return string XML content, with re-arranged link tags.
43      */
44     protected function fixNastyLinks($xmlContent)
45     {
46         preg_match_all(
47             '#(?:<.*>)?<link:internal>.+</link:internal><link:internal>.+</link:internal>#U',
48             $xmlContent,
49             $matches
50         );
51
52         foreach ($matches[0] as $nastyLink) {
53             $cleaner = str_replace('</link:internal><link:internal>', '', $nastyLink);
54             $cleaner = preg_replace('#<([a-z]+)><(link:internal)>#U', '<\2><\1>', $cleaner);
55             $cleaner = preg_replace('#</(link:internal)></([a-z]+)>#U', '</\2></\1>', $cleaner);
56             $cleaner = str_replace('</link:internal><link:internal>', '', $cleaner);
57             $xmlContent = str_replace($nastyLink, $cleaner, $xmlContent);
58         }
59
60         return $xmlContent;
61     }
62 }
63 ?>