1<?php
2if (! class_exists('syntax_plugin_bomfix')) {
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_bomfix.php </tt>- A PHP4 class that implements
14 * a <tt>DokuWiki</tt> plugin for <tt>UTF8 "magic" bytes</tt>.
15 *
16 * <p>
17 * External editors (i.e. separate standalone programs like word processing
18 * software) usually mark a file in UTF8 format by prepending its content
19 * with a "magic" byte sequence at the very start of file. While there is
20 * no harm in it as far as DokuWiki is concerned those "magic" bytes
21 * (Byte Order Mark) <em>do</em> appear in the page presented to the user.
22 * </p><p>
23 * Depending on a page's actual content and the respective CSS rules in
24 * effect this may lead to undesired results. One way to get rid of this
25 * problem would be to open the affected page(s) with DokuWiki's built-in
26 * edit feature and simply remove those bytes. However, such an approach
27 * would cause the word processor to open the file as plain text assuming
28 * it's in ASCII or, say, ISO-8859-1 format - whatever may be configured
29 * as the default text format. That, in consequence, would invalidate (or
30 * at least render strangely) all UTF8 character sequences.
31 * </p><p>
32 * Actually that is the recommended approach <em>if</em> (i.e. <tt>if</tt>)
33 * you never intend to edit the wiki pages by an external editor.
34 * </p><p>
35 * As it happens, personally I prefer to edit the pages (of a local DokuWiki
36 * installation) by OpenOffice.org for various reasons. (And, yes, I know
37 * that I bypass DokuWiki's changes-system this way.) Therefor I need those
38 * "magic" bytes <em>but</em> I don't want them to show up in the pages
39 * presented to the end user (reader). Enter <tt>syntax_plugin_bomfix</tt>.
40 * The whole purpose of this plugin is to suppress the output of that
41 * "magic" byte sequence. And nothing more. There are no new wiki language
42 * features introduced by this plugin. Nor is there anything special you
43 * have to remember when editing one of your already existing or newly
44 * created pages.
45 * </p><p>
46 * To use it just install the plugin in your DokuWiki's plugin folder.
47 * That's all.
48 * </p><pre>
49 *	Copyright (C) 2006, 2010  M.Watermann, D-10247 Berlin, FRG
50 *			All rights reserved
51 *		EMail : &lt;support@mwat.de&gt;
52 * </pre>
53 * <div class="disclaimer">
54 * This program is free software; you can redistribute it and/or modify
55 * it under the terms of the GNU General Public License as published by
56 * the Free Software Foundation; either
57 * <a href="http://www.gnu.org/licenses/gpl.html">version 3</a> of the
58 * License, or (at your option) any later version.<br>
59 * This software is distributed in the hope that it will be useful,
60 * but WITHOUT ANY WARRANTY; without even the implied warranty of
61 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
62 * General Public License for more details.
63 * </div>
64 * @author <a href="mailto:support@mwat.de">Matthias Watermann</a>
65 * @version <tt>$Id: syntax_plugin_bomfix.php,v 1.6 2010/02/22 10:49:59 matthias Exp $</tt>
66 * @since created 24-Dec-2006
67 */
68class syntax_plugin_bomfix extends DokuWiki_Syntax_Plugin {
69
70	/**
71	 * @publicsection
72	 */
73	//@{
74
75	/**
76	 * Tell the parser whether the plugin accepts syntax mode
77	 * <tt>$aMode</tt> within its own markup.
78	 *
79	 * @param $aMode String The requested syntaxmode.
80	 * @return Boolean <tt>FALSE</tt> always since no nested markup
81	 * is possible with this plugin.
82	 * @public
83	 */
84	function accepts($aMode) {
85		return FALSE;
86	} // accepts()
87
88	/**
89	 * Connect lookup pattern to lexer.
90	 *
91	 * @param $aMode String The desired rendermode.
92	 * @public
93	 * @see render()
94	 */
95	function connectTo($aMode) {
96		$this->Lexer->addSpecialPattern('^\xEF\xBB\xBF',
97			$aMode, 'plugin_bomfix');
98	} // connectTo()
99
100	/**
101	 * Get an associative array with plugin info.
102	 *
103	 * <p>
104	 * The returned array holds the following fields:
105	 * <dl>
106	 * <dt>author</dt><dd>Author of the plugin</dd>
107	 * <dt>email</dt><dd>Email address to contact the author</dd>
108	 * <dt>date</dt><dd>Last modified date of the plugin in
109	 * <tt>YYYY-MM-DD</tt> format</dd>
110	 * <dt>name</dt><dd>Name of the plugin</dd>
111	 * <dt>desc</dt><dd>Short description of the plugin (Text only)</dd>
112	 * <dt>url</dt><dd>Website with more information on the plugin
113	 * (eg. syntax description)</dd>
114	 * </dl>
115	 * @return Array Information about this plugin class.
116	 * @public
117	 * @static
118	 */
119	function getInfo() {
120		return array(
121			'author' =>	'Matthias Watermann',
122			'email' =>	'support@mwat.de',
123			'date' =>	'2010-02-22',
124			'name' =>	'BOMfix Syntax Plugin',
125			'desc' =>	'Ignore UTF8 "magic" bytes at start of page',
126			'url' =>	'http://www.dokuwiki.org/plugin:bomfix');
127	} // getInfo()
128
129	/**
130	 * Where to sort in?
131	 *
132	 * @return Integer <tt>380</tt> (doesn't really matter).
133	 * @static
134	 * @public
135	 */
136	function getSort() {
137		return 380;
138	} // getSort()
139
140	/**
141	 * Get the type of syntax this plugin defines.
142	 *
143	 * @return String <tt>'substition'</tt> (i.e. 'substitution').
144	 * @static
145	 * @public
146	 */
147	function getType() {
148		return 'substition';	// sic! should be __substitution__
149	} // getType()
150
151	/**
152	 * Handler to prepare matched data for the rendering process.
153	 *
154	 * <p>
155	 * The <tt>$aState</tt> parameter gives the type of pattern
156	 * which triggered the call to this method:
157	 * </p>
158	 * <dl>
159	 * <dt>DOKU_LEXER_ENTER</dt>
160	 * <dd>a pattern set by <tt>addEntryPattern()</tt></dd>
161	 * <dt>DOKU_LEXER_MATCHED</dt>
162	 * <dd>a pattern set by <tt>addPattern()</tt></dd>
163	 * <dt>DOKU_LEXER_EXIT</dt>
164	 * <dd> a pattern set by <tt>addExitPattern()</tt></dd>
165	 * <dt>DOKU_LEXER_SPECIAL</dt>
166	 * <dd>a pattern set by <tt>addSpecialPattern()</tt></dd>
167	 * <dt>DOKU_LEXER_UNMATCHED</dt>
168	 * <dd>ordinary text encountered within the plugin's syntax mode
169	 * which doesn't match any pattern.</dd>
170	 * </dl><p>
171	 * This implementation does nothing (ignoring the passed arguments)
172	 * and just returns the given <tt>$aState</tt>.
173	 * </p>
174	 * @param $aMatch String The text matched by the patterns.
175	 * @param $aState Integer The lexer state for the match.
176	 * @param $aPos Integer The character position of the matched text.
177	 * @param $aHandler Object Reference to the Doku_Handler object.
178	 * @return Integer The current lexer state.
179	 * @public
180	 * @see render()
181	 * @static
182	 */
183	function handle($aMatch, $aState, $aPos, &$aHandler) {
184		return $aState;	// doesn't really matter as it's ignored anyway ...
185	} // handle()
186
187	/**
188	 * Handle the actual output creation.
189	 *
190	 * <p>
191	 * The method checks for the given <tt>$aFormat</tt> and returns
192	 * <tt>FALSE</tt> when a format isn't supported.
193	 * <tt>$aRenderer</tt> contains a reference to the renderer object
194	 * which is currently handling the rendering.
195	 * The contents of <tt>$aData</tt> is the return value of the
196	 * <tt>handle()</tt> method.
197	 * </p><p>
198	 * Besides "eating" the BOM implicitely this implementation does
199	 * nothing (ignoring all passed arguments) and always returns
200	 * <tt>TRUE</tt>.
201	 * </p>
202	 * @param $aFormat String The output format to generate.
203	 * @param $aRenderer Object A reference to the renderer object.
204	 * @param $aData Integer The data created/returned by the
205	 * <tt>handle()</tt> method.
206	 * @return Boolean <tt>TRUE</tt> always since there's no actual
207	 * rendering done and hence can't ever fail.
208	 * @public
209	 * @see handle()
210	 * @static
211	 */
212	function render($aFormat, &$aRenderer, $aData) {
213		// nothing to do here - just 'eat' the BOM
214		return TRUE;
215	} // render()
216
217	//@}
218} // class syntax_plugin_bomfix
219} // if
220?>
221