1<?php
2/**
3 * Plugin Replace: Replaces words with DokuWiki snippets
4 *
5 * Create replace.conf file and populate with words and snippets like in acronyms
6 * e.g.
7 *
8 * HelloWorld **Hello World**
9 * This example would replace HelloWorld with bold Hello World
10 *
11 * HTTPS [[wp>HTTPS]]
12 *
13 * This example would replace words HTTPS with a Wikipedia link to HTTPS
14 *
15 * @url        http://www.jdempster.com/
16 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
17 * @author     James Dempster <letssurf@gmail.com>
18 */
19
20if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
21if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
22require_once(DOKU_PLUGIN.'syntax.php');
23
24class Syntax_Plugin_Replace extends DokuWiki_Syntax_Plugin {
25
26    function getInfo() {
27        return array(
28            'author' => 'James Dempster',
29            'email'  => 'letssurf@gmail.com',
30            'date'   => '2009-04-13',
31            'name'   => 'Replacer',
32            'desc'   => 'Replaces words thought DokuWiki automaticly from replace.conf with the snippet defined.',
33            'url'    => 'http://www.dokuwiki.org/wiki:plugins',
34        );
35    }
36
37    function getType() { return 'substition'; }
38
39    function getAllowedTypes() {
40        return array('container','substition','protected','disabled','formatting','paragraphs');
41    }
42
43    function getSort() {
44        return 999;
45    }
46
47    function Syntax_Plugin_Replace() {
48        $this->nesting = false;
49        $this->replace = confToHash(DOKU_CONF . 'replace.conf');
50    }
51
52    function preConnect() {
53        if(!count($this->replace)) return;
54        $replacers = array_map('preg_quote', array_keys($this->replace));
55        $this->pattern = '\b' . join('|', $replacers) . '\b';
56    }
57
58    function connectTo($mode) {
59        if(!count($this->replace)) return;
60        if(strlen($this->pattern) > 0) {
61            $this->Lexer->addSpecialPattern($this->pattern, $mode, 'plugin_replace');
62        }
63    }
64
65    function handle($match, $state, $pos, &$handler) {
66        if ($this->nesting) {
67            $handler->_addCall('cdata', array($match), $pos);
68        }
69        else {
70            $this->nesting = true;
71            $nestedWriter = & new Doku_Handler_Nest($handler->CallWriter);
72            $handler->CallWriter = & $nestedWriter;
73
74            $this->Lexer->parse($this->replace[$match]);
75
76            $nestedWriter->process();
77            $handler->CallWriter = & $nestedWriter->CallWriter;
78            $handler->calls[count($handler->calls) - 1][2] = $pos;
79            $this->nesting = false;
80        }
81        return false;
82    }
83
84    function render($mode, &$renderer, $data) {
85        return true;
86    }
87}
88