Commit ea07e303 authored by Pietro Saccardi's avatar Pietro Saccardi
Browse files

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
Loading
Loading
Loading
Loading
+30 −84
Original line number Original line Diff line number Diff line
@@ -15,64 +15,34 @@ require_once(__DIR__ . '/lib/grammar.php');


class syntax_plugin_ifauthex extends DokuWiki_Syntax_Plugin
class syntax_plugin_ifauthex extends DokuWiki_Syntax_Plugin
{
{
    private $_authExpression = null;
    private $_doRender = null;
    /**
     * @return string Syntax mode type
     */
    public function getType()
    {
        return 'substition';
    }


    /**
    public function getType() { return 'formatting'; }
     * @return string Paragraph type
     */
    public function getPType()
    {
        return 'normal';
    }


    /**
    public function getSort() { return 350; }
     * @return int Sort order - Low numbers go before high numbers
     */
    public function getSort()
    {
        return 350;
    }


    /**
    public function connectTo($mode) {
     * Connect lookup pattern to lexer.
        $this->Lexer->addEntryPattern('<ifauth\b.*?>(?=.*?</ifauth>)', $mode, 'plugin_ifauthex');
     *
     * @param string $mode Parser mode
     */
    public function connectTo($mode)
    {
        $this->Lexer->addEntryPattern('<ifauth(ex)?\s+.*?>(?=.*?\x3C/ifauth(ex)?\x3E)', $mode, 'plugin_ifauthex');
    }
    }


   public function postConnect()
    public function postConnect() {
   {
       $this->Lexer->addExitPattern('</ifauth>', 'plugin_ifauthex');
       $this->Lexer->addExitPattern('<\/ifauth(ex)?>', 'plugin_ifauthex');
    }
    }


    /**
    public function handle($match, $state, $pos, Doku_Handler $handler) {
     * 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)
    {
        switch ($state) {
        switch ($state) {
            case DOKU_LEXER_ENTER:
            case DOKU_LEXER_ENTER:
                $matches = null;
                $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
                    $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;
                break;
            case DOKU_LEXER_UNMATCHED:
            case DOKU_LEXER_UNMATCHED:
                return array($state, $match);
                return array($state, $match);
@@ -84,51 +54,27 @@ class syntax_plugin_ifauthex extends DokuWiki_Syntax_Plugin
        return array();
        return array();
    }
    }


    /**
    public function render($mode, Doku_Renderer $renderer, $data) {
     * 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)
    {
        if ($mode == 'xhtml') {
        if ($mode == 'xhtml') {
            list($state, $expr) = $data;
            list($state, $exprOrMatch) = $data;
            switch ($state) {
            switch ($state) {
                case DOKU_LEXER_ENTER:
                case DOKU_LEXER_ENTER:
                    // Parse and store the expression
                    if ($exprOrMatch === null) {
                    try {
                        $this->_authExpression = parse($expr);
                    } catch (Exception $e) {
                        $this->_authExpression = null;
                        return false;
                        return false;
                    }
                    }
                    break;
                case DOKU_LEXER_UNMATCHED:
                    $render = false;
                    if ($this->_authExpression !== null) {
                    try {
                    try {
                            $render = $this->_authExpression->evaluate();
                        $this->_doRender = $exprOrMatch->evaluate();
                    } catch (Exception $e) {
                    } catch (Exception $e) {
                        $this->_doRender = null;
                        return false;
                        return false;
                        } finally {
                            $this->_authExpression = null;
                    }
                    }
                    }
                    break;
                    if ($render) {
                case DOKU_LEXER_UNMATCHED:
                        $output = p_render('xhtml', p_get_instructions($match), $info);
                    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.
                        // Remove '\n<p>\n' from start and '\n</p>\n' from the end.
                        preg_match('/^\s*<p>\s*/i', $output, $match);
                        $output = preg_replace('/^\s*<p>\s*/i', '', $output);
                        if (count($match) > 0) {
                        $output = preg_replace('/\s*<\/p>\s*$/i', '', $output);
                            $output = substr($output, strlen($match[0]));
                        }
                        preg_match('/\s*<\/p>\s*$/i', $output, $match);
                        if (count($match) > 0) {
                            $output = substr($output, -strlen($match[0]));
                        }
                        $renderer->doc .= $output;
                        $renderer->doc .= $output;
                    }
                    }
                    $renderer->nocache();
                    $renderer->nocache();