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

Added meaningful plugin code.

parent 50b6955c
Loading
Loading
Loading
Loading
+61 −11
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ require_once('grammar.php');

class syntax_plugin_ifauthex extends DokuWiki_Syntax_Plugin
{
    private $_authExpression = null;
    /**
     * @return string Syntax mode type
     */
@@ -46,14 +47,13 @@ class syntax_plugin_ifauthex extends DokuWiki_Syntax_Plugin
     */
    public function connectTo($mode)
    {
        $this->Lexer->addSpecialPattern('<FIXME>', $mode, 'plugin_ifauthex_ifauthex');
//        $this->Lexer->addEntryPattern('<FIXME>', $mode, 'plugin_ifauthex_ifauthex');
        $this->Lexer->addEntryPattern('<ifauth(ex)?\s+.*?>(?=.*?\x3C/ifauth(ex)?\x3E)', $mode, 'plugin_ifauthex_ifauthex');
    }

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

    /**
     * Handle matches of the ifauthex syntax
@@ -67,9 +67,21 @@ class syntax_plugin_ifauthex extends DokuWiki_Syntax_Plugin
     */
    public function handle($match, $state, $pos, Doku_Handler $handler)
    {
        $data = array();

        return $data;
        switch ($state) {
            case DOKU_LEXER_ENTER:
                $matches = null;
                preg_match('^<ifauth(ex)?\s+(.*?)>$', $match);
                $authExpr = $matches[count($matches) - 1]; // the last group
                return array($state, $authExpr);
                break;
            case DOKU_LEXER_UNMATCHED:
                return array($state, $match);
                break;
            case DOKU_LEXER_EXIT:
                return array($state, null);
                break;
        }
        return array();
    }

    /**
@@ -84,9 +96,47 @@ class syntax_plugin_ifauthex extends DokuWiki_Syntax_Plugin
    public function render($mode, Doku_Renderer $renderer, $data)
    {
        if ($mode !== 'xhtml') {
            list($state, $expr) = $data;
            switch ($state) {
                case DOKU_LEXER_ENTER:
                    // Parse and store the expression
                    try {
                        $this->_authExpression = parse($expr);
                    } catch (Exception $e) {
                        $this->_authExpression = null;
                        return false;
                    }

                    break;
                case DOKU_LEXER_UNMATCHED:
                    $render = false;
                    if ($this->_authExpression !== null) {
                        try {
                            $render = $this->_authExpression->evaluate();
                        } catch (Exception $e) {
                            return false;
                        } finally {
                            $this->_authExpression = null;
                        }
                    }
                    if ($render) {
                        $output = p_render('xhtml', p_get_instructions($match), $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]));
                        }
                        $renderer->doc .= $output;
                    }
                    $renderer->nocache();
                    break;
                case DOKU_LEXER_EXIT:
                    break;
            }
        }
        return true;
    }
}