1<?php
2  if (!defined('DOKU_INC')) die();
3
4  if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
5  if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
6  if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
7
8  require_once DOKU_PLUGIN . 'action.php';
9
10  /*
11   * plugin should use this method to register its handlers
12   * with the dokuwiki's event controller
13   */
14
15  class action_plugin_spreadout extends DokuWiki_Action_Plugin {
16
17    function register(Doku_Event_Handler $controller) {
18      $controller->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE', $this, '_spreadout_postprocess', array(), PHP_INT_MAX - 127);
19    }
20
21    /**
22     * Reprocess the text to handle typography settings.
23     *
24     * <p>
25     * Any text sent here is checked with effectively the same code
26     * that is used in <tt>/inc/Parsing/Parsermode/Quotes.php</tt>.
27     * The reason for this is because when you are using typography
28     * settings to render curly quotes then they are tokenized and
29     * handled outside the level a syntax plugin can handle.  To make
30     * it so that quoted sentence breaks can be handled we have to
31     * turn off typography.
32     * </p><p>
33     * This is undesirable because I suspect most users turn on at
34     * least curly double quotes the settings, so what I do is still
35     * turn it off, but upon loading this plugin I grab a copy of the
36     * value first and duplicate its work by processing the text to
37     * transform the characters in the rendered output from the
38     * default values.  It's a <em>hideous</em> hack, but it seems to
39     * work (at least for English), and people who know my actual
40     * work should know I have to dole out some hideous hacks to do
41     * what I am usually asked to do, so...
42     * </p>
43     *
44     * @param $event Object The event object passed in.
45     * @param $param variant The parameters passed to register_hook
46     * @public
47     * @see handle()
48     */
49    function _spreadout_postprocess(Doku_Event $event, $param) {
50      global $lang, $conf;
51
52      // This plugin inerferes with the EditTable plugin; this will make it not take effect during table editing
53      if (preg_match('/"(edittable__editor|wiki__text)"/', $event->data))
54        return;
55
56      $ws   =  '\s/\#~:+=&%@\-\x28\x29\]\[{}><"\'';   // whitespace
57      $punc =  ';,\.?!';
58
59      if ($conf['spreadout_typography'] == 2) {
60        $event->data = preg_replace("`(?<=^|[$ws])&#039;(?=[^$ws$punc])`", $lang['singlequoteopening'], $event->data);
61        $event->data = preg_replace("`(?<=^|[^$ws]|[$punc])&#039;(?=$|[$ws$punc])`", $lang['singlequoteclosing'], $event->data);
62        $event->data = preg_replace("`(?<=^|[^$ws$punc])&#039;(?=$|[^$ws$punc])`", $lang['apostrophe'], $event->data);
63      }
64
65      if ($conf['spreadout_typography'] > 0) {
66        $event->data = preg_replace("`(?<=^|[$ws])&quot;(?=[^$ws$punc])`", $lang['doublequoteopening'], $event->data);
67        $event->data = preg_replace("`&quot;`", $lang['doublequoteclosing'], $event->data);
68      }
69    }
70  }
71
72  global $conf;
73  if (!isset($conf['spreadout_typography']))
74    $conf['spreadout_typography'] = $conf['typography'];
75  $conf['typography'] = 0;
76
77?>
78