Skip to content

Commit

Permalink
feature: add dokuwiki functions to template parser
Browse files Browse the repository at this point in the history
this commit adds an additional syntax to bureaucracy template parser that introduces simple functions calls:

    @function_name(arg)@

the functions are parsed after the fields replacements. this will work:

    @function(@@some_field@@)@

currently four functions are supported:
  - @curns()@
  - @getns()@
  - @nons()@
  - @p_get_first_heading()@

only functions with one argument are supported

i've found this useful when using bureaucracy together with struct's "Page" field. My use cases:

1. Use the page title in template replacement:

    @p_get_first_heading(@@schema.page@@)

2. In template action as new page name:
    action template tpl:template1 "somens:@nons(@@schema.page@@)@"
  • Loading branch information
Szymon Olewniczak committed Jan 11, 2018
1 parent 286c578 commit 3c63c4d
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions syntax.php
Expand Up @@ -21,13 +21,15 @@ class syntax_plugin_bureaucracy extends DokuWiki_Syntax_Plugin {
private $form_id = 0;
var $patterns = array();
var $values = array();
var $functions = array();

/**
* Prepare some replacements
*/
public function __construct() {
$this->prepareDateTimereplacements();
$this->prepareNamespacetemplateReplacements();
$this->prepareFunctions();
}

/**
Expand Down Expand Up @@ -491,6 +493,17 @@ function replace($input, $strftime = true) {
array($this, 'replacedate'),
$input
);

//run functions
foreach ($this->functions as $name => $callback) {
$pattern = '/@' . preg_quote($name) . '\((.*?)\)@/';
if (is_callable($callback)) {
$input = preg_replace_callback($pattern, function ($matches) use ($callback) {
return call_user_func($callback, $matches[1]);
}, $input);
}
}

return $input;
}

Expand Down Expand Up @@ -543,8 +556,8 @@ function prepareNamespacetemplateReplacements() {
$file = noNS($ID);
$page = strtr($file, $conf['sepchar'], ' ');
$this->values['__formpage_id__'] = $ID;
$this->values['__formpage_ns__'] = curNS($ID);
$this->values['__formpage_curns__'] = getNS($ID);
$this->values['__formpage_ns__'] = getNS($ID);
$this->values['__formpage_curns__'] = curNS($ID);
$this->values['__formpage_file__'] = $file;
$this->values['__formpage_!file__'] = utf8_ucfirst($file);
$this->values['__formpage_!file!__'] = utf8_strtoupper($file);
Expand Down Expand Up @@ -576,4 +589,14 @@ function prepareDateTimereplacements() {
$this->values['__timesec__'] = date('H:i:s');

}

/**
* Functions that can be used after replacements
*/
function prepareFunctions() {
$this->functions['curNS'] = 'curNS';
$this->functions['getNS'] = 'getNS';
$this->functions['noNS'] = 'noNS';
$this->functions['p_get_first_heading'] = 'p_get_first_heading';
}
}

0 comments on commit 3c63c4d

Please sign in to comment.