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]) : $this->getConf('default_padding'); 68 $attributes->logo = $barcode->attributes()['logo'] ? strval($barcode->attributes()['logo']) : null; 69 $data->attributes = $attributes; 70 71 // return 72 return $data; 73 } 74 75 public function render($mode, Doku_Renderer $renderer, $data) 76 { 77 if ($mode == 'xhtml') { 78 // check if an parsing error has occured 79 if ($data->error) { 80 $renderer->doc .= $this->renderErrorMessage($data->error); 81 return false; 82 } 83 84 // render barcode 85 try { 86 $barcode = new BarcodesWrapper($data->attributes); 87 $renderer->doc .= $barcode->getHtml(); 88 return true; 89 } 90 catch (BarcodesException $e) { 91 $renderer->doc .= $this->renderErrorMessage($e->getMessage()); 92 return false; 93 } 94 } 95 return false; 96 } 97 98 private function renderErrorMessage($error_message) { 99 return '<span style="font-weight: bold; color: red;"><barcode: ' . htmlspecialchars($error_message) . ' /></span>'; 100 } 101} 102