summaryrefslogtreecommitdiff
path: root/lib/converter/html.php
blob: a6a92bc6e9693f8c74ceb60aa3bffcd688248530 (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
<?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 HTML
 *
 * Tomboy already ships with a converter:
 * https://git.gnome.org/browse/tomboy/tree/Tomboy/Addins/ExportToHtml/ExportToHtml.xsl
 * We cannot use it since we want nice callbacks, and we do not want to rely
 * on the PHP XSL extension, and we have to fix the links.
 *
 * @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 Html extends Base
{
    protected static $tagMap = array(
        'list'      => 'ul',
        'list-item' => 'li',
        'bold'      => 'b',
        'italic'    => 'i',
        'monospace' => 'tt',
    );

    protected static $styleClassMap = array(
        'strikethrough' => 'strikethrough',
        'highlight'  => 'highlight',
        'size:small' => 'small',
        'size:large' => 'large',
        'size:huge'  => 'huge',
    );

    public $internalLinkHandler;



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

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

        $html = '';
        $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 = &$html;
        while ($reader->read()) {
            switch ($reader->nodeType) {
            case XMLReader::ELEMENT:
                //echo $reader->name . "\n";
                if (isset(static::$tagMap[$reader->name])) {
                    $store .= '<' . static::$tagMap[$reader->name] . '>';
                } else if (isset(static::$styleClassMap[$reader->name])) {
                    $store .= '<span class="'
                        . static::$styleClassMap[$reader->name]
                        . '">';
                } else if (substr($reader->name, 0, 5) == 'link:') {
                    $withinLink = true;
                    $linkText    = '';
                    $store       = &$linkText;
                }
                break;
            case XMLReader::END_ELEMENT:
                if (isset(static::$tagMap[$reader->name])) {
                    $store .= '</' . static::$tagMap[$reader->name] . '>';
                } else if (isset(static::$styleClassMap[$reader->name])) {
                    $store .= '</span>';
                } else if (substr($reader->name, 0, 5) == 'link:') {
                    $withinLink = false;
                    $store      = &$html;
                    $linkUrl = htmlspecialchars_decode(strip_tags($linkText));
                    if ($reader->name == 'link:internal') {
                        $linkUrl = call_user_func($this->internalLinkHandler, $linkUrl);
                    } else {
                        $linkUrl = $this->fixLinkUrl($linkUrl);
                    }
                    $store .= '<a href="' . htmlspecialchars($linkUrl) . '">'
                        . $linkText
                        . '</a>';
                }
                break;
            case XMLReader::TEXT:
            case XMLReader::SIGNIFICANT_WHITESPACE:
                $store .= nl2br(htmlspecialchars($reader->value));
                break;
            default:
                throw new Exception(
                    'Unsupported XML node type: ' . $reader->nodeType
                );
            }
        }

        $html = str_replace("</ul><br />\n", "</ul>\n", $html);

        return $html;
    }

    /**
     * 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';
    }
}
?>