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 | 104 | 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 | 19 | 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 | 107 | 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 | 46 | 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 | 9 | 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 | protected function _doImages_reference_callback($matches) { |
||
| 885 | |||
| 886 | /** |
||
| 887 | * Callback to parse inline image tags |
||
| 888 | * @param array $matches |
||
| 889 | * @return string |
||
| 890 | */ |
||
| 891 | 2 | protected function _doImages_inline_callback($matches) { |
|
| 912 | |||
| 913 | /** |
||
| 914 | * Parse Markdown heading elements to HTML |
||
| 915 | * @param string $text |
||
| 916 | * @return string |
||
| 917 | */ |
||
| 918 | 48 | protected function doHeaders($text) { |
|
| 950 | |||
| 951 | /** |
||
| 952 | * Setext header parsing callback |
||
| 953 | * @param array $matches |
||
| 954 | * @return string |
||
| 955 | */ |
||
| 956 | 5 | protected function _doHeaders_callback_setext($matches) { |
|
| 970 | |||
| 971 | /** |
||
| 972 | * ATX header parsing callback |
||
| 973 | * @param array $matches |
||
| 974 | * @return string |
||
| 975 | */ |
||
| 976 | 8 | protected function _doHeaders_callback_atx($matches) { |
|
| 984 | |||
| 985 | /** |
||
| 986 | * If a header_id_func property is set, we can use it to automatically |
||
| 987 | * generate an id attribute. |
||
| 988 | * |
||
| 989 | * This method returns a string in the form id="foo", or an empty string |
||
| 990 | * otherwise. |
||
| 991 | * @param string $headerValue |
||
| 992 | * @return string |
||
| 993 | */ |
||
| 994 | 9 | protected function _generateIdFromHeaderValue($headerValue) { |
|
| 1006 | |||
| 1007 | /** |
||
| 1008 | * Form HTML ordered (numbered) and unordered (bulleted) lists. |
||
| 1009 | * @param string $text |
||
| 1010 | * @return string |
||
| 1011 | */ |
||
| 1012 | 110 | protected function doLists($text) { |
|
| 1073 | |||
| 1074 | /** |
||
| 1075 | * List parsing callback |
||
| 1076 | * @param array $matches |
||
| 1077 | * @return string |
||
| 1078 | */ |
||
| 1079 | 24 | protected function _doLists_callback($matches) { |
|
| 1113 | |||
| 1114 | /** |
||
| 1115 | * Nesting tracker for list levels |
||
| 1116 | * @var integer |
||
| 1117 | */ |
||
| 1118 | protected $list_level = 0; |
||
| 1119 | |||
| 1120 | /** |
||
| 1121 | * Process the contents of a single ordered or unordered list, splitting it |
||
| 1122 | * into individual list items. |
||
| 1123 | * @param string $list_str |
||
| 1124 | * @param string $marker_any_re |
||
| 1125 | * @return string |
||
| 1126 | */ |
||
| 1127 | 24 | protected function processListItems($list_str, $marker_any_re) { |
|
| 1170 | |||
| 1171 | /** |
||
| 1172 | * List item parsing callback |
||
| 1173 | * @param array $matches |
||
| 1174 | * @return string |
||
| 1175 | */ |
||
| 1176 | 24 | protected function _processListItems_callback($matches) { |
|
| 1197 | |||
| 1198 | /** |
||
| 1199 | * Process Markdown `<pre><code>` blocks. |
||
| 1200 | * @param string $text |
||
| 1201 | * @return string |
||
| 1202 | */ |
||
| 1203 | 110 | protected function doCodeBlocks($text) { |
|
| 1218 | |||
| 1219 | /** |
||
| 1220 | * Code block parsing callback |
||
| 1221 | * @param array $matches |
||
| 1222 | * @return string |
||
| 1223 | */ |
||
| 1224 | 34 | protected function _doCodeBlocks_callback($matches) { |
|
| 1240 | |||
| 1241 | /** |
||
| 1242 | * Create a code span markup for $code. Called from handleSpanToken. |
||
| 1243 | * @param string $code |
||
| 1244 | * @return string |
||
| 1245 | */ |
||
| 1246 | 21 | protected function makeCodeSpan($code) { |
|
| 1254 | |||
| 1255 | /** |
||
| 1256 | * Define the emphasis operators with their regex matches |
||
| 1257 | * @var array |
||
| 1258 | */ |
||
| 1259 | protected $em_relist = array( |
||
| 1260 | '' => '(?:(?<!\*)\*(?!\*)|(?<!_)_(?!_))(?![\.,:;]?\s)', |
||
| 1261 | '*' => '(?<![\s*])\*(?!\*)', |
||
| 1262 | '_' => '(?<![\s_])_(?!_)', |
||
| 1263 | ); |
||
| 1264 | |||
| 1265 | /** |
||
| 1266 | * Define the strong operators with their regex matches |
||
| 1267 | * @var array |
||
| 1268 | */ |
||
| 1269 | protected $strong_relist = array( |
||
| 1270 | '' => '(?:(?<!\*)\*\*(?!\*)|(?<!_)__(?!_))(?![\.,:;]?\s)', |
||
| 1271 | '**' => '(?<![\s*])\*\*(?!\*)', |
||
| 1272 | '__' => '(?<![\s_])__(?!_)', |
||
| 1273 | ); |
||
| 1274 | |||
| 1275 | /** |
||
| 1276 | * Define the emphasis + strong operators with their regex matches |
||
| 1277 | * @var array |
||
| 1278 | */ |
||
| 1279 | protected $em_strong_relist = array( |
||
| 1280 | '' => '(?:(?<!\*)\*\*\*(?!\*)|(?<!_)___(?!_))(?![\.,:;]?\s)', |
||
| 1281 | '***' => '(?<![\s*])\*\*\*(?!\*)', |
||
| 1282 | '___' => '(?<![\s_])___(?!_)', |
||
| 1283 | ); |
||
| 1284 | |||
| 1285 | /** |
||
| 1286 | * Container for prepared regular expressions |
||
| 1287 | * @var array |
||
| 1288 | */ |
||
| 1289 | protected $em_strong_prepared_relist; |
||
| 1290 | |||
| 1291 | /** |
||
| 1292 | * Prepare regular expressions for searching emphasis tokens in any |
||
| 1293 | * context. |
||
| 1294 | * @return void |
||
| 1295 | */ |
||
| 1296 | 5 | protected function prepareItalicsAndBold() { |
|
| 1313 | |||
| 1314 | /** |
||
| 1315 | * Convert Markdown italics (emphasis) and bold (strong) to HTML |
||
| 1316 | * @param string $text |
||
| 1317 | * @return string |
||
| 1318 | */ |
||
| 1319 | 107 | protected function doItalicsAndBold($text) { |
|
| 1443 | |||
| 1444 | /** |
||
| 1445 | * Parse Markdown blockquotes to HTML |
||
| 1446 | * @param string $text |
||
| 1447 | * @return string |
||
| 1448 | */ |
||
| 1449 | 110 | protected function doBlockQuotes($text) { |
|
| 1464 | |||
| 1465 | /** |
||
| 1466 | * Blockquote parsing callback |
||
| 1467 | * @param array $matches |
||
| 1468 | * @return string |
||
| 1469 | */ |
||
| 1470 | 10 | protected function _doBlockQuotes_callback($matches) { |
|
| 1484 | |||
| 1485 | /** |
||
| 1486 | * Blockquote parsing callback |
||
| 1487 | * @param array $matches |
||
| 1488 | * @return string |
||
| 1489 | */ |
||
| 1490 | 2 | protected function _doBlockQuotes_callback2($matches) { |
|
| 1495 | |||
| 1496 | /** |
||
| 1497 | * Parse paragraphs |
||
| 1498 | * |
||
| 1499 | * @param string $text String to process in paragraphs |
||
| 1500 | * @param boolean $wrap_in_p Whether paragraphs should be wrapped in <p> tags |
||
| 1501 | * @return string |
||
| 1502 | */ |
||
| 1503 | 48 | protected function formParagraphs($text, $wrap_in_p = true) { |
|
| 1566 | |||
| 1567 | /** |
||
| 1568 | * Encode text for a double-quoted HTML attribute. This function |
||
| 1569 | * is *not* suitable for attributes enclosed in single quotes. |
||
| 1570 | * @param string $text |
||
| 1571 | * @return string |
||
| 1572 | */ |
||
| 1573 | 36 | protected function encodeAttribute($text) { |
|
| 1578 | |||
| 1579 | /** |
||
| 1580 | * Encode text for a double-quoted HTML attribute containing a URL, |
||
| 1581 | * applying the URL filter if set. Also generates the textual |
||
| 1582 | * representation for the URL (removing mailto: or tel:) storing it in $text. |
||
| 1583 | * This function is *not* suitable for attributes enclosed in single quotes. |
||
| 1584 | * |
||
| 1585 | * @param string $url |
||
| 1586 | * @param string $text Passed by reference |
||
| 1587 | * @return string URL |
||
| 1588 | */ |
||
| 1589 | 33 | protected function encodeURLAttribute($url, &$text = null) { |
|
| 1606 | |||
| 1607 | /** |
||
| 1608 | * Smart processing for ampersands and angle brackets that need to |
||
| 1609 | * be encoded. Valid character entities are left alone unless the |
||
| 1610 | * no-entities mode is set. |
||
| 1611 | * @param string $text |
||
| 1612 | * @return string |
||
| 1613 | */ |
||
| 1614 | 109 | protected function encodeAmpsAndAngles($text) { |
|
| 1628 | |||
| 1629 | /** |
||
| 1630 | * Parse Markdown automatic links to anchor HTML tags |
||
| 1631 | * @param string $text |
||
| 1632 | * @return string |
||
| 1633 | */ |
||
| 1634 | 107 | protected function doAutoLinks($text) { |
|
| 1661 | |||
| 1662 | /** |
||
| 1663 | * Parse URL callback |
||
| 1664 | * @param array $matches |
||
| 1665 | * @return string |
||
| 1666 | */ |
||
| 1667 | 4 | protected function _doAutoLinks_url_callback($matches) { |
|
| 1672 | |||
| 1673 | /** |
||
| 1674 | * Parse email address callback |
||
| 1675 | * @param array $matches |
||
| 1676 | * @return string |
||
| 1677 | */ |
||
| 1678 | 4 | protected function _doAutoLinks_email_callback($matches) { |
|
| 1684 | |||
| 1685 | /** |
||
| 1686 | * Input: some text to obfuscate, e.g. "mailto:[email protected]" |
||
| 1687 | * |
||
| 1688 | * Output: the same text but with most characters encoded as either a |
||
| 1689 | * decimal or hex entity, in the hopes of foiling most address |
||
| 1690 | * harvesting spam bots. E.g.: |
||
| 1691 | * |
||
| 1692 | * mailto:foo |
||
| 1693 | * @example.co |
||
| 1694 | * m |
||
| 1695 | * |
||
| 1696 | * Note: the additional output $tail is assigned the same value as the |
||
| 1697 | * ouput, minus the number of characters specified by $head_length. |
||
| 1698 | * |
||
| 1699 | * Based by a filter by Matthew Wickline, posted to BBEdit-Talk. |
||
| 1700 | * With some optimizations by Milian Wolff. Forced encoding of HTML |
||
| 1701 | * attribute special characters by Allan Odgaard. |
||
| 1702 | * |
||
| 1703 | * @param string $text |
||
| 1704 | * @param string $tail Passed by reference |
||
| 1705 | * @param integer $head_length |
||
| 1706 | * @return string |
||
| 1707 | */ |
||
| 1708 | 4 | protected function encodeEntityObfuscatedAttribute($text, &$tail = null, $head_length = 0) { |
|
| 1739 | |||
| 1740 | /** |
||
| 1741 | * Take the string $str and parse it into tokens, hashing embeded HTML, |
||
| 1742 | * escaped characters and handling code spans. |
||
| 1743 | * @param string $str |
||
| 1744 | * @return string |
||
| 1745 | */ |
||
| 1746 | 109 | protected function parseSpan($str) { |
|
| 1797 | |||
| 1798 | /** |
||
| 1799 | * Handle $token provided by parseSpan by determining its nature and |
||
| 1800 | * returning the corresponding value that should replace it. |
||
| 1801 | * @param string $token |
||
| 1802 | * @param string $str Passed by reference |
||
| 1803 | * @return string |
||
| 1804 | */ |
||
| 1805 | 37 | protected function handleSpanToken($token, &$str) { |
|
| 1823 | |||
| 1824 | /** |
||
| 1825 | * Remove one level of line-leading tabs or spaces |
||
| 1826 | * @param string $text |
||
| 1827 | * @return string |
||
| 1828 | */ |
||
| 1829 | 47 | protected function outdent($text) { |
|
| 1832 | |||
| 1833 | |||
| 1834 | /** |
||
| 1835 | * String length function for detab. `_initDetab` will create a function to |
||
| 1836 | * handle UTF-8 if the default function does not exist. |
||
| 1837 | * @var string |
||
| 1838 | */ |
||
| 1839 | protected $utf8_strlen = 'mb_strlen'; |
||
| 1840 | |||
| 1841 | /** |
||
| 1842 | * Replace tabs with the appropriate amount of spaces. |
||
| 1843 | * |
||
| 1844 | * For each line we separate the line in blocks delemited by tab characters. |
||
| 1845 | * Then we reconstruct every line by adding the appropriate number of space |
||
| 1846 | * between each blocks. |
||
| 1847 | * |
||
| 1848 | * @param string $text |
||
| 1849 | * @return string |
||
| 1850 | */ |
||
| 1851 | 110 | protected function detab($text) { |
|
| 1857 | |||
| 1858 | /** |
||
| 1859 | * Replace tabs callback |
||
| 1860 | * @param string $matches |
||
| 1861 | * @return string |
||
| 1862 | */ |
||
| 1863 | 34 | protected function _detab_callback($matches) { |
|
| 1880 | |||
| 1881 | /** |
||
| 1882 | * Check for the availability of the function in the `utf8_strlen` property |
||
| 1883 | * (initially `mb_strlen`). If the function is not available, create a |
||
| 1884 | * function that will loosely count the number of UTF-8 characters with a |
||
| 1885 | * regular expression. |
||
| 1886 | * @return void |
||
| 1887 | */ |
||
| 1888 | 5 | protected function _initDetab() { |
|
| 1898 | |||
| 1899 | /** |
||
| 1900 | * Swap back in all the tags hashed by _HashHTMLBlocks. |
||
| 1901 | * @param string $text |
||
| 1902 | * @return string |
||
| 1903 | */ |
||
| 1904 | 110 | protected function unhash($text) { |
|
| 1908 | |||
| 1909 | /** |
||
| 1910 | * Unhashing callback |
||
| 1911 | * @param array $matches |
||
| 1912 | * @return string |
||
| 1913 | */ |
||
| 1914 | 82 | protected function _unhash_callback($matches) { |
|
| 1917 | } |
||
| 1918 |
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.