Complex classes like _Markdown often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use _Markdown, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class _Markdown { |
||
| 42 | |||
| 43 | ### Version ### |
||
| 44 | |||
| 45 | const MARKDOWNLIB_VERSION = "1.3"; |
||
| 46 | |||
| 47 | ### Configuration Variables ### |
||
| 48 | |||
| 49 | # Change to ">" for HTML output. |
||
| 50 | public $empty_element_suffix = " />"; |
||
| 51 | public $tab_width = 4; |
||
| 52 | |||
| 53 | # Change to `true` to disallow markup or entities. |
||
| 54 | public $no_markup = false; |
||
| 55 | public $no_entities = false; |
||
| 56 | |||
| 57 | # Predefined urls and titles for reference links and images. |
||
| 58 | public $predef_urls = array(); |
||
| 59 | public $predef_titles = array(); |
||
| 60 | |||
| 61 | |||
| 62 | ### Parser Implementation ### |
||
| 63 | |||
| 64 | # Regex to match balanced [brackets]. |
||
| 65 | # Needed to insert a maximum bracked depth while converting to PHP. |
||
| 66 | protected $nested_brackets_depth = 6; |
||
| 67 | protected $nested_brackets_re; |
||
| 68 | |||
| 69 | protected $nested_url_parenthesis_depth = 4; |
||
| 70 | protected $nested_url_parenthesis_re; |
||
| 71 | |||
| 72 | # Table of hash values for escaped characters: |
||
| 73 | protected $escape_chars = '\`*_{}[]()>#+-.!'; |
||
| 74 | protected $escape_chars_re; |
||
| 75 | |||
| 76 | |||
| 77 | public function __construct() { |
||
| 78 | # |
||
| 79 | # Constructor function. Initialize appropriate member variables. |
||
| 80 | # |
||
| 81 | $this->_initDetab(); |
||
| 82 | $this->prepareItalicsAndBold(); |
||
| 83 | |||
| 84 | $this->nested_brackets_re = |
||
| 85 | str_repeat('(?>[^\[\]]+|\[', $this->nested_brackets_depth). |
||
| 86 | str_repeat('\])*', $this->nested_brackets_depth); |
||
| 87 | |||
| 88 | $this->nested_url_parenthesis_re = |
||
| 89 | str_repeat('(?>[^()\s]+|\(', $this->nested_url_parenthesis_depth). |
||
| 90 | str_repeat('(?>\)))*', $this->nested_url_parenthesis_depth); |
||
| 91 | |||
| 92 | $this->escape_chars_re = '['.preg_quote($this->escape_chars).']'; |
||
| 93 | |||
| 94 | # Sort document, block, and span gamut in ascendent priority order. |
||
| 95 | asort($this->document_gamut); |
||
| 96 | asort($this->block_gamut); |
||
| 97 | asort($this->span_gamut); |
||
| 98 | } |
||
| 99 | |||
| 100 | |||
| 101 | # Internal hashes used during transformation. |
||
| 102 | protected $urls = array(); |
||
| 103 | protected $titles = array(); |
||
| 104 | protected $html_hashes = array(); |
||
| 105 | |||
| 106 | # Status flag to avoid invalid nesting. |
||
| 107 | protected $in_anchor = false; |
||
| 108 | |||
| 109 | |||
| 110 | protected function setup() { |
||
| 111 | # |
||
| 112 | # Called before the transformation process starts to setup parser |
||
| 113 | # states. |
||
| 114 | # |
||
| 115 | # Clear global hashes. |
||
| 116 | $this->urls = $this->predef_urls; |
||
| 117 | $this->titles = $this->predef_titles; |
||
| 118 | $this->html_hashes = array(); |
||
| 119 | |||
| 120 | $this->in_anchor = false; |
||
| 121 | } |
||
| 122 | |||
| 123 | protected function teardown() { |
||
| 124 | # |
||
| 125 | # Called after the transformation process to clear any variable |
||
| 126 | # which may be taking up memory unnecessarly. |
||
| 127 | # |
||
| 128 | $this->urls = array(); |
||
| 129 | $this->titles = array(); |
||
| 130 | $this->html_hashes = array(); |
||
| 131 | } |
||
| 132 | |||
| 133 | |||
| 134 | public function transform($text) { |
||
| 135 | # |
||
| 136 | # Main function. Performs some preprocessing on the input text |
||
| 137 | # and pass it through the document gamut. |
||
| 138 | # |
||
| 139 | $this->setup(); |
||
| 140 | |||
| 141 | # Remove UTF-8 BOM and marker character in input, if present. |
||
| 142 | $text = preg_replace('{^\xEF\xBB\xBF|\x1A}', '', $text); |
||
| 143 | |||
| 144 | # Standardize line endings: |
||
| 145 | # DOS to Unix and Mac to Unix |
||
| 146 | $text = preg_replace('{\r\n?}', "\n", $text); |
||
| 147 | |||
| 148 | # Make sure $text ends with a couple of newlines: |
||
| 149 | $text .= "\n\n"; |
||
| 150 | |||
| 151 | # Convert all tabs to spaces. |
||
| 152 | $text = $this->detab($text); |
||
| 153 | |||
| 154 | # Turn block-level HTML blocks into hash entries |
||
| 155 | $text = $this->hashHTMLBlocks($text); |
||
| 156 | |||
| 157 | # Strip any lines consisting only of spaces and tabs. |
||
| 158 | # This makes subsequent regexen easier to write, because we can |
||
| 159 | # match consecutive blank lines with /\n+/ instead of something |
||
| 160 | # contorted like /[ ]*\n+/ . |
||
| 161 | $text = preg_replace('/^[ ]+$/m', '', $text); |
||
| 162 | |||
| 163 | # Run document gamut methods. |
||
| 164 | foreach ($this->document_gamut as $method => $priority) { |
||
| 165 | $text = $this->$method($text); |
||
| 166 | } |
||
| 167 | |||
| 168 | $this->teardown(); |
||
| 169 | |||
| 170 | $text = preg_replace_callback("/<\/?(\!doctype|html|head|body|link|title|input|select|button|textarea|style|noscript)[^>]*>/is", |
||
| 171 | array($this, '_doEscape_callback'), $text); |
||
| 172 | |||
| 173 | return $text . "\n"; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * _doEscape_callback |
||
| 178 | * |
||
| 179 | * @param array $matches |
||
| 180 | * @access protected |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | protected function _doEscape_callback($matches) { |
||
| 184 | return htmlspecialchars($matches[0], ENT_NOQUOTES); |
||
| 185 | } |
||
| 186 | |||
| 187 | protected $document_gamut = array( |
||
| 188 | # Strip link definitions, store in hashes. |
||
| 189 | "stripLinkDefinitions" => 20, |
||
| 190 | |||
| 191 | "runBasicBlockGamut" => 30, |
||
| 192 | ); |
||
| 193 | |||
| 194 | |||
| 195 | protected function stripLinkDefinitions($text) { |
||
| 196 | # |
||
| 197 | # Strips link definitions from text, stores the URLs and titles in |
||
| 198 | # hash references. |
||
| 199 | # |
||
| 200 | $less_than_tab = $this->tab_width - 1; |
||
| 201 | |||
| 202 | # Link defs are in the form: ^[id]: url "optional title" |
||
| 203 | $text = preg_replace_callback('{ |
||
| 204 | ^[ ]{0,'.$less_than_tab.'}\[(.+)\][ ]?: # id = $1 |
||
| 205 | [ ]* |
||
| 206 | \n? # maybe *one* newline |
||
| 207 | [ ]* |
||
| 208 | (?: |
||
| 209 | <(.+?)> # url = $2 |
||
| 210 | | |
||
| 211 | (\S+?) # url = $3 |
||
| 212 | ) |
||
| 213 | [ ]* |
||
| 214 | \n? # maybe one newline |
||
| 215 | [ ]* |
||
| 216 | (?: |
||
| 217 | (?<=\s) # lookbehind for whitespace |
||
| 218 | ["(] |
||
| 219 | (.*?) # title = $4 |
||
| 220 | [")] |
||
| 221 | [ ]* |
||
| 222 | )? # title is optional |
||
| 223 | (?:\n+|\Z) |
||
| 224 | }xm', |
||
| 225 | array($this, '_stripLinkDefinitions_callback'), |
||
| 226 | $text); |
||
| 227 | return $text; |
||
| 228 | } |
||
| 229 | protected function _stripLinkDefinitions_callback($matches) { |
||
| 230 | $link_id = strtolower($matches[1]); |
||
| 231 | $url = $matches[2] == '' ? $matches[3] : $matches[2]; |
||
| 232 | $this->urls[$link_id] = $url; |
||
| 233 | $this->titles[$link_id] =& $matches[4]; |
||
| 234 | return ''; # String that will replace the block |
||
| 235 | } |
||
| 236 | |||
| 237 | |||
| 238 | protected function hashHTMLBlocks($text) { |
||
| 239 | if ($this->no_markup) return $text; |
||
| 240 | |||
| 241 | $less_than_tab = $this->tab_width - 1; |
||
| 242 | |||
| 243 | # Hashify HTML blocks: |
||
| 244 | # We only want to do this for block-level HTML tags, such as headers, |
||
| 245 | # lists, and tables. That's because we still want to wrap <p>s around |
||
| 246 | # "paragraphs" that are wrapped in non-block-level tags, such as anchors, |
||
| 247 | # phrase emphasis, and spans. The list of tags we're looking for is |
||
| 248 | # hard-coded: |
||
| 249 | # |
||
| 250 | # * List "a" is made of tags which can be both inline or block-level. |
||
| 251 | # These will be treated block-level when the start tag is alone on |
||
| 252 | # its line, otherwise they're not matched here and will be taken as |
||
| 253 | # inline later. |
||
| 254 | # * List "b" is made of tags which are always block-level; |
||
| 255 | # |
||
| 256 | $block_tags_a_re = 'ins|del'; |
||
| 257 | $block_tags_b_re = 'p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|address|'. |
||
| 258 | 'script|noscript|form|fieldset|iframe|math|svg|'. |
||
| 259 | 'article|section|nav|aside|hgroup|header|footer|'. |
||
| 260 | 'figure'; |
||
| 261 | |||
| 262 | # Regular expression for the content of a block tag. |
||
| 263 | $nested_tags_level = 4; |
||
| 264 | $attr = ' |
||
| 265 | (?> # optional tag attributes |
||
| 266 | \s # starts with whitespace |
||
| 267 | (?> |
||
| 268 | [^>"/]+ # text outside quotes |
||
| 269 | | |
||
| 270 | /+(?!>) # slash not followed by ">" |
||
| 271 | | |
||
| 272 | "[^"]*" # text inside double quotes (tolerate ">") |
||
| 273 | | |
||
| 274 | \'[^\']*\' # text inside single quotes (tolerate ">") |
||
| 275 | )* |
||
| 276 | )? |
||
| 277 | '; |
||
| 278 | $content = |
||
| 279 | str_repeat(' |
||
| 280 | (?> |
||
| 281 | [^<]+ # content without tag |
||
| 282 | | |
||
| 283 | <\2 # nested opening tag |
||
| 284 | '.$attr.' # attributes |
||
| 285 | (?> |
||
| 286 | /> |
||
| 287 | | |
||
| 288 | >', $nested_tags_level). # end of opening tag |
||
| 289 | '.*?'. # last level nested tag content |
||
| 290 | str_repeat(' |
||
| 291 | </\2\s*> # closing nested tag |
||
| 292 | ) |
||
| 293 | | |
||
| 294 | <(?!/\2\s*> # other tags with a different name |
||
| 295 | ) |
||
| 296 | )*', |
||
| 297 | $nested_tags_level); |
||
| 298 | $content2 = str_replace('\2', '\3', $content); |
||
| 299 | |||
| 300 | # First, look for nested blocks, e.g.: |
||
| 301 | # <div> |
||
| 302 | # <div> |
||
| 303 | # tags for inner block must be indented. |
||
| 304 | # </div> |
||
| 305 | # </div> |
||
| 306 | # |
||
| 307 | # The outermost tags must start at the left margin for this to match, and |
||
| 308 | # the inner nested divs must be indented. |
||
| 309 | # We need to do this before the next, more liberal match, because the next |
||
| 310 | # match will start at the first `<div>` and stop at the first `</div>`. |
||
| 311 | $text = preg_replace_callback('{(?> |
||
| 312 | (?> |
||
| 313 | (?<=\n\n) # Starting after a blank line |
||
| 314 | | # or |
||
| 315 | \A\n? # the beginning of the doc |
||
| 316 | ) |
||
| 317 | ( # save in $1 |
||
| 318 | |||
| 319 | # Match from `\n<tag>` to `</tag>\n`, handling nested tags |
||
| 320 | # in between. |
||
| 321 | |||
| 322 | [ ]{0,'.$less_than_tab.'} |
||
| 323 | <('.$block_tags_b_re.')# start tag = $2 |
||
| 324 | '.$attr.'> # attributes followed by > and \n |
||
| 325 | '.$content.' # content, support nesting |
||
| 326 | </\2> # the matching end tag |
||
| 327 | [ ]* # trailing spaces/tabs |
||
| 328 | (?=\n+|\Z) # followed by a newline or end of document |
||
| 329 | |||
| 330 | | # Special version for tags of group a. |
||
| 331 | |||
| 332 | [ ]{0,'.$less_than_tab.'} |
||
| 333 | <('.$block_tags_a_re.')# start tag = $3 |
||
| 334 | '.$attr.'>[ ]*\n # attributes followed by > |
||
| 335 | '.$content2.' # content, support nesting |
||
| 336 | </\3> # the matching end tag |
||
| 337 | [ ]* # trailing spaces/tabs |
||
| 338 | (?=\n+|\Z) # followed by a newline or end of document |
||
| 339 | |||
| 340 | | # Special case just for <hr />. It was easier to make a special |
||
| 341 | # case than to make the other regex more complicated. |
||
| 342 | |||
| 343 | [ ]{0,'.$less_than_tab.'} |
||
| 344 | <(hr) # start tag = $2 |
||
| 345 | '.$attr.' # attributes |
||
| 346 | /?> # the matching end tag |
||
| 347 | [ ]* |
||
| 348 | (?=\n{2,}|\Z) # followed by a blank line or end of document |
||
| 349 | |||
| 350 | | # Special case for standalone HTML comments: |
||
| 351 | |||
| 352 | [ ]{0,'.$less_than_tab.'} |
||
| 353 | (?s: |
||
| 354 | <!-- .*? --> |
||
| 355 | ) |
||
| 356 | [ ]* |
||
| 357 | (?=\n{2,}|\Z) # followed by a blank line or end of document |
||
| 358 | |||
| 359 | | # PHP and ASP-style processor instructions (<? and <%) |
||
| 360 | |||
| 361 | [ ]{0,'.$less_than_tab.'} |
||
| 362 | (?s: |
||
| 363 | <([?%]) # $2 |
||
| 364 | .*? |
||
| 365 | \2> |
||
| 366 | ) |
||
| 367 | [ ]* |
||
| 368 | (?=\n{2,}|\Z) # followed by a blank line or end of document |
||
| 369 | |||
| 370 | ) |
||
| 371 | )}Sxmi', |
||
| 372 | array($this, '_hashHTMLBlocks_callback'), |
||
| 373 | $text); |
||
| 374 | |||
| 375 | return $text; |
||
| 376 | } |
||
| 377 | protected function _hashHTMLBlocks_callback($matches) { |
||
| 378 | $text = $matches[1]; |
||
| 379 | return $this->hashBlock($text); |
||
| 380 | // return "\n\n$key\n\n"; |
||
| 381 | } |
||
| 382 | |||
| 383 | |||
| 384 | protected function hashPart($text, $boundary = 'X') { |
||
| 385 | # |
||
| 386 | # Called whenever a tag must be hashed when a function insert an atomic |
||
| 387 | # element in the text stream. Passing $text to through this function gives |
||
| 388 | # a unique text-token which will be reverted back when calling unhash. |
||
| 389 | # |
||
| 390 | # The $boundary argument specify what character should be used to surround |
||
| 391 | # the token. By convension, "B" is used for block elements that needs not |
||
| 392 | # to be wrapped into paragraph tags at the end, ":" is used for elements |
||
| 393 | # that are word separators and "X" is used in the general case. |
||
| 394 | # |
||
| 395 | # Swap back any tag hash found in $text so we do not have to `unhash` |
||
| 396 | # multiple times at the end. |
||
| 397 | $text = $this->unhash($text); |
||
| 398 | |||
| 399 | # Then hash the block. |
||
| 400 | static $i = 0; |
||
| 401 | $key = "$boundary\x1A" . ++$i . $boundary; |
||
| 402 | $this->html_hashes[$key] = $text; |
||
| 403 | return $key; # String that will replace the tag. |
||
| 404 | } |
||
| 405 | |||
| 406 | |||
| 407 | protected function hashBlock($text) { |
||
| 408 | # |
||
| 409 | # Shortcut function for hashPart with block-level boundaries. |
||
| 410 | # |
||
| 411 | return $this->hashPart($text, 'B'); |
||
| 412 | } |
||
| 413 | |||
| 414 | |||
| 415 | protected $block_gamut = array( |
||
| 416 | # |
||
| 417 | # These are all the transformations that form block-level |
||
| 418 | # tags like paragraphs, headers, and list items. |
||
| 419 | # |
||
| 420 | "doHeaders" => 10, |
||
| 421 | "doHorizontalRules" => 20, |
||
| 422 | |||
| 423 | "doLists" => 40, |
||
| 424 | "doCodeBlocks" => 50, |
||
| 425 | "doBlockQuotes" => 60, |
||
| 426 | ); |
||
| 427 | |||
| 428 | protected function runBlockGamut($text) { |
||
| 429 | # |
||
| 430 | # Run block gamut tranformations. |
||
| 431 | # |
||
| 432 | # We need to escape raw HTML in Markdown source before doing anything |
||
| 433 | # else. This need to be done for each block, and not only at the |
||
| 434 | # begining in the Markdown function since hashed blocks can be part of |
||
| 435 | # list items and could have been indented. Indented blocks would have |
||
| 436 | # been seen as a code block in a previous pass of hashHTMLBlocks. |
||
| 437 | $text = $this->hashHTMLBlocks($text); |
||
| 438 | |||
| 439 | return $this->runBasicBlockGamut($text); |
||
| 440 | } |
||
| 441 | |||
| 442 | protected function runBasicBlockGamut($text) { |
||
| 443 | # |
||
| 444 | # Run block gamut tranformations, without hashing HTML blocks. This is |
||
| 445 | # useful when HTML blocks are known to be already hashed, like in the first |
||
| 446 | # whole-document pass. |
||
| 447 | # |
||
| 448 | foreach ($this->block_gamut as $method => $priority) { |
||
| 449 | $text = $this->$method($text); |
||
| 450 | } |
||
| 451 | |||
| 452 | # Finally form paragraph and restore hashed blocks. |
||
| 453 | $text = $this->formParagraphs($text); |
||
| 454 | |||
| 455 | return $text; |
||
| 456 | } |
||
| 457 | |||
| 458 | |||
| 459 | protected function doHorizontalRules($text) { |
||
| 460 | # Do Horizontal Rules: |
||
| 461 | return preg_replace( |
||
| 462 | '{ |
||
| 463 | ^[ ]{0,3} # Leading space |
||
| 464 | ([-*_]) # $1: First marker |
||
| 465 | (?> # Repeated marker group |
||
| 466 | [ ]{0,2} # Zero, one, or two spaces. |
||
| 467 | \1 # Marker character |
||
| 468 | ){2,} # Group repeated at least twice |
||
| 469 | [ ]* # Tailing spaces |
||
| 470 | $ # End of line. |
||
| 471 | }mx', |
||
| 472 | "\n".$this->hashBlock("<hr$this->empty_element_suffix")."\n", |
||
| 473 | $text); |
||
| 474 | } |
||
| 475 | |||
| 476 | |||
| 477 | protected $span_gamut = array( |
||
| 478 | # |
||
| 479 | # These are all the transformations that occur *within* block-level |
||
| 480 | # tags like paragraphs, headers, and list items. |
||
| 481 | # |
||
| 482 | # Process character escapes, code spans, and inline HTML |
||
| 483 | # in one shot. |
||
| 484 | "parseSpan" => -30, |
||
| 485 | |||
| 486 | # Process anchor and image tags. Images must come first, |
||
| 487 | # because ![foo][f] looks like an anchor. |
||
| 488 | "doImages" => 10, |
||
| 489 | "doAnchors" => 20, |
||
| 490 | |||
| 491 | # Make links out of things like `<http://example.com/>` |
||
| 492 | # Must come after doAnchors, because you can use < and > |
||
| 493 | # delimiters in inline links like [this](<url>). |
||
| 494 | "doAutoLinks" => 30, |
||
| 495 | "encodeAmpsAndAngles" => 40, |
||
| 496 | |||
| 497 | "doItalicsAndBold" => 50, |
||
| 498 | "doHardBreaks" => 60, |
||
| 499 | ); |
||
| 500 | |||
| 501 | protected function runSpanGamut($text) { |
||
| 502 | # |
||
| 503 | # Run span gamut tranformations. |
||
| 504 | # |
||
| 505 | foreach ($this->span_gamut as $method => $priority) { |
||
| 506 | $text = $this->$method($text); |
||
| 507 | } |
||
| 508 | |||
| 509 | return $text; |
||
| 510 | } |
||
| 511 | |||
| 512 | |||
| 513 | protected function doHardBreaks($text) { |
||
| 514 | # Do hard breaks: |
||
| 515 | return preg_replace_callback('/ {2,}\n/', |
||
| 516 | array($this, '_doHardBreaks_callback'), $text); |
||
| 517 | } |
||
| 518 | protected function _doHardBreaks_callback($matches) { |
||
| 519 | return $this->hashPart("<br$this->empty_element_suffix\n"); |
||
| 520 | } |
||
| 521 | |||
| 522 | |||
| 523 | protected function doAnchors($text) { |
||
| 524 | # |
||
| 525 | # Turn Markdown link shortcuts into XHTML <a> tags. |
||
| 526 | # |
||
| 527 | if ($this->in_anchor) return $text; |
||
| 528 | $this->in_anchor = true; |
||
| 529 | |||
| 530 | # |
||
| 531 | # First, handle reference-style links: [link text] [id] |
||
| 532 | # |
||
| 533 | $text = preg_replace_callback('{ |
||
| 534 | ( # wrap whole match in $1 |
||
| 535 | \[ |
||
| 536 | ('.$this->nested_brackets_re.') # link text = $2 |
||
| 537 | \] |
||
| 538 | |||
| 539 | [ ]? # one optional space |
||
| 540 | (?:\n[ ]*)? # one optional newline followed by spaces |
||
| 541 | |||
| 542 | \[ |
||
| 543 | (.*?) # id = $3 |
||
| 544 | \] |
||
| 545 | ) |
||
| 546 | }xs', |
||
| 547 | array($this, '_doAnchors_reference_callback'), $text); |
||
| 548 | |||
| 549 | # |
||
| 550 | # Next, inline-style links: [link text](url "optional title") |
||
| 551 | # |
||
| 552 | $text = preg_replace_callback('{ |
||
| 553 | ( # wrap whole match in $1 |
||
| 554 | \[ |
||
| 555 | ('.$this->nested_brackets_re.') # link text = $2 |
||
| 556 | \] |
||
| 557 | \( # literal paren |
||
| 558 | [ \n]* |
||
| 559 | (?: |
||
| 560 | <(.+?)> # href = $3 |
||
| 561 | | |
||
| 562 | ('.$this->nested_url_parenthesis_re.') # href = $4 |
||
| 563 | ) |
||
| 564 | [ \n]* |
||
| 565 | ( # $5 |
||
| 566 | ([\'"]) # quote char = $6 |
||
| 567 | (.*?) # Title = $7 |
||
| 568 | \6 # matching quote |
||
| 569 | [ \n]* # ignore any spaces/tabs between closing quote and ) |
||
| 570 | )? # title is optional |
||
| 571 | \) |
||
| 572 | ) |
||
| 573 | }xs', |
||
| 574 | array($this, '_doAnchors_inline_callback'), $text); |
||
| 575 | |||
| 576 | # |
||
| 577 | # Last, handle reference-style shortcuts: [link text] |
||
| 578 | # These must come last in case you've also got [link text][1] |
||
| 579 | # or [link text](/foo) |
||
| 580 | # |
||
| 581 | $text = preg_replace_callback('{ |
||
| 582 | ( # wrap whole match in $1 |
||
| 583 | \[ |
||
| 584 | ([^\[\]]+) # link text = $2; can\'t contain [ or ] |
||
| 585 | \] |
||
| 586 | ) |
||
| 587 | }xs', |
||
| 588 | array($this, '_doAnchors_reference_callback'), $text); |
||
| 589 | |||
| 590 | $this->in_anchor = false; |
||
| 591 | return $text; |
||
| 592 | } |
||
| 593 | protected function _doAnchors_reference_callback($matches) { |
||
| 594 | $whole_match = $matches[1]; |
||
| 595 | $link_text = $matches[2]; |
||
| 596 | $link_id =& $matches[3]; |
||
| 597 | |||
| 598 | if ($link_id == "") { |
||
| 599 | # for shortcut links like [this][] or [this]. |
||
| 600 | $link_id = $link_text; |
||
| 601 | } |
||
| 602 | |||
| 603 | # lower-case and turn embedded newlines into spaces |
||
| 604 | $link_id = strtolower($link_id); |
||
| 605 | $link_id = preg_replace('{[ ]?\n}', ' ', $link_id); |
||
| 606 | |||
| 607 | if (isset($this->urls[$link_id])) { |
||
| 608 | $url = $this->urls[$link_id]; |
||
| 609 | $url = $this->encodeAttribute($url); |
||
| 610 | |||
| 611 | $result = "<a href=\"$url\""; |
||
| 612 | if ( isset( $this->titles[$link_id] ) ) { |
||
| 613 | $title = $this->titles[$link_id]; |
||
| 614 | $title = $this->encodeAttribute($title); |
||
| 615 | $result .= " title=\"$title\""; |
||
| 616 | } |
||
| 617 | |||
| 618 | $link_text = $this->runSpanGamut($link_text); |
||
| 619 | $result .= ">$link_text</a>"; |
||
| 620 | $result = $this->hashPart($result); |
||
| 621 | } |
||
| 622 | else { |
||
| 623 | $result = $whole_match; |
||
| 624 | } |
||
| 625 | return $result; |
||
| 626 | } |
||
| 627 | protected function _doAnchors_inline_callback($matches) { |
||
| 628 | $whole_match = $matches[1]; |
||
| 629 | $link_text = $this->runSpanGamut($matches[2]); |
||
| 630 | $url = $matches[3] == '' ? $matches[4] : $matches[3]; |
||
| 631 | $title =& $matches[7]; |
||
| 632 | |||
| 633 | $url = $this->encodeAttribute($url); |
||
| 634 | |||
| 635 | $result = "<a href=\"$url\""; |
||
| 636 | if (isset($title)) { |
||
| 637 | $title = $this->encodeAttribute($title); |
||
| 638 | $result .= " title=\"$title\""; |
||
| 639 | } |
||
| 640 | |||
| 641 | $link_text = $this->runSpanGamut($link_text); |
||
| 642 | $result .= ">$link_text</a>"; |
||
| 643 | |||
| 644 | return $this->hashPart($result); |
||
| 645 | } |
||
| 646 | |||
| 647 | |||
| 648 | protected function doImages($text) { |
||
| 649 | # |
||
| 650 | # Turn Markdown image shortcuts into <img> tags. |
||
| 651 | # |
||
| 652 | # |
||
| 653 | # First, handle reference-style labeled images: ![alt text][id] |
||
| 654 | # |
||
| 655 | $text = preg_replace_callback('{ |
||
| 656 | ( # wrap whole match in $1 |
||
| 657 | !\[ |
||
| 658 | ('.$this->nested_brackets_re.') # alt text = $2 |
||
| 659 | \] |
||
| 660 | |||
| 661 | [ ]? # one optional space |
||
| 662 | (?:\n[ ]*)? # one optional newline followed by spaces |
||
| 663 | |||
| 664 | \[ |
||
| 665 | (.*?) # id = $3 |
||
| 666 | \] |
||
| 667 | |||
| 668 | ) |
||
| 669 | }xs', |
||
| 670 | array($this, '_doImages_reference_callback'), $text); |
||
| 671 | |||
| 672 | # |
||
| 673 | # Next, handle inline images:  |
||
| 674 | # Don't forget: encode * and _ |
||
| 675 | # |
||
| 676 | $text = preg_replace_callback('{ |
||
| 677 | ( # wrap whole match in $1 |
||
| 678 | !\[ |
||
| 679 | ('.$this->nested_brackets_re.') # alt text = $2 |
||
| 680 | \] |
||
| 681 | \s? # One optional whitespace character |
||
| 682 | \( # literal paren |
||
| 683 | [ \n]* |
||
| 684 | (?: |
||
| 685 | <(\S*)> # src url = $3 |
||
| 686 | | |
||
| 687 | ('.$this->nested_url_parenthesis_re.') # src url = $4 |
||
| 688 | ) |
||
| 689 | [ \n]* |
||
| 690 | ( # $5 |
||
| 691 | ([\'"]) # quote char = $6 |
||
| 692 | (.*?) # title = $7 |
||
| 693 | \6 # matching quote |
||
| 694 | [ \n]* |
||
| 695 | )? # title is optional |
||
| 696 | \) |
||
| 697 | ) |
||
| 698 | }xs', |
||
| 699 | array($this, '_doImages_inline_callback'), $text); |
||
| 700 | |||
| 701 | return $text; |
||
| 702 | } |
||
| 703 | protected function _doImages_reference_callback($matches) { |
||
| 704 | $whole_match = $matches[1]; |
||
| 705 | $alt_text = $matches[2]; |
||
| 706 | $link_id = strtolower($matches[3]); |
||
| 707 | |||
| 708 | if ($link_id == "") { |
||
| 709 | $link_id = strtolower($alt_text); # for shortcut links like ![this][]. |
||
| 710 | } |
||
| 711 | |||
| 712 | $alt_text = $this->encodeAttribute($alt_text); |
||
| 713 | if (isset($this->urls[$link_id])) { |
||
| 714 | $url = $this->encodeAttribute($this->urls[$link_id]); |
||
| 715 | $result = "<img src=\"$url\" alt=\"$alt_text\""; |
||
| 716 | if (isset($this->titles[$link_id])) { |
||
| 717 | $title = $this->titles[$link_id]; |
||
| 718 | $title = $this->encodeAttribute($title); |
||
| 719 | $result .= " title=\"$title\""; |
||
| 720 | } |
||
| 721 | $result .= $this->empty_element_suffix; |
||
| 722 | $result = $this->hashPart($result); |
||
| 723 | } |
||
| 724 | else { |
||
| 725 | # If there's no such link ID, leave intact: |
||
| 726 | $result = $whole_match; |
||
| 727 | } |
||
| 728 | |||
| 729 | return $result; |
||
| 730 | } |
||
| 731 | protected function _doImages_inline_callback($matches) { |
||
| 732 | $whole_match = $matches[1]; |
||
| 733 | $alt_text = $matches[2]; |
||
| 734 | $url = $matches[3] == '' ? $matches[4] : $matches[3]; |
||
| 735 | $title =& $matches[7]; |
||
| 736 | |||
| 737 | $alt_text = $this->encodeAttribute($alt_text); |
||
| 738 | $url = $this->encodeAttribute($url); |
||
| 739 | $result = "<img src=\"$url\" alt=\"$alt_text\""; |
||
| 740 | if (isset($title)) { |
||
| 741 | $title = $this->encodeAttribute($title); |
||
| 742 | $result .= " title=\"$title\""; # $title already quoted |
||
| 743 | } |
||
| 744 | $result .= $this->empty_element_suffix; |
||
| 745 | |||
| 746 | return $this->hashPart($result); |
||
| 747 | } |
||
| 748 | |||
| 749 | |||
| 750 | protected function doHeaders($text) { |
||
| 751 | # Setext-style headers: |
||
| 752 | # Header 1 |
||
| 753 | # ======== |
||
| 754 | # |
||
| 755 | # Header 2 |
||
| 756 | # -------- |
||
| 757 | # |
||
| 758 | $text = preg_replace_callback('{ ^(.+?)[ ]*\n(=+|-+)[ ]*\n+ }mx', |
||
| 759 | array($this, '_doHeaders_callback_setext'), $text); |
||
| 760 | |||
| 761 | # atx-style headers: |
||
| 762 | # # Header 1 |
||
| 763 | # ## Header 2 |
||
| 764 | # ## Header 2 with closing hashes ## |
||
| 765 | # ... |
||
| 766 | # ###### Header 6 |
||
| 767 | # |
||
| 768 | $text = preg_replace_callback('{ |
||
| 769 | ^(\#{1,6}) # $1 = string of #\'s |
||
| 770 | [ ]* |
||
| 771 | (.+?) # $2 = Header text |
||
| 772 | [ ]* |
||
| 773 | \#* # optional closing #\'s (not counted) |
||
| 774 | \n+ |
||
| 775 | }xm', |
||
| 776 | array($this, '_doHeaders_callback_atx'), $text); |
||
| 777 | |||
| 778 | return $text; |
||
| 779 | } |
||
| 780 | protected function _doHeaders_callback_setext($matches) { |
||
| 781 | # Terrible hack to check we haven't found an empty list item. |
||
| 782 | if ($matches[2] == '-' && preg_match('{^-(?: |$)}', $matches[1])) |
||
| 783 | return $matches[0]; |
||
| 784 | |||
| 785 | $level = $matches[2]{0} == '=' ? 1 : 2; |
||
| 786 | $block = "<h$level>".$this->runSpanGamut($matches[1])."</h$level>"; |
||
| 787 | return "\n" . $this->hashBlock($block) . "\n\n"; |
||
| 788 | } |
||
| 789 | protected function _doHeaders_callback_atx($matches) { |
||
| 790 | $level = strlen($matches[1]); |
||
| 791 | $block = "<h$level>".$this->runSpanGamut($matches[2])."</h$level>"; |
||
| 792 | return "\n" . $this->hashBlock($block) . "\n\n"; |
||
| 793 | } |
||
| 794 | |||
| 795 | |||
| 796 | protected function doLists($text) { |
||
| 797 | # |
||
| 798 | # Form HTML ordered (numbered) and unordered (bulleted) lists. |
||
| 799 | # |
||
| 800 | $less_than_tab = $this->tab_width - 1; |
||
| 801 | |||
| 802 | # Re-usable patterns to match list item bullets and number markers: |
||
| 803 | $marker_ul_re = '[*+-]'; |
||
| 804 | $marker_ol_re = '\d+[\.]'; |
||
| 805 | $marker_any_re = "(?:$marker_ul_re|$marker_ol_re)"; |
||
| 806 | |||
| 807 | $markers_relist = array( |
||
| 808 | $marker_ul_re => $marker_ol_re, |
||
| 809 | $marker_ol_re => $marker_ul_re, |
||
| 810 | ); |
||
| 811 | |||
| 812 | foreach ($markers_relist as $marker_re => $other_marker_re) { |
||
| 813 | # Re-usable pattern to match any entirel ul or ol list: |
||
| 814 | $whole_list_re = ' |
||
| 815 | ( # $1 = whole list |
||
| 816 | ( # $2 |
||
| 817 | ([ ]{0,'.$less_than_tab.'}) # $3 = number of spaces |
||
| 818 | ('.$marker_re.') # $4 = first list item marker |
||
| 819 | [ ]+ |
||
| 820 | ) |
||
| 821 | (?s:.+?) |
||
| 822 | ( # $5 |
||
| 823 | \z |
||
| 824 | | |
||
| 825 | \n{2,} |
||
| 826 | (?=\S) |
||
| 827 | (?! # Negative lookahead for another list item marker |
||
| 828 | [ ]* |
||
| 829 | '.$marker_re.'[ ]+ |
||
| 830 | ) |
||
| 831 | | |
||
| 832 | (?= # Lookahead for another kind of list |
||
| 833 | \n |
||
| 834 | \3 # Must have the same indentation |
||
| 835 | '.$other_marker_re.'[ ]+ |
||
| 836 | ) |
||
| 837 | ) |
||
| 838 | ) |
||
| 839 | '; // mx |
||
| 840 | |||
| 841 | # We use a different prefix before nested lists than top-level lists. |
||
| 842 | # See extended comment in _ProcessListItems(). |
||
| 843 | |||
| 844 | if ($this->list_level) { |
||
| 845 | $text = preg_replace_callback('{ |
||
| 846 | ^ |
||
| 847 | '.$whole_list_re.' |
||
| 848 | }mx', |
||
| 849 | array($this, '_doLists_callback'), $text); |
||
| 850 | } |
||
| 851 | else { |
||
| 852 | $text = preg_replace_callback('{ |
||
| 853 | (?:(?<=\n)\n?|\A\n?) # Must eat the newline |
||
| 854 | '.$whole_list_re.' |
||
| 855 | }mx', |
||
| 856 | array($this, '_doLists_callback'), $text); |
||
| 857 | } |
||
| 858 | } |
||
| 859 | |||
| 860 | return $text; |
||
| 861 | } |
||
| 862 | protected function _doLists_callback($matches) { |
||
| 863 | # Re-usable patterns to match list item bullets and number markers: |
||
| 864 | $marker_ul_re = '[*+-]'; |
||
| 865 | $marker_ol_re = '\d+[\.]'; |
||
| 866 | $marker_any_re = "(?:$marker_ul_re|$marker_ol_re)"; |
||
| 867 | |||
| 868 | $list = $matches[1]; |
||
| 869 | $list_type = preg_match("/$marker_ul_re/", $matches[4]) ? "ul" : "ol"; |
||
| 870 | |||
| 871 | $marker_any_re = ( $list_type == "ul" ? $marker_ul_re : $marker_ol_re ); |
||
| 872 | |||
| 873 | $list .= "\n"; |
||
| 874 | $result = $this->processListItems($list, $marker_any_re); |
||
| 875 | |||
| 876 | $result = $this->hashBlock("<$list_type>\n" . $result . "</$list_type>"); |
||
| 877 | return "\n". $result ."\n\n"; |
||
| 878 | } |
||
| 879 | |||
| 880 | protected $list_level = 0; |
||
| 881 | |||
| 882 | protected function processListItems($list_str, $marker_any_re) { |
||
| 883 | # |
||
| 884 | # Process the contents of a single ordered or unordered list, splitting it |
||
| 885 | # into individual list items. |
||
| 886 | # |
||
| 887 | # The $this->list_level global keeps track of when we're inside a list. |
||
| 888 | # Each time we enter a list, we increment it; when we leave a list, |
||
| 889 | # we decrement. If it's zero, we're not in a list anymore. |
||
| 890 | # |
||
| 891 | # We do this because when we're not inside a list, we want to treat |
||
| 892 | # something like this: |
||
| 893 | # |
||
| 894 | # I recommend upgrading to version |
||
| 895 | # 8. Oops, now this line is treated |
||
| 896 | # as a sub-list. |
||
| 897 | # |
||
| 898 | # As a single paragraph, despite the fact that the second line starts |
||
| 899 | # with a digit-period-space sequence. |
||
| 900 | # |
||
| 901 | # Whereas when we're inside a list (or sub-list), that line will be |
||
| 902 | # treated as the start of a sub-list. What a kludge, huh? This is |
||
| 903 | # an aspect of Markdown's syntax that's hard to parse perfectly |
||
| 904 | # without resorting to mind-reading. Perhaps the solution is to |
||
| 905 | # change the syntax rules such that sub-lists must start with a |
||
| 906 | # starting cardinal number; e.g. "1." or "a.". |
||
| 907 | |||
| 908 | $this->list_level++; |
||
| 909 | |||
| 910 | # trim trailing blank lines: |
||
| 911 | $list_str = preg_replace("/\n{2,}\\z/", "\n", $list_str); |
||
| 912 | |||
| 913 | $list_str = preg_replace_callback('{ |
||
| 914 | (\n)? # leading line = $1 |
||
| 915 | (^[ ]*) # leading whitespace = $2 |
||
| 916 | ('.$marker_any_re.' # list marker and space = $3 |
||
| 917 | (?:[ ]+|(?=\n)) # space only required if item is not empty |
||
| 918 | ) |
||
| 919 | ((?s:.*?)) # list item text = $4 |
||
| 920 | (?:(\n+(?=\n))|\n) # tailing blank line = $5 |
||
| 921 | (?= \n* (\z | \2 ('.$marker_any_re.') (?:[ ]+|(?=\n)))) |
||
| 922 | }xm', |
||
| 923 | array($this, '_processListItems_callback'), $list_str); |
||
| 924 | |||
| 925 | $this->list_level--; |
||
| 926 | return $list_str; |
||
| 927 | } |
||
| 928 | protected function _processListItems_callback($matches) { |
||
| 929 | $item = $matches[4]; |
||
| 930 | $leading_line =& $matches[1]; |
||
| 931 | $leading_space =& $matches[2]; |
||
| 932 | $marker_space = $matches[3]; |
||
| 933 | $tailing_blank_line =& $matches[5]; |
||
| 934 | |||
| 935 | if ($leading_line || $tailing_blank_line || |
||
| 936 | preg_match('/\n{2,}/', $item)) |
||
| 937 | { |
||
| 938 | # Replace marker with the appropriate whitespace indentation |
||
| 939 | $item = $leading_space . str_repeat(' ', strlen($marker_space)) . $item; |
||
| 940 | $item = $this->runBlockGamut($this->outdent($item)."\n"); |
||
| 941 | } |
||
| 942 | else { |
||
| 943 | # Recursion for sub-lists: |
||
| 944 | $item = $this->doLists($this->outdent($item)); |
||
| 945 | $item = preg_replace('/\n+$/', '', $item); |
||
| 946 | $item = $this->runSpanGamut($item); |
||
| 947 | } |
||
| 948 | |||
| 949 | return "<li>" . $item . "</li>\n"; |
||
| 950 | } |
||
| 951 | |||
| 952 | |||
| 953 | protected function doCodeBlocks($text) { |
||
| 954 | # |
||
| 955 | # Process Markdown `<pre><code>` blocks. |
||
| 956 | # |
||
| 957 | $text = preg_replace_callback('{ |
||
| 958 | (?:\n\n|\A\n?) |
||
| 959 | ( # $1 = the code block -- one or more lines, starting with a space/tab |
||
| 960 | (?> |
||
| 961 | [ ]{'.$this->tab_width.'} # Lines must start with a tab or a tab-width of spaces |
||
| 962 | .*\n+ |
||
| 963 | )+ |
||
| 964 | ) |
||
| 965 | ((?=^[ ]{0,'.$this->tab_width.'}\S)|\Z) # Lookahead for non-space at line-start, or end of doc |
||
| 966 | }xm', |
||
| 967 | array($this, '_doCodeBlocks_callback'), $text); |
||
| 968 | |||
| 969 | return $text; |
||
| 970 | } |
||
| 971 | protected function _doCodeBlocks_callback($matches) { |
||
| 972 | $codeblock = $this->unhash($matches[1]); |
||
| 973 | |||
| 974 | $codeblock = $this->outdent($codeblock); |
||
| 975 | $codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES); |
||
| 976 | |||
| 977 | # trim leading newlines and trailing newlines |
||
| 978 | $codeblock = preg_replace('/\A\n+|\n+\z/', '', $codeblock); |
||
| 979 | |||
| 980 | $codeblock = "<pre><code>$codeblock\n</code></pre>"; |
||
| 981 | return "\n\n".$this->hashBlock($codeblock)."\n\n"; |
||
| 982 | } |
||
| 983 | |||
| 984 | |||
| 985 | protected function makeCodeSpan($code) { |
||
| 986 | # |
||
| 987 | # Create a code span markup for $code. Called from handleSpanToken. |
||
| 988 | # |
||
| 989 | $code = htmlspecialchars(trim($code), ENT_NOQUOTES); |
||
| 990 | return $this->hashPart("<code>$code</code>"); |
||
| 991 | } |
||
| 992 | |||
| 993 | |||
| 994 | protected $em_relist = array( |
||
| 995 | '' => '(?:(?<!\*)\*(?!\*)|(?<!_)_(?!_))(?=\S|$)(?![\.,:;]\s)', |
||
| 996 | '*' => '(?<=\S|^)(?<!\*)\*(?!\*)', |
||
| 997 | '_' => '(?<=\S|^)(?<!_)_(?!_)', |
||
| 998 | ); |
||
| 999 | protected $strong_relist = array( |
||
| 1000 | '' => '(?:(?<!\*)\*\*(?!\*)|(?<!_)__(?!_))(?=\S|$)(?![\.,:;]\s)', |
||
| 1001 | '**' => '(?<=\S|^)(?<!\*)\*\*(?!\*)', |
||
| 1002 | '__' => '(?<=\S|^)(?<!_)__(?!_)', |
||
| 1003 | ); |
||
| 1004 | protected $em_strong_relist = array( |
||
| 1005 | '' => '(?:(?<!\*)\*\*\*(?!\*)|(?<!_)___(?!_))(?=\S|$)(?![\.,:;]\s)', |
||
| 1006 | '***' => '(?<=\S|^)(?<!\*)\*\*\*(?!\*)', |
||
| 1007 | '___' => '(?<=\S|^)(?<!_)___(?!_)', |
||
| 1008 | ); |
||
| 1009 | protected $em_strong_prepared_relist; |
||
| 1010 | |||
| 1011 | protected function prepareItalicsAndBold() { |
||
| 1012 | # |
||
| 1013 | # Prepare regular expressions for searching emphasis tokens in any |
||
| 1014 | # context. |
||
| 1015 | # |
||
| 1016 | foreach ($this->em_relist as $em => $em_re) { |
||
| 1017 | foreach ($this->strong_relist as $strong => $strong_re) { |
||
| 1018 | # Construct list of allowed token expressions. |
||
| 1019 | $token_relist = array(); |
||
| 1020 | if (isset($this->em_strong_relist["$em$strong"])) { |
||
| 1021 | $token_relist[] = $this->em_strong_relist["$em$strong"]; |
||
| 1022 | } |
||
| 1023 | $token_relist[] = $em_re; |
||
| 1024 | $token_relist[] = $strong_re; |
||
| 1025 | |||
| 1026 | # Construct master expression from list. |
||
| 1027 | $token_re = '{('. implode('|', $token_relist) .')}'; |
||
| 1028 | $this->em_strong_prepared_relist["$em$strong"] = $token_re; |
||
| 1029 | } |
||
| 1030 | } |
||
| 1031 | } |
||
| 1032 | |||
| 1033 | protected function doItalicsAndBold($text) { |
||
| 1034 | $token_stack = array(''); |
||
| 1035 | $text_stack = array(''); |
||
| 1036 | $em = ''; |
||
| 1037 | $strong = ''; |
||
| 1038 | $tree_char_em = false; |
||
| 1039 | |||
| 1040 | while (1) { |
||
| 1041 | # |
||
| 1042 | # Get prepared regular expression for seraching emphasis tokens |
||
| 1043 | # in current context. |
||
| 1044 | # |
||
| 1045 | $token_re = $this->em_strong_prepared_relist["$em$strong"]; |
||
| 1046 | |||
| 1047 | # |
||
| 1048 | # Each loop iteration search for the next emphasis token. |
||
| 1049 | # Each token is then passed to handleSpanToken. |
||
| 1050 | # |
||
| 1051 | $parts = preg_split($token_re, $text, 2, PREG_SPLIT_DELIM_CAPTURE); |
||
| 1052 | $text_stack[0] .= $parts[0]; |
||
| 1053 | $token =& $parts[1]; |
||
| 1054 | $text =& $parts[2]; |
||
| 1055 | |||
| 1056 | if (empty($token)) { |
||
| 1057 | # Reached end of text span: empty stack without emitting. |
||
| 1058 | # any more emphasis. |
||
| 1059 | while ($token_stack[0]) { |
||
| 1060 | $text_stack[1] .= array_shift($token_stack); |
||
| 1061 | $text_stack[0] .= array_shift($text_stack); |
||
| 1062 | } |
||
| 1063 | break; |
||
| 1064 | } |
||
| 1065 | |||
| 1066 | $token_len = strlen($token); |
||
| 1067 | if ($tree_char_em) { |
||
| 1068 | # Reached closing marker while inside a three-char emphasis. |
||
| 1069 | if ($token_len == 3) { |
||
| 1070 | # Three-char closing marker, close em and strong. |
||
| 1071 | array_shift($token_stack); |
||
| 1072 | $span = array_shift($text_stack); |
||
| 1073 | $span = $this->runSpanGamut($span); |
||
| 1074 | $span = "<strong><em>$span</em></strong>"; |
||
| 1075 | $text_stack[0] .= $this->hashPart($span); |
||
| 1076 | $em = ''; |
||
| 1077 | $strong = ''; |
||
| 1078 | } else { |
||
| 1079 | # Other closing marker: close one em or strong and |
||
| 1080 | # change current token state to match the other |
||
| 1081 | $token_stack[0] = str_repeat($token{0}, 3-$token_len); |
||
| 1082 | $tag = $token_len == 2 ? "strong" : "em"; |
||
| 1083 | $span = $text_stack[0]; |
||
| 1084 | $span = $this->runSpanGamut($span); |
||
| 1085 | $span = "<$tag>$span</$tag>"; |
||
| 1086 | $text_stack[0] = $this->hashPart($span); |
||
| 1087 | $$tag = ''; # $$tag stands for $em or $strong |
||
| 1088 | } |
||
| 1089 | $tree_char_em = false; |
||
| 1090 | } else if ($token_len == 3) { |
||
| 1091 | if ($em) { |
||
| 1092 | # Reached closing marker for both em and strong. |
||
| 1093 | # Closing strong marker: |
||
| 1094 | for ($i = 0; $i < 2; ++$i) { |
||
| 1095 | $shifted_token = array_shift($token_stack); |
||
| 1096 | $tag = strlen($shifted_token) == 2 ? "strong" : "em"; |
||
| 1097 | $span = array_shift($text_stack); |
||
| 1098 | $span = $this->runSpanGamut($span); |
||
| 1099 | $span = "<$tag>$span</$tag>"; |
||
| 1100 | $text_stack[0] .= $this->hashPart($span); |
||
| 1101 | $$tag = ''; # $$tag stands for $em or $strong |
||
| 1102 | } |
||
| 1103 | } else { |
||
| 1104 | # Reached opening three-char emphasis marker. Push on token |
||
| 1105 | # stack; will be handled by the special condition above. |
||
| 1106 | $em = $token{0}; |
||
| 1107 | $strong = "$em$em"; |
||
| 1108 | array_unshift($token_stack, $token); |
||
| 1109 | array_unshift($text_stack, ''); |
||
| 1110 | $tree_char_em = true; |
||
| 1111 | } |
||
| 1112 | } else if ($token_len == 2) { |
||
| 1113 | if ($strong) { |
||
| 1114 | # Unwind any dangling emphasis marker: |
||
| 1115 | if (strlen($token_stack[0]) == 1) { |
||
| 1116 | $text_stack[1] .= array_shift($token_stack); |
||
| 1117 | $text_stack[0] .= array_shift($text_stack); |
||
| 1118 | } |
||
| 1119 | # Closing strong marker: |
||
| 1120 | array_shift($token_stack); |
||
| 1121 | $span = array_shift($text_stack); |
||
| 1122 | $span = $this->runSpanGamut($span); |
||
| 1123 | $span = "<strong>$span</strong>"; |
||
| 1124 | $text_stack[0] .= $this->hashPart($span); |
||
| 1125 | $strong = ''; |
||
| 1126 | } else { |
||
| 1127 | array_unshift($token_stack, $token); |
||
| 1128 | array_unshift($text_stack, ''); |
||
| 1129 | $strong = $token; |
||
| 1130 | } |
||
| 1131 | } else { |
||
| 1132 | # Here $token_len == 1 |
||
| 1133 | if ($em) { |
||
| 1134 | if (strlen($token_stack[0]) == 1) { |
||
| 1135 | # Closing emphasis marker: |
||
| 1136 | array_shift($token_stack); |
||
| 1137 | $span = array_shift($text_stack); |
||
| 1138 | $span = $this->runSpanGamut($span); |
||
| 1139 | $span = "<em>$span</em>"; |
||
| 1140 | $text_stack[0] .= $this->hashPart($span); |
||
| 1141 | $em = ''; |
||
| 1142 | } else { |
||
| 1143 | $text_stack[0] .= $token; |
||
| 1144 | } |
||
| 1145 | } else { |
||
| 1146 | array_unshift($token_stack, $token); |
||
| 1147 | array_unshift($text_stack, ''); |
||
| 1148 | $em = $token; |
||
| 1149 | } |
||
| 1150 | } |
||
| 1151 | } |
||
| 1152 | return $text_stack[0]; |
||
| 1153 | } |
||
| 1154 | |||
| 1155 | |||
| 1156 | protected function doBlockQuotes($text) { |
||
| 1157 | $text = preg_replace_callback('/ |
||
| 1158 | ( # Wrap whole match in $1 |
||
| 1159 | (?> |
||
| 1160 | ^[ ]*>[ ]? # ">" at the start of a line |
||
| 1161 | .+\n # rest of the first line |
||
| 1162 | (.+\n)* # subsequent consecutive lines |
||
| 1163 | \n* # blanks |
||
| 1164 | )+ |
||
| 1165 | ) |
||
| 1166 | /xm', |
||
| 1167 | array($this, '_doBlockQuotes_callback'), $text); |
||
| 1168 | |||
| 1169 | return $text; |
||
| 1170 | } |
||
| 1171 | protected function _doBlockQuotes_callback($matches) { |
||
| 1172 | $bq = $matches[1]; |
||
| 1173 | # trim one level of quoting - trim whitespace-only lines |
||
| 1174 | $bq = preg_replace('/^[ ]*>[ ]?|^[ ]+$/m', '', $bq); |
||
| 1175 | $bq = $this->runBlockGamut($bq); # recurse |
||
| 1176 | |||
| 1177 | $bq = preg_replace('/^/m', " ", $bq); |
||
| 1178 | # These leading spaces cause problem with <pre> content, |
||
| 1179 | # so we need to fix that: |
||
| 1180 | $bq = preg_replace_callback('{(\s*<pre>.+?</pre>)}sx', |
||
| 1181 | array($this, '_doBlockQuotes_callback2'), $bq); |
||
| 1182 | |||
| 1183 | return "\n". $this->hashBlock("<blockquote>\n$bq\n</blockquote>")."\n\n"; |
||
| 1184 | } |
||
| 1185 | protected function _doBlockQuotes_callback2($matches) { |
||
| 1186 | $pre = $matches[1]; |
||
| 1187 | $pre = preg_replace('/^ /m', '', $pre); |
||
| 1188 | return $pre; |
||
| 1189 | } |
||
| 1190 | |||
| 1191 | |||
| 1192 | protected function formParagraphs($text) { |
||
| 1193 | # |
||
| 1194 | # Params: |
||
| 1195 | # $text - string to process with html <p> tags |
||
| 1196 | # |
||
| 1197 | # Strip leading and trailing lines: |
||
| 1198 | $text = preg_replace('/\A\n+|\n+\z/', '', $text); |
||
| 1199 | |||
| 1200 | $grafs = preg_split('/\n{2,}/', $text, -1, PREG_SPLIT_NO_EMPTY); |
||
| 1201 | |||
| 1202 | # |
||
| 1203 | # Wrap <p> tags and unhashify HTML blocks |
||
| 1204 | # |
||
| 1205 | foreach ($grafs as $key => $value) { |
||
| 1206 | if (!preg_match('/^B\x1A[0-9]+B$/', $value)) { |
||
| 1207 | # Is a paragraph. |
||
| 1208 | $value = $this->runSpanGamut($value); |
||
| 1209 | $value = preg_replace('/^([ ]*)/', "<p>", $value); |
||
| 1210 | $value .= "</p>"; |
||
| 1211 | $grafs[$key] = $this->unhash($value); |
||
| 1212 | } |
||
| 1213 | else { |
||
| 1214 | # Is a block. |
||
| 1215 | # Modify elements of @grafs in-place... |
||
| 1216 | $graf = $value; |
||
| 1217 | $block = $this->html_hashes[$graf]; |
||
| 1218 | $graf = $block; |
||
| 1219 | // if (preg_match('{ |
||
| 1220 | // \A |
||
| 1221 | // ( # $1 = <div> tag |
||
| 1222 | // <div \s+ |
||
| 1223 | // [^>]* |
||
| 1224 | // \b |
||
| 1225 | // markdown\s*=\s* ([\'"]) # $2 = attr quote char |
||
| 1226 | // 1 |
||
| 1227 | // \2 |
||
| 1228 | // [^>]* |
||
| 1229 | // > |
||
| 1230 | // ) |
||
| 1231 | // ( # $3 = contents |
||
| 1232 | // .* |
||
| 1233 | // ) |
||
| 1234 | // (</div>) # $4 = closing tag |
||
| 1235 | // \z |
||
| 1236 | // }xs', $block, $matches)) |
||
| 1237 | // { |
||
| 1238 | // list(, $div_open, , $div_content, $div_close) = $matches; |
||
| 1239 | // |
||
| 1240 | // # We can't call Markdown(), because that resets the hash; |
||
| 1241 | // # that initialization code should be pulled into its own sub, though. |
||
| 1242 | // $div_content = $this->hashHTMLBlocks($div_content); |
||
| 1243 | // |
||
| 1244 | // # Run document gamut methods on the content. |
||
| 1245 | // foreach ($this->document_gamut as $method => $priority) { |
||
| 1246 | // $div_content = $this->$method($div_content); |
||
| 1247 | // } |
||
| 1248 | // |
||
| 1249 | // $div_open = preg_replace( |
||
| 1250 | // '{\smarkdown\s*=\s*([\'"]).+?\1}', '', $div_open); |
||
| 1251 | // |
||
| 1252 | // $graf = $div_open . "\n" . $div_content . "\n" . $div_close; |
||
| 1253 | // } |
||
| 1254 | $grafs[$key] = $graf; |
||
| 1255 | } |
||
| 1256 | } |
||
| 1257 | |||
| 1258 | return implode("\n\n", $grafs); |
||
| 1259 | } |
||
| 1260 | |||
| 1261 | |||
| 1262 | protected function encodeAttribute($text) { |
||
| 1263 | # |
||
| 1264 | # Encode text for a double-quoted HTML attribute. This function |
||
| 1265 | # is *not* suitable for attributes enclosed in single quotes. |
||
| 1266 | # |
||
| 1267 | $text = $this->encodeAmpsAndAngles($text); |
||
| 1268 | $text = str_replace('"', '"', $text); |
||
| 1269 | return $text; |
||
| 1270 | } |
||
| 1271 | |||
| 1272 | |||
| 1273 | protected function encodeAmpsAndAngles($text) { |
||
| 1274 | # |
||
| 1275 | # Smart processing for ampersands and angle brackets that need to |
||
| 1276 | # be encoded. Valid character entities are left alone unless the |
||
| 1277 | # no-entities mode is set. |
||
| 1278 | # |
||
| 1279 | if ($this->no_entities) { |
||
| 1280 | $text = str_replace('&', '&', $text); |
||
| 1281 | } else { |
||
| 1282 | # Ampersand-encoding based entirely on Nat Irons's Amputator |
||
| 1283 | # MT plugin: <http://bumppo.net/projects/amputator/> |
||
| 1284 | $text = preg_replace('/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/', |
||
| 1285 | '&', $text);; |
||
| 1286 | } |
||
| 1287 | # Encode remaining <'s |
||
| 1288 | $text = str_replace('<', '<', $text); |
||
| 1289 | |||
| 1290 | return $text; |
||
| 1291 | } |
||
| 1292 | |||
| 1293 | |||
| 1294 | protected function doAutoLinks($text) { |
||
| 1295 | $text = preg_replace_callback('/(="|<)?\b(https?|ftp)(:\/\/[-A-Z0-9+&@#\/%?=~_|\[\]\(\)!:,\.;]*[-A-Z0-9+&@#\/%=~_|\[\])])(?=$|\W)/i', |
||
| 1296 | array($this, '_doAutoLinks_url_callback'), $text); |
||
| 1297 | $text = preg_replace_callback('{<((https?|ftp|dict):[^\'">\s]+)>}i', |
||
| 1298 | array($this, '_doAutoLinks_url_callback_replace'), $text); |
||
| 1299 | |||
| 1300 | # Email addresses: <[email protected]> |
||
| 1301 | $text = preg_replace_callback('{ |
||
| 1302 | < |
||
| 1303 | (?:mailto:)? |
||
| 1304 | ( |
||
| 1305 | (?: |
||
| 1306 | [-!#$%&\'*+/=?^_`.{|}~\w\x80-\xFF]+ |
||
| 1307 | | |
||
| 1308 | ".*?" |
||
| 1309 | ) |
||
| 1310 | \@ |
||
| 1311 | (?: |
||
| 1312 | [-a-z0-9\x80-\xFF]+(\.[-a-z0-9\x80-\xFF]+)*\.[a-z]+ |
||
| 1313 | | |
||
| 1314 | \[[\d.a-fA-F:]+\] # IPv4 & IPv6 |
||
| 1315 | ) |
||
| 1316 | ) |
||
| 1317 | > |
||
| 1318 | }xi', |
||
| 1319 | array($this, '_doAutoLinks_email_callback'), $text); |
||
| 1320 | |||
| 1321 | return $text; |
||
| 1322 | } |
||
| 1323 | protected function _doAutoLinks_url_callback_replace($matches) { |
||
| 1324 | $url = $this->encodeAttribute($matches[1]); |
||
| 1325 | $link = "<a href=\"$url\">$url</a>"; |
||
| 1326 | return $this->hashPart($link); |
||
| 1327 | } |
||
| 1328 | |||
| 1329 | private $tail; |
||
| 1330 | |||
| 1331 | protected function _doAutoLinks_url_callback($matches) { |
||
| 1332 | list ($wholeMatch, $lookbehind, $protocol, $link) = $matches; |
||
| 1333 | |||
| 1334 | if ($lookbehind) { |
||
| 1335 | return $wholeMatch; |
||
| 1336 | } |
||
| 1337 | |||
| 1338 | if ($link[strlen($link) - 1] != ')') { |
||
| 1339 | return '<' . $protocol . $link . '>'; |
||
| 1340 | } |
||
| 1341 | |||
| 1342 | $level = 0; |
||
| 1343 | if (preg_match_all("/[()]/", $link, $matches)) { |
||
| 1344 | foreach ($matches[0] as $op) { |
||
| 1345 | if ('(' == $op) { |
||
| 1346 | if ($level <= 0) { |
||
| 1347 | $level = 1; |
||
| 1348 | } else { |
||
| 1349 | $level ++; |
||
| 1350 | } |
||
| 1351 | } else { |
||
| 1352 | $level --; |
||
| 1353 | } |
||
| 1354 | } |
||
| 1355 | } |
||
| 1356 | |||
| 1357 | $this->tail = ''; |
||
| 1358 | if ($level < 0) { |
||
| 1359 | $link = preg_replace_callback("/\){1," . (- $level) . "}$/", |
||
| 1360 | array($this, '_doAutoLinks_url_callback_callback'), $link); |
||
| 1361 | } |
||
| 1362 | |||
| 1363 | $url = $this->encodeAttribute($protocol . $link); |
||
| 1364 | $link = "<a href=\"$url\">$url</a>"; |
||
| 1365 | return $this->hashPart($link) . $this->tail; |
||
| 1366 | } |
||
| 1367 | |||
| 1368 | protected function _doAutoLinks_email_callback($matches) { |
||
| 1369 | $address = $matches[1]; |
||
| 1370 | $link = $this->encodeEmailAddress($address); |
||
| 1371 | return $this->hashPart($link); |
||
| 1372 | } |
||
| 1373 | |||
| 1374 | protected function _doAutoLinks_url_callback_callback($matches) { |
||
| 1375 | $this->tail = $matches[0]; |
||
| 1376 | return ''; |
||
| 1377 | } |
||
| 1378 | |||
| 1379 | protected function encodeEmailAddress($addr) { |
||
| 1380 | # |
||
| 1381 | # Input: an email address, e.g. "[email protected]" |
||
| 1382 | # |
||
| 1383 | # Output: the email address as a mailto link, with each character |
||
| 1384 | # of the address encoded as either a decimal or hex entity, in |
||
| 1385 | # the hopes of foiling most address harvesting spam bots. E.g.: |
||
| 1386 | # |
||
| 1387 | # <p><a href="mailto:foo |
||
| 1388 | # @example.co |
||
| 1389 | # m">foo@exampl |
||
| 1390 | # e.com</a></p> |
||
| 1391 | # |
||
| 1392 | # Based by a filter by Matthew Wickline, posted to BBEdit-Talk. |
||
| 1393 | # With some optimizations by Milian Wolff. |
||
| 1394 | # |
||
| 1395 | $addr = "mailto:" . $addr; |
||
| 1396 | $chars = preg_split('/(?<!^)(?!$)/', $addr); |
||
| 1397 | $seed = (int)abs(crc32($addr) / strlen($addr)); # Deterministic seed. |
||
| 1398 | |||
| 1399 | foreach ($chars as $key => $char) { |
||
| 1400 | $ord = ord($char); |
||
| 1401 | # Ignore non-ascii chars. |
||
| 1402 | if ($ord < 128) { |
||
| 1403 | $r = ($seed * (1 + $key)) % 100; # Pseudo-random function. |
||
| 1404 | # roughly 10% raw, 45% hex, 45% dec |
||
| 1405 | # '@' *must* be encoded. I insist. |
||
| 1406 | if ($r > 90 && $char != '@') /* do nothing */; |
||
| 1407 | else if ($r < 45) $chars[$key] = '&#x'.dechex($ord).';'; |
||
| 1408 | else $chars[$key] = '&#'.$ord.';'; |
||
| 1409 | } |
||
| 1410 | } |
||
| 1411 | |||
| 1412 | $addr = implode('', $chars); |
||
| 1413 | $text = implode('', array_slice($chars, 7)); # text without `mailto:` |
||
| 1414 | $addr = "<a href=\"$addr\">$text</a>"; |
||
| 1415 | |||
| 1416 | return $addr; |
||
| 1417 | } |
||
| 1418 | |||
| 1419 | |||
| 1420 | protected function parseSpan($str) { |
||
| 1421 | # |
||
| 1422 | # Take the string $str and parse it into tokens, hashing embeded HTML, |
||
| 1423 | # escaped characters and handling code spans. |
||
| 1424 | # |
||
| 1425 | $output = ''; |
||
| 1426 | |||
| 1427 | $span_re = '{ |
||
| 1428 | ( |
||
| 1429 | \\\\'.$this->escape_chars_re.' |
||
| 1430 | | |
||
| 1431 | (?<![`\\\\]) |
||
| 1432 | `+ # code span marker |
||
| 1433 | '.( $this->no_markup ? '' : ' |
||
| 1434 | | |
||
| 1435 | <!-- .*? --> # comment |
||
| 1436 | | |
||
| 1437 | <\?.*?\?> | <%.*?%> # processing instruction |
||
| 1438 | | |
||
| 1439 | <[!$]?[-a-zA-Z0-9:_]+ # regular tags |
||
| 1440 | (?> |
||
| 1441 | \s |
||
| 1442 | (?>[^"\'>]+|"[^"]*"|\'[^\']*\')* |
||
| 1443 | )? |
||
| 1444 | > |
||
| 1445 | | |
||
| 1446 | <[-a-zA-Z0-9:_]+\s*/> # xml-style empty tag |
||
| 1447 | | |
||
| 1448 | </[-a-zA-Z0-9:_]+\s*> # closing tag |
||
| 1449 | ').' |
||
| 1450 | ) |
||
| 1451 | }xs'; |
||
| 1452 | |||
| 1453 | while (1) { |
||
| 1454 | # |
||
| 1455 | # Each loop iteration seach for either the next tag, the next |
||
| 1456 | # openning code span marker, or the next escaped character. |
||
| 1457 | # Each token is then passed to handleSpanToken. |
||
| 1458 | # |
||
| 1459 | $parts = preg_split($span_re, $str, 2, PREG_SPLIT_DELIM_CAPTURE); |
||
| 1460 | |||
| 1461 | # Create token from text preceding tag. |
||
| 1462 | if ($parts[0] != "") { |
||
| 1463 | $output .= $parts[0]; |
||
| 1464 | } |
||
| 1465 | |||
| 1466 | # Check if we reach the end. |
||
| 1467 | if (isset($parts[1])) { |
||
| 1468 | $output .= $this->handleSpanToken($parts[1], $parts[2]); |
||
| 1469 | $str = $parts[2]; |
||
| 1470 | } |
||
| 1471 | else { |
||
| 1472 | break; |
||
| 1473 | } |
||
| 1474 | } |
||
| 1475 | |||
| 1476 | return $output; |
||
| 1477 | } |
||
| 1478 | |||
| 1479 | |||
| 1480 | protected function handleSpanToken($token, &$str) { |
||
| 1481 | # |
||
| 1482 | # Handle $token provided by parseSpan by determining its nature and |
||
| 1483 | # returning the corresponding value that should replace it. |
||
| 1484 | # |
||
| 1485 | switch ($token{0}) { |
||
| 1486 | case "\\": |
||
| 1487 | return $this->hashPart("&#". ord($token{1}). ";"); |
||
| 1488 | case "`": |
||
| 1489 | # Search for end marker in remaining text. |
||
| 1490 | if (preg_match('/^(.*?[^`])'.preg_quote($token).'(?!`)(.*)$/sm', |
||
| 1491 | $str, $matches)) |
||
| 1492 | { |
||
| 1493 | $str = $matches[2]; |
||
| 1494 | $codespan = $this->makeCodeSpan($matches[1]); |
||
| 1495 | return $this->hashPart($codespan); |
||
| 1496 | } |
||
| 1497 | return $token; // return as text since no ending marker found. |
||
| 1498 | default: |
||
| 1499 | return $this->hashPart($token); |
||
| 1500 | } |
||
| 1501 | } |
||
| 1502 | |||
| 1503 | |||
| 1504 | protected function outdent($text) { |
||
| 1505 | # |
||
| 1506 | # Remove one level of line-leading tabs or spaces |
||
| 1507 | # |
||
| 1508 | return preg_replace('/^(\t|[ ]{1,'.$this->tab_width.'})/m', '', $text); |
||
| 1509 | } |
||
| 1510 | |||
| 1511 | |||
| 1512 | # String length function for detab. `_initDetab` will create a function to |
||
| 1513 | # hanlde UTF-8 if the default function does not exist. |
||
| 1514 | protected $utf8_strlen = 'mb_strlen'; |
||
| 1515 | |||
| 1516 | protected function detab($text) { |
||
| 1517 | # |
||
| 1518 | # Replace tabs with the appropriate amount of space. |
||
| 1519 | # |
||
| 1520 | # For each line we separate the line in blocks delemited by |
||
| 1521 | # tab characters. Then we reconstruct every line by adding the |
||
| 1522 | # appropriate number of space between each blocks. |
||
| 1523 | |||
| 1524 | $text = preg_replace_callback('/^.*\t.*$/m', |
||
| 1525 | array($this, '_detab_callback'), $text); |
||
| 1526 | |||
| 1527 | return $text; |
||
| 1528 | } |
||
| 1529 | protected function _detab_callback($matches) { |
||
| 1530 | $line = $matches[0]; |
||
| 1531 | $strlen = $this->utf8_strlen; # strlen function for UTF-8. |
||
| 1532 | |||
| 1533 | # Split in blocks. |
||
| 1534 | $blocks = explode("\t", $line); |
||
| 1535 | # Add each blocks to the line. |
||
| 1536 | $line = $blocks[0]; |
||
| 1537 | unset($blocks[0]); # Do not add first block twice. |
||
| 1538 | foreach ($blocks as $block) { |
||
| 1539 | # Calculate amount of space, insert spaces, insert block. |
||
| 1540 | $amount = $this->tab_width - |
||
| 1541 | call_user_func($strlen, $line, 'UTF-8') % $this->tab_width; |
||
| 1542 | $line .= str_repeat(" ", $amount) . $block; |
||
| 1543 | } |
||
| 1544 | return $line; |
||
| 1545 | } |
||
| 1546 | protected function _initDetab() { |
||
| 1547 | # |
||
| 1548 | # Check for the availability of the function in the `utf8_strlen` property |
||
| 1549 | # (initially `mb_strlen`). If the function is not available, create a |
||
| 1550 | # function that will loosely count the number of UTF-8 characters with a |
||
| 1551 | # regular expression. |
||
| 1552 | # |
||
| 1553 | if (function_exists($this->utf8_strlen)) return; |
||
| 1554 | $this->utf8_strlen = array($this, '_utf8_strlen'); |
||
| 1555 | } |
||
| 1556 | |||
| 1557 | protected function _utf8_strlen($text) { |
||
| 1558 | return preg_match_all( |
||
| 1559 | "/[\\x00-\\xBF]|[\\xC0-\\xFF][\\x80-\\xBF]*/", |
||
| 1560 | $text, $m); |
||
| 1561 | } |
||
| 1562 | |||
| 1563 | |||
| 1564 | protected function unhash($text) { |
||
| 1565 | # |
||
| 1566 | # Swap back in all the tags hashed by _HashHTMLBlocks. |
||
| 1567 | # |
||
| 1568 | return preg_replace_callback('/(.)\x1A[0-9]+\1/', |
||
| 1569 | array($this, '_unhash_callback'), $text); |
||
| 1570 | } |
||
| 1571 | protected function _unhash_callback($matches) { |
||
| 1572 | return $this->html_hashes[trim($matches[0])]; |
||
| 1573 | } |
||
| 1574 | } |
||
| 1575 | |||
| 3326 |