Skip to content

Instantly share code, notes, and snippets.

@xube
Last active April 2, 2020 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xube/ab22eefd6795af27ab23db3a63d1b1f4 to your computer and use it in GitHub Desktop.
Save xube/ab22eefd6795af27ab23db3a63d1b1f4 to your computer and use it in GitHub Desktop.
added function properxhtml() for better readability
<?php
/********************************************************************************************************************************
*
* Dokuwiki Infobox
*
* www.github.com/xube/dokuwiki-infobox
* www.triglaw.sk
* www.drop.sk
*
/*******************************************************************************************************************************/
// must be run within DokuWiki
if(!defined('DOKU_INC')) die();
// if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
/********************************************************************************************************************************
* All DokuWiki plugins to extend the parser/rendering mechanism
* need to inherit from this class
********************************************************************************************************************************/
class syntax_plugin_infobox extends DokuWiki_Syntax_Plugin {
/* REGEX PATTERNS */
protected $entry_pattern = '<infobox>\n.*?(?=.*</infobox>)';
protected $row_pattern = '\|.*?(?=.*?)\n';
protected $exit_pattern = '</infobox>';
protected $special_pattern = '<div\b[^>\r\n]*?/>'; //\|.*?(?=.*?)\n
function getType(){ return 'formatting';}
function getAllowedTypes() { return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs'); }
function getPType(){ return 'stack';}
function getSort(){ return 195; }
// override default accepts() method to allow nesting - ie, to get the plugin accepts its own entry syntax
// function accepts($mode) {
// if ($mode == substr(get_class($this), 7)) return true;
// return parent::accepts($mode);
// }
public function connectTo($mode) {
$this->Lexer->addSpecialPattern($this->special_pattern,$mode,'plugin_wrap_'.$this->getPluginComponent());
$this->Lexer->addEntryPattern($this->entry_pattern, $mode, 'plugin_infobox');
//Add Internal Pattern Match
$this->Lexer->addPattern($this->row_pattern, 'plugin_infobox');
}
public function postConnect() {
$this->Lexer->addExitPattern($this->exit_pattern, 'plugin_infobox');
$this->Lexer->addPattern('[ \t]*={2,}[^\n]+={2,}[ \t]*(?=\n)', 'plugin_wrap_'.$this->getPluginComponent()); //FROM WRAP, NOT KNOWING WHAT IS HAPPENING
}
public function handle($match, $state, $pos, Doku_Handler $handler) {
switch ($state){
case DOKU_LEXER_ENTER :
break;
case DOKU_LEXER_MATCHED :
//Find The Token And Value (Before '=' remove white space, convert to lower case).
$tokenDiv = strpos($match, '='); //Find Token Value Divider ('=')
$prettyToken = trim(substr($match, 1, ($tokenDiv - 1))); //Everything Before '=', Remove White Space
$token = strtolower($prettyToken); //Convert To Lower Case
$value = trim(substr($match, ($tokenDiv + 1))); //Everything after '='
return array($state, $value, $prettyToken);
case DOKU_LEXER_UNMATCHED :
return array($state, $match);
case DOKU_LEXER_EXIT :
return array($state, '');
case DOKU_LEXER_SPECIAL :
return array($state, $data, '');
}
return array($state, $match);
}
public function properxhtml($data) {
$instructions = p_get_instructions($data);
$data = p_render('xhtml', $instructions, $info);
$data = str_replace(['<p>', '</p>'], '', $data); //Temporarely workaround for removing <p> inside table cause p_render
return $data;
}
public function render($mode, Doku_Renderer $renderer, $indata){
if (empty($indata)) return false;
list($state, $data, $title) = $indata;
$token = strtolower($title); //$words = preg_replace('/[0-9]+/', '', $words);
if($mode == 'xhtml'){
switch ($state){
case DOKU_LEXER_ENTER :
$renderer->startSectionEdit(0, $indata);
$renderer->doc .= '<table class="infobox">';
break;
case DOKU_LEXER_MATCHED :
if ($token === 'title' || $token === 'caption' ) $renderer->doc .= '<caption class="infobox-title">'.$data.'</caption>';
if ($token) :
switch ($token) {
case 'caption';
case 'title';
break;
case 'header';
$renderer->doc .= '<tr><th colspan="2" class="infobox-header">'.$title.'</th></tr>';
break;
case 'subtitle';
$renderer->doc .= '<tr><th colspan="2" class="infobox-subtitle">'.$title.'</th></tr>';
break;
case 'fullrow';
case 'desc';
case 'full';
$renderer->doc .= '<tr><td colspan="2" class="infobox-fullrow">'.$this->properxhtml($data).'</td></tr>';
break;
case 'image';
// $image = str_replace(['{{', '}}'], '', $data); //ONE WAY TO DO IT
// $renderer->doc .= '<td colspan="2">'.$renderer->internalmedia($image,'','center','auto','auto','cache','nolink',true).'</td>';
$renderer->doc .= '<tr><td colspan="2">'.$this->properxhtml($data).'</td></tr>';
break;
case 'footer';
$renderer->doc .= '<tr><td colspan="2" class="infobox-footer">'.$this->properxhtml($data).'</td></tr>';
break;
default;
$renderer->doc .= '<tr><th>'.$title.'</th>';
// $renderer->doc .= '<td>'.$data.'</td>'; //Proper way which is not working
$renderer->doc .= '<td>'.$this->properxhtml($data).'</td></tr>';
break;
}
endif;
break;
case DOKU_LEXER_SPECIAL :
break;
case DOKU_LEXER_UNMATCHED :
$renderer->doc .= $renderer->_xmlEntities($data);
break;
case DOKU_LEXER_EXIT :
$renderer->doc .= '</table>';
$renderer->finishSectionEdit();
break;
}
return true;
}
return false;
}
}
?>
@xube
Copy link
Author

xube commented Apr 1, 2020

Momentálne v infoboxe nefunguje parsovanie interného markup kódu. Neviem prísť na to prečo.
getAllowedTypes() a getType() majú všetko nastavené správne, alebo nie?

Žeby $data bolo treba spracovať ešte pred parsovaním vo funkci handle?

@xube
Copy link
Author

xube commented Apr 2, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment