1<?php
2
3/**
4 * DokuWiki Barcodes Plugin
5 * Copyright (C) 2023 Matthias Lohr <mail@mlohr.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21require dirname(__FILE__) . '/vendor/autoload.php';
22
23use DokuWiki\Barcodes\BarcodesWrapper;
24use DokuWiki\Barcodes\BarcodesException;
25use DokuWiki\Barcodes\Color;
26
27class syntax_plugin_barcodes extends DokuWiki_Syntax_Plugin
28{
29    public function getType()
30    {
31        return 'substition';
32    }
33
34    public function getSort()
35    {
36        return 32;
37    }
38
39    public function connectTo($mode)
40    {
41        $this->Lexer->addSpecialPattern('<barcode[^>]*/?>', $mode, 'plugin_barcodes');
42    }
43
44    public function handle($match, $state, $pos, Doku_Handler $handler)
45    {
46        $data = new stdClass();
47
48        // parse <barcode /> element
49        try {
50            $barcode = new SimpleXMLElement($match);
51
52        }
53        catch (Exception $e) {
54            $data->error = 'error parsing barcode tag';
55            return $data;
56        }
57
58        // parse attributes
59        $attributes = new stdClass();
60        $attributes->type = strtoupper($barcode->attributes()['type']);
61        $attributes->value = strval($barcode->attributes()['value']);
62        $attributes->img_type = $barcode->attributes()['img-type'] ? strval($barcode->attributes()['img-type']) : $this->getConf('default_img_type');
63        $attributes->color = $barcode->attributes()['color'] ? Color::str2hex($barcode->attributes()['color']) : Color::str2hex($this->getConf('default_color'));
64        $attributes->background_color = $barcode->attributes()['background-color'] ? Color::str2hex($barcode->attributes()['background-color']) : ($this->getConf('default_background_color') ? Color::str2hex($this->getConf('default_background_color')) : null);
65        $attributes->size = preg_match('/^([0-9]+)(px)?$/', $barcode->attributes()['size'], $matches) ? intval($matches[1]) : null;
66        $attributes->scale = $barcode->attributes()['scale'] ? floatval($barcode->attributes()['scale']) : 1;
67        $attributes->padding = preg_match('/^([0-9]+)(px)?$/', $barcode->attributes()['padding'], $matches) ? intval($matches[1]) : 0;
68        $data->attributes = $attributes;
69
70        // return
71        return $data;
72    }
73
74    public function render($mode, Doku_Renderer $renderer, $data)
75    {
76        if ($mode == 'xhtml') {
77            // check if an parsing error has occured
78            if ($data->error) {
79                $renderer->doc .= $this->renderErrorMessage($data->error);
80                return false;
81            }
82
83            // render barcode
84            try {
85                $barcode = new BarcodesWrapper($data->attributes);
86                $renderer->doc .= $barcode->getHtml();
87                return true;
88            }
89            catch (BarcodesException $e) {
90                $renderer->doc .= $this->renderErrorMessage($e->getMessage());
91                return false;
92            }
93        }
94        return false;
95    }
96
97    private function renderErrorMessage($error_message) {
98        return '<span style="font-weight: bold; color: red;">&lt;barcode: ' . htmlspecialchars($error_message) . ' /&gt;</span>';
99    }
100}
101