1<?php
2/**
3 * DokuWiki Plugin linkpagechild (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Brendan Kidwell <snarf@glump.net>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14class syntax_plugin_linkpagechild extends DokuWiki_Syntax_Plugin
15{
16    /**
17     * @return string Syntax mode type
18     */
19    public function getType()
20    {
21        return 'substition';
22    }
23
24    /**
25     * @return string Paragraph type
26     */
27    public function getPType()
28    {
29        return 'normal';
30    }
31
32    /**
33     * @return int Sort order - Low numbers go before high numbers
34     */
35    public function getSort()
36    {
37        return 299;
38    }
39
40    /**
41     * Connect lookup pattern to lexer.
42     *
43     * @param string $mode Parser mode
44     */
45    public function connectTo($mode)
46    {
47        // match internal links and media links with paths starting with "::"
48        $this->Lexer->addSpecialPattern('\[\[ ?::[^\]]+\]\]', $mode, 'plugin_linkpagechild');
49        $this->Lexer->addSpecialPattern('\{\{ ?::[^\}]+\}\}', $mode, 'plugin_linkpagechild');
50    }
51
52    /**
53     * Handle matches of the linkpagechild syntax
54     *
55     * @param string       $match   The match of the syntax
56     * @param int          $state   The state of the handler
57     * @param int          $pos     The position in the document
58     * @param Doku_Handler $handler The handler
59     *
60     * @return array Data for the renderer
61     */
62    public function handle($match, $state, $pos, Doku_Handler $handler)
63    {
64        // Rewrite "::" at start of target path to be the namespace given by the current page ID.
65        $id = pageinfo()['id'];
66        preg_match('/(\[\[|\{\{)( ?)::(.*)/', $match, $out);
67        $rewritten = $out[1] . $out[2] . ":$id:" . $out[3];
68        //var_dump($id);print("<br />\n");var_dump($rewritten);exit(0);
69        if($out[1] == '[[') {
70            // Call core internal link handler.
71            $handler->internallink($rewritten, $state, $pos);
72        } else {
73            // Call core media link handler.
74            $handler->media($rewritten, $state, $pos);
75        }
76        return [];
77    }
78
79    /**
80     * Render xhtml output or metadata
81     *
82     * @param string        $mode     Renderer mode (supported modes: xhtml)
83     * @param Doku_Renderer $renderer The renderer
84     * @param array         $data     The data from the handler() function
85     *
86     * @return bool If rendering was successful.
87     */
88    public function render($mode, Doku_Renderer $renderer, $data)
89    {
90        // The core should be rendering our output, so we do nothing here.
91        return false;
92    }
93}
94
95