Commit 2584271a authored by annda's avatar annda Committed by GitHub

Merge pull request #2 from cosmocode/instructiontest

Another go at fixing this plugin
parents 171b7e37 bf6ba64e
<?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,12 +15,6 @@ require_once(__DIR__ . '/lib/grammar.php');
class syntax_plugin_ifauthex extends DokuWiki_Syntax_Plugin
{
/** @var null|string The original document */
protected $originalDoc = null;
/** @var bool is the current document diverted? */
protected $isDiverted = false;
/** @inheritDoc */
public function getType()
{
......@@ -30,7 +24,7 @@ class syntax_plugin_ifauthex extends DokuWiki_Syntax_Plugin
/** @inheritDoc */
function getAllowedTypes()
{
return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs');
return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs', 'baseonly');
}
/** @inheritDoc */
......@@ -42,7 +36,7 @@ class syntax_plugin_ifauthex extends DokuWiki_Syntax_Plugin
/** @inheritDoc */
public function getSort()
{
return 158;
return 195;
}
/** @inheritDoc */
......@@ -55,7 +49,6 @@ class syntax_plugin_ifauthex extends DokuWiki_Syntax_Plugin
public function postConnect()
{
$this->Lexer->addExitPattern('</ifauth>', 'plugin_ifauthex');
$this->Lexer->addPattern('[ \t]*={2,}[^\n]+={2,}[ \t]*(?=\n)', 'plugin_ifauthex');
}
/** @inheritDoc */
......@@ -75,20 +68,6 @@ class syntax_plugin_ifauthex extends DokuWiki_Syntax_Plugin
}
return array($state, null);
case DOKU_LEXER_MATCHED:
// source of the following solution: plugin wrap
// we have a == header ==, use the core header() renderer
// (copied from core header() in inc/parser/handler.php)
$title = trim($match);
$level = 7 - strspn($title,'=');
if($level < 1) $level = 1;
$title = trim($title,'=');
$title = trim($title);
$handler->_addCall('header',array($title,$level,$pos), $pos);
// close the section edit the header could open
if ($title && $level <= $conf['maxseclevel']) {
$handler->addPluginCall('ifauthex_closesection', array(), DOKU_LEXER_SPECIAL, $pos, '');
}
break;
case DOKU_LEXER_UNMATCHED:
return array($state, $match);
......@@ -118,10 +97,17 @@ class syntax_plugin_ifauthex extends DokuWiki_Syntax_Plugin
$shouldRender = (bool) $exprOrMatch->evaluate();
if(!$shouldRender) {
// point the renderer's doc to something else, remembering the old one
$this->originalDoc = &$renderer->doc;
$ignoredDoc = '';
$renderer->meta['ifauthex.originalDoc'] = &$renderer->doc;
$ignoredDoc = is_array($renderer->doc) ? [] : '';
$renderer->doc = &$ignoredDoc;
$this->isDiverted = true;
// 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) {
// something went wrong parsing the expression
......@@ -133,9 +119,11 @@ class syntax_plugin_ifauthex extends DokuWiki_Syntax_Plugin
$renderer->cdata($exprOrMatch);
break;
case DOKU_LEXER_EXIT:
// point the renderer's doc back to the original
if($this->isDiverted) {
$renderer->doc = &$this->originalDoc;
// 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;
}
......
<?php
/**
* Copy of syntax_plugin_wrap_closesection
* Used for special handling of headers
*/
class syntax_plugin_ifauthex_closesection extends DokuWiki_Syntax_Plugin
{
function getType() { return 'substition';}
function getPType() { return 'block';}
function getSort() { return 195; }
/**
* Dummy handler
*/
function handle($match, $state, $pos, Doku_Handler $handler)
{}
/**
* Create output
*/
function render($mode, Doku_Renderer $renderer, $indata)
{
if($mode == 'xhtml'){
/** @var Doku_Renderer_xhtml $renderer */
$renderer->finishSectionEdit();
return true;
}
return false;
}
}
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