1<?php
2/**
3 * DokuWiki Plugin newsticker (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Michael Große <grosse@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12class syntax_plugin_newsticker_ticker extends DokuWiki_Syntax_Plugin {
13    /**
14     * @return string Syntax mode type
15     */
16    public function getType() {
17        return 'container';
18    }
19
20    /**
21     * @return string Paragraph type
22     */
23    public function getPType() {
24        return 'block';
25    }
26    /**
27     * @return int Sort order - Low numbers go before high numbers
28     */
29    public function getSort() {
30        return 100;
31    }
32
33    /**
34     * Connect lookup pattern to lexer.
35     *
36     * @param string $mode Parser mode
37     */
38    public function connectTo($mode) {
39        $this->Lexer->addEntryPattern('<newsticker>',$mode,'plugin_newsticker_ticker');
40    }
41
42    public function postConnect() {
43        $this->Lexer->addExitPattern('</newsticker>','plugin_newsticker_ticker');
44    }
45
46    /**
47     * Handle matches of the newsticker syntax
48     *
49     * @param string $match The match of the syntax
50     * @param int    $state The state of the handler
51     * @param int    $pos The position in the document
52     * @param Doku_Handler    $handler The handler
53     * @return array Data for the renderer
54     */
55    public function handle($match, $state, $pos, Doku_Handler $handler){
56        if ($state !== 3) {
57            return array();
58        }
59        $data = array();
60        $lines = explode("\n",$match);
61        $lines = $this->cleanData($lines);
62        foreach ($lines as $newsItem) {
63            $instructions = p_get_instructions($newsItem);
64            $render = p_render('xhtml',$instructions, $info);
65            $render = trim($render);
66            $render = ltrim($render,'<p>');
67            $render = rtrim($render,'</p>');
68            $data[] = $render;
69        }
70
71        return $data;
72    }
73
74    public function cleanData($data) {
75        $cleanedData = array();
76        foreach ($data as $item) {
77            $item = trim($item);
78            if (!empty($item)) {
79                $cleanedData[] = $item;
80            }
81        }
82        return $cleanedData;
83    }
84
85    /**
86     * Render xhtml output or metadata
87     *
88     * @param string         $mode      Renderer mode (supported modes: xhtml)
89     * @param Doku_Renderer  $renderer  The renderer
90     * @param array          $data      The data from the handler() function
91     * @return bool If rendering was successful.
92     */
93    public function render($mode, Doku_Renderer $renderer, $data) {
94        if($mode != 'xhtml') return false;
95        if (empty($data)) return false;
96
97        $renderer->doc .= '<div id="plugin-newsticker" class="ticking">';
98        $renderer->doc .= '<div class="no ticker-buttons">';
99        $renderer->doc .= '<button class="plugin_newsticker" type="button" id="plugin_newsticker_unticker" title="' . $this->getLang('previous') . '">' . ''  . '</button>';
100        $renderer->doc .= '<button class="plugin_newsticker" type="button" id="plugin_newsticker_ticker" title="' . $this->getLang('next') . '">' . '' . '</button>';
101        $renderer->doc .= '</div>';
102        $renderer->doc .= '<ul id="tickerlist">';
103        foreach ($data as $index => $newsItem) {
104            $renderer->doc .= '<li>';
105            $renderer->doc .= $newsItem;
106            $renderer->doc .= '<span class="newsticker-counter">'. ($index+1) . '/' . count($data) . '</span>';
107            $renderer->doc .= '</li>';
108        }
109        $renderer->doc .= "</ul>";
110        $renderer->doc .= '</div>';
111
112        return true;
113    }
114}
115
116// vim:ts=4:sw=4:et:
117