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