DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:replace
no way to compare when less than two revisions

Differences

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


Previous revision
Next revision
plugin:replace [2011-06-11 19:52] – [Integration with txt-manager-plugin] 84.57.51.19
Line 1: Line 1:
 +====== replace plugin ======
 +
 +---- plugin ----
 +description: Replaces words with DokuWiki snippets
 +author     : James Dempster (letssurf)
 +email      : letssurf@gmail.com
 +type       : Syntax Substition
 +lastupdate : 2009-04-13
 +compatible : 2009-02-14
 +similar    : easyvar, kixovar, fields, macros, textinsert
 +tags       : search, replace
 +
 +downloadurl: http://dwp-forge.googlecode.com/files/replace-2009-04-13.zip
 +----
 +
 +This is my first plugin so please forgive any errors.
 +
 +Comments welcome.
 +
 +
 +===== Download and Installation =====
 +
 +Download and install the plugin using the [[:plugin:plugin|Plugin Manager]] using the following URL. Refer to [[:plugins]] on how to install plugins manually.
 +
 +  * [[http://dwp-forge.googlecode.com/files/replace-2009-04-13.zip|replace-2009-04-13]]
 +
 +
 +===== 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/wiki:plugins',
 +        );
 +    }
 +
 +    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* -- //[[xxx.de|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//
  
plugin/replace.txt · Last modified: 2018-06-05 22:56 by Klap-in

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki