1<?php
2/**
3 * DokuWiki Plugin articlelinks (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Michael Große <dokuwiki@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12class syntax_plugin_articlelinks extends DokuWiki_Syntax_Plugin {
13    /**
14     * @return string Syntax mode type
15     */
16    public function getType() {
17        return 'formatting';
18    }
19    /**
20     * @return string Paragraph type
21     */
22    public function getPType() {
23        return 'block';
24    }
25    /**
26     * @return int Sort order - Low numbers go before high numbers
27     */
28    public function getSort() {
29        return 81;
30    }
31
32    /**
33     * Connect lookup pattern to lexer.
34     *
35     * @param string $mode Parser mode
36     */
37    public function connectTo($mode) {
38        $this->Lexer->addSpecialPattern('<(?:relatedsection|relatedarticle|relatedarticles|mainarticle)>.*?</(?:article|section)>',$mode,'plugin_articlelinks');
39    }
40
41    /**
42     * Handle matches of the articlelinks syntax
43     *
44     * @param string $match The match of the syntax
45     * @param int    $state The state of the handler
46     * @param int    $pos The position in the document
47     * @param Doku_Handler    $handler The handler
48     * @return array Data for the renderer
49     */
50    public function handle($match, $state, $pos, Doku_Handler $handler){
51        $data = array();
52        $type = substr($match,1,strpos($match, '>')-1);
53
54        switch ($type) {
55            case 'relatedarticles':
56                $links = $this->getLang('related articles');
57                $endtag = '</article>';
58                break;
59            case 'relatedarticle':
60                $links = $this->getLang('related article');
61                $endtag = '</article>';
62                break;
63            case 'mainarticle':
64                $links = $this->getLang('main article');
65                $endtag = '</article>';
66                break;
67            case 'relatedsection':
68                $links = $this->getLang('related section');
69                $endtag = '</section>';
70                break;
71            default:
72                $links = '';
73        }
74
75        $links .= substr($match,strpos($match, '>') + 1 , strpos($match, $endtag) - strpos($match, '>') -1);
76        $data['links'] =  p_get_instructions($links);
77        return $data;
78    }
79
80    /**
81     * Render xhtml output or metadata
82     *
83     * @param string         $mode      Renderer mode (supported modes: xhtml)
84     * @param Doku_Renderer  $renderer  The renderer
85     * @param array          $data      The data from the handler() function
86     * @return bool If rendering was successful.
87     */
88    public function render($mode, Doku_Renderer $renderer, $data) {
89        if($mode != 'xhtml') return false;
90
91        $renderer->doc .= '<div class="mainarticle">';
92        $info = array();
93        $renderer->doc .= p_render('xhtml', $data['links'], $info);
94        $renderer->doc .= '</div>';
95
96        return true;
97    }
98}
99
100// vim:ts=4:sw=4:et:
101