Skip to content

Commit 3286e71

Browse files
Copilotswissspidy
andauthored
Fix multi-paragraph parameter descriptions losing indentation in README generation (#282)
* Initial plan * Fix multi-line descriptions not correctly indented in README generation Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 4da876e commit 3286e71

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

features/scaffold-package-readme.feature

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,46 @@ Feature: Scaffold a README.md file for an existing package
386386
"""
387387
**Alias:** `cpt`
388388
"""
389+
390+
Scenario: README correctly indents multi-paragraph parameter descriptions
391+
Given an empty directory
392+
And a foo/command.php file:
393+
"""
394+
<?php
395+
/**
396+
* Multi-paragraph test command.
397+
*/
398+
class Multi_Para_Test_Command {
399+
/**
400+
* Test command with a multi-paragraph parameter description.
401+
*
402+
* ## OPTIONS
403+
*
404+
* [<file>]
405+
* : Read content from <file>. If this value is present, the
406+
* `--content` argument will be ignored.
407+
*
408+
* Passing `-` will cause content to
409+
* be read from STDIN.
410+
*
411+
* @when before_wp_load
412+
*/
413+
public function __invoke( $args, $assoc_args ) {}
414+
}
415+
WP_CLI::add_command( 'multi-para-test', 'Multi_Para_Test_Command' );
416+
"""
417+
And a foo/composer.json file:
418+
"""
419+
{
420+
"name": "wp-cli/multi-para-test",
421+
"description": "Test",
422+
"extra": {
423+
"commands": ["multi-para-test"]
424+
}
425+
}
426+
"""
427+
428+
When I run `wp --require=foo/command.php scaffold package-readme foo`
429+
Then the foo/README.md file should exist
430+
And the contents of the foo/README.md file should match /\t\t.*Read content from/
431+
And the contents of the foo/README.md file should match /\t\t.*Passing/

src/ScaffoldPackageCommand.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ public function package_readme( $args, $assoc_args ) {
365365
$longdesc = (string) preg_replace( '/##\s(.+)/', '**$1**', $longdesc );
366366

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

370370
$command_data = [
371371
'name' => "wp {$command}",
@@ -777,14 +777,16 @@ public function package_tests( $args, $assoc_args ) {
777777

778778
private static function rewrap_param_desc( $matches ) {
779779
$param = $matches[1];
780-
$desc = self::indent( "\t\t", $matches[2] );
780+
$desc = self::indent( "\t\t", rtrim( $matches[2] ) );
781781
return "\t$param\n$desc\n\n";
782782
}
783783

784784
private static function indent( $whitespace, $text ) {
785785
$lines = explode( "\n", $text );
786786
foreach ( $lines as &$line ) {
787-
$line = $whitespace . $line;
787+
if ( '' !== $line ) {
788+
$line = $whitespace . $line;
789+
}
788790
}
789791
return implode( "\n", $lines );
790792
}

0 commit comments

Comments
 (0)