includes/wikiformat.php
author Dan
Sat, 20 Mar 2010 16:05:38 -0400
changeset 1223 7dca925b0209
parent 1217 feeb49aa6270
child 1227 bdac73ed481e
permissions -rw-r--r--
Fixed "unapproved" status taking precedence over "spam" flag in comment submit and some display related bugs in comments.js
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
     1
<?php
536
218a627eb53e Rebrand as 1.1.4 (Caoineag alpha 4)
Dan
parents: 460
diff changeset
     2
218a627eb53e Rebrand as 1.1.4 (Caoineag alpha 4)
Dan
parents: 460
diff changeset
     3
/*
218a627eb53e Rebrand as 1.1.4 (Caoineag alpha 4)
Dan
parents: 460
diff changeset
     4
 * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
1081
745200a9cc2a Fixed some upgrade bugs; added support for choosing one's own date/time formats; rebrand as 1.1.7
Dan
parents: 1078
diff changeset
     5
 * Copyright (C) 2006-2009 Dan Fuhry
536
218a627eb53e Rebrand as 1.1.4 (Caoineag alpha 4)
Dan
parents: 460
diff changeset
     6
 *
218a627eb53e Rebrand as 1.1.4 (Caoineag alpha 4)
Dan
parents: 460
diff changeset
     7
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
218a627eb53e Rebrand as 1.1.4 (Caoineag alpha 4)
Dan
parents: 460
diff changeset
     8
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
218a627eb53e Rebrand as 1.1.4 (Caoineag alpha 4)
Dan
parents: 460
diff changeset
     9
 *
218a627eb53e Rebrand as 1.1.4 (Caoineag alpha 4)
Dan
parents: 460
diff changeset
    10
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
218a627eb53e Rebrand as 1.1.4 (Caoineag alpha 4)
Dan
parents: 460
diff changeset
    11
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
218a627eb53e Rebrand as 1.1.4 (Caoineag alpha 4)
Dan
parents: 460
diff changeset
    12
 */
