1<?php
2  if (!class_exists('syntax_plugin_spreadout')) {
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_spreadout.php</tt>, a PHP class that lets the
14     * traditional amongst us to have double spaces after a sentence.
15     *
16     * <p>
17     * This automatically detects and replaces double spaces after sentences.
18     * Basically, any text that ends with a period ('.'), question mark ('?')
19     * or exclamation point ('!'), with an optional bracket after that
20     * punctuation, followed by two or more regular spaces, will be replaced
21     * with a space followed by a non-breaking space entity ("&nbsp;").  This
22     * makes things "spread out" by adding double space punctuation afterwards.
23     * </p><p>
24     * Before you ask, yes, I am a later generation than millennial.
25     * </p>
26     *
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.
36     * See the GNU General Public License for more details.
37     * </div>
38     *
39     * @author <a href="mailto:mpb@pobox.com">Michael Bowers</a>
40     * @version v1.0
41     */
42    class syntax_plugin_spreadout extends DokuWiki_Syntax_Plugin {
43
44      /**
45       * Tell the parser whether the plugin accepts syntax mode
46       * <tt>$aMode</tt> within its own markup.
47       *
48       * <p>
49       * This method always returns <tt>FALSE</tt> since no other data
50       * can be nested inside a non-breaking space.
51       * </p>
52       * @param $aMode String The requested syntaxmode.
53       * @return Boolean <tt>FALSE</tt> always.
54       * @public
55       * @see getAllowedTypes()
56       */
57      function accepts($aMode) {
58        return FALSE;
59      } // accepts()
60
61      /**
62       * Connect lookup patterns to lexer.
63       *
64       * @param $aMode String The desired rendermode.
65       * @public
66       * @see render()
67       */
68      function connectTo($aMode) {
69        global $lang;
70
71        $this->Lexer->addSpecialPattern('(?<=[.\?\!\:]) {2,}', $aMode, 'plugin_spreadout');
72        $this->Lexer->addSpecialPattern('(?<=[.\?\!\:][\)\]\}\"\']) {2,}', $aMode, 'plugin_spreadout');
73        $this->Lexer->addSpecialPattern('(?<=[.\?\!\:][\]\}\"\']) {2,}', $aMode, 'plugin_spreadout');
74      } // connectTo()
75
76      /**
77       * Get an associative array with plugin info.
78       *
79       * <p>
80       * The returned array holds the following fields:
81       * <dl>
82       * <dt>author</dt><dd>Author of the plugin</dd>
83       * <dt>email</dt><dd>Email address to contact the author</dd>
84       * <dt>date</dt><dd>Last modified date of the plugin in
85       * <tt>YYYY-MM-DD</tt> format</dd>
86       * <dt>name</dt><dd>Name of the plugin</dd>
87       * <dt>desc</dt><dd>Short description of the plugin (Text only)</dd>
88       * <dt>url</dt><dd>Website with more information on the plugin
89       * (eg. syntax description)</dd>
90       * </dl>
91       * @return Array Information about this plugin class.
92       * @public
93       * @static
94       */
95      function getInfo() {
96        return array (
97          'author' =>	'Michael Bowers',
98          'email' =>	'mpb@pobox.com',
99          'date' =>	'2022-08-02',
100          'name' =>	'Spreadout Plugin',
101          'desc' =>	'A simple plugin that allows for two spaces between content sentences if the user types two spaces rather than one.',
102          'url' =>	'http://www.dokuwiki.org/plugin:spreadout');
103      } // getInfo()
104
105      /**
106       * Where to sort in?
107       *
108       * @return Integer <tt>174</tt>.
109       * @public
110       * @static
111       */
112      function getSort() {
113        return 174;
114      } // getSort()
115
116      /**
117       * Get the type of syntax this plugin defines.
118       *
119       * @return String <tt>'substition'</tt> (a mispelled 'substitution').
120       * @public
121       * @static
122       */
123      function getType() {
124        return 'disabled';
125      } // getType()
126
127      /**
128       * Handler to prepare matched data for the rendering process.
129       *
130       * <p>
131       * This implementation does nothing (ignoring the passed
132       * arguments) and just returns the given <tt>$aState</tt>.
133       * </p>
134       *
135       * @param $aMatch String The text matched by the patterns.
136       * @param $aState Integer The lexer state for the match.
137       * @param $aPos Integer The character position of the matched text.
138       * @param $aHandler Object Reference to the Doku_Handler object.
139       * @return Integer The given <tt>$aState</tt> value.
140       * @public
141       * @see render()
142       * @static
143       */
144      function handle($aMatch, $aState, $aPos, Doku_Handler $aHandler) {
145        return $aState;		// nothing more to do here ...
146      } // handle()
147
148      /**
149       * Handle the actual output creation.
150       *
151       * <p>
152       * The method checks for the given <tt>$aMode</tt> and returns
153       * <tt>FALSE</tt> when a mode isn't supported.
154       * <tt>$aRenderer</tt> contains a reference to the renderer object
155       * which is currently handling the rendering.
156       * The contents of <tt>$aData</tt> is the return value of the
157       * <tt>handle()</tt> method.
158       * </p><p>
159       * This implementation ignores the passed <tt>$aFormat</tt>
160       * argument adding a raw UTF-8 character sequence to the
161       * renderer's document.
162       * </p>
163       *
164       * @param $aFormat String The output format to generate.
165       * @param $aRenderer Object A reference to the renderer object.
166       * @param $aData Integer The state value returned by <tt>handle()</tt>.
167       * @return Boolean <tt>TRUE</tt> always.
168       * @public
169       * @see handle()
170       */
171      function render($aFormat, Doku_Renderer $aRenderer, $aData) {
172        if (DOKU_LEXER_SPECIAL == $aData) {
173          $aRenderer->doc .= '&nbsp; ';
174        } // if
175        return TRUE;
176      } // render()
177    } // class syntax_plugin_nbsp
178  } // if
179
180?>
181