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

Merge pull request #3 from cosmocode/master

Fix sections and section editing
parents 2b2c6c84 2584271a
Pipeline #630 failed with stage
in 59 seconds
<?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;
}
}
[
["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", []]
]
==== Sec 1 ====
1abc
<ifauth admin>
==== Sec 2 ====
2def
</ifauth>
==== Sec 3 ====
3ghi
<ifauth admina>
==== Sec 4 ====
4jkl
</ifauth>
==== Sec 5 ====
5mno
......@@ -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');
}
public function getSort() { return 350; }
/** @inheritDoc */
function getPType()
{
return 'stack';
}
public function connectTo($mode) {
/** @inheritDoc */
public function getSort()
{
return 195;
}
/** @inheritDoc */
public function connectTo($mode)
{
$this->Lexer->addEntryPattern('<ifauth\b.*?>(?=.*?</ifauth>)', $mode, 'plugin_ifauthex');
}
public function postConnect() {
$this->Lexer->addExitPattern('</ifauth>', 'plugin_ifauthex');
/** @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') {
list($state, $exprOrMatch) = $data;
switch ($state) {
case DOKU_LEXER_ENTER:
if ($exprOrMatch === null) {
return false;
}
try {
$exprOrMatch = auth_expr_parse($exprOrMatch);
$this->_doRender = $exprOrMatch->evaluate();
} catch (Exception $e) {
$this->_doRender = null;
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;
/** @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);
$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;
}
$renderer->nocache();
break;
case DOKU_LEXER_EXIT:
break;
}
} catch (Exception $e) {
// something went wrong parsing the expression
msg(hsc($e->getMessage()), -1);
return false;
}
break;
case DOKU_LEXER_UNMATCHED:
$renderer->cdata($exprOrMatch);
break;
case DOKU_LEXER_EXIT:
// 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;
}
}
......
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