1<?php
2    /**
3     *  news item
4     * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
5     * @author     Myron Turner <turnermm02@shaw.ca>
6     */
7
8    // must be run within DokuWiki
9    if(!defined('DOKU_INC')) die();
10
11    if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12    require_once(DOKU_PLUGIN.'syntax.php');
13
14    /**
15     * All DokuWiki plugins to extend the parser/rendering mechanism
16     * need to inherit from this class
17     */
18    class syntax_plugin_news_item extends DokuWiki_Syntax_Plugin {
19        var $rss_index = 0;
20        /**
21         * return some info
22         */
23        function getInfo(){
24            return array(
25                'author' => 'Myron Turner',
26                'email'  => 'turnermm02@shaw.ca',
27                'date'   => '2010-03-19',
28                'name'   => 'news Plugin',
29                'desc'   => 'identifies news item and creates html anchor to item',
30                'url'    => 'http://www.mturner.org',
31            );
32        }
33
34        function getType(){ return 'formatting'; }
35		function getPType(){ return 'stack';}
36	    function accepts($mode) {
37			if ($mode == substr(get_class($this), 7)) return true;
38			return parent::accepts($mode);
39		}
40
41        function getAllowedTypes() { return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs'); }
42
43		function getSort(){ return 168; }
44
45		function connectTo($mode) {
46			$this->Lexer->addEntryPattern('<news.*?>(?=.*?</news>)',$mode,'plugin_news_item');
47			}
48
49        function postConnect() { $this->Lexer->addExitPattern('</news>','plugin_news_item'); }
50
51
52        /**
53         * Handle the match
54         */
55        function handle($match, $state, $pos, Doku_Handler $handler){
56
57
58            switch ($state) {
59                case DOKU_LEXER_ENTER : return array($state, "");
60
61				case DOKU_LEXER_UNMATCHED:
62				/*  From Wrap plugin
63				/* @author: Anika Henke <anika@selfthinker.org>
64				*/
65					// check if $match is a == header ==
66					$headerMatch = preg_grep('/([ \t]*={2,}[^\n]+={2,}[ \t]*(?=))/msSi', array($match));
67					if (empty($headerMatch)) {
68						$handler->_addCall('cdata', array($match), $pos);
69					} else {
70						// if it's a == header ==, use the core header() renderer
71						// (copied from core header() in inc/parser/handler.php)
72						$title = trim($match);
73						$level = 7 - strspn($title,'=');
74						if($level < 1) $level = 1;
75						$title = trim($title,'=');
76						$title = trim($title);
77
78						$handler->_addCall('header',array($title,$level,$pos), $pos);
79					}
80					return false;
81
82
83                case DOKU_LEXER_EXIT :       return array($state, '');
84			    case DOKU_LEXER_SPECIAL :       return array($state, '');
85
86            }
87
88            return false;
89        }
90
91        /**
92         * Create output
93         */
94        function render($mode, Doku_Renderer $renderer, $data) {
95		if (empty($data)) return false;
96            if($mode == 'xhtml'){
97                list($state, $match) = $data;
98                switch ($state) {
99
100                  case DOKU_LEXER_ENTER :
101				    $this->rss_index++;
102				    $renderer->doc .= "<a name='rss_" . $this->rss_index . "'>&nbsp;</a>";
103                   	break;
104                  case DOKU_LEXER_EXIT :
105				  case DOKU_LEXER_SPECIAL :
106   				    $renderer->doc .= ""; break;  ;
107                }
108                return true;
109            }
110            return false;
111        }
112
113
114
115}
116