Skip to content

Commit

Permalink
code cleanup for syntax.php
Browse files Browse the repository at this point in the history
* mostly reformatting
* adding doc blocks
* simplify switch statement
* add missing return
  • Loading branch information
splitbrain committed Jul 12, 2023
1 parent 077cb16 commit da8860c
Showing 1 changed file with 85 additions and 53 deletions.
138 changes: 85 additions & 53 deletions syntax.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,83 +6,115 @@
* @author Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com>
* @copyright (C) 2015-2016, Giuseppe Di Terlizzi
*/
// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();
class syntax_plugin_datatables extends DokuWiki_Syntax_Plugin
{

if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');

class syntax_plugin_datatables extends DokuWiki_Syntax_Plugin {

function getType(){ return 'container';}
function getAllowedTypes() { return array('container', 'substition'); }
function getPType(){ return 'block';}
function getSort(){ return 195; }

function connectTo($mode) {
$this->Lexer->addEntryPattern('<(?:DATATABLES?|datatables?)\b.*?>(?=.*?</(?:DATATABLES?|datatables?)>)', $mode, 'plugin_datatables');
}
/** @inheritdoc */
public function getType()
{
return 'container';
}

public function postConnect() {
$this->Lexer->addExitPattern('</(?:DATATABLES?|datatables?)>', 'plugin_datatables');
}
/** @inheritdoc */
public function getAllowedTypes()
{
return ['container', 'substition'];
}

function handle($match, $state, $pos, Doku_Handler $handler) {
/** @inheritdoc */
public function getPType()
{
return 'block';
}

switch ($state) {
case DOKU_LEXER_ENTER : return array($state, $match);
case DOKU_LEXER_UNMATCHED : return array($state, $match);
case DOKU_LEXER_EXIT : return array($state, $match);
/** @inheritdoc */
public function getSort()
{
return 195;
}

return array();
/** @inheritdoc */
public function connectTo($mode)
{
$this->Lexer->addEntryPattern(
'<(?:DATATABLES?|datatables?)\b.*?>(?=.*?</(?:DATATABLES?|datatables?)>)',
$mode,
'plugin_datatables'
);
}

}
/** @inheritdoc */
public function postConnect()
{
$this->Lexer->addExitPattern('</(?:DATATABLES?|datatables?)>', 'plugin_datatables');
}

function render($mode, Doku_Renderer $renderer, $data) {
/** @inheritdoc */
public function handle($match, $state, $pos, Doku_Handler $handler)
{

if (empty($data)) return false;
if ($mode !== 'xhtml') return false;
switch ($state) {
case DOKU_LEXER_UNMATCHED:
case DOKU_LEXER_EXIT:
case DOKU_LEXER_ENTER:
return [$state, $match];
}

/** @var Doku_Renderer_xhtml $renderer */
return [];
}

list($state, $match) = $data;
/** @inheritdoc */
public function render($mode, Doku_Renderer $renderer, $data)
{

switch($state) {
if (empty($data)) return false;
if ($mode !== 'xhtml') return false;

case DOKU_LEXER_ENTER:
/** @var Doku_Renderer_xhtml $renderer */

$html5_data = array();
$xml = @simplexml_load_string(str_replace('>', '/>', $match));
list($state, $match) = $data;

if (! is_object($xml)) {
switch ($state) {

$xml = simplexml_load_string('<foo />');
case DOKU_LEXER_ENTER:

global $ACT;
$html5_data = array();
$xml = @simplexml_load_string(str_replace('>', '/>', $match));

if ($ACT == 'preview') {
msg(sprintf('<strong>DataTable Plugin</strong> - Malformed tag (<code>%s</code>). Please check your code!', hsc($match)), -1);
}
if (!is_object($xml)) {
$xml = simplexml_load_string('<foo />');

}
global $ACT;
if ($ACT == 'preview') {
msg(
sprintf(
'<strong>DataTable Plugin</strong> - Malformed tag (<code>%s</code>).' .
' Please check your code!',
hsc($match)
),
-1
);
}
}

foreach ($xml->attributes() as $key => $value) {
$html5_data[] = sprintf("data-%s='%s'", $key, str_replace("'", "&apos;", (string) $value));
}
foreach ($xml->attributes() as $key => $value) {
$html5_data[] = sprintf("data-%s='%s'", $key, str_replace("'", "&apos;", (string)$value));
}

$renderer->doc .= sprintf('<div class="dt-wrapper" %s>', implode(' ', $html5_data));
return true;
$renderer->doc .= sprintf('<div class="dt-wrapper" %s>', implode(' ', $html5_data));
return true;

case DOKU_LEXER_EXIT:
$renderer->doc .= '</div>';
return true;
case DOKU_LEXER_EXIT:
$renderer->doc .= '</div>';
return true;

case DOKU_LEXER_UNMATCHED:
$renderer->doc .= $match;
return true;
case DOKU_LEXER_UNMATCHED:
$renderer->doc .= $match;
return true;
}

// should never be reached
return false;
}

}

}

0 comments on commit da8860c

Please sign in to comment.