Commit ea07e303 authored by Pietro Saccardi's avatar Pietro Saccardi

Completely refactoring plugin.

There are some weird quirks in DokuWiki.
1. The regex syntax for the Entry/Exit pattern is somewhat reduced.
2. Using a substitution type does not work.
parent 9a7d8d45
......@@ -15,64 +15,34 @@ require_once(__DIR__ . '/lib/grammar.php');
class syntax_plugin_ifauthex extends DokuWiki_Syntax_Plugin
{
private $_authExpression = null;
/**
* @return string Syntax mode type
*/
public function getType()
{
return 'substition';
}
private $_doRender = null;
/**
* @return string Paragraph type
*/
public function getPType()
{
return 'normal';
}
public function getType() { return 'formatting'; }
/**
* @return int Sort order - Low numbers go before high numbers
*/
public function getSort()
{
return 350;
}
public function getSort() { return 350; }
/**
* Connect lookup pattern to lexer.
*
* @param string $mode Parser mode
*/
public function connectTo($mode)
{
$this->Lexer->addEntryPattern('<ifauth(ex)?\s+.*?>(?=.*?\x3C/ifauth(ex)?\x3E)', $mode, 'plugin_ifauthex');
public function connectTo($mode) {
$this->Lexer->addEntryPattern('<ifauth\b.*?>(?=.*?</ifauth>)', $mode, 'plugin_ifauthex');
}
public function postConnect()
{
$this->Lexer->addExitPattern('<\/ifauth(ex)?>', 'plugin_ifauthex');
public function postConnect() {
$this->Lexer->addExitPattern('</ifauth>', 'plugin_ifauthex');
}
/**
* Handle matches of the ifauthex syntax
*
* @param string $match The match of the syntax
* @param int $state The state of the handler
* @param int $pos The position in the document
* @param Doku_Handler $handler The handler
*
* @return array Data for the renderer
*/
public function handle($match, $state, $pos, Doku_Handler $handler)
{
public function handle($match, $state, $pos, Doku_Handler $handler) {
switch ($state) {
case DOKU_LEXER_ENTER:
$matches = null;
preg_match('^<ifauth(ex)?\s+(.*?)>$', $match);
preg_match('/^<ifauth\b(.*?)>$/', $match, $matches);
if (is_array($matches) && count($matches) > 0) {
$authExpr = $matches[count($matches) - 1]; // the last group
return array($state, $authExpr);
try {
return array($state, auth_expr_parse($authExpr));
} catch (Exception $e) {
// Simply continue
}
}
return array($state, null);
break;
case DOKU_LEXER_UNMATCHED:
return array($state, $match);
......@@ -84,51 +54,27 @@ class syntax_plugin_ifauthex extends DokuWiki_Syntax_Plugin
return array();
}
/**
* Render xhtml output or metadata
*
* @param string $mode Renderer mode (supported modes: xhtml)
* @param Doku_Renderer $renderer The renderer
* @param array $data The data from the handler() function
*
* @return bool If rendering was successful.
*/
public function render($mode, Doku_Renderer $renderer, $data)
{
public function render($mode, Doku_Renderer $renderer, $data) {
if ($mode == 'xhtml') {
list($state, $expr) = $data;
list($state, $exprOrMatch) = $data;
switch ($state) {
case DOKU_LEXER_ENTER:
// Parse and store the expression
try {
$this->_authExpression = parse($expr);
} catch (Exception $e) {
$this->_authExpression = null;
if ($exprOrMatch === null) {
return false;
}
break;
case DOKU_LEXER_UNMATCHED:
$render = false;
if ($this->_authExpression !== null) {
try {
$render = $this->_authExpression->evaluate();
$this->_doRender = $exprOrMatch->evaluate();
} catch (Exception $e) {
$this->_doRender = null;
return false;
} finally {
$this->_authExpression = null;
}
}
if ($render) {
$output = p_render('xhtml', p_get_instructions($match), $info);
break;
case DOKU_LEXER_UNMATCHED:
if ($this->_doRender === true) {
$output = p_render('xhtml', p_get_instructions($exprOrMatch), $info);
// Remove '\n<p>\n' from start and '\n</p>\n' from the end.
preg_match('/^\s*<p>\s*/i', $output, $match);
if (count($match) > 0) {
$output = substr($output, strlen($match[0]));
}
preg_match('/\s*<\/p>\s*$/i', $output, $match);
if (count($match) > 0) {
$output = substr($output, -strlen($match[0]));
}
$output = preg_replace('/^\s*<p>\s*/i', '', $output);
$output = preg_replace('/\s*<\/p>\s*$/i', '', $output);
$renderer->doc .= $output;
}
$renderer->nocache();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment