1<?php
2/**
3 * DokuWiki Plugin symbols4odt (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Thomas Schäfer <thomas.schaefer@itschert.net>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14abstract class Symbols_Syntax_Plugin extends DokuWiki_Syntax_Plugin {
15
16	protected array $patternMap;
17
18	protected abstract function getPatterns();
19
20	/**
21     * @return string Syntax mode type
22     */
23    public function getType()
24    {
25        return 'substition';
26    }
27
28    /**
29     * @return string Paragraph type
30     */
31    public function getPType()
32    {
33        return 'normal';
34    }
35
36    /**
37     * @return int Sort order - Low numbers go before high numbers
38     */
39    public function getSort()
40    {
41        return 1;
42    }
43
44    /**
45     * Connect lookup pattern to lexer.
46     *
47     * @param string $mode Parser mode
48     */
49    public function connectTo($mode)
50    {
51		foreach($this->getPatterns() as $pluginMode => $substitutionSet) {
52			foreach($substitutionSet["pattern"] as $substitionString) {
53				$this->Lexer->addSpecialPattern($substitionString, $mode, substr(get_class($this), 7));
54			}
55		}
56
57		$this->Lexer->addSpecialPattern("{{utf8symbol>.+?}}", $mode, substr(get_class($this), 7));
58	}
59
60    /**
61     * Handle matches of the symbols4odt syntax
62     *
63     * @param string       $match   The match of the syntax
64     * @param int          $state   The state of the handler
65     * @param int          $pos     The position in the document
66     * @param Doku_Handler $handler The handler
67     *
68     * @return array Data for the renderer
69     */
70    public function handle($match, $state, $pos, Doku_Handler $handler)
71    {
72		foreach($this->getPatterns() as $pluginMode => $substitutionSet) {
73
74			$substitutesArray =  array(
75				"substitute4XHTML" => $substitutionSet["substitute4XHTML"],
76				"substitute4ODT" => $substitutionSet["substitute4ODT"],
77			);
78
79			foreach($substitutionSet["pattern"] as $patternString) {
80				if (strcasecmp($patternString,$match) === 0) {
81					return $substitutesArray;
82				}
83
84				if ($substitutionSet["matchhelper"]) {
85					foreach($substitutionSet["matchhelper"] as $matchHelper) {
86						if (strcasecmp($matchHelper,$match) === 0) {
87							return $substitutesArray;
88						}
89					}
90				}
91			}
92		}
93
94		//$substitutesArray =  array(
95		//		"substitute4XHTML" => $match,
96		//		"substitute4ODT" => $match,
97		//	);
98		//return $substitutesArray;
99
100		if (strpos($match, 'utf8symbol') != false) {
101			$utf8_code = substr($match, 13, -2); //strip markup
102			$substitutesArray =  array(
103				"substitute4XHTML" => $this->getUTF8forHexadecimal($utf8_code),
104				"substitute4ODT" => $this->getUTF8forHexadecimal($utf8_code),
105			);
106
107			return $substitutesArray;
108		}
109
110        return array();
111    }
112
113    /**
114     * Render xhtml output or metadata
115     *
116     * @param string        $mode     Renderer mode (supported modes: xhtml)
117     * @param Doku_Renderer $renderer The renderer
118     * @param array         $data     The data from the handler() function
119     *
120     * @return bool If rendering was successful.
121     */
122    public function render($mode, Doku_Renderer $renderer, $data)
123    {
124        if ($mode == 'xhtml') {
125
126			$renderer->doc .= $data["substitute4XHTML"];
127            return true;
128
129        } elseif ($mode == 'odt') {
130
131			$renderer->cdata($data["substitute4ODT"]);
132			return true;
133
134		}
135
136        return false;
137    }
138
139
140    /**
141     * Returns a unicode character for the given unicode number.
142     *
143     * @param string    $unicodenumber    Unicode number of the character to be returned - only the number!
144	                                      For example, in order to get an 'A' character (unicode number: U+0041), provide a parameter $unicodenumber with a value of '0041'.
145     *
146     * @return The encoded String or Array on success, false in case of an error. (see return value of mb_convert_encoding, https://www.php.net/manual/de/function.mb-convert-encoding.php)
147     */
148	public function getUTF8forHexadecimal(string $unicodenumber){
149		return mb_convert_encoding('&#x'.$unicodenumber.';', 'UTF-8', 'HTML-ENTITIES');
150	}
151}
152
153class syntax_plugin_symbols4odt extends Symbols_Syntax_Plugin
154{
155	private $patterns;
156
157	// see https://www.dokuwiki.org/plugin:symbols4odt?do=edit#configuration_and_settings on how to create new patterns
158	protected function getPatterns() {
159		if (!isset($this->patterns)) {
160			$this->patterns = array(
161				"shy" => array(
162					"pattern" 			=> array('(?<![\x20-\x2F\x5C])\x5C\x2D','<SHY>','<shy>','<->'),
163					"matchhelper"		=> array('\-'),
164					"substitute4XHTML"	=> '&shy;',
165					"substitute4ODT" 	=>$this->getUTF8forHexadecimal('00AD'), // alternative: chr(194).chr(173),
166				),
167				"checkbox_empty" => array(
168					"pattern" 			=> array('<checkbox>','<CHECKBOX>'),
169					"substitute4XHTML"	=>  "<input type='checkbox'/>",
170					"substitute4ODT" 	=> $this->getUTF8forHexadecimal('2610'), // better would be to insert ODT code for a checkbox like: <draw:control text:anchor-type='as-char' draw:z-index='3' draw:name='Form1' draw:style-name='gr1' draw:text-style-name='P24' svg:width='0.32cm' svg:height='0.32cm' draw:control='control1'/>
171				),
172				"checkbox_filled" => array(
173					"pattern" 			=> array('<checkbox_checked>','<CHECKBOX_CHECKED>'),
174					"substitute4XHTML"	=>  "<input type='checkbox' checked/>",
175					"substitute4ODT" 	=> $this->getUTF8forHexadecimal('2612'),
176				),
177			);
178		}
179
180		return $this->patterns;
181	}
182}
183
184