@@ -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 ;
0 commit comments