Skip to content

Commit 4fc0c75

Browse files
committed
Add markdown parsing..
1 parent f45bd00 commit 4fc0c75

File tree

1 file changed

+81
-5
lines changed

1 file changed

+81
-5
lines changed

lib/runner.php

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,7 @@ function export_docblock_from_data( $docblock_data ) {
360360
$long_description = $docblock_data['description'] ?? '';
361361

362362
if ( $long_description ) {
363-
// Remove linebreaks and normalize whitespace
364-
$long_description = preg_replace( '/\s+/', ' ', trim( $long_description ) );
365-
if ( ! str_contains( $long_description, '<p>' ) ) {
366-
$long_description = '<p>' . $long_description . '</p>';
367-
}
363+
$long_description = apply_markup( $long_description );
368364
}
369365

370366
return array(
@@ -468,5 +464,85 @@ function export_parse_tag( $tag_name, $value ) {
468464
);
469465
}
470466

467+
foreach ( array( 'content', 'description' ) as $field ) {
468+
if ( isset( $result[ $field ] ) ) {
469+
$result[ $field ] = apply_markup( $result[ $field ], false );
470+
}
471+
}
472+
471473
return $result;
472474
}
475+
476+
/**
477+
* Apply simple markup to a string for legacy long_description.
478+
*
479+
* Marks up `code` and >quoted text along with paragraphs.
480+
*
481+
* TODO: This should be the markdown parser, but AI coded this up pretty quickly.
482+
*
483+
* @param string $string Input string.
484+
* @return string Marked up string.
485+
*/
486+
function apply_markup( $string, $paragraphs = true ) {
487+
if ( ! $string ) {
488+
return '';
489+
}
490+
491+
// HTML Entities
492+
$string = htmlspecialchars( $string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8' );
493+
494+
// Convert `code` to <code>code</code>
495+
$string = preg_replace_callback(
496+
'/`([^`]+?)`/',
497+
static function( $matches ) {
498+
$code_content = html_entity_decode( $matches[1], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8' );
499+
return '<code>' . $code_content . '</code>';
500+
},
501+
$string
502+
);
503+
504+
// Italics
505+
$string = preg_replace( '/\b_([^_]+?)_\b/', '<em>$1</em>', $string );
506+
507+
// Bold
508+
$string = preg_replace( '/\b\*([^\*]+?)\*\b/', '<strong>$1</strong>', $string );
509+
510+
// Convert >quoted text to <blockquote>quoted text</blockquote>
511+
$string = preg_replace( '/^(>|&gt;)\s*(.+)/m', '<blockquote>$2</blockquote>', $string );
512+
$string = preg_replace( '#</blockquote>(\s*)<blockquote>#', '$1', $string ); // Merge adjacent blockquotes
513+
514+
// Headings
515+
$string = preg_replace( '/^######\s*(.+)$/m', '<h6>$1</h6>', $string );
516+
$string = preg_replace( '/^#####\s*(.+)$/m', '<h5>$1</h5>', $string );
517+
$string = preg_replace( '/^####\s*(.+)$/m', '<h4>$1</h4>', $string );
518+
$string = preg_replace( '/^###\s*(.+)$/m', '<h3>$1</h3>', $string );
519+
$string = preg_replace( '/^##\s*(.+)$/m', '<h2>$1</h2>', $string );
520+
$string = preg_replace( '/^#\s*(.+)$/m', '<h1>$1</h1>', $string );
521+
522+
// Lists.
523+
$string = preg_replace( '/^\s*[\*\-\+]\s+(.+)$/m', '<li>$1</li>', $string );
524+
$string = preg_replace( '/(<li>.*<\/li>)/sU', '<ul>$1</ul>', $string );
525+
$string = preg_replace( '/<\/ul>\s*<ul>/', '', $string ); // Merge adjacent lists
526+
527+
// Convert blocks to paragraps.
528+
$string = str_replace( "\n\n", "\nPARAGRAPHBREAKHERE", $string );
529+
$string = str_replace( "\n", ' ', $string );
530+
$string = str_replace( 'PARAGRAPHBREAKHERE', "\n", $string );
531+
532+
// Wrap paragraphs in <p> tags
533+
if ( $paragraphs ) {
534+
$paragraphs = explode( "\n", trim( $string ) );
535+
$paragraphs = array_map( 'trim', $paragraphs );
536+
$paragraphs = array_filter( $paragraphs ); // Remove empty paragraphs
537+
$paragraphs = array_map( function( $p ) {
538+
if ( str_starts_with( $p, '<blockquote>' ) && str_ends_with( $p, '</blockquote>' ) ) {
539+
// Paragraph goes inside the block quote. Probably an error.
540+
return str_replace( array( '<blockquote>', '</blockquote>' ), array( '<blockquote><p>', '</p></blockquote>' ), $p );
541+
}
542+
return '<p>' . $p . '</p>';
543+
}, $paragraphs );
544+
$string = implode( ' ', $paragraphs );
545+
}
546+
547+
return $string;
548+
}

0 commit comments

Comments
 (0)