* @version 2016-11-08 */ if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/'); if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); if(!defined('IMAGE_PATH')) define('IMAGE_PATH',DOKU_BASE.'lib/plugins/chessdiagram/images/'); require_once(DOKU_PLUGIN.'syntax.php'); /** * All DokuWiki plugins to extend the parser/rendering mechanism * need to inherit from this class */ class syntax_plugin_chessdiagram extends DokuWiki_Syntax_Plugin { /** * function constructor */ function syntax_plugin_chessdiagram(){ // enable direct access to language strings $this->setupLocale(); } /** * return some info */ function getInfo(){ return array( 'author' => 'Michael Arlt', 'email' => 'michael.arlt@sk-schwanstetten.de', 'date' => '2016-11-08', 'name' => 'Chessdiagram Plugin', 'desc' => $lang['desc'], 'url' => 'http://wiki.splitbrain.org/plugin:chessdiagram' ); } /** * What kind of syntax are we? */ function getType(){ return 'substition'; } /** * Where to sort in? */ function getSort(){ return 999; } /** * Connect pattern to lexer */ function connectTo($mode) { $this->Lexer->addSpecialPattern('<'.$this->lang['kwpattern'].'>.+?'.$this->lang['kwpattern'].'>',$mode,'plugin_chessdiagram'); } /** * Handle the match */ function handle($match, $state, $pos, Doku_Handler $handler) { $match=substr($match,strlen($this->lang['kwpattern'])+2,-(strlen($this->lang['kwpattern'])+3)); foreach (preg_split('/\r\n|\r|\n/',$match) as $line) { $line=preg_replace("/#.*/","",trim($line)); if(substr($line,0,1) != "") { $lines.=$line; }; } $pos = strpos($lines,')'); if (preg_match('/[0-9]/',$lines)) { return strip_tags(strtr(substr($lines,0,$pos),$this->lang['kwoptions'],'TBLRdl').substr($lines,$pos)); } else { return strip_tags(strtr(substr($lines,0,$pos),$this->lang['kwoptions'],'TBLRdl').strtr(substr($lines,$pos),$this->lang['kwpieces'],'kqrbnpKQRBNP')); } } /** * Create output */ function render($mode, Doku_Renderer $renderer,$data) { if($mode == 'xhtml') { // for debug: $renderer->doc .= '
'.$data.''; $renderer->doc .= '
'.chessboard_render($data).'
'; return true; } return false; } } /* Copyright (C) 2006 http://www.vyvy.org/ This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA Changes: 15-Jun-06: [v1.0] Initial Release Usage: To convert a board to XHTML code, simply get the return value from: chessboard_render($board) */ /*================================*/ /* Routines */ /*================================*/ function chessboard_border_filename($border) { // $border: T, B, L, R, TL, TR, BL, BR switch ($border) { case 'T': case 'B': return IMAGE_PATH.'h.png'; case 'L': case 'R': return IMAGE_PATH.'v.png'; default: return IMAGE_PATH.'c.png'; } } function chessboard_piece_filename($piece, $square_color) { // $piece : KQBNRPkqbnrp- // $square_color : 0 1 switch ($piece) { case 'x': $name = 'xx'; break; case '-': $name = ''; break; default: $name = strtolower($piece).(ctype_lower($piece)?'d':'l'); break; } return IMAGE_PATH.$name.($square_color?'d':'l').'.png'; } function chessboard_render($content) { static $table_constructed = false; static $table_xhtml; if (!$table_constructed) { // Construct the translation table // Empty and marked squares $table_xhtml = array('-0' => '', '-1' => '', 'x0' => '', 'x1' => '', ); // Pieces $pieces = 'kqbnrp'; for ($i=0; $i<6; $i++) { $piece = $pieces[$i]; $table_xhtml += array(strtoupper($piece) . '0' => '', strtoupper($piece) . '1' => '', $piece . '0' => '', $piece . '1' => '', ); } // Borders $table_xhtml += array('T' => '', 'B' => '', 'L' => '', 'R' => '', 'TL' => '', 'TR' => '', 'BL' => '', 'BR' => '', ); $table_constructed = true; } preg_match('@^\s*(?:\((.*?)\))?(.*)$@s', $content, $matches); // Parameter Parsing //-------------------------------- $params =& $matches[1]; // Number of files: any integer (default: 8) $file_max = preg_match('@[0-9]+@', $params, $m) ? $m[0] : 8; // Color of the upper left square: [l]ight (default), or [d]ark $square_color_first = (strpos($params, 'd') !== false) ? 1 : 0; // Borders $border = array('T' => (strpos($params, 'T') !== false), 'B' => (strpos($params, 'B') !== false), 'L' => (strpos($params, 'L') !== false), 'R' => (strpos($params, 'R') !== false)); // Render the board in XHTML syntax //---------------------------------- $board =& $matches[2]; $xhtml = ''; // Top border if ($border['T']) { if ($border['L']) $xhtml .= $table_xhtml['TL']; $xhtml .= str_repeat($table_xhtml['T'], $file_max); if ($border['R']) $xhtml .= $table_xhtml['TR']; $file = $file_max; } else { $file = 0; $square_color = $square_color_first; $square_color_first = 1 - $square_color_first; } // Left border, board, right border for ($i=0; isset($board[$i]); $i++) { // Ignore unknown characters if (strpos('12345678KQBNRPkqbnrp-x', $board[$i]) === false) { continue; } if ($file >= $file_max) { $xhtml .= '