-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathFormatting.php
707 lines (632 loc) · 28.1 KB
/
Formatting.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
<?php
namespace Aivec\Plugins\DocParser;
/**
* Utility methods for handling formatting of parsed content and
* auto-adding link references
*/
class Formatting
{
/**
* Initializes class
*
* @return void
*/
public static function init() {
add_action('init', [get_class(), 'doInit']);
}
/**
* Handles adding/removing hooks to perform formatting as needed.
*
* @return void
*/
public static function doInit() {
// NOTE: This filtering is temporarily disabled and then restored in
// reference/template-explanation.php
add_filter('the_content', [get_class(), 'fixUnintendedMarkdown'], 1);
add_filter('the_content', [get_class(), 'makeDoclinkClickable'], 10, 5);
add_filter('the_excerpt', [get_class(), 'removeInlineInternal']);
add_filter('the_content', [get_class(), 'removeInlineInternal']);
add_filter('the_excerpt', [get_class(), 'autolinkReferences'], 11);
add_filter('the_content', [get_class(), 'autolinkReferences'], 11);
add_filter('avcapps-parameter-type', [get_class(), 'autolinkReferences']);
add_filter('avcpdp-format-hash-param-description', [get_class(), 'autolinkReferences']);
add_filter('avcpdp-format-hash-param-description', [get_class(), 'fixParamDescriptionParsedownBug']);
add_filter('avcapps-function-return-type', [get_class(), 'autolinkReferences'], 10, 2);
add_filter('syntaxhighlighter_htmlresult', [get_class(), 'fixCodeEntityEncoding'], 20);
}
/**
* Fixes bug in (or at least in using) SyntaxHighlighter code shortcodes that
* causes double-encoding of `>` character.
*
* @param string $content The text being handled as code.
* @return string
*/
public static function fixCodeEntityEncoding($content) {
return str_replace('&gt;', '>', $content);
}
/**
* Prevents display of the inline use of {@internal}} as it is not meant to be shown.
*
* @param string $content The post content.
* @param null|string $post_type Optional. The post type. Default null.
* @return string
*/
public static function removeInlineInternal($content, $post_type = null) {
// Only attempt a change for a parsed post type with an @internal reference in the text.
if (avcpdp_is_parsed_post_type($post_type) && false !== strpos($content, '{@internal ')) {
$content = preg_replace('/\{@internal (.+)\}\}/', '', $content);
}
return $content;
}
/**
* Makes phpDoc @see and @link references clickable.
*
* Handles these six different types of links:
*
* - {@link https://en.wikipedia.org/wiki/ISO_8601}
* - {@see WP_Rewrite::$index}
* - {@see WP_Query::query()}
* - {@see esc_attr()}
* - {@see 'pre_get_search_form'}
* - {@link https://codex.wordpress.org/The_Loop Use new WordPress Loop}
*
* Note: Though @see and @link are semantically different in meaning, that isn't always
* the case with use so this function handles them identically.
*
* @param string $content The content.
* @return string
*/
public static function makeDoclinkClickable($content) {
// Nothing to change unless a @link or @see reference is in the text.
if (false === strpos($content, '{@link ') && false === strpos($content, '{@see ')) {
return $content;
}
return preg_replace_callback(
'/\{@(?:link|see) ([^\}]+)\}/',
function ($matches) {
$link = $matches[1];
// We may have encoded a link, so unencode if so.
// (This would never occur natually.)
if (0 === strpos($link, '<a ')) {
$link = html_entity_decode($link);
}
// Undo links made clickable during initial parsing
if (0 === strpos($link, '<a ')) {
if (preg_match('/^<a .*href=[\'\"]([^\'\"]+)[\'\"]>(.*)<\/a>(.*)$/', $link, $parts)) {
$link = $parts[1];
if ($parts[3]) {
$link .= ' ' . $parts[3];
}
}
}
// Link to an external resource.
if (0 === strpos($link, 'http')) {
$parts = explode(' ', $link, 2);
// Link without linked text: {@link https://en.wikipedia.org/wiki/ISO_8601}
if (1 === count($parts)) {
$url = $text = $link;
} else {
// Link with linked text: {@link https://codex.wordpress.org/The_Loop Use new WordPress Loop}
$url = $parts[0];
$text = $parts[1];
}
$link = self::generateLink($url, $text);
} else {
// Link to an internal resource.
$link = self::linkInternalElement($link);
}
return $link;
},
$content
);
}
/**
* Parses and links an internal element if a valid element is found.
*
* @param string $link Element string.
* @return string HTML link markup if a valid element was found.
*/
public static function linkInternalElement($link) {
$url = '';
// Exceptions for externally-linked elements.
$exceptions = [
'error_log()' => 'https://secure.php.net/manual/en/function.error-log.php',
];
// Link exceptions that should actually point to external resources.
if (!empty($exceptions[$link])) {
$url = $exceptions[$link];
// Link to class variable: {@see WP_Rewrite::$index}
} elseif (false !== strpos($link, '::$')) {
// Nothing to link to currently.
} elseif (false !== strpos($link, '::')) {
// Link to class method: {@see \Namespace\Classname::someMethod()}
$post = self::getPostFromReference($link, 'wp-parser-method');
if ($post !== null) {
$url = get_permalink($post->ID);
}
} elseif (1 === preg_match('/^(?:\'|(?:‘))([\$\w\-&;]+)(?:\'|(?:’))$/', $link, $hook)) {
// Link to hook: {@see 'pre_get_search_form'}
if (!empty($hook[1])) {
$post = self::getPostFromReference($hook[1], 'wp-parser-hook');
if ($post !== null) {
$url = get_permalink($post->ID);
}
}
} elseif (1 === preg_match('/\\\?(?:[A-Z]+[A-Za-z]*)+(?:\\\{1}[A-Z]+[A-Za-z]*)+/', $link)) {
// Link to a PSR-4 class: {@see \Namespace\Classname}
$post = self::getPostFromReference($link, 'wp-parser-class');
if ($post !== null) {
$url = get_permalink($post->ID);
}
} else {
// Link to function: {@see esc_attr()}
$post = self::getPostFromReference($link, 'wp-parser-function');
if ($post !== null) {
$url = get_permalink($post->ID);
}
}
if ($url) {
$link = self::generateLink($url, $link);
}
return $link;
}
/**
* Generates a link given a URL and text.
*
* @param string $url The URL, for the link's href attribute.
* @param string $text The text content of the link.
* @return string The HTML for the link.
*/
public static function generateLink($url, $text) {
/*
* Filters the HTML attributes applied to a link's anchor element.
*
* @param array $attrs The HTML attributes applied to the link's anchor element.
* @param string $url The URL for the link.
*/
$attrs = (array)apply_filters('avcpdp-format-link-attributes', ['href' => $url], $url);
// Make sure the filter didn't completely remove the href attribute.
if (empty($attrs['href'])) {
$attrs['href'] = $url;
}
$attributes = '';
foreach ($attrs as $name => $value) {
$value = 'href' === $name ? esc_url($value) : esc_attr($value);
$attributes .= sprintf(' %s="%s"', esc_attr($name), $value);
}
return sprintf('<a%s>%s</a>', $attributes, esc_html($text));
}
/**
* Fixes unintended markup generated by Markdown during parsing.
*
* The parser interprets underscores surrounding text as Markdown indicating
* italics. That is never the intention, so undo it.
*
* @param string $content The post content.
* @param null|string $post_type Optional. The post type. Default null.
* @return string
*/
public static function fixUnintendedMarkdown($content, $post_type = null) {
// Only apply to parsed content that have the em tag.
if (avcpdp_is_parsed_post_type($post_type) && false !== strpos($content, '<em>')) {
$content = preg_replace_callback(
'/([^\s])<em>(.+)<\/em>/U',
function ($matches) {
return $matches[1] . '_' . $matches[2] . '_';
},
$content
);
}
return $content;
}
/**
* Handles formatting of the parameter description.
*
* @param string $text The parameter description.
* @return string
*/
public static function formatParamDescription($text) {
// Undo parser's Markdown conversion of '*' to `<em>` and `</em>`.
// In pretty much all cases, the docs mean literal '*' and never emphasis.
// ....... The above is from wporg-developer ..........
// Replace em tags with the italics i tag
$text = str_replace('<em>', '<i>', $text);
$text = str_replace('</em>', '</i>', $text);
// Undo parser's Markdown conversion of '__' to `<strong>` and `</strong>`.
// $text = str_replace( array( '<strong>', '</strong>' ), '__', $text );
// Encode all htmlentities (but don't double-encode).
$text = htmlentities($text, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);
// Simple allowable tags that should get unencoded.
// Note: This precludes them from being able to be used in an encoded fashion
// within a parameter description.
$allowable_tags = ['code', 'em', 'strong', 'i'];
foreach ($allowable_tags as $tag) {
$text = str_replace(["<{$tag}>", "</{$tag}>"], ["<{$tag}>", "</{$tag}>"], $text);
}
// Convert any @link or @see to actual link.
$text = self::makeDoclinkClickable($text);
$text = self::autolinkReferences($text);
$text = self::fixParamDescriptionHtmlAsCode($text);
$text = self::convertListsToMarkup($text);
return $text;
}
/**
* Returns the post given a function/method/class/hook raw reference string
*
* @author Evan D Shaw <[email protected]>
* @param string $ref
* @param string $parser_type
* @return \WP_Post|null
*/
public static function getPostFromReference($ref, $parser_type) {
$postname = sanitize_title(str_replace('\\', '-', str_replace('::', '-', $ref)));
$posts = get_posts([
'name' => $postname,
'post_type' => $parser_type,
]);
if (empty($posts)) {
return null;
}
return $posts[0];
}
/**
* Strips the namespace from a function/method/class reference
*
* @author Evan D Shaw <[email protected]>
* @param string $ref
* @return string
*/
public static function stripNamespaceFromReference($ref) {
$parts = explode('\\', $ref);
return $parts[count($parts) - 1];
}
/**
* Automatically detects inline references to parsed resources and links to them.
*
* Examples:
* - Functions: get_item()
* - Classes: My\PSR\Four\Class, \TopLevelClass
* - Methods: My\PSR\Four\Class::isSingle(), \My\PSR\Four\Class::isSingle()
*
* Note: currently there is not a reliable way to infer references to hooks. Recommend
* using the {@}see 'hook_name'} notation as used in the inline docs.
*
* @param string $text The text.
* @param bool $strip_namespaces Whether to strip PSR-4 namespaces from the link text
* @return string
*/
public static function autolinkReferences($text, $strip_namespaces = true) {
// Temporary: Don't do anything if the text is a hash notation string.
if ($text && '{' === $text[0]) {
return $text;
}
$r = '';
$textarr = preg_split('/(<[^<>]+>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE); // split out HTML tags
$nested_code_pre = 0; // Keep track of how many levels link is nested inside <pre> or <code>
foreach ($textarr as $piece) {
if (preg_match('|^<code[\s>]|i', $piece) || preg_match('|^<pre[\s>]|i', $piece) || preg_match('|^<script[\s>]|i', $piece) || preg_match('|^<style[\s>]|i', $piece)) {
$nested_code_pre++;
} elseif ($nested_code_pre && ('</code>' === strtolower($piece) || '</pre>' === strtolower($piece) || '</script>' === strtolower($piece) || '</style>' === strtolower($piece))) {
$nested_code_pre--;
}
if ($nested_code_pre || empty($piece) || ($piece[0] === '<' && !preg_match('|^<\s*[\w]{1,20}+://|', $piece))) {
$r .= $piece;
continue;
}
// Long strings might contain expensive edge cases ...
if (10000 < strlen($piece)) {
// ... break it up
foreach (_split_str_by_whitespace($piece, 2100) as $chunk) { // 2100: Extra room for scheme and leading and trailing paretheses
if (2101 < strlen($chunk)) {
$r .= $chunk; // Too big, no whitespace: bail.
} else {
$r .= make_clickable($chunk);
}
}
} else {
/*
* Everthing outside of this conditional block was copied from core's
*`make_clickable()`.
*/
$content = " $piece "; // Pad with whitespace to simplify the regexes
// Only if the text contains something that might be a function.
if (false !== strpos($content, '()')) {
// Detect references to class methods, e.g. MyNamespace\MyClass::query()
// or functions, e.g. get_item().
$content = preg_replace_callback(
'~
(?!<.*?) # Non-capturing check to ensure not matching what looks like the inside of an HTML tag.
( # 1: The full method or function name.
(([a-zA-Z0-9_\\\]+)::)? # 2: The PSR-4 class prefix, if a method reference.
([a-zA-Z0-9_\\\]+) # 3: The method or function name.
)
\(\) # The () that signifies either a method or function.
(?![^<>]*?>) # Non-capturing check to ensure not matching what looks like the inside of an HTML tag.
~x',
function ($matches) use ($strip_namespaces) {
// Reference to a class method.
if ($matches[2]) {
// Only link actually parsed methods.
$post = self::getPostFromReference($matches[1], 'wp-parser-method');
if ($post !== null) {
return sprintf(
'<a href="%s">%s</a>',
get_permalink($post->ID),
$strip_namespaces ? self::stripNamespaceFromReference($matches[0]) : $matches[0]
);
}
// Reference to a function.
} else {
// Only link actually parsed functions.
$post = self::getPostFromReference($matches[1], 'wp-parser-function');
if ($post !== null) {
return sprintf(
'<a href="%s">%s</a>',
get_permalink($post->ID),
$strip_namespaces ? self::stripNamespaceFromReference($matches[0]) : $matches[0]
);
}
}
// It's not a reference to an actual thing, so restore original text.
return $matches[0];
},
$content
);
}
// Detect references to classes
$content = preg_replace_callback(
// Resolves PSR-4 class names
// If referencing a top level class (ie: MyClass), the class name MUST be prefixed with
// a backslash (ie: \MyClass).
// For all other non top level classes (ie: \MyNamespace\MyClass), the leading backslash
// is optional (ie: MyNamespace\MyClass).
//
// Note that WordPress style class names, such as WP_Post, are not resolved. Class names
// MUST be PSR-4 compliant.
'~'
. '(?<!/)'
. '(' // Primary match grouping
. '\\\?(?:[A-Z]+[A-Za-z]*)+(?:\\\{1}[A-Z]+[A-Za-z]*)+' // Resolves PSR-4 namespaces.
. ')' // End primary match grouping
. '\b' // Word boundary
. '(?!([<:]|"|\'>))' // Does not appear within a tag
. '~',
function ($matches) use ($strip_namespaces) {
// Only link actually parsed classes.
$post = self::getPostFromReference($matches[0], 'wp-parser-class');
if ($post !== null) {
return sprintf(
'<a href="%s">%s</a>',
get_permalink($post->ID),
$strip_namespaces ? self::stripNamespaceFromReference($matches[0]) : $matches[0]
);
}
// Not a class reference, so put the original reference back in.
return $matches[0];
},
$content
);
// Maybelater: Detect references to hooks, Currently not deemed reliably possible.
$content = substr($content, 1, -1); // Remove our whitespace padding.
$r .= $content;
} // end else
} // end foreach
// Cleanup of accidental links within links
return preg_replace('#(<a([ \r\n\t]+[^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i', '$1$3</a>', $r);
}
/**
* Converts simple Markdown-like lists into list markup.
*
* Necessary in cases like hash param descriptions which don't see Markdown
* list processing during parsing.
*
* Recognizes lists where list items are denoted with an asterisk or dash.
*
* Does not handle nesting of lists.
*
* @param string $text The text to process for lists.
* @return string
*/
public static function convertListsToMarkup($text) {
$inline_list = false;
$li = '<br /> * ';
// Convert asterisks to a list.
// Example: https://developer.wordpress.org/reference/functions/add_menu_page/
if (false !== strpos($text, ' * ')) {
// Display as simple plaintext list.
$text = str_replace(' * ', "\n" . $li, $text);
$inline_list = true;
}
// Convert dashes to a list.
// Example: https://developer.wordpress.org/reference/classes/wp_term_query/__construct/
// Example: https://developer.wordpress.org/reference/hooks/password_change_email/
if (false !== strpos($text, ' - ')) {
// Display as simple plaintext list.
$text = str_replace(' - ', "\n" . $li, $text);
$inline_list = true;
}
// If list detected.
if ($inline_list) {
// Replace first item, ensuring the opening 'ul' tag is prepended.
$text = preg_replace('~^' . preg_quote($li) . '(.+)$~mU', "<ul><li>\$1</li>\n", $text, 1);
// Wrap subsequent list items in 'li' tags.
$text = preg_replace('~^' . preg_quote($li) . '(.+)$~mU', "<li>\$1</li>\n", $text);
$text = trim($text);
// Close the list if it hasn't been closed before start of next hash parameter.
// $text = preg_replace( '~(</li>)(\s+</li>)~smU', '$1</ul>$2', $text );
$text = preg_replace('~(</li>)(\s*</li>)~smU', '$1</ul>$2', $text);
// Close the list if it hasn't been closed and it's the end of the description.
if ('</li>' === substr(trim($text), -5)) {
$text .= '</ul>';
}
}
return $text;
}
/**
* Returns hierarchical array for a param hash array type
*
* @author Evan D Shaw <[email protected]>
* @param string $text
* @param array $tpieces Translated pieces
* @param array $pieces
* @param string $key
* @return array
*/
public static function getParamHashMapRecursive($text, $tpieces = [], $pieces = [], $key = '') {
if (!$text || '{' != $text[0]) {
return [];
}
$index = 0;
$noprocessrange = 0;
$text = trim(substr($text, 1, -1));
$text = str_replace('@type', "\n@type", $text);
$parts = explode("\n", $text);
foreach ($parts as $part) {
if ($index < $noprocessrange) {
$index++;
continue;
}
$part = preg_replace('/\s+/', ' ', $part);
// extra spaces ensure we'll always have 4 items.
list($wordtype, $type, $name, $rawdescription) = explode(' ', $part . ' ', 4);
$description = trim($rawdescription);
if ('@type' != $wordtype) {
$tvalue = isset($tpieces['description']) ? $tpieces['description'] : '';
$value = $part;
if (is_string($tvalue) && !empty($tvalue)) {
$value = $tvalue;
}
$value = rtrim(trim(self::formatParamDescription($value)), '}');
$types = avcpdp_build_param_types(['array']);
$pieces['description'] = array_merge(
[
'name' => $key,
'cleanname' => str_replace('$', '', $key),
'wordtype' => null,
'raw_value' => $part,
'raw_translated_value' => $tvalue,
'value' => $value,
],
$types
);
} else {
$tvalue = isset($tpieces[$name]) ? $tpieces[$name] : '';
$value = $description;
if (is_string($tvalue) && !empty($tvalue)) {
$value = $tvalue;
}
$value = rtrim(trim(self::formatParamDescription($value)), '}');
$types = explode('|', $type);
$types = avcpdp_build_param_types($types);
$pieces[$name] = array_merge(
[
'name' => $name,
'cleanname' => str_replace('$', '', $name),
'wordtype' => $wordtype,
'raw_value' => $description,
'raw_translated_value' => $tvalue,
'value' => $value,
],
$types
);
}
$islinkbrace = false;
preg_match('/({.*(?:@see|@link).+?})(.*)/', $description, $matches);
if (!empty($matches)) {
if (trim($matches[count($matches) - 1]) === '') {
// If there are no braces after the @see|@link closing brace,
// the closing brace is a link brace, not a hash param closing brace
$islinkbrace = true;
}
}
// Handle nested hashes.
if (($description && '{' === $description[0]) || '{' === $name) {
$deschashpieces = explode('{', $rawdescription, 2);
$nestedtext = join(' ', array_slice($parts, $index + 1));
$nestedtext = '{' . $deschashpieces[1] . ' ' . $nestedtext;
$pieces[$name] = self::getParamHashMapRecursive(
$nestedtext,
isset($tpieces[$name]) ? (array)$tpieces[$name] : [],
[],
$name
);
$numprocessed = self::countNestedHashParamLeafNodes($pieces[$name]);
$noprocessrange = $index + $numprocessed;
// Sometimes nested hashes contain links (eg. {@see 'hook_name'}) so we
// need to make sure that if the last character is a closing brace it
// isn't for a link
} elseif ('}' === substr($description, -1) && !$islinkbrace) {
return $pieces;
}
$index++;
}
return $pieces;
}
/**
* Recursively counts all leaf nodes of a nested hash param
*
* @author Evan D Shaw <[email protected]>
* @param array $na
* @return int
*/
public static function countNestedHashParamLeafNodes($na) {
$num = 0;
foreach ($na as $el) {
if (!isset($el['value']) || is_array($el['value'])) {
$num += self::countNestedHashParamLeafNodes($el);
} else {
$num++;
}
}
return $num;
}
/**
* Fix Parsedown bug that introduces unbalanced 'code' tags.
*
* Under very specific criteria, a bug in the Parsedown package used by the
* parser causes backtick-to-code-tag conversions to get mishandled, skipping
* conversion of a backtick and causing subsequent backticks to be converted
* incorrectly as an open or close 'code' tag (opposite of what it should've
* been). See referenced tickets for more details.
*
* Intended to be a temporary fix until/unless Parsedown is fixed.
*
* @see https://meta.trac.wordpress.org/ticket/2900
* @see https://github.com/erusev/parsedown/pull/515
* @param string $text
* @return string
*/
public static function fixParamDescriptionParsedownBug($text) {
$fixes = [
'/`(.+)<code>/' => '<code>$1</code>',
'/<\/code>(.+)`/' => ' <code>$1</code>',
];
// Determine if code tags look inverted.
$first_start = strpos($text, '<code>');
$first_end = strpos($text, '</code>');
if (false !== $first_start && false !== $first_end && $first_end < $first_start) {
$fixes['~</code>(.+)<code>~U'] = ' <code>$1</code>';
}
$matched = true;
foreach ($fixes as $regex => $replace) {
$text = preg_replace($regex, $replace, $text);
}
return $text;
}
/**
* Wraps single-quoted HTML within 'code' tags.
*
* The HTML should have been denoted with backticks in the original source, in
* which case it would have been parsed properly, but committers aren't
* always sticklers for documentation formatting.
*
* @param string $text
* @return string
*/
public static function fixParamDescriptionHtmlAsCode($text) {
if (false !== strpos($text, "'<")) {
$text = preg_replace('/\'(<[^\']+>)\'/', '<code>$1</code>', $text);
}
return $text;
}
}