aboutsummaryrefslogtreecommitdiff
path: root/lib/converter/restructuredtext.php
blob: bc2055113e040fc0fb2d6c60d1b9af3fa374c13e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/**
 * Part of grauphel
 *
 * PHP version 5
 *
 * @category  Tools
 * @package   Grauphel
 * @author    Christian Weiske <cweiske@cweiske.de>
 * @copyright 2014 Christian Weiske
 * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
 * @link      http://cweiske.de/grauphel.htm
 */
namespace OCA\Grauphel\Converter;
use \XMLReader;

/**
 * Convert Tomboy note XML to reStructuredText.
 * Mainly used to paste the content of a note into an e-mail
 *
 * @category  Tools
 * @package   Grauphel
 * @author    Christian Weiske <cweiske@cweiske.de>
 * @copyright 2014 Christian Weiske
 * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
 * @version   Release: @package_version@
 * @link      http://cweiske.de/grauphel.htm
 */
class ReStructuredText extends Base
{
    protected static $simpleMap = array(
        'bold'      => '**',
        'italic'    => '*',
        'monospace' => '``',
        'strikethrough' => '-',
        'highlight'  => '**',
    );

    public $internalLinkHandler;



    public function __construct()
    {
        $this->internalLinkHandler = array($this, 'internalLinkHandler');
    }

    /**
     * Converts the tomboy note XML into reStructuredText
     *
     * @param string $xmlContent Tomboy note content
     *
     * @return string Plain text
     */
    public function convert($xmlContent)
    {
        if (strpos($xmlContent, '</link:internal><link:internal>') !== false) {
            $xmlContent = $this->fixNastyLinks($xmlContent);
        }

        $rst = '';
        $reader = new XMLReader();
        $reader->xml(
            '<?xml version="1.0" encoding="utf-8"?>' . "\n"
            . '<content xmlns:size="size" xmlns:link="link">'
            . $xmlContent
            . '</content>'
        );

        $withinLink = false;
        $store = &$rst;
        $listLevel  = -1;
        $listPrefix = '';
        $listItemCount = 0;
        $heading = false;
        $headingLength = 0;
        while ($reader->read()) {
            switch ($reader->nodeType) {
            case XMLReader::ELEMENT:
                //echo $reader->name . "\n";
                if (isset(static::$simpleMap[$reader->name])) {
                    $store .= static::$simpleMap[$reader->name];
                } else if ($reader->name == 'list') {
                    ++$listLevel;
                    $listItemCount = 0;
                    $listPrefix = str_repeat('  ', $listLevel);
                } else if ($reader->name == 'list-item') {
                    ++$listItemCount;
                    if ($listItemCount == 1) {
                        $store .= "\n";
                    }
                    $store .= $listPrefix . '- ';
                } else if ($reader->name == 'size:large'
                    || $reader->name == 'size:huge'
                ) {
                    $store .= "\n";
                    $heading = true;
                } else if (substr($reader->name, 0, 5) == 'link:') {
                    $withinLink = true;
                    $linkText    = '';
                    $store       = &$linkText;
                }
                break;
            case XMLReader::END_ELEMENT:
                if (isset(static::$simpleMap[$reader->name])) {
                    $store .= static::$simpleMap[$reader->name];
                } else if ($reader->name == 'list') {
                    --$listLevel;
                    $listPrefix = str_repeat(
                        '  ', $listLevel < 0 ? 0 : $listLevel
                    );
                    if ($listLevel == -1) {
                        $store .= "\n";
                    }
                } else if ($reader->name == 'size:large') {
                    $store .= "\n" . str_repeat('-', $headingLength);
                    $heading = false;
                } else if ($reader->name == 'size:huge') {
                    $store .= "\n" . str_repeat('=', $headingLength);
                    $heading = false;
                } else if (substr($reader->name, 0, 5) == 'link:') {
                    $withinLink = false;
                    $store      = &$rst;
                    $linkUrl = htmlspecialchars_decode(strip_tags($linkText));
                    if ($reader->name == 'link:internal') {
                        $linkUrl = call_user_func($this->internalLinkHandler, $linkUrl);
                    } else {
                        $linkUrl = $this->fixLinkUrl($linkUrl);
                    }
                    $store .= $linkUrl;
                }
                break;
            case XMLReader::TEXT:
            case XMLReader::SIGNIFICANT_WHITESPACE:
                if ($heading) {
                    $headingLength = strlen(trim($reader->value));
                    $store .= trim($reader->value);
                } else {
                    $text = wordwrap($reader->value, 72 - 2 * $listLevel, "\n");
                    $parts = explode("\n", $text);
                    foreach ($parts as $k => $v) {
                        if ($k == 0) {
                            continue;
                        }
                        if ($v != '') {
                            $parts[$k] = str_repeat(' ', $listLevel * 2 + 2) . $v;
                        }
                    }
                    $store .= implode("\n", $parts);
                }
                break;
            default:
                throw new Exception(
                    'Unsupported XML node type: ' . $reader->nodeType
                );
            }
        }

        return $rst;
    }

    /**
     * Fixes external URLs without a protocol
     *
     * @param string $linkUrl URL to fix
     *
     * @return string Fixed URL
     */
    protected function fixLinkUrl($linkUrl)
    {
        if ($linkUrl{0} == '/') {
            //Unix file path
            $linkUrl = 'file://' . $linkUrl;
        }
        return $linkUrl;
    }

    /**
     * Dummy internal link handler that simply adds ".htm" to the note title
     *
     * @param string $linkUrl Title of page that is linked
     *
     * @return string URL to link to
     */
    public function internalLinkHandler($linkUrl)
    {
        return $linkUrl . '.htm';
    }
}
?>