Skip to content

Commit 0db836a

Browse files
Copilotswissspidy
andauthored
Fix multi-line parameter description indentation in README generation (#285)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 362d57b commit 0db836a

File tree

2 files changed

+116
-4
lines changed

2 files changed

+116
-4
lines changed

features/scaffold-package-readme.feature

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,5 +437,89 @@ Feature: Scaffold a README.md file for an existing package
437437

438438
When I run `wp --require=foo/command.php scaffold package-readme foo`
439439
Then the foo/README.md file should exist
440-
And the contents of the foo/README.md file should match /\t\t.*Read content from/
441-
And the contents of the foo/README.md file should match /\t\t.*Passing/
440+
And the contents of the foo/README.md file should match /\t\tRead content from/
441+
And the contents of the foo/README.md file should match /\t\tPassing/
442+
443+
Scenario: README correctly indents continuation paragraphs with zero leading spaces
444+
Given an empty directory
445+
And a foo/command.php file:
446+
"""
447+
<?php
448+
/**
449+
* Zero-space continuation test command.
450+
*/
451+
class Zero_Space_Test_Command {
452+
/**
453+
* Test command where continuation paragraph has no leading spaces.
454+
*
455+
* ## OPTIONS
456+
*
457+
* [--extra]
458+
* : Show extended version information.
459+
*
460+
* Note: to retrieve the database revision for an individual subsite,
461+
* use `wp option get db_version --url=<subsite>`.
462+
*
463+
* @when before_wp_load
464+
*/
465+
public function __invoke( $args, $assoc_args ) {}
466+
}
467+
WP_CLI::add_command( 'zero-space-test', 'Zero_Space_Test_Command' );
468+
"""
469+
And a foo/composer.json file:
470+
"""
471+
{
472+
"name": "wp-cli/zero-space-test",
473+
"description": "Test",
474+
"extra": {
475+
"commands": ["zero-space-test"]
476+
}
477+
}
478+
"""
479+
480+
When I run `wp --require=foo/command.php scaffold package-readme foo`
481+
Then the foo/README.md file should exist
482+
And the contents of the foo/README.md file should match /\t\tShow extended version information/
483+
And the contents of the foo/README.md file should match /\t\tNote: to retrieve/
484+
485+
Scenario: README correctly indents continuation paragraphs with one leading space
486+
Given an empty directory
487+
And a foo/command.php file:
488+
"""
489+
<?php
490+
/**
491+
* One-space continuation test command.
492+
*/
493+
class One_Space_Test_Command {
494+
/**
495+
* Test command where continuation paragraph has one leading space.
496+
*
497+
* ## OPTIONS
498+
*
499+
* [--extra]
500+
* : Show extended version information.
501+
*
502+
* Note: to retrieve the database revision for an individual subsite,
503+
* use `wp option get db_version --url=<subsite>`.
504+
*
505+
* @when before_wp_load
506+
*/
507+
public function __invoke( $args, $assoc_args ) {}
508+
}
509+
WP_CLI::add_command( 'one-space-test', 'One_Space_Test_Command' );
510+
"""
511+
And a foo/composer.json file:
512+
"""
513+
{
514+
"name": "wp-cli/one-space-test",
515+
"description": "Test",
516+
"extra": {
517+
"commands": ["one-space-test"]
518+
}
519+
}
520+
"""
521+
522+
When I run `wp --require=foo/command.php scaffold package-readme foo`
523+
Then the foo/README.md file should exist
524+
And the contents of the foo/README.md file should match /\t\tShow extended version information/
525+
And the contents of the foo/README.md file should match /\t\tNote: to retrieve/

src/ScaffoldPackageCommand.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ public function package_readme( $args, $assoc_args ) {
423423
$longdesc = (string) preg_replace( '/##\s(.+)/', '**$1**', $longdesc );
424424

425425
// definition lists
426-
$longdesc = preg_replace_callback( '/([^\n]+)\n: (.+?)(\n\n(?=\S)|\n*$)/s', [ __CLASS__, 'rewrap_param_desc' ], $longdesc );
426+
$longdesc = preg_replace_callback( '/([^\n]+)\n: (.+?)(\n\n(?=[^\n]+\n: |\*\*)|\n*$)/s', [ __CLASS__, 'rewrap_param_desc' ], $longdesc );
427427

428428
$command_data = [
429429
'name' => "wp {$command}",
@@ -836,10 +836,38 @@ public function package_tests( $args, $assoc_args ) {
836836

837837
private static function rewrap_param_desc( $matches ) {
838838
$param = $matches[1];
839-
$desc = self::indent( "\t\t", rtrim( $matches[2] ) );
839+
$desc = self::indent( "\t\t", self::normalize_desc_indentation( rtrim( $matches[2] ) ) );
840840
return "\t$param\n$desc\n\n";
841841
}
842842

843+
private static function normalize_desc_indentation( $text ) {
844+
$paragraphs = explode( "\n\n", $text );
845+
foreach ( $paragraphs as &$paragraph ) {
846+
$lines = explode( "\n", $paragraph );
847+
$min_indent = PHP_INT_MAX;
848+
foreach ( $lines as $line ) {
849+
if ( '' !== trim( $line ) ) {
850+
$stripped = ltrim( $line );
851+
$indent = strlen( $line ) - strlen( $stripped );
852+
if ( $indent < $min_indent ) {
853+
$min_indent = $indent;
854+
}
855+
}
856+
}
857+
if ( $min_indent > 0 && PHP_INT_MAX !== $min_indent ) {
858+
foreach ( $lines as &$line ) {
859+
if ( '' !== $line ) {
860+
$line = substr( $line, $min_indent );
861+
}
862+
}
863+
unset( $line );
864+
$paragraph = implode( "\n", $lines );
865+
}
866+
}
867+
unset( $paragraph );
868+
return implode( "\n\n", $paragraphs );
869+
}
870+
843871
private static function indent( $whitespace, $text ) {
844872
$lines = explode( "\n", $text );
845873
foreach ( $lines as &$line ) {

0 commit comments

Comments
 (0)