Table of Contents
regex template Plugin
Compatible with DokuWiki
2009-02-14b
This extension has not been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues.
Extension name contains underscore, will not generate popularity points.
This plugin works similar to namespace templates but the template to use is chosen based on a configuration matching regular expressions against the page name.
Download and Installation
Search and install the plugin using the Extension Manager. Refer to Plugins on how to install plugins manually. If installed manually, rename the plugin directory to regextemplate
(no underscore).
Source
- action.php
<?php /** * Regex Template plugin, Choose template from regex on $ID * @author Cédric Villemain <cedric.villemain@dalibo.com> */ if(!defined('DOKU_INC')) die(); if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); require_once(DOKU_PLUGIN.'action.php'); class action_plugin_regextemplate extends DokuWiki_Action_Plugin { /** * return some info */ function getInfo(){ return confToHash(dirname(__FILE__).'/info.txt'); } /** * register the eventhandlers */ function register(&$contr){ $contr->register_hook('COMMON_PAGETPL_LOAD', 'BEFORE', $this, 'regextemplate', array()); } function regextemplate(&$event, $param) { global $conf; $array_regex = (array)explode("\n",$conf['plugin']['regextemplate']['reg_tpl_regex']); reset($array_regex); foreach ($array_regex as $regex) { list($pattern, $replacement) = explode(',', $regex); $my_new_ID = @preg_replace($pattern, $replacement, $event->data['id']); $my_new_file = wikiFN($my_new_ID); if(@file_exists($my_new_file)){ $tpl = io_readFile($my_new_file); break; } } if($tpl) $event->data['tpl'] = $tpl; if(!$tpl) return ''; // replace placeholders $tpl = str_replace('@ID@',$ID,$tpl); $tpl = str_replace('@NS@',getNS($ID),$tpl); $tpl = str_replace('@PAGE@',strtr(noNS($ID),'_',' '),$tpl); $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl); $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl); $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl); $tpl = str_replace('@DATE@',$conf['dformat'],$tpl); // we need the callback to work around strftime's char limit $tpl = preg_replace_callback('/%./',create_function('$m','return strftime($m[0]);'),$tpl); $event->result=$tpl; $event->preventDefault(); } }
Adjust setting
There is one configuration option to add regex, they must be added one per line, take care of spaces, in the following format:
pattern,replacement
pattern and replacement MUST follow the preg_replace rules.
Example
/.*:info:start/,templates:info_start /.*:info:.*:issue/,templates:info_issue
You here just have to edit the templates/info_start and templates/info_issue to make them templates (no worry of _template.txt and __template.txt they are used if the regex does not match or if the templates in the regex does not exists.
Discussion
NS replace does not work, $id needs to be replaced by $ID (line 65 in action.php)
// replace placeholders $tpl = str_replace('@ID@',$ID,$tpl); $tpl = str_replace('@NS@',getNS($ID),$tpl);
Otherwise great plugin. Jörg
The plugin does not work with rc2010-10-27 “Busy Wednesday”, for me this works:
/** * register the eventhandlers */ function register(&$contr){ $contr->register_hook('COMMON_PAGE_FROMTEMPLATE', 'BEFORE', $this, 'regex_template', array()); } function regex_template(&$event, $param) { global $conf; $array_regex = (array)explode("\n",$conf['plugin']['regex_template']['reg_tpl_regex']); reset($array_regex); foreach ($array_regex as $regex) { list($pattern, $replacement) = explode(',', $regex); $my_new_ID = @preg_replace($pattern, $replacement, $event->data['id']); $my_new_file = wikiFN($my_new_ID); if(@file_exists($my_new_file)){ $tpl = io_readFile($my_new_file); break; } } if($tpl) $event->data['tpl'] = $tpl; } }
(from lahondes at sibio.fr) This work with latest version using the code just above and modifying the hook (which was changed in 2011-02-03), using COMMON_PAGETPL_LOAD instead of COMMON_PAGE_FROMTEMPLATE. This give the following version of action.php:
/** * register the eventhandlers */ function register(&$contr){ $contr->register_hook('COMMON_PAGETPL_LOAD', 'BEFORE', $this, 'regex_template', array()); } function regex_template(&$event, $param) { global $conf; $array_regex = (array)explode("\n",$conf['plugin']['regex_template']['reg_tpl_regex']); reset($array_regex); foreach ($array_regex as $regex) { list($pattern, $replacement) = explode(',', $regex); $my_new_ID = @preg_replace($pattern, $replacement, $event->data['id']); $my_new_file = wikiFN($my_new_ID); if(@file_exists($my_new_file)){ $tpl = io_readFile($my_new_file); break; } } if($tpl) $event->data['tpl'] = $tpl; } }