Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue where newlines are stripped in <pre> blocks #165

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions lib/runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ function export_docblock( $element ) {
}

$output = array(
'description' => preg_replace( '/[\n\r]+/', ' ', $docblock->getShortDescription() ),
'long_description' => preg_replace( '/[\n\r]+/', ' ', $docblock->getLongDescription()->getFormattedContents() ),
'description' => strip_newlines( $docblock->getShortDescription() ),
'long_description' => strip_newlines( $docblock->getLongDescription()->getFormattedContents() ),
'tags' => array(),
);

foreach ( $docblock->getTags() as $tag ) {
$tag_data = array(
'name' => $tag->getName(),
'content' => preg_replace( '/[\n\r]+/', ' ', format_description( $tag->getDescription() ) ),
'content' => strip_newlines( format_description( $tag->getDescription() ) ),
);
if ( method_exists( $tag, 'getTypes' ) ) {
$tag_data['types'] = $tag->getTypes();
Expand All @@ -173,7 +173,7 @@ function export_docblock( $element ) {
}
// Description string.
if ( method_exists( $tag, 'getDescription' ) ) {
$description = preg_replace( '/[\n\r]+/', ' ', format_description( $tag->getDescription() ) );
$description = strip_newlines( format_description( $tag->getDescription() ) );
if ( ! empty( $description ) ) {
$tag_data['description'] = $description;
}
Expand Down Expand Up @@ -354,3 +354,34 @@ function format_description( $description ) {
}
return $description;
}

/**
* Strips newline characters from descriptions, saving out pre-formatted blocks.
*
* @param string $description The descriptions to parse.
* @return string The string with newlines replaced by spaces, except in pre-formatted blocks.
*/
function strip_newlines( $description ) {
$pre_matcher = '/<pre.*<\/pre>/s';
$line_matcher = '/[\r\n]+/';

// See if any pre-formatted tags exist.
preg_match_all( $pre_matcher, $description, $matches );
$has_blocks = ( is_array( $matches ) && ! empty( $matches[0] ) );

// If we have pre-formatted blocks, strip them out and replace with a token.
if ( $has_blocks ) {
$token = '%%%' . md5( serialize( $matches ) ) . '%%%';
$description = preg_replace( $pre_matcher, $token, $description );
}

// Strip newlines, replacing them with spaces.
$description = preg_replace( $line_matcher, ' ', $description );

// Put back the protected pre-formatted blocks.
if ( $has_blocks ) {
$description = str_replace( array_fill( 0, count( $matches[0] ), $token ), $matches[0], $description );
}

return $description;
}