Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 16 | class Markdown implements MarkdownInterface { |
||
| 17 | /** |
||
| 18 | * Define the package version |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | const MARKDOWNLIB_VERSION = "1.9.0"; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Simple function interface - Initialize the parser and return the result |
||
| 25 | * of its transform method. This will work fine for derived classes too. |
||
| 26 | * |
||
| 27 | * @api |
||
| 28 | * |
||
| 29 | * @param string $text |
||
| 30 | * @return string |
||
| 31 | */ |
||
| 32 | 107 | public static function defaultTransform($text) { |
|
| 48 | |||
| 49 | /** |
||
| 50 | * Configuration variables |
||
| 51 | */ |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Change to ">" for HTML output. |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | public $empty_element_suffix = " />"; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The width of indentation of the output markup |
||
| 61 | * @var int |
||
| 62 | */ |
||
| 63 | public $tab_width = 4; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Change to `true` to disallow markup or entities. |
||
| 67 | * @var boolean |
||
| 68 | */ |
||
| 69 | public $no_markup = false; |
||
| 70 | public $no_entities = false; |
||
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * Change to `true` to enable line breaks on \n without two trailling spaces |
||
| 75 | * @var boolean |
||
| 76 | */ |
||
| 77 | public $hard_wrap = false; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Predefined URLs and titles for reference links and images. |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | public $predef_urls = array(); |
||
| 84 | public $predef_titles = array(); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Optional filter function for URLs |
||
| 88 | * @var callable|null |
||
| 89 | */ |
||
| 90 | public $url_filter_func = null; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Optional header id="" generation callback function. |
||
| 94 | * @var callable|null |
||
| 95 | */ |
||
| 96 | public $header_id_func = null; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Optional function for converting code block content to HTML |
||
| 100 | * @var callable|null |
||
| 101 | */ |
||
| 102 | public $code_block_content_func = null; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Optional function for converting code span content to HTML. |
||
| 106 | * @var callable|null |
||
| 107 | */ |
||
| 108 | public $code_span_content_func = null; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Class attribute to toggle "enhanced ordered list" behaviour |
||
| 112 | * setting this to true will allow ordered lists to start from the index |
||
| 113 | * number that is defined first. |
||
| 114 | * |
||
| 115 | * For example: |
||
| 116 | * 2. List item two |
||
| 117 | * 3. List item three |
||
| 118 | * |
||
| 119 | * Becomes: |
||
| 120 | * <ol start="2"> |
||
| 121 | * <li>List item two</li> |
||
| 122 | * <li>List item three</li> |
||
| 123 | * </ol> |
||
| 124 | * |
||
| 125 | * @var bool |
||
| 126 | */ |
||
| 127 | public $enhanced_ordered_list = false; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Parser implementation |
||
| 131 | */ |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Regex to match balanced [brackets]. |
||
| 135 | * Needed to insert a maximum bracked depth while converting to PHP. |
||
| 136 | * @var int |
||
| 137 | */ |
||
| 138 | protected $nested_brackets_depth = 6; |
||
| 139 | protected $nested_brackets_re; |
||
| 140 | |||
| 141 | protected $nested_url_parenthesis_depth = 4; |
||
| 142 | protected $nested_url_parenthesis_re; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Table of hash values for escaped characters: |
||
| 146 | * @var string |
||
| 147 | */ |
||
| 148 | protected $escape_chars = '\`*_{}[]()>#+-.!'; |
||
| 149 | protected $escape_chars_re; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Constructor function. Initialize appropriate member variables. |
||
| 153 | * @return void |
||
|
|
|||
| 154 | */ |
||
| 155 | 5 | public function __construct() { |
|
| 174 | |||
| 175 | |||
| 176 | /** |
||
| 177 | * Internal hashes used during transformation. |
||
| 178 | * @var array |
||
| 179 | */ |
||
| 180 | protected $urls = array(); |
||
| 181 | protected $titles = array(); |
||
| 182 | protected $html_hashes = array(); |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Status flag to avoid invalid nesting. |
||
| 186 | * @var boolean |
||
| 187 | */ |
||
| 188 | protected $in_anchor = false; |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Status flag to avoid invalid nesting. |
||
| 192 | * @var boolean |
||
| 193 | */ |
||
| 194 | protected $in_emphasis_processing = false; |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Called before the transformation process starts to setup parser states. |
||
| 198 | * @return void |
||
| 199 | */ |
||
| 200 | 110 | protected function setup() { |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Called after the transformation process to clear any variable which may |
||
| 211 | * be taking up memory unnecessarly. |
||
| 212 | * @return void |
||
| 213 | */ |
||
| 214 | 102 | protected function teardown() { |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Main function. Performs some preprocessing on the input text and pass |
||
| 222 | * it through the document gamut. |
||
| 223 | * |
||
| 224 | * @api |
||
| 225 | * |
||
| 226 | * @param string $text |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | 110 | public function transform($text) { |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Define the document gamut |
||
| 266 | * @var array |
||
| 267 | */ |
||
| 268 | protected $document_gamut = array( |
||
| 269 | // Strip link definitions, store in hashes. |
||
| 270 | "stripLinkDefinitions" => 20, |
||
| 271 | "runBasicBlockGamut" => 30, |
||
| 272 | ); |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Strips link definitions from text, stores the URLs and titles in |
||
| 276 | * hash references |
||
| 277 | * @param string $text |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | 48 | View Code Duplication | protected function stripLinkDefinitions($text) { |
| 312 | |||
| 313 | /** |
||
| 314 | * The callback to strip link definitions |
||
| 315 | * @param array $matches |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | 9 | protected function _stripLinkDefinitions_callback($matches) { |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Hashify HTML blocks |
||
| 328 | * @param string $text |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | 48 | protected function hashHTMLBlocks($text) { |
|
| 478 | |||
| 479 | /** |
||
| 480 | * The callback for hashing HTML blocks |
||
| 481 | * @param string $matches |
||
| 482 | * @return string |
||
| 483 | */ |
||
| 484 | 9 | protected function _hashHTMLBlocks_callback($matches) { |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Called whenever a tag must be hashed when a function insert an atomic |
||
| 492 | * element in the text stream. Passing $text to through this function gives |
||
| 493 | * a unique text-token which will be reverted back when calling unhash. |
||
| 494 | * |
||
| 495 | * The $boundary argument specify what character should be used to surround |
||
| 496 | * the token. By convension, "B" is used for block elements that needs not |
||
| 497 | * to be wrapped into paragraph tags at the end, ":" is used for elements |
||
| 498 | * that are word separators and "X" is used in the general case. |
||
| 499 | * |
||
| 500 | * @param string $text |
||
| 501 | * @param string $boundary |
||
| 502 | * @return string |
||
| 503 | */ |
||
| 504 | 110 | protected function hashPart($text, $boundary = 'X') { |
|
| 515 | |||
| 516 | /** |
||
| 517 | * Shortcut function for hashPart with block-level boundaries. |
||
| 518 | * @param string $text |
||
| 519 | * @return string |
||
| 520 | */ |
||
| 521 | 110 | protected function hashBlock($text) { |
|
| 524 | |||
| 525 | /** |
||
| 526 | * Define the block gamut - these are all the transformations that form |
||
| 527 | * block-level tags like paragraphs, headers, and list items. |
||
| 528 | * @var array |
||
| 529 | */ |
||
| 530 | protected $block_gamut = array( |
||
| 531 | "doHeaders" => 10, |
||
| 532 | "doHorizontalRules" => 20, |
||
| 533 | "doLists" => 40, |
||
| 534 | "doCodeBlocks" => 50, |
||
| 535 | "doBlockQuotes" => 60, |
||
| 536 | ); |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Run block gamut tranformations. |
||
| 540 | * |
||
| 541 | * We need to escape raw HTML in Markdown source before doing anything |
||
| 542 | * else. This need to be done for each block, and not only at the |
||
| 543 | * begining in the Markdown function since hashed blocks can be part of |
||
| 544 | * list items and could have been indented. Indented blocks would have |
||
| 545 | * been seen as a code block in a previous pass of hashHTMLBlocks. |
||
| 546 | * |
||
| 547 | * @param string $text |
||
| 548 | * @return string |
||
| 549 | */ |
||
| 550 | 20 | protected function runBlockGamut($text) { |
|
| 554 | |||
| 555 | /** |
||
| 556 | * Run block gamut tranformations, without hashing HTML blocks. This is |
||
| 557 | * useful when HTML blocks are known to be already hashed, like in the first |
||
| 558 | * whole-document pass. |
||
| 559 | * |
||
| 560 | * @param string $text |
||
| 561 | * @return string |
||
| 562 | */ |
||
| 563 | 110 | protected function runBasicBlockGamut($text) { |
|
| 574 | |||
| 575 | /** |
||
| 576 | * Convert horizontal rules |
||
| 577 | * @param string $text |
||
| 578 | * @return string |
||
| 579 | */ |
||
| 580 | 110 | protected function doHorizontalRules($text) { |
|
| 596 | |||
| 597 | /** |
||
| 598 | * These are all the transformations that occur *within* block-level |
||
| 599 | * tags like paragraphs, headers, and list items. |
||
| 600 | * @var array |
||
| 601 | */ |
||
| 602 | protected $span_gamut = array( |
||
| 603 | // Process character escapes, code spans, and inline HTML |
||
| 604 | // in one shot. |
||
| 605 | "parseSpan" => -30, |
||
| 606 | // Process anchor and image tags. Images must come first, |
||
| 607 | // because ![foo][f] looks like an anchor. |
||
| 608 | "doImages" => 10, |
||
| 609 | "doAnchors" => 20, |
||
| 610 | // Make links out of things like `<https://example.com/>` |
||
| 611 | // Must come after doAnchors, because you can use < and > |
||
| 612 | // delimiters in inline links like [this](<url>). |
||
| 613 | "doAutoLinks" => 30, |
||
| 614 | "encodeAmpsAndAngles" => 40, |
||
| 615 | "doItalicsAndBold" => 50, |
||
| 616 | "doHardBreaks" => 60, |
||
| 617 | ); |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Run span gamut transformations |
||
| 621 | * @param string $text |
||
| 622 | * @return string |
||
| 623 | */ |
||
| 624 | 109 | protected function runSpanGamut($text) { |
|
| 631 | |||
| 632 | /** |
||
| 633 | * Do hard breaks |
||
| 634 | * @param string $text |
||
| 635 | * @return string |
||
| 636 | */ |
||
| 637 | 108 | protected function doHardBreaks($text) { |
|
| 646 | |||
| 647 | /** |
||
| 648 | * Trigger part hashing for the hard break (callback method) |
||
| 649 | * @param array $matches |
||
| 650 | * @return string |
||
| 651 | */ |
||
| 652 | 3 | protected function _doHardBreaks_callback($matches) { |
|
| 655 | |||
| 656 | /** |
||
| 657 | * Turn Markdown link shortcuts into XHTML <a> tags. |
||
| 658 | * @param string $text |
||
| 659 | * @return string |
||
| 660 | */ |
||
| 661 | 47 | View Code Duplication | protected function doAnchors($text) { |
| 724 | |||
| 725 | /** |
||
| 726 | * Callback method to parse referenced anchors |
||
| 727 | * @param string $matches |
||
| 728 | * @return string |
||
| 729 | */ |
||
| 730 | 9 | protected function _doAnchors_reference_callback($matches) { |
|
| 763 | |||
| 764 | /** |
||
| 765 | * Callback method to parse inline anchors |
||
| 766 | * @param string $matches |
||
| 767 | * @return string |
||
| 768 | */ |
||
| 769 | 10 | protected function _doAnchors_inline_callback($matches) { |
|
| 794 | |||
| 795 | /** |
||
| 796 | * Turn Markdown image shortcuts into <img> tags. |
||
| 797 | * @param string $text |
||
| 798 | * @return string |
||
| 799 | */ |
||
| 800 | 47 | View Code Duplication | protected function doImages($text) { |
| 848 | |||
| 849 | /** |
||
| 850 | * Callback to parse references image tags |
||
| 851 | * @param array $matches |
||
| 852 | * @return string |
||
| 853 | */ |
||
| 854 | 1 | protected function _doImages_reference_callback($matches) { |
|
| 881 | |||
| 882 | /** |
||
| 883 | * Callback to parse inline image tags |
||
| 884 | * @param array $matches |
||
| 885 | * @return string |
||
| 886 | */ |
||
| 887 | 2 | protected function _doImages_inline_callback($matches) { |
|
| 904 | |||
| 905 | /** |
||
| 906 | * Parse Markdown heading elements to HTML |
||
| 907 | * @param string $text |
||
| 908 | * @return string |
||
| 909 | */ |
||
| 910 | 48 | protected function doHeaders($text) { |
|
| 942 | |||
| 943 | /** |
||
| 944 | * Setext header parsing callback |
||
| 945 | * @param array $matches |
||
| 946 | * @return string |
||
| 947 | */ |
||
| 948 | 5 | protected function _doHeaders_callback_setext($matches) { |
|
| 962 | |||
| 963 | /** |
||
| 964 | * ATX header parsing callback |
||
| 965 | * @param array $matches |
||
| 966 | * @return string |
||
| 967 | */ |
||
| 968 | 8 | protected function _doHeaders_callback_atx($matches) { |
|
| 976 | |||
| 977 | /** |
||
| 978 | * If a header_id_func property is set, we can use it to automatically |
||
| 979 | * generate an id attribute. |
||
| 980 | * |
||
| 981 | * This method returns a string in the form id="foo", or an empty string |
||
| 982 | * otherwise. |
||
| 983 | * @param string $headerValue |
||
| 984 | * @return string |
||
| 985 | */ |
||
| 986 | 9 | protected function _generateIdFromHeaderValue($headerValue) { |
|
| 998 | |||
| 999 | /** |
||
| 1000 | * Form HTML ordered (numbered) and unordered (bulleted) lists. |
||
| 1001 | * @param string $text |
||
| 1002 | * @return string |
||
| 1003 | */ |
||
| 1004 | 110 | protected function doLists($text) { |
|
| 1065 | |||
| 1066 | /** |
||
| 1067 | * List parsing callback |
||
| 1068 | * @param array $matches |
||
| 1069 | * @return string |
||
| 1070 | */ |
||
| 1071 | 25 | protected function _doLists_callback($matches) { |
|
| 1072 | // Re-usable patterns to match list item bullets and number markers: |
||
| 1073 | 25 | $marker_ul_re = '[*+-]'; |
|
| 1074 | 25 | $marker_ol_re = '\d+[\.]'; |
|
| 1075 | 25 | $marker_any_re = "(?:$marker_ul_re|$marker_ol_re)"; |
|
| 1076 | 25 | $marker_ol_start_re = '[0-9]+'; |
|
| 1077 | |||
| 1078 | 25 | $list = $matches[1]; |
|
| 1079 | 25 | $list_type = preg_match("/$marker_ul_re/", $matches[4]) ? "ul" : "ol"; |
|
| 1080 | |||
| 1081 | 25 | $marker_any_re = ( $list_type == "ul" ? $marker_ul_re : $marker_ol_re ); |
|
| 1082 | |||
| 1083 | 25 | $list .= "\n"; |
|
| 1084 | 25 | $result = $this->processListItems($list, $marker_any_re); |
|
| 1085 | |||
| 1086 | 24 | $ol_start = 1; |
|
| 1087 | 24 | if ($this->enhanced_ordered_list) { |
|
| 1088 | // Get the start number for ordered list. |
||
| 1089 | 14 | if ($list_type == 'ol') { |
|
| 1090 | 6 | $ol_start_array = array(); |
|
| 1091 | 6 | $ol_start_check = preg_match("/$marker_ol_start_re/", $matches[4], $ol_start_array); |
|
| 1092 | 6 | if ($ol_start_check){ |
|
| 1093 | 6 | $ol_start = $ol_start_array[0]; |
|
| 1094 | } |
||
| 1095 | } |
||
| 1096 | } |
||
| 1097 | |||
| 1098 | 24 | if ($ol_start > 1 && $list_type == 'ol'){ |
|
| 1099 | 1 | $result = $this->hashBlock("<$list_type start=\"$ol_start\">\n" . $result . "</$list_type>"); |
|
| 1100 | } else { |
||
| 1101 | 24 | $result = $this->hashBlock("<$list_type>\n" . $result . "</$list_type>"); |
|
| 1102 | } |
||
| 1103 | 24 | return "\n". $result ."\n\n"; |
|
| 1104 | } |
||
| 1105 | |||
| 1106 | /** |
||
| 1107 | * Nesting tracker for list levels |
||
| 1108 | * @var integer |
||
| 1109 | */ |
||
| 1110 | protected $list_level = 0; |
||
| 1111 | |||
| 1112 | /** |
||
| 1113 | * Process the contents of a single ordered or unordered list, splitting it |
||
| 1114 | * into individual list items. |
||
| 1115 | * @param string $list_str |
||
| 1116 | * @param string $marker_any_re |
||
| 1117 | * @return string |
||
| 1118 | */ |
||
| 1119 | 25 | protected function processListItems($list_str, $marker_any_re) { |
|
| 1162 | |||
| 1163 | /** |
||
| 1164 | * List item parsing callback |
||
| 1165 | * @param array $matches |
||
| 1166 | * @return string |
||
| 1167 | */ |
||
| 1168 | 25 | protected function _processListItems_callback($matches) { |
|
| 1189 | |||
| 1190 | /** |
||
| 1191 | * Process Markdown `<pre><code>` blocks. |
||
| 1192 | * @param string $text |
||
| 1193 | * @return string |
||
| 1194 | */ |
||
| 1195 | 109 | protected function doCodeBlocks($text) { |
|
| 1210 | |||
| 1211 | /** |
||
| 1212 | * Code block parsing callback |
||
| 1213 | * @param array $matches |
||
| 1214 | * @return string |
||
| 1215 | */ |
||
| 1216 | 34 | protected function _doCodeBlocks_callback($matches) { |
|
| 1232 | |||
| 1233 | /** |
||
| 1234 | * Create a code span markup for $code. Called from handleSpanToken. |
||
| 1235 | * @param string $code |
||
| 1236 | * @return string |
||
| 1237 | */ |
||
| 1238 | 20 | protected function makeCodeSpan($code) { |
|
| 1239 | 20 | View Code Duplication | if (is_callable($this->code_span_content_func)) { |
| 1240 | $code = call_user_func($this->code_span_content_func, $code); |
||
| 1241 | } else { |
||
| 1242 | 20 | $code = htmlspecialchars(trim($code), ENT_NOQUOTES); |
|
| 1243 | } |
||
| 1244 | 20 | return $this->hashPart("<code>$code</code>"); |
|
| 1245 | } |
||
| 1246 | |||
| 1247 | /** |
||
| 1248 | * Define the emphasis operators with their regex matches |
||
| 1249 | * @var array |
||
| 1250 | */ |
||
| 1251 | protected $em_relist = array( |
||
| 1252 | '' => '(?:(?<!\*)\*(?!\*)|(?<!_)_(?!_))(?![\.,:;]?\s)', |
||
| 1253 | '*' => '(?<![\s*])\*(?!\*)', |
||
| 1254 | '_' => '(?<![\s_])_(?!_)', |
||
| 1255 | ); |
||
| 1256 | |||
| 1257 | /** |
||
| 1258 | * Define the strong operators with their regex matches |
||
| 1259 | * @var array |
||
| 1260 | */ |
||
| 1261 | protected $strong_relist = array( |
||
| 1262 | '' => '(?:(?<!\*)\*\*(?!\*)|(?<!_)__(?!_))(?![\.,:;]?\s)', |
||
| 1263 | '**' => '(?<![\s*])\*\*(?!\*)', |
||
| 1264 | '__' => '(?<![\s_])__(?!_)', |
||
| 1265 | ); |
||
| 1266 | |||
| 1267 | /** |
||
| 1268 | * Define the emphasis + strong operators with their regex matches |
||
| 1269 | * @var array |
||
| 1270 | */ |
||
| 1271 | protected $em_strong_relist = array( |
||
| 1272 | '' => '(?:(?<!\*)\*\*\*(?!\*)|(?<!_)___(?!_))(?![\.,:;]?\s)', |
||
| 1273 | '***' => '(?<![\s*])\*\*\*(?!\*)', |
||
| 1274 | '___' => '(?<![\s_])___(?!_)', |
||
| 1275 | ); |
||
| 1276 | |||
| 1277 | /** |
||
| 1278 | * Container for prepared regular expressions |
||
| 1279 | * @var array |
||
| 1280 | */ |
||
| 1281 | protected $em_strong_prepared_relist; |
||
| 1282 | |||
| 1283 | /** |
||
| 1284 | * Prepare regular expressions for searching emphasis tokens in any |
||
| 1285 | * context. |
||
| 1286 | * @return void |
||
| 1287 | */ |
||
| 1288 | 5 | protected function prepareItalicsAndBold() { |
|
| 1305 | |||
| 1306 | /** |
||
| 1307 | * Convert Markdown italics (emphasis) and bold (strong) to HTML |
||
| 1308 | * @param string $text |
||
| 1309 | * @return string |
||
| 1310 | */ |
||
| 1311 | 108 | protected function doItalicsAndBold($text) { |
|
| 1435 | |||
| 1436 | /** |
||
| 1437 | * Parse Markdown blockquotes to HTML |
||
| 1438 | * @param string $text |
||
| 1439 | * @return string |
||
| 1440 | */ |
||
| 1441 | 109 | protected function doBlockQuotes($text) { |
|
| 1456 | |||
| 1457 | /** |
||
| 1458 | * Blockquote parsing callback |
||
| 1459 | * @param array $matches |
||
| 1460 | * @return string |
||
| 1461 | */ |
||
| 1462 | 11 | protected function _doBlockQuotes_callback($matches) { |
|
| 1476 | |||
| 1477 | /** |
||
| 1478 | * Blockquote parsing callback |
||
| 1479 | * @param array $matches |
||
| 1480 | * @return string |
||
| 1481 | */ |
||
| 1482 | 2 | protected function _doBlockQuotes_callback2($matches) { |
|
| 1487 | |||
| 1488 | /** |
||
| 1489 | * Parse paragraphs |
||
| 1490 | * |
||
| 1491 | * @param string $text String to process in paragraphs |
||
| 1492 | * @param boolean $wrap_in_p Whether paragraphs should be wrapped in <p> tags |
||
| 1493 | * @return string |
||
| 1494 | */ |
||
| 1495 | 48 | protected function formParagraphs($text, $wrap_in_p = true) { |
|
| 1558 | |||
| 1559 | /** |
||
| 1560 | * Encode text for a double-quoted HTML attribute. This function |
||
| 1561 | * is *not* suitable for attributes enclosed in single quotes. |
||
| 1562 | * @param string $text |
||
| 1563 | * @return string |
||
| 1564 | */ |
||
| 1565 | 30 | protected function encodeAttribute($text) { |
|
| 1570 | |||
| 1571 | /** |
||
| 1572 | * Encode text for a double-quoted HTML attribute containing a URL, |
||
| 1573 | * applying the URL filter if set. Also generates the textual |
||
| 1574 | * representation for the URL (removing mailto: or tel:) storing it in $text. |
||
| 1575 | * This function is *not* suitable for attributes enclosed in single quotes. |
||
| 1576 | * |
||
| 1577 | * @param string $url |
||
| 1578 | * @param string $text Passed by reference |
||
| 1579 | * @return string URL |
||
| 1580 | */ |
||
| 1581 | 27 | protected function encodeURLAttribute($url, &$text = null) { |
|
| 1598 | |||
| 1599 | /** |
||
| 1600 | * Smart processing for ampersands and angle brackets that need to |
||
| 1601 | * be encoded. Valid character entities are left alone unless the |
||
| 1602 | * no-entities mode is set. |
||
| 1603 | * @param string $text |
||
| 1604 | * @return string |
||
| 1605 | */ |
||
| 1606 | 108 | protected function encodeAmpsAndAngles($text) { |
|
| 1620 | |||
| 1621 | /** |
||
| 1622 | * Parse Markdown automatic links to anchor HTML tags |
||
| 1623 | * @param string $text |
||
| 1624 | * @return string |
||
| 1625 | */ |
||
| 1626 | 108 | protected function doAutoLinks($text) { |
|
| 1653 | |||
| 1654 | /** |
||
| 1655 | * Parse URL callback |
||
| 1656 | * @param array $matches |
||
| 1657 | * @return string |
||
| 1658 | */ |
||
| 1659 | 4 | protected function _doAutoLinks_url_callback($matches) { |
|
| 1664 | |||
| 1665 | /** |
||
| 1666 | * Parse email address callback |
||
| 1667 | * @param array $matches |
||
| 1668 | * @return string |
||
| 1669 | */ |
||
| 1670 | 4 | protected function _doAutoLinks_email_callback($matches) { |
|
| 1676 | |||
| 1677 | /** |
||
| 1678 | * Input: some text to obfuscate, e.g. "mailto:[email protected]" |
||
| 1679 | * |
||
| 1680 | * Output: the same text but with most characters encoded as either a |
||
| 1681 | * decimal or hex entity, in the hopes of foiling most address |
||
| 1682 | * harvesting spam bots. E.g.: |
||
| 1683 | * |
||
| 1684 | * mailto:foo |
||
| 1685 | * @example.co |
||
| 1686 | * m |
||
| 1687 | * |
||
| 1688 | * Note: the additional output $tail is assigned the same value as the |
||
| 1689 | * ouput, minus the number of characters specified by $head_length. |
||
| 1690 | * |
||
| 1691 | * Based by a filter by Matthew Wickline, posted to BBEdit-Talk. |
||
| 1692 | * With some optimizations by Milian Wolff. Forced encoding of HTML |
||
| 1693 | * attribute special characters by Allan Odgaard. |
||
| 1694 | * |
||
| 1695 | * @param string $text |
||
| 1696 | * @param string $tail Passed by reference |
||
| 1697 | * @param integer $head_length |
||
| 1698 | * @return string |
||
| 1699 | */ |
||
| 1700 | 4 | protected function encodeEntityObfuscatedAttribute($text, &$tail = null, $head_length = 0) { |
|
| 1731 | |||
| 1732 | /** |
||
| 1733 | * Take the string $str and parse it into tokens, hashing embeded HTML, |
||
| 1734 | * escaped characters and handling code spans. |
||
| 1735 | * @param string $str |
||
| 1736 | * @return string |
||
| 1737 | */ |
||
| 1738 | 109 | protected function parseSpan($str) { |
|
| 1789 | |||
| 1790 | /** |
||
| 1791 | * Handle $token provided by parseSpan by determining its nature and |
||
| 1792 | * returning the corresponding value that should replace it. |
||
| 1793 | * @param string $token |
||
| 1794 | * @param string $str Passed by reference |
||
| 1795 | * @return string |
||
| 1796 | */ |
||
| 1797 | 34 | protected function handleSpanToken($token, &$str) { |
|
| 1815 | |||
| 1816 | /** |
||
| 1817 | * Remove one level of line-leading tabs or spaces |
||
| 1818 | * @param string $text |
||
| 1819 | * @return string |
||
| 1820 | */ |
||
| 1821 | 48 | protected function outdent($text) { |
|
| 1824 | |||
| 1825 | |||
| 1826 | /** |
||
| 1827 | * String length function for detab. `_initDetab` will create a function to |
||
| 1828 | * handle UTF-8 if the default function does not exist. |
||
| 1829 | * @var string |
||
| 1830 | */ |
||
| 1831 | protected $utf8_strlen = 'mb_strlen'; |
||
| 1832 | |||
| 1833 | /** |
||
| 1834 | * Replace tabs with the appropriate amount of spaces. |
||
| 1835 | * |
||
| 1836 | * For each line we separate the line in blocks delemited by tab characters. |
||
| 1837 | * Then we reconstruct every line by adding the appropriate number of space |
||
| 1838 | * between each blocks. |
||
| 1839 | * |
||
| 1840 | * @param string $text |
||
| 1841 | * @return string |
||
| 1842 | */ |
||
| 1843 | 110 | protected function detab($text) { |
|
| 1849 | |||
| 1850 | /** |
||
| 1851 | * Replace tabs callback |
||
| 1852 | * @param string $matches |
||
| 1853 | * @return string |
||
| 1854 | */ |
||
| 1855 | 34 | protected function _detab_callback($matches) { |
|
| 1872 | |||
| 1873 | /** |
||
| 1874 | * Check for the availability of the function in the `utf8_strlen` property |
||
| 1875 | * (initially `mb_strlen`). If the function is not available, create a |
||
| 1876 | * function that will loosely count the number of UTF-8 characters with a |
||
| 1877 | * regular expression. |
||
| 1878 | * @return void |
||
| 1879 | */ |
||
| 1880 | 5 | protected function _initDetab() { |
|
| 1890 | |||
| 1891 | /** |
||
| 1892 | * Swap back in all the tags hashed by _HashHTMLBlocks. |
||
| 1893 | * @param string $text |
||
| 1894 | * @return string |
||
| 1895 | */ |
||
| 1896 | 110 | protected function unhash($text) { |
|
| 1900 | |||
| 1901 | /** |
||
| 1902 | * Unhashing callback |
||
| 1903 | * @param array $matches |
||
| 1904 | * @return string |
||
| 1905 | */ |
||
| 1906 | 80 | protected function _unhash_callback($matches) { |
|
| 1909 | } |
||
| 1910 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.