218a627eb53e Rebrand as 1.1.4 (Caoineag alpha 4)
Dan
parents: 460
diff changeset
    13
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    14
/**
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    15
 * Framework for parsing and rendering various formats. In Enano by default, this is MediaWiki-style wikitext being
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    16
 * rendered to XHTML, but this framework allows other formats to be supported as well.
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    17
 *
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    18
 * @package Enano
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    19
 * @subpackage Content
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    20
 * @author Dan Fuhry <dan@enanocms.org>
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    21
 * @copyright (C) 2009 Enano CMS Project
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    22
 * @license GNU General Public License, version 2 or later <http://www.gnu.org/licenses/gpl-2.0.html>
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    23
 */
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    24
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    25
class Carpenter
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    26
{
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    27
  /**
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    28
   * Parser token
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    29
   * @const string
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    30
   */
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    31
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    32
  const PARSER_TOKEN = "\xFF";
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    33
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    34
  /**
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    35
   * Parsing engine
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    36
   * @var string
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    37
   */
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    38
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    39
  private $parser = 'mediawiki';
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    40
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    41
  /**
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    42
   * Rendering engine
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    43
   * @var string
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    44
   */
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    45
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    46
  private $renderer = 'xhtml';
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    47
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    48
  /**
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    49
   * Rendering flags
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    50
   */
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    51
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    52
  public $flags = RENDER_WIKI_DEFAULT;
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    53
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    54
  /**
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    55
   * List of rendering rules
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    56
   * @var array
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    57
   */
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    58
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    59
  private $rules = array(
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    60
      'lang',
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    61
      'templates',
1078
67a4c839c7e1 Blockquote functionality in wikitext parser now allows rendering of other block level elements properly
Dan
parents: 1073
diff changeset
    62
      'blockquote',
1174
def792dd9b1b Wikitext parer: re-added the <code> tag thing
Dan
parents: 1156
diff changeset
    63
      'code',
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    64
      'tables',
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    65
      'heading',
1106
01315acbc22b Wikitext: added horizontal rule support
Dan
parents: 1093
diff changeset
    66
      'hr',
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    67
      // note: can't be named list ("list" is a PHP language construct)
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    68
      'multilist',
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    69
      'bold',
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    70
      'italic',
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    71
      'underline',
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    72
      'externalwithtext',
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    73
      'externalnotext',
1156
417e66a664d0 Wikitext parser: re-added mailto support
Dan
parents: 1127
diff changeset
    74
      'mailtowithtext',
417e66a664d0 Wikitext parser: re-added mailto support
Dan
parents: 1127
diff changeset
    75
      'mailtonotext',
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    76
      'image',
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    77
      'internallink',
1078
67a4c839c7e1 Blockquote functionality in wikitext parser now allows rendering of other block level elements properly
Dan
parents: 1073
diff changeset
    78
      'paragraph',
67a4c839c7e1 Blockquote functionality in wikitext parser now allows rendering of other block level elements properly
Dan
parents: 1073
diff changeset
    79
      'blockquotepost'
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    80
    );
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    81
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    82
  /**
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    83
   * List of render hooks
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    84
   * @var array
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    85
   */
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    86
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    87
  private $hooks = array();
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    88
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    89
  /* private $rules = array('prefilter', 'delimiter', 'code', 'function', 'html', 'raw', 'include', 'embed', 'anchor',
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    90
           'heading', 'toc', 'horiz', 'break', 'blockquote', 'list', 'deflist', 'table', 'image',
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    91
           'phplookup', 'center', 'newline', 'paragraph', 'url', 'freelink', 'interwiki',
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    92
           'wikilink', 'colortext', 'strong', 'bold', 'emphasis', 'italic', 'underline', 'tt',
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    93
           'superscript', 'subscript', 'revise', 'tighten'); */
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    94
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    95
  /**
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    96
   * Render the text!
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    97
   * @param string Text to render
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    98
   * @return string
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
    99
   */
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   100
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   101
  public function render($text)
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   102
  {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   103
    $parser_class = "Carpenter_Parse_" . ucwords($this->parser);
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   104
    $renderer_class = "Carpenter_Render_" . ucwords($this->renderer);
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   105
    
1217
feeb49aa6270 Modified paragraph rule to not use recursive parsing; made parsing of code and pre tags much more reliable. Fixes issue 1 (QA: RE-TEST).
Dan
parents: 1174
diff changeset
   106
    // empty? (don't remove this. the parser will shit bricks later about rules returning empty strings)
1093
be48c0a49c7d Wiki engine: optimization for empty-text case
Dan
parents: 1081
diff changeset
   107
    if ( trim($text) === '' )
be48c0a49c7d Wiki engine: optimization for empty-text case
Dan
parents: 1081
diff changeset
   108
      return $text;
be48c0a49c7d Wiki engine: optimization for empty-text case
Dan
parents: 1081
diff changeset
   109
    
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   110
    // include files, if we haven't already
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   111
    if ( !class_exists($parser_class) )
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   112
    {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   113
      require_once( ENANO_ROOT . "/includes/wikiengine/parse_{$this->parser}.php");
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   114
    }
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   115
    
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   116
    if ( !class_exists($renderer_class) )
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   117
    {
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   118
      require_once( ENANO_ROOT . "/includes/wikiengine/render_{$this->renderer}.php");
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   119
    }
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   120
    
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   121
    $parser = new $parser_class;
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   122
    $renderer = new $renderer_class;
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   123
    
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   124
    // run prehooks
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   125
    foreach ( $this->hooks as $hook )
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   126
    {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   127
      if ( $hook['when'] === PO_FIRST )
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   128
      {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   129
        $text = call_user_func($hook['callback'], $text);
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   130
        if ( !is_string($text) || empty($text) )
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   131
        {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   132
          trigger_error("Hook returned empty/invalid text: " . print_r($hook['callback'], true), E_USER_WARNING);
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   133
          // *sigh*
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   134
          $text = '';
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   135
        }
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   136
      }
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   137
    }
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   138
    
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   139
    // perform render
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   140
    foreach ( $this->rules as $rule )
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   141
    {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   142
      // run prehooks
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   143
      foreach ( $this->hooks as $hook )
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   144
      {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   145
        if ( $hook['when'] === PO_BEFORE && $hook['rule'] === $rule )
407
35d94240a197 Mass-fixed all AJAX functions to also check the HTTP status code before parsing the response
Dan
parents: 371
diff changeset
   146
        {
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   147
          $text = call_user_func($hook['callback'], $text);
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   148
          if ( !is_string($text) || empty($text) )
438
c9522c961e10 Fixed broken installer due to bad $plugins object
Dan
parents: 407
diff changeset
   149
          {
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   150
            trigger_error("Hook returned empty/invalid text: " . print_r($hook['callback'], true), E_USER_WARNING);
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   151
            // *sigh*
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   152
            $text = '';
438
c9522c961e10 Fixed broken installer due to bad $plugins object
Dan
parents: 407
diff changeset
   153
          }
407
35d94240a197 Mass-fixed all AJAX functions to also check the HTTP status code before parsing the response
Dan
parents: 371
diff changeset
   154
        }
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   155
      }
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   156
      
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   157
      // execute rule
1127
4b858862c35c More parser work: fixed a few bugs with [[intlinks]] in headers, a bug that caused the paragraph parser to return an empty string, and added a warning/backup-and-restore for when a render stage returns an empty string.
Dan
parents: 1108
diff changeset
   158
      $text_before = $text;
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   159
      $text = $this->perform_render_step($text, $rule, $parser, $renderer);
1127
4b858862c35c More parser work: fixed a few bugs with [[intlinks]] in headers, a bug that caused the paragraph parser to return an empty string, and added a warning/backup-and-restore for when a render stage returns an empty string.
Dan
parents: 1108
diff changeset
   160
      if ( empty($text) )
4b858862c35c More parser work: fixed a few bugs with [[intlinks]] in headers, a bug that caused the paragraph parser to return an empty string, and added a warning/backup-and-restore for when a render stage returns an empty string.
Dan
parents: 1108
diff changeset
   161
      {
1217
feeb49aa6270 Modified paragraph rule to not use recursive parsing; made parsing of code and pre tags much more reliable. Fixes issue 1 (QA: RE-TEST).
Dan
parents: 1174
diff changeset
   162
        trigger_error("Wikitext was completely empty after rule \"$rule\"; restoring backup", E_USER_WARNING);
1127
4b858862c35c More parser work: fixed a few bugs with [[intlinks]] in headers, a bug that caused the paragraph parser to return an empty string, and added a warning/backup-and-restore for when a render stage returns an empty string.
Dan
parents: 1108
diff changeset
   163
        $text = $text_before;
4b858862c35c More parser work: fixed a few bugs with [[intlinks]] in headers, a bug that caused the paragraph parser to return an empty string, and added a warning/backup-and-restore for when a render stage returns an empty string.
Dan
parents: 1108
diff changeset
   164
      }
4b858862c35c More parser work: fixed a few bugs with [[intlinks]] in headers, a bug that caused the paragraph parser to return an empty string, and added a warning/backup-and-restore for when a render stage returns an empty string.
Dan
parents: 1108
diff changeset
   165
      unset($text_before);
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   166
      
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   167
      // run posthooks
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   168
      foreach ( $this->hooks as $hook )
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   169
      {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   170
        if ( $hook['when'] === PO_AFTER && $hook['rule'] === $rule )
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   171
        {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   172
          $text = call_user_func($hook['callback'], $text);
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   173
          if ( !is_string($text) || empty($text) )
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   174
          {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   175
            trigger_error("Hook returned empty/invalid text: " . print_r($hook['callback'], true), E_USER_WARNING);
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   176
            // *sigh*
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   177
            $text = '';
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   178
          }
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   179
        }
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   180
      }
1217
feeb49aa6270 Modified paragraph rule to not use recursive parsing; made parsing of code and pre tags much more reliable. Fixes issue 1 (QA: RE-TEST).
Dan
parents: 1174
diff changeset
   181
      
feeb49aa6270 Modified paragraph rule to not use recursive parsing; made parsing of code and pre tags much more reliable. Fixes issue 1 (QA: RE-TEST).
Dan
parents: 1174
diff changeset
   182
      RenderMan::tag_strip_push('final', $text, $final_stripdata);
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   183
    }
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   184
    
1217
feeb49aa6270 Modified paragraph rule to not use recursive parsing; made parsing of code and pre tags much more reliable. Fixes issue 1 (QA: RE-TEST).
Dan
parents: 1174
diff changeset
   185
    RenderMan::tag_unstrip('final', $text, $final_stripdata);
feeb49aa6270 Modified paragraph rule to not use recursive parsing; made parsing of code and pre tags much more reliable. Fixes issue 1 (QA: RE-TEST).
Dan
parents: 1174
diff changeset
   186
    
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   187
    // run posthooks
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   188
    foreach ( $this->hooks as $hook )
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   189
    {
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   190
      if ( $hook['when'] === PO_LAST )
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   191
      {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   192
        $text = call_user_func($hook['callback'], $text);
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   193
        if ( !is_string($text) || empty($text) )
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   194
        {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   195
          trigger_error("Hook returned empty/invalid text: " . print_r($hook['callback'], true), E_USER_WARNING);
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   196
          // *sigh*
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   197
          $text = '';
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   198
        }
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   199
      }
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   200
    }
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   201
    
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   202
    return (( defined('ENANO_DEBUG') && isset($_GET['parserdebug']) ) ? '<pre>' . htmlspecialchars($text) . '</pre>' : $text) . "\n\n";
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   203
  }
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   204
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   205
  /**
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   206
   * Performs a step in the rendering process.
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   207
   * @param string Text to render
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   208
   * @param string Rule to execute
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   209
   * @param object Parser instance
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   210
   * @param object Renderer instance
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   211
   * @return string
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   212
   * @access private
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   213
   */
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   214
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   215
  private function perform_render_step($text, $rule, $parser, $renderer)
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   216
  {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   217
    // First look for a direct function
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   218
    if ( function_exists("parser_{$this->parser}_{$this->renderer}_{$rule}") )
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   219
    {
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   220
      return call_user_func("parser_{$this->parser}_{$this->renderer}_{$rule}", $text, $this->flags);
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   221
    }
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   222
    
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   223
    // We don't have that, so start looking for other ways or means of doing this
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   224
    if ( method_exists($parser, $rule) && method_exists($renderer, $rule) )
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   225
    {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   226
      // Both the parser and render have callbacks they want to use.
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   227
      $pieces = $parser->$rule($text);
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   228
      $text = call_user_func(array($renderer, $rule), $text, $pieces);
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   229
    }
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   230
    else if ( method_exists($parser, $rule) && !method_exists($renderer, $rule) && isset($renderer->rules[$rule]) )
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   231
    {
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   232
      // The parser has a callback, but the renderer does not
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   233
      $pieces = $parser->$rule($text);
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   234
      $text = $this->generic_render($text, $pieces, $renderer->rules[$rule]);
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   235
    }
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   236
    else if ( !method_exists($parser, $rule) && isset($parser->rules[$rule]) && method_exists($renderer, $rule) )
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   237
    {
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   238
      // The parser has no callback, but the renderer does
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   239
      $text = preg_replace_callback($parser->rules[$rule], array($renderer, $rule), $text);
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   240
    }
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   241
    else if ( isset($parser->rules[$rule]) && isset($renderer->rules[$rule]) )
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   242
    {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   243
      // This is a straight-up regex only rule
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   244
      $text = preg_replace($parser->rules[$rule], $renderer->rules[$rule], $text);
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   245
    }
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   246
    else
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   247
    {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   248
      // Either the renderer or parser does not support this rule, ignore it
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   249
    }
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   250
    
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   251
    return $text;
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   252
  }
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   253
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   254
  /**
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   255
   * Generic renderer
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   256
   * @access protected
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   257
   */
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   258
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   259
  protected function generic_render($text, $pieces, $rule)
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   260
  {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   261
    foreach ( $pieces as $i => $piece )
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   262
    {
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   263
      $replacement = $rule;
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   264
      
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   265
      // if the piece is an array, replace $1, $2, etc. in the rule with each value in the piece
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   266
      if ( is_array($piece) )
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   267
      {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   268
        $j = 0;
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   269
        foreach ( $piece as $part )
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   270
        {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   271
          $j++;
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   272
          $replacement = str_replace(array("\\$j", "\${$j}"), $part, $replacement);
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   273
        }
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   274
      }
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   275
      // else, just replace \\1 or $1 in the rule with the piece
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   276
      else
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   277
      {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   278
        $replacement = str_replace(array("\\1", "\$1"), $piece, $replacement);
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   279
      }
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   280
      
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   281
      $text = str_replace(self::generate_token($i), $replacement, $text);
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   282
    }
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   283
    
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   284
    return $text;
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   285
  }
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   286
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   287
  /**
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   288
   * Add a hook into the parser.
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   289
   * @param callback Function to call
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   290
   * @param int PO_* constant
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   291
   * @param string If PO_{BEFORE,AFTER} used, rule
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   292
   */
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   293
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   294
  public function hook($callback, $when, $rule = false)
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   295
  {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   296
    if ( !is_int($when) )
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   297
      return null;
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   298
    if ( ($when == PO_BEFORE || $when == PO_AFTER) && !is_string($rule) )
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   299
      return null;
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   300
    if ( ( is_string($callback) && !function_exists($callback) ) || ( is_array($callback) && !method_exists($callback[0], $callback[1]) ) || ( !is_string($callback) && !is_array($callback) ) )
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   301
    {
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   302
      trigger_error("Attempt to hook with undefined function/method " . print_r($callback, true), E_USER_ERROR);
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   303
      return null;
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   304
    }
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   305
    
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   306
    $this->hooks[] = array(
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   307
        'callback' => $callback,
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   308
        'when'     => $when,
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   309
        'rule'     => $rule
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   310
      );
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   311
  }
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   312
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   313
  /**
1031
8a4b75e73137 Wiki formatting: Headings: tolerate spaces after line; added disable_rule method (required for rev. 1029)
Dan
parents: 1027
diff changeset
   314
   * Disable a render stage
8a4b75e73137 Wiki formatting: Headings: tolerate spaces after line; added disable_rule method (required for rev. 1029)
Dan
parents: 1027
diff changeset
   315
   * @param string stage
8a4b75e73137 Wiki formatting: Headings: tolerate spaces after line; added disable_rule method (required for rev. 1029)
Dan
parents: 1027
diff changeset
   316
   * @return null
8a4b75e73137 Wiki formatting: Headings: tolerate spaces after line; added disable_rule method (required for rev. 1029)
Dan
parents: 1027
diff changeset
   317
   */
8a4b75e73137 Wiki formatting: Headings: tolerate spaces after line; added disable_rule method (required for rev. 1029)
Dan
parents: 1027
diff changeset
   318
  
8a4b75e73137 Wiki formatting: Headings: tolerate spaces after line; added disable_rule method (required for rev. 1029)
Dan
parents: 1027
diff changeset
   319
  public function disable_rule($rule)
8a4b75e73137 Wiki formatting: Headings: tolerate spaces after line; added disable_rule method (required for rev. 1029)
Dan
parents: 1027
diff changeset
   320
  {
8a4b75e73137 Wiki formatting: Headings: tolerate spaces after line; added disable_rule method (required for rev. 1029)
Dan
parents: 1027
diff changeset
   321
    foreach ( $this->rules as $i => $current_rule )
8a4b75e73137 Wiki formatting: Headings: tolerate spaces after line; added disable_rule method (required for rev. 1029)
Dan
parents: 1027
diff changeset
   322
    {
8a4b75e73137 Wiki formatting: Headings: tolerate spaces after line; added disable_rule method (required for rev. 1029)
Dan
parents: 1027
diff changeset
   323
      if ( $current_rule === $rule )
8a4b75e73137 Wiki formatting: Headings: tolerate spaces after line; added disable_rule method (required for rev. 1029)
Dan
parents: 1027
diff changeset
   324
      {
8a4b75e73137 Wiki formatting: Headings: tolerate spaces after line; added disable_rule method (required for rev. 1029)
Dan
parents: 1027
diff changeset
   325
        unset($this->rules[$i]);
8a4b75e73137 Wiki formatting: Headings: tolerate spaces after line; added disable_rule method (required for rev. 1029)
Dan
parents: 1027
diff changeset
   326
        return null;
8a4b75e73137 Wiki formatting: Headings: tolerate spaces after line; added disable_rule method (required for rev. 1029)
Dan
parents: 1027
diff changeset
   327
      }
8a4b75e73137 Wiki formatting: Headings: tolerate spaces after line; added disable_rule method (required for rev. 1029)
Dan
parents: 1027
diff changeset
   328
    }
8a4b75e73137 Wiki formatting: Headings: tolerate spaces after line; added disable_rule method (required for rev. 1029)
Dan
parents: 1027
diff changeset
   329
    return null;
8a4b75e73137 Wiki formatting: Headings: tolerate spaces after line; added disable_rule method (required for rev. 1029)
Dan
parents: 1027
diff changeset
   330
  }
8a4b75e73137 Wiki formatting: Headings: tolerate spaces after line; added disable_rule method (required for rev. 1029)
Dan
parents: 1027
diff changeset
   331
  
8a4b75e73137 Wiki formatting: Headings: tolerate spaces after line; added disable_rule method (required for rev. 1029)
Dan
parents: 1027
diff changeset
   332
  /**
1108
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   333
   * Disables all rules.
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   334
   * @return null
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   335
   */
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   336
  
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   337
  public function disable_all_rules()
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   338
  {
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   339
    $this->rules = array();
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   340
    return null;
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   341
  }
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   342
  
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   343
  /**
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   344
   * Enables a rule
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   345
   * @param string rule
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   346
   * @return null
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   347
   */
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   348
   
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   349
  public function enable_rule($rule)
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   350
  {
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   351
    $this->rules[] = $rule;
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   352
    return null;
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   353
  }
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   354
  
c1be67a50d81 Removed the $userpage parameter from Namespace_Default::error_404(). It screwed up a couple plugins. (Thanks Mazza for discovering the issue)
Dan
parents: 1106
diff changeset
   355
  /**
1054
e6b14d33ac55 Renderer: added "smart paragraphs" for templates. <p><b>Foo</b> {bar}</p> where bar is multiline is basically turned into proper XHTML paragraphs.
Dan
parents: 1031
diff changeset
   356
   * Make a rule exclusive (the only one called)
e6b14d33ac55 Renderer: added "smart paragraphs" for templates. <p><b>Foo</b> {bar}</p> where bar is multiline is basically turned into proper XHTML paragraphs.
Dan
parents: 1031
diff changeset
   357
   * @param string stage
e6b14d33ac55 Renderer: added "smart paragraphs" for templates. <p><b>Foo</b> {bar}</p> where bar is multiline is basically turned into proper XHTML paragraphs.
Dan
parents: 1031
diff changeset
   358
   * @return null
e6b14d33ac55 Renderer: added "smart paragraphs" for templates. <p><b>Foo</b> {bar}</p> where bar is multiline is basically turned into proper XHTML paragraphs.
Dan
parents: 1031
diff changeset
   359
   */
e6b14d33ac55 Renderer: added "smart paragraphs" for templates. <p><b>Foo</b> {bar}</p> where bar is multiline is basically turned into proper XHTML paragraphs.
Dan
parents: 1031
diff changeset
   360
  
e6b14d33ac55 Renderer: added "smart paragraphs" for templates. <p><b>Foo</b> {bar}</p> where bar is multiline is basically turned into proper XHTML paragraphs.
Dan
parents: 1031
diff changeset
   361
  public function exclusive_rule($rule)
e6b14d33ac55 Renderer: added "smart paragraphs" for templates. <p><b>Foo</b> {bar}</p> where bar is multiline is basically turned into proper XHTML paragraphs.
Dan
parents: 1031
diff changeset
   362
  {
e6b14d33ac55 Renderer: added "smart paragraphs" for templates. <p><b>Foo</b> {bar}</p> where bar is multiline is basically turned into proper XHTML paragraphs.
Dan
parents: 1031
diff changeset
   363
    if ( is_string($rule) )
e6b14d33ac55 Renderer: added "smart paragraphs" for templates. <p><b>Foo</b> {bar}</p> where bar is multiline is basically turned into proper XHTML paragraphs.
Dan
parents: 1031
diff changeset
   364
      $this->rules = array($rule);
e6b14d33ac55 Renderer: added "smart paragraphs" for templates. <p><b>Foo</b> {bar}</p> where bar is multiline is basically turned into proper XHTML paragraphs.
Dan
parents: 1031
diff changeset
   365
    
e6b14d33ac55 Renderer: added "smart paragraphs" for templates. <p><b>Foo</b> {bar}</p> where bar is multiline is basically turned into proper XHTML paragraphs.
Dan
parents: 1031
diff changeset
   366
    return null;
e6b14d33ac55 Renderer: added "smart paragraphs" for templates. <p><b>Foo</b> {bar}</p> where bar is multiline is basically turned into proper XHTML paragraphs.
Dan
parents: 1031
diff changeset
   367
  }
e6b14d33ac55 Renderer: added "smart paragraphs" for templates. <p><b>Foo</b> {bar}</p> where bar is multiline is basically turned into proper XHTML paragraphs.
Dan
parents: 1031
diff changeset
   368
  
e6b14d33ac55 Renderer: added "smart paragraphs" for templates. <p><b>Foo</b> {bar}</p> where bar is multiline is basically turned into proper XHTML paragraphs.
Dan
parents: 1031
diff changeset
   369
  /**
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   370
   * Generate a token
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   371
   * @param int Token index
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   372
   * @return string
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   373
   * @static
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   374
   */
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   375
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   376
  public static function generate_token($i)
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   377
  {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   378
    return self::PARSER_TOKEN . $i . self::PARSER_TOKEN;
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   379
  }
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   380
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   381
  /**
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   382
   * Tokenize string
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   383
   * @param string
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   384
   * @param array Matches
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   385
   * @return string
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   386
   * @static
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   387
   */
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   388
  
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   389
  public static function tokenize($text, $matches)
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   390
  {
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   391
    $matches = array_values($matches);
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   392
    foreach ( $matches as $i => $match )
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   393
    {
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   394
      $text = str_replace_once($match, self::generate_token($i), $text);
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   395
    }
1027
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   396
    
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   397
    return $text;
98c052fc3337 First implementation of new parser; Text_Wiki is now gone. VERY BETA! WiP.
Dan
parents: 801
diff changeset
   398
  }
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   399
}
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   400