Skip to content

Commit bc8a478

Browse files
committed
Merge branch 'lines-to-lists'
2 parents 7b46ba1 + 13cd059 commit bc8a478

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed

src/Utility/Convert.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,32 +1012,43 @@ public static function queryToData(array $query): array
10121012
}
10131013

10141014
/**
1015-
* Remove duplicates in a string where 'top-level' lines ("section names")
1016-
* are grouped with any subsequent 'child' lines ("list items")
1015+
* Remove duplicates in a string where top-level lines ("sections") are
1016+
* grouped with "list items" below
10171017
*
1018-
* Lines that match `$regex` are regarded as list items. Other lines are
1019-
* used as the section name for subsequent list items. Blank lines between
1020-
* list items clear the current section name.
1018+
* Lines that match `$regex` are regarded as list items, and other lines are
1019+
* used as the section name for subsequent list items. If `$loose` is
1020+
* `false` (the default), blank lines between list items clear the current
1021+
* section name.
1022+
*
1023+
* Top-level lines with no children, including any list items orphaned by
1024+
* blank lines above them, are returned before sections with children.
10211025
*
10221026
* If a named subpattern in `$regex` called `indent` matches a non-empty
10231027
* string, subsequent lines with the same number of spaces for indentation
10241028
* as there are characters in the match are treated as part of the item,
10251029
* including any blank lines.
10261030
*
1027-
* @param string $separator Used between top-level lines and sections.
1028-
* @param string|null $marker Added before each section name. The equivalent
1029-
* number of spaces are added before each list item. To add a leading `"- "`
1030-
* to top-level lines and indent others with two spaces, set `$marker` to
1031-
* `"-"`.
1031+
* Line endings used in `$text` may be any combination of LF, CRLF and CR,
1032+
* but LF (`"\n"`) line endings are used in the return value.
1033+
*
1034+
* @param string $separator Used between top-level lines and sections. Has
1035+
* no effect on the end-of-line sequence used between items, which is always
1036+
* LF (`"\n"`).
1037+
* @param string|null $marker Added before each section name. Nested list
1038+
* items are indented by the equivalent number of spaces. To add a leading
1039+
* `"- "` to top-level lines and indent others with two spaces, set
1040+
* `$marker` to `"-"`.
10321041
* @param bool $clean If `true`, the first match of `$regex` in each section
10331042
* name is removed.
1043+
* @param bool $loose If `true`, blank lines between list items are ignored.
10341044
*/
10351045
public static function linesToLists(
10361046
string $text,
10371047
string $separator = "\n",
10381048
?string $marker = null,
10391049
string $regex = '/^(?P<indent>\h*[-*] )/',
1040-
bool $clean = false
1050+
bool $clean = false,
1051+
bool $loose = false
10411052
): string {
10421053
$marker = ($marker ?? '') !== '' ? $marker . ' ' : null;
10431054
$indent = $marker !== null ? str_repeat(' ', mb_strlen($marker)) : '';
@@ -1058,7 +1069,7 @@ public static function linesToLists(
10581069

10591070
// Treat blank lines between items as section breaks
10601071
if (trim($line) === '') {
1061-
if ($lastWasItem) {
1072+
if (!$loose && $lastWasItem) {
10621073
unset($section);
10631074
}
10641075
continue;

tests/unit/Utility/ConvertTest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use DateTimeInterface;
99
use Generator;
1010
use LogicException;
11+
use ReflectionParameter;
1112

1213
final class ConvertTest extends \Lkrms\Tests\TestCase
1314
{
@@ -648,6 +649,10 @@ public function testLinesToLists(string $expected, ...$args)
648649
*/
649650
public static function linesToListsProvider(): array
650651
{
652+
$defaultRegex = (
653+
new ReflectionParameter([Convert::class, 'linesToLists'], 'regex')
654+
)->getDefaultValue();
655+
651656
$input1 = <<<EOF
652657
- Before lists
653658
@@ -688,6 +693,7 @@ public static function linesToListsProvider(): array
688693
### Changes
689694
690695
- Description
696+
691697
- Description
692698
over
693699
multiple
@@ -700,6 +706,7 @@ public static function linesToListsProvider(): array
700706
701707
- Description
702708
with different details
709+
703710
- Description
704711
over
705712
multiple
@@ -826,7 +833,7 @@ public static function linesToListsProvider(): array
826833
"\n\n",
827834
'📍',
828835
],
829-
'Markdown (multiline #1)' => [
836+
'Markdown (multiline #1, loose)' => [
830837
<<<EOF
831838
### Changes
832839
@@ -843,6 +850,29 @@ public static function linesToListsProvider(): array
843850
EOF,
844851
$input3,
845852
"\n\n",
853+
null,
854+
$defaultRegex,
855+
false,
856+
true,
857+
],
858+
'Markdown (multiline #1, not loose)' => [
859+
<<<EOF
860+
- Description
861+
over
862+
multiple
863+
864+
```
865+
lines
866+
```
867+
868+
### Changes
869+
870+
- Description
871+
- Description
872+
with different details
873+
EOF,
874+
$input3,
875+
"\n\n",
846876
],
847877
'Markdown (multiline #2)' => [
848878
<<<EOF

0 commit comments

Comments
 (0)