Unverified Commit 53d6a4da authored by Pietro Saccardi's avatar Pietro Saccardi Committed by GitHub
Browse files

Merge pull request #3 from cosmocode/master

Fix sections and section editing
parents 2b2c6c84 2584271a
Loading
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
<?php

/**
 * General tests for the ifauthex plugin
 *
 * @group plugin_ifauthex
 * @group plugins
 */
class instructions_plugin_ifauthex_test extends DokuWikiTest
{

    protected $pluginsEnabled = array('ifauthex');



    public function test_instructions()
    {

        $calls = p_get_instructions(file_get_contents(__DIR__.'/testpage.txt'));

        $calls = array_map([self::class, 'stripByteIndex'], $calls);

        $this->assertJsonStringEqualsJsonFile(__DIR__.'/testpage.json', json_encode($calls));

        //print_r($calls);
    }

    /**
     * copied from the core test suite, removes the byte positions
     *
     * @param $call
     * @return mixed
     */
    public static function stripByteIndex($call) {
        unset($call[2]);
        if ($call[0] == "nest") {
            $call[1][0] = array_map('stripByteIndex',$call[1][0]);
        }
        return $call;
    }
}

_test/testpage.json

0 → 100644
+41 −0
Original line number Diff line number Diff line
[
    ["document_start", []],
        ["header", ["Sec 1", 3, 1]],
        ["section_open", [3]],
            ["p_open", []],
                ["cdata", ["1abc"]],
            ["p_close", []],
            ["plugin", ["ifauthex", [1, " admin"], 1, "<ifauth admin>"]],
                ["section_close", []],
                ["header", ["Sec 2", 3, 40]],
                ["section_open", [3]],
                ["p_open", []],
                    ["plugin", ["ifauthex", [3, "2def"], 3, "2def"]],
                ["p_close", []],
            ["plugin", ["ifauthex", [4, null], 4, "<\/ifauth>"]],
        ["section_close", []],

        ["header", ["Sec 3", 3, 74]],
        ["section_open", [3]],
            ["p_open", []],
                ["cdata", ["3ghi"]],
            ["p_close", []],
            ["plugin", ["ifauthex", [1, " admina"], 1, "<ifauth admina>"]],
                ["section_close", []],
                ["header", ["Sec 4", 3, 114]],
                ["section_open", [3]],
                ["p_open", []],
                    ["plugin", ["ifauthex", [3, "4jkl"], 3, "4jkl"]],
                ["p_close", []],
            ["plugin", ["ifauthex", [4, null], 4, "<\/ifauth>"]],
        ["section_close", []],

        ["header", ["Sec 5", 3, 148]],
        ["section_open", [3]],
            ["p_open", []],
                ["cdata", ["5mno"]],
            ["p_close", []],
        ["section_close", []],

    ["document_end", []]
]

_test/testpage.txt

0 → 100644
+28 −0
Original line number Diff line number Diff line
==== Sec 1 ====

1abc

<ifauth admin>

==== Sec 2 ====

2def

</ifauth>

==== Sec 3 ====

3ghi

<ifauth admina>

==== Sec 4 ====

4jkl

</ifauth>

==== Sec 5 ====

5mno
+83 −38
Original line number Diff line number Diff line
@@ -15,21 +15,46 @@ require_once(__DIR__ . '/lib/grammar.php');

class syntax_plugin_ifauthex extends DokuWiki_Syntax_Plugin
{
    private $_doRender = null;
    /** @inheritDoc */
    public function getType()
    {
        return 'formatting';
    }

    public function getType() { return 'formatting'; }
    /** @inheritDoc */
    function getAllowedTypes()
    {
        return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs', 'baseonly');
    }

    /** @inheritDoc */
    function getPType()
    {
        return 'stack';
    }

    public function getSort() { return 350; }
    /** @inheritDoc */
    public function getSort()
    {
        return 195;
    }

    public function connectTo($mode) {
    /** @inheritDoc */
    public function connectTo($mode)
    {
        $this->Lexer->addEntryPattern('<ifauth\b.*?>(?=.*?</ifauth>)', $mode, 'plugin_ifauthex');
    }

    public function postConnect() {
    /** @inheritDoc */
    public function postConnect()
    {
        $this->Lexer->addExitPattern('</ifauth>', 'plugin_ifauthex');
    }

    public function handle($match, $state, $pos, Doku_Handler $handler) {
    /** @inheritDoc */
    public function handle($match, $state, $pos, Doku_Handler $handler)
    {
        global $conf;
        switch ($state) {
            case DOKU_LEXER_ENTER:
                $matches = null;
@@ -42,47 +67,67 @@ class syntax_plugin_ifauthex extends DokuWiki_Syntax_Plugin
                    return array($state, $matches[count($matches) - 1]);
                }
                return array($state, null);
            case DOKU_LEXER_MATCHED:
                break;
            case DOKU_LEXER_UNMATCHED:
                return array($state, $match);
                break;
            case DOKU_LEXER_EXIT:
                return array($state, null);
                break;
        }
        return array();
        return false;
    }

    public function render($mode, Doku_Renderer $renderer, $data) {
        if ($mode == 'xhtml') {
    /** @inheritDoc */
    public function render($mode, Doku_Renderer $renderer, $data)
    {
        list($state, $exprOrMatch) = $data;

        // never cache
        $renderer->nocache();

        switch ($state) {
            case DOKU_LEXER_ENTER:
                if ($exprOrMatch === null) {
                    // something went wrong
                    return false;
                }
                try {
                    // check if current user should see the content
                    $exprOrMatch = auth_expr_parse($exprOrMatch);
                        $this->_doRender = $exprOrMatch->evaluate();
                    $shouldRender = (bool) $exprOrMatch->evaluate();
                    if(!$shouldRender) {
                        // point the renderer's doc to something else, remembering the old one
                        $renderer->meta['ifauthex.originalDoc'] = &$renderer->doc;
                        $ignoredDoc = is_array($renderer->doc) ? [] : '';
                        $renderer->doc = &$ignoredDoc;

                        // do the same for the toc list
                        $renderer->meta['ifauthex.originalToc'] = &$renderer->toc;
                        $ignoredToc = [];
                        $renderer->toc = &$ignoredToc;

                        $renderer->meta['ifauthex.isDiverted'] = true;

                    }
                } catch (Exception $e) {
                        $this->_doRender = null;
                    // something went wrong parsing the expression
                    msg(hsc($e->getMessage()), -1);
                    return false;
                }
                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.
                        $output = preg_replace('/^\s*<p>\s*/i', '', $output);
                        $output = preg_replace('/\s*<\/p>\s*$/i', '', $output);
                        $renderer->doc .= $output;
                    }
                    $renderer->nocache();
                $renderer->cdata($exprOrMatch);
                break;
            case DOKU_LEXER_EXIT:
                    break;
                // point the renderer's doc and toc back to the original
                if($renderer->meta['ifauthex.isDiverted']) {
                    $renderer->doc = &$renderer->meta['ifauthex.originalDoc'];
                    $renderer->toc = &$renderer->meta['ifauthex.originalToc'];
                    $renderer->meta['ifauthex.isDiverted'] = false;
                }
                break;
        }

        return true;
    }
}