<?php
/**
 * DokuWiki Plugin reproduce (Syntax Component)
 *
 * @description  :  This plugin allows to reproduce a code marked with a label
 *                  located within the same source page, or in an other source
 *                  page specified by its ID.
 *
 * @requires     :  "Plugin label", writen by Pascal Bihler <bihler@iai.uni-bonn.de>.
 *
 * @sytnax       :  <reproduce LABEL [PAGEID] />
 *                      --> Where LABEL refers to a label name defined with <label NAME> ... </label>.
 *                          --> Where NAME matches the regular expression [a-zA-Z0-9_]+
 *                          --> Where ... refers to the code to reproduce.
 *                      --> Where [PAGEID] (optional) refers to a source page ID
 *                          that matches the regular expression [a-zA-Z0-9_:-]*.
 *                          If PAGEID is omitted, the current page will be used.
 *
 * @license       :  GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
 * @author        :  Patrice Bonneau <patrice.bonneau@live.ca>
 * @lastupdate    :  2012-07-18
 * @compatible    :  2012-01-25 "Angua"
 */

// must be run within Dokuwiki
if (!defined('DOKU_INC'))     die();
if (!defined('DOKU_LF'))      define('DOKU_LF', "\n");
if (!defined('DOKU_TAB'))     define('DOKU_TAB', "\t");
if (!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');

require_once DOKU_PLUGIN.'syntax.php';


class syntax_plugin_reproduce extends DokuWiki_Syntax_Plugin {

    var $LABEL_PATTERN = "[a-zA-Z0-9_]+";
    var $PAGE_ID_PATTERN = "[a-zA-Z0-9_:-]*";


    public function getType()    { return 'substition'; }
    public function getPType()   { return 'normal'; }
    public function getSort()    { return 100; }
    public function getInfo()    {
        return array(
            'author' => 'Patrice Bonneau',
            'email'  => 'patrice.bonneau@live.ca',
            'date'   => '2012-05-18',
            'name'   => 'Reproduce Plugin',
            'desc'   => 'Allows to reproduce a code marked with a label located within the same source page, or in an other source page specified by its ID.',
            'url'    => 'http://www.dokuwiki.org/plugin:reproduce',
        );
    }


    public function connectTo($mode) {
        $this->Lexer->addSpecialPattern('<reproduce\s+' . $this->LABEL_PATTERN . '\s*' . $this->PAGE_ID_PATTERN . '\s*/>', $mode, 'plugin_reproduce');
    }
	

    public function handle($match, $state, $pos, &$handler){
        switch ($state) {
            case DOKU_LEXER_SPECIAL :

                // Find the label name and the optional source page ID in the <reproduce ... /> tag
                if (preg_match('/<reproduce\s+(' . $this->LABEL_PATTERN . ')\s*(' . $this->PAGE_ID_PATTERN . ')\s*\/>/', $match, $matches)) {
                    $labelName = $matches[1];
                    $sourcePageID = $matches[2];

                    if (!empty($sourcePageID)) {
                        $sourcePageContent = io_readfile(wikiFN($sourcePageID));
                    } else {
                        // Load the source code of the current DokuWiki page
                        global $ID;        // This is the current page ID
                        $sourcePageContent = io_readfile(wikiFN($ID));
                    }

                    // To be compatible with the plugin « autonumbering ».
                    // This will make sure to keep the original numbering and not restart
                    // numbering at 1, by doing the numbering before to reproduce the code.
                    $pluginList = plugin_list();
                    if (in_array('autonumbering', $pluginList)) {
                        $autonumbering = new syntax_plugin_autonumbering();
                        $sourcePageContent = $autonumbering->doNumbering($sourcePageContent);
                    }

                    // Find the corresponding <label> and get its content
                    if (preg_match('/<label\s+' . $labelName . '\s*>(.*?)<\/label>/ms', $sourcePageContent, $matches)) {
                        $labelContent = $matches[1];
                    } else {
                        $labelContent = '\\\ 
| @orange:**ERROR !**         | @orange:The source for **%%' . $match . '%%** can\'t be found. |
| @yellow:\\\ **Syntax : **%%<reproduce LABEL [PAGEID] />%% \\\ \\\ --- Where LABEL refers to a label name defined with %%<label NAME> ... </label>%%. \\\ --------- Where NAME is an identifier consisting of letters (a-z, A-Z), number (0-9) and underscore (_). \\\ --------- Where **...** refers to the wikicode to reproduce. \\\ \\\ --- Where [PAGEID] (optional) refers to a source page ID, consisting of letters (a-z, A-Z), number (0-9), underscore (_) and a colon (:). \\\ --------- If PAGEID is omitted, the current page will be used. \\\\ \\\ ||
|  @orange:**Contact your admin.**  ||
\\\ 
\\\ 
';
                    }
                    return array($labelName, $labelContent);
                }
            break;
        }
        return array();
    }


    public function render($mode, &$renderer, $data) {
        if(($mode == 'xhtml') && (!empty($data))) {
            list($labelName, $labelContent) = $data;
            $renderer->doc .= p_render('xhtml', p_get_instructions($labelContent), $info);
            return true;
        }
        return false;
    }
}

// vim:ts=4:sw=4:et:
