DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:replace

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Both sides next revision
plugin:replace [2009-05-09 18:32]
panone Added reply to the missing object discussion
plugin:replace [2018-06-05 22:56]
Klap-in [Download and Installation]
Line 1: Line 1:
 +====== replace Plugin ======
  
 +---- plugin ----
 +description: Replaces words with DokuWiki snippets
 +author     : James Dempster (letssurf)
 +email      : letssurf@gmail.com
 +type       : syntax
 +lastupdate : 2009-04-13
 +compatible : 2009-02-14
 +similar    : easyvar, kixovar, fields, macros, textinsert
 +tags       : search, replace
 +
 +downloadurl: https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/dwp-forge/replace-2009-04-13.zip
 +bugtracker : 
 +sourcerepo : 
 +donationurl: 
 +
 +screenshot_img: 
 +----
 +
 +This is my first plugin so please forgive any errors.
 +
 +Comments welcome.
 +
 +===== Download and Installation =====
 +
 +Search and install the plugin using the [[plugin:extension|Extension Manager]]. Refer to [[:Plugins]] on how to install plugins manually.
 +
 +  * [[https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/dwp-forge/replace-2009-04-13.zip|replace-2009-04-13.zip]]
 +
 +===== Version History =====
 +
 +==== 2009-04-13 ====
 +
 +  * Fixed compatibility issue with DokuWiki 2009-02-14.
 +
 +==== 2008-01-31 ====
 +
 +  * First release.
 +
 +===== Source Code =====
 +
 +<code php>
 +<?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;
 +    }
 +}
 +?>
 +</code>
 +
 +===== 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|confmanager-plugin]] and modify following lines:
 +
 +admn.php (line 9): add "replace"
 +<code php>
 +    var $cnffiles = array('acronyms','entities','interwiki','mime','smileys','replace'); // add conf-files here; must be filename
 +</code>
 +
 +lang.php (for german)
 +<code css>
 +$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>';
 +</code>
 +
 +==== 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: 
 +> <code>
 +little big
 +red green
 +</code>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*  --- //[[xxx.de|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. --- //[[spambox03@mail.ru|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
 +<file>
 + start brc  Brc bRc  brC end \\  
 + start haccp ifs ifs6 brc5 end
 +</file>
 +replaced
 +<file>
 +start BRC BRC bRc brC end
 +start HACCP IFS IFS6 BRC5 end
 +</file>
 +
 +==== 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.net|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:
 +
 +<code php>
 +<                    'obj'  => $obj,
 +---
 +>                    'obj'  => &$obj,
 +</code>
 +
 +It did the trick at my place, but I have no idea if it will break something else.
 +
 +Or better yet, send [[http://www.php.net/releases/#v4|this link]] to your hosting provider ;-)  --- //[[spambox03@mail.ru|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?)
 +><code php>
 +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,
 +  );
 +};
 +</code> --- //[[holger@doessing.net|Holger Doessing]] 2009/05/11 16:50//
 +
 +
 +==== Bugfix for special chars (2012/23/01) ====
 +
 +<code php>
 +<?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);
 +}
 +
 +</code>
 +[[robert.jaeckel@verwaltung.uni-halle.de|Robert Jäckel]]
plugin/replace.txt · Last modified: 2018-06-05 22:56 by Klap-in