1<?php
2if (! class_exists('syntax_plugin_hr')) {
3	if (! defined('DOKU_PLUGIN')) {
4		if (! defined('DOKU_INC')) {
5			define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . '/');
6		} // if
7		define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
8	} // if
9	// include parent class
10	require_once(DOKU_PLUGIN . 'syntax.php');
11
12/**
13 * <tt>syntax_plugin_hr.php </tt>- A PHP4 class that implements
14 * a <tt>DokuWiki</tt> plugin for <tt>horizonal rule</tt> (HR)
15 * elements.
16 *
17 * <p>
18 * Just put four (or more) consecutive hyphens (minus signs) on
19 * a separate line:<br>
20 * <tt>----</tt>
21 * </p>
22 * <pre>
23 *	Copyright (C) 2005, 2007 DFG/M.Watermann, D-10247 Berlin, FRG
24 *			All rights reserved
25 *		EMail : &lt;support@mwat.de&gt;
26 * </pre>
27 * <div class="disclaimer">
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation; either
31 * <a href="http://www.gnu.org/licenses/gpl.html">version 3</a> of the
32 * License, or (at your option) any later version.<br>
33 * This software is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 * General Public License for more details.
37 * </div>
38 * @author <a href="mailto:support@mwat.de">Matthias Watermann</a>
39 * @version <tt>$Id: syntax_plugin_hr.php,v 1.4 2007/08/15 12:36:19 matthias Exp $</tt>
40 * @since created 29-Aug-2005
41 */
42class syntax_plugin_hr extends DokuWiki_Syntax_Plugin {
43
44	/**
45	 * @publicsection
46	 */
47	//@{
48
49	/**
50	 * Tell the parser whether the plugin accepts syntax mode
51	 * <tt>$aMode</tt> within its own markup.
52	 *
53	 * @param $aMode String The requested syntaxmode.
54	 * @return Boolean <tt>TRUE</tt> unless <tt>$aMode</tt> is
55	 * <tt>'plugin_hr'</tt> (which would result in a
56	 * <tt>FALSE</tt> method result).
57	 * @public
58	 * @see getAllowedTypes()
59	 * @static
60	 */
61	function accepts($aMode) {
62		return FALSE;
63	} // accepts()
64
65	/**
66	 * Connect lookup pattern to lexer.
67	 *
68	 * @param $aMode String The desired rendermode.
69	 * @public
70	 * @see render()
71	 */
72	function connectTo($aMode) {
73		$this->Lexer->addSpecialPattern('\n[\t\x20]*-{4,}[\t\x20]*(?=\n)',
74			$aMode, 'plugin_hr');
75	} // connectTo()
76
77	/**
78	 * Get an associative array with plugin info.
79	 *
80	 * <p>
81	 * The returned array holds the following fields:
82	 * <dl>
83	 * <dt>author</dt><dd>Author of the plugin</dd>
84	 * <dt>email</dt><dd>Email address to contact the author</dd>
85	 * <dt>date</dt><dd>Last modified date of the plugin in
86	 * <tt>YYYY-MM-DD</tt> format</dd>
87	 * <dt>name</dt><dd>Name of the plugin</dd>
88	 * <dt>desc</dt><dd>Short description of the plugin (Text only)</dd>
89	 * <dt>url</dt><dd>Website with more information on the plugin
90	 * (eg. syntax description)</dd>
91	 * </dl>
92	 * @return Array Information about this plugin class.
93	 * @public
94	 * @static
95	 */
96	function getInfo() {
97		return array(
98			'author' =>	'Matthias Watermann',
99			'email' =>	'support@mwat.de',
100			'date' =>	'2007-08-15',
101			'name' =>	'Horizontal Rule Syntax Plugin',
102			'desc' =>	'Add HTML Style Horizontal Rule  [ ---- ]',
103			'url' =>	'http://wiki.splitbrain.org/plugin:hr');
104	} // getInfo()
105
106	/**
107	 * Define how this plugin is handled regarding paragraphs.
108	 *
109	 * <p>
110	 * This method is important for correct XHTML nesting. It returns
111	 * one of the following values:
112	 * </p>
113	 * <dl>
114	 * <dt>normal</dt><dd>The plugin can be used inside paragraphs.</dd>
115	 * <dt>block</dt><dd>Open paragraphs need to be closed before
116	 * plugin output.</dd>
117	 * <dt>stack</dt><dd>Special case: Plugin wraps other paragraphs.</dd>
118	 * </dl>
119	 * @return String <tt>'block'</tt>.
120	 * @public
121	 * @static
122	 */
123	function getPType() {
124		return 'block';
125	} // getPType()
126
127	/**
128	 * Where to sort in?
129	 *
130	 * @return Integer <tt>6</tt>.
131	 * @public
132	 * @static
133	 */
134	function getSort() {
135		// class 'Doku_Parser_Mode_hr' returns 160
136		// class 'Doku_Parser_Mode_listblock' returns 10
137		// class 'syntax_plugin_lists' returns 8
138		return 6;
139	} // getSort()
140
141	/**
142	 * Get the type of syntax this plugin defines.
143	 *
144	 * @return String <tt>'substition'</tt> (i.e. 'substitution').
145	 * @public
146	 * @static
147	 */
148	function getType() {
149		return 'substition';	// sic! should be __substitution__
150	} // getType()
151
152	/**
153	 * Handler to prepare matched data for the rendering process.
154	 *
155	 * <p>
156	 * The <tt>$aState</tt> parameter gives the type of pattern
157	 * which triggered the call to this method:
158	 * </p>
159	 * <dl>
160	 * <dt>DOKU_LEXER_ENTER</dt>
161	 * <dd>a pattern set by <tt>addEntryPattern()</tt></dd>
162	 * <dt>DOKU_LEXER_MATCHED</dt>
163	 * <dd>a pattern set by <tt>addPattern()</tt></dd>
164	 * <dt>DOKU_LEXER_EXIT</dt>
165	 * <dd> a pattern set by <tt>addExitPattern()</tt></dd>
166	 * <dt>DOKU_LEXER_SPECIAL</dt>
167	 * <dd>a pattern set by <tt>addSpecialPattern()</tt></dd>
168	 * <dt>DOKU_LEXER_UNMATCHED</dt>
169	 * <dd>ordinary text encountered within the plugin's syntax mode
170	 * which doesn't match any pattern.</dd>
171	 * </dl>
172	 * @param $aMatch String The text matched by the patterns.
173	 * @param $aState Integer The lexer state for the match.
174	 * @param $aPos Integer The character position of the matched text.
175	 * @param $aHandler Object Reference to the Doku_Handler object.
176	 * @return Integer The current lexer state for the match.
177	 * @public
178	 * @see render()
179	 * @static
180	 */
181	function handle($aMatch, $aState, $aPos, &$aHandler) {
182		return $aState;
183	} // handle()
184
185	/**
186	 * Handle the actual output creation.
187	 *
188	 * <p>
189	 * The method checks for the given <tt>$aFormat</tt> and returns
190	 * <tt>FALSE</tt> when a format isn't supported. <tt>$aRenderer</tt>
191	 * contains a reference to the renderer object which is currently
192	 * handling the rendering. The contents of <tt>$aData</tt> is the
193	 * return value of the <tt>handle()</tt> method.
194	 * </p>
195	 * @param $aFormat String The output format to generate.
196	 * @param $aRenderer Object A reference to the renderer object.
197	 * @param $aData Array The data created by the <tt>handle()</tt>
198	 * method.
199	 * @return Boolean <tt>TRUE</tt> if rendered successfully, or
200	 * <tt>FALSE</tt> otherwise.
201	 * @public
202	 * @see handle()
203	 */
204	function render($aFormat, &$aRenderer, &$aData) {
205		if (DOKU_LEXER_SPECIAL == $aData) {
206			if ('xhtml' != $aFormat) {
207				return FALSE;
208			} // if
209			$hits = array();
210			$aRenderer->doc =
211				(preg_match('|\s*<p>(?:\s*</p>)*\s*$|', $aRenderer->doc, $hits))
212					? substr($aRenderer->doc, 0, -strlen($hits[0])) . '<hr />'
213					: preg_replace('|\s*<p>\s*</p>\s*|', '',
214						$aRenderer->doc) . '<hr />';
215		} // if
216		return TRUE;
217	} // render()
218
219	//@}
220} // class syntax_plugin_hr
221} // if
222?>
223