DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:replace

replace plugin

Compatible with DokuWiki

2009-02-14

plugin Replaces words with DokuWiki snippets

Last updated on
2009-04-13
Provides
Syntax

This extension has not been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues.

Similar to easyvar, fields, kixovar, macros, textinsert

Tagged with replace, search

This is my first plugin so please forgive any errors.

Comments welcome.

Download and Installation

Download and install the plugin using the Plugin Manager using the following URL. Refer to plugins on how to install plugins manually.

Version History

2009-04-13

  • Fixed compatibility issue with DokuWiki 2009-02-14.

2008-01-31

  • First release.

Source Code

<?php
/**
 * Plugin Replace: Replaces words with DokuWiki snippets
 *
 * Create replace.conf file and populate with words and snippets like in acronyms
 * e.g.
 *
 * HelloWorld **Hello World**
 * This example would replace HelloWorld with bold Hello World
 *
 * HTTPS [[wp>HTTPS]]
 *
 * This example would replace words HTTPS with a Wikipedia link to HTTPS
 *
 * @url        http://www.jdempster.com/
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     James Dempster <letssurf@gmail.com>
 */
 
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
 
class Syntax_Plugin_Replace extends DokuWiki_Syntax_Plugin {
 
    function getInfo(){
        return array(
            'author' => 'James Dempster',
            'email'  => 'letssurf@gmail.com',
            'date'   => '2008-01-31',
            'name'   => 'Replacer',
            'desc'   => 'Replaces words thought DokuWiki automatically from replace.conf with the snippet defined.',
            'url'    => 'http://www.dokuwiki.org/plugin:replace',
        );
    }
 
    function getType(){ return 'substition'; }
    function getAllowedTypes() {
        return array('container','substition','protected','disabled','formatting','paragraphs');
    }
 
    function getSort() {
        return 999;
    }
 
    function Syntax_Plugin_Replace() {
        $this->replace = confToHash(DOKU_CONF.'replace.conf');
    }
 
    function preConnect() {
        if(!count($this->replace)) return;
        $replacers = array_map('preg_quote', array_keys($this->replace));
        $this->pattern = '\b'.join('|',$replacers).'\b';
    }
 
    function connectTo($mode) {
        if(!count($this->replace)) return;
        if(strlen($this->pattern) > 0) {
            $this->Lexer->addSpecialPattern($this->pattern, $mode, 'plugin_replace');
        }
    }
 
    function handle($match, $state, $pos, &$handler){
        switch($state) {
            case DOKU_LEXER_SPECIAL :
                $this->Lexer->parse($this->replace[$match]);
                break;
        }
        return array();
    }
 
    function render($mode, &$renderer, $data) {
        return true;
    }
}
?>

Comments

Thank you very much for this plugin. For me it has been extremely helpful. (For beginners: Copy the PHP code into dokuwiki/lib/plugins/replace/syntax.php and create the file dokuwiki/conf/replace.conf with the substitutions. Use the %% markup to substitute plain text)

Integration with confmanager-plugin

To modify replace.conf via admin-menu install confmanager-plugin and modify following lines:

admn.php (line 9): add “replace”

    var $cnffiles = array('acronyms','entities','interwiki','mime','smileys','replace'); // add conf-files here; must be filename

lang.php (for german)

$lang['cnf_replace']   = 'Replace';
$lang['head_replace']   = 'Replace';
$lang['text_replace']   = 'Hier sind die Einstellungen f&uuml;r die Replace-Plugin-Ersetzung. F&uuml;r genauere Informationen besuchen sie <a class="interwiki iw_doku" href="http://www.dokuwiki.org/plugin:replace">Replace-Plugin</a>';

Feature suggestions

  • Please add mutiple words support - I would like to hack it but I don't get it *arg* – Christian 2009/12/10 13:05
  • Any way to do the replace within a source line? I would like to do the substitution for copy-pasting into multiple conf files. I guess I'm looking to also include the 'protected' + 'substition' modes.



Replacement text is not shown

In Version DokuWiki-2008-05-05 this tag don´t work for me.
The keywords (from conf/replace.conf) are found in text, but the replacer isn't shown.

E.g. -
replace.conf:
little big
red green

Text:

My little car is red. 

My replace result:

My  car is . 

The right replace result:

My big car is green. 


I don´t know the trick to get it work.

Knut


I second this statement, I cannot figure out why it does not work. Using same version of DokuWiki…

Too bad :-(


The Same here - no function as in the big green car example :( Using Debian 5.0.1, DokuWiki 0.0.20080505-4, php5 5.2.6.dfsg.1-1+lenny2 …. *sad* — Christian 2009/04/12 11:53


Fixed in version 2009-04-13. No extensive testing, I was just looking for a similar feature for another plugin and got curious if it's possible to make this one to work. Seems so. — Mykola Ostrovskyy 2009/04/13 19:41


Working 2011-01-23. replace.conf needs double % before and after replacement text. (sry example replace.conf breaks page formatting). original

 start brc  Brc bRc  brC end \\  
 start haccp ifs ifs6 brc5 end

replaced

start BRC BRC bRc brC end
start HACCP IFS IFS6 BRC5 end

Plugin (2009-04-13) fails at line 74

I get the following message upon detection of a word from replace.conf:

Fatal error: Call to a member function on a non-object in /dokuwiki-2009-02-14/lib/plugins/replace/syntax.php on line 74

Is this a problem with the PHP version? (My host uses PHP 4.4.7.) — Holger Doessing 2009/05/07 13:09


Yep. Passing objects around in PHP 4 is a real pain — forget one reference operator and now you have to deal with a clone. Unfortunately it cannot be fixed within plugin. If you feel adventurous you can fix it in inc/parserutils.php, line 471:

<                    'obj'  => $obj,
---
>                    'obj'  => &$obj,

It did the trick at my place, but I have no idea if it will break something else.

Or better yet, send this link to your hosting provider ;-)Mykola Ostrovskyy 2009/05/09 18:18

It also worked for me, thanks Mykola! (Maybe inc/parserutils.php could be revised with this simple change to help out those of us that are still stuck with PHP4?)
if (version_compare(PHP_VERSION, '5.0.0', '<')) {
  $modes[] = array(
    'sort' => $obj->getSort(),
    'mode' => "plugin_$p",
    'obj'  => &$obj,
  );
} else {
  $modes[] = array(
    'sort' => $obj->getSort(),
    'mode' => "plugin_$p",
    'obj'  => $obj,
  );
};

Holger Doessing 2009/05/11 16:50

Bugfix for special chars (2012/23/01)

<?php
 
function preConnect() {
    if(!count($this->replace)) return;
    $replacers = array_map('preg_quote', array_keys($this->replace));
    // removed '\b' at beginning and end. Had problems to replace "@cloud." with "~~TAGCLOUD~~"
    $this->pattern = join('|', $replacers);
}

Robert Jäckel

plugin/replace.txt · Last modified: 2014-05-11 18:38 by ach