Skip to content

Add parameter support #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 84 additions & 1 deletion helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function getMethods() {
* Overrides standard values for showfooter and firstseconly settings
*/
function get_flags($setflags) {

// load defaults
$flags = $this->defaults;
foreach ($setflags as $flag) {
Expand Down Expand Up @@ -220,6 +221,10 @@ function get_flags($setflags) {
case 'exclude':
$flags['exclude'] = $value;
break;
case 'parameters':
case 'params':
$flags['parameters'] = $this->_parse_parameter_string($value);
break;
}
}
// the include_content URL parameter overrides flags
Expand All @@ -228,6 +233,38 @@ function get_flags($setflags) {
return $flags;
}

/**
* Parse parameter flag value into array, to create 'dictionary' of replace values.
*/
function _parse_parameter_string($value) {

$param_array = array();

$count = 0;
$value = preg_split('/(?<!\\\\)\\|/', $value);
foreach($value as $param) {
if (preg_match('/(?<!\\\\)=/', $param)) {
// There's one '=' character: This is named parameter.
list($name, $value) = preg_split('/(?<!\\\\)=/', $param, 2);
$param_array[$name] = $this->_escape_value($value);
} else {
// There's no '=' character: This is unnamed parameter.
$count += 1;
$param_array[strval($count)] = $this->_escape_value($param);
}
}

return $param_array;
}

/**
* Escapes '\|', '\&', '\=', '\}' in parameter value.
*/
function _escape_value($value) {
return preg_replace('/\\\\([|&=}])/', '\1', $value);
}


/**
* Returns the converted instructions of a give page/section
*
Expand Down Expand Up @@ -314,7 +351,10 @@ function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $inc

$this->adapt_links($ins, $page, $included_pages);

for($i=0; $i<$num; $i++) {
for ($i=0; $i<$num; $i++) {
if (!isset($ins[$i][0])) {
continue;
}
switch($ins[$i][0]) {
case 'document_start':
case 'document_end':
Expand Down Expand Up @@ -369,6 +409,47 @@ function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $inc
if (!$flags['inline'] && $flags['indent'])
$ins[$i][1][1][4] += $lvl;
break;
case 'include_placeholder':
if (!isset($flags['parameters']))
break;

if (array_key_exists($ins[$i][1][1][0], $flags['parameters'])) {

// Call dokuwiki parser to get instructions of included text.
$included_ins = p_get_instructions($flags['parameters'][$ins[$i][1][1][0]]);

// Get starting and ending position
for ($start=0; $start<count($included_ins); $start++) {
if ($included_ins[$start][0] == "p_open") {
break;
}
}

for ($end=$start; $end<count($included_ins); $end++) {
if ($included_ins[$end][0] == "p_close") {
break;
}
}

$included_ins = array_slice($included_ins, $start+1, $end-$start-1);

# Before inserting instructions, we have to move instructions after this.
$ins_count = count($ins);
for ($j=$i+1; $j<$ins_count; $j++) {
$ins[$j+count($included_ins)-1] = $ins[$j];
}

# Insert instructions and move pointer.
for($j=0; $j<count($included_ins); $j++) {
$ins[$i+$j] = $included_ins[$j];
}

$i += $j;
$num += $j;

}

break;
/*
* if there is already a closelastsecedit instruction (was added by one of the section
* functions), store its position but delete it as it can't be determined yet if it is needed,
Expand All @@ -386,6 +467,8 @@ function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $inc
}
}

ksort($ins, SORT_NUMERIC);

// calculate difference between header/section level and include level
$diff = 0;
if (!isset($lvl_max)) $lvl_max = 0; // if no level found in target, set to 0
Expand Down
2 changes: 2 additions & 0 deletions syntax/footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ function html_footer($page, $sect, $sect_title, $flags, $footer_lvl, &$renderer)
'target' => $conf['target']['wiki'],
'class' => $class . ' permalink',
'more' => 'rel="bookmark"',
'pre' => '',
'suf' => ''
);
$xhtml[] = $renderer->_formatLink($link);
}
Expand Down
2 changes: 1 addition & 1 deletion syntax/include.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function handle($match, $state, $pos, Doku_Handler $handler) {
$check = false;
if (isset($sect)) $sect = sectionID($sect, $check);
$level = NULL;
return array($mode, $page, $sect, explode('&', $flags), $level, $pos);
return array($mode, $page, $sect, explode('(?<!\\)&', $flags), $level, $pos);
}

/**
Expand Down
61 changes: 61 additions & 0 deletions syntax/placeholder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Include Plugin parameter
*
* @author github @alexdraconian
*/

/**
* All DokuWiki plugins to extend the parser/rendering mechanism
* need to inherit from this class
*/
class syntax_plugin_include_placeholder extends DokuWiki_Syntax_Plugin {

/** @var $helper helper_plugin_include */
var $helper = null;

/**
* Get syntax plugin type.
*
* @return string The plugin type.
*/
function getType() { return 'substition'; }

/**
* Get sort order of syntax plugin.
*
* @return int The sort order.
*/
function getSort() { return 300; }

/**
* Connect patterns/modes
*
* @param $mode mixed The current mode
*/
function connectTo($mode) {
$this->Lexer->addSpecialPattern("{{{.+?}}}", $mode, 'plugin_include_placeholder');
}

/**
* Handle syntax matches
*
* @param string $match The current match
* @param int $state The match state
* @param int $pos The position of the match
* @param Doku_Handler $handler The hanlder object
* @return array The instructions of the plugin
*/
function handle($match, $state, $pos, Doku_handler $handler) {
$name = substr($match, 3, -3); // strip markup
return array($name, $pos);
}

/**
* Skip rendering of template field.
*/
function render($format, Doku_Renderer $renderer, $data) {
return true;
}

}