From f0de1106fa27a1e3e6ccd3fff8cbbc7f578aaad6 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Tue, 21 Jan 2025 21:33:46 +0700 Subject: all: fix section detected as paragraph after list and comment Previously, given the following markup, ---- * Sub list + Sub list content. //}}} //{{{ == Sub 2 //}} ---- The section "Sub 2" will be parsed as paragraph instead of new section. --- document_parser.go | 24 +++++-- testdata/comment_test.txt | 158 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+), 6 deletions(-) create mode 100644 testdata/comment_test.txt diff --git a/document_parser.go b/document_parser.go index 3a0a2f8..74c14b6 100644 --- a/document_parser.go +++ b/document_parser.go @@ -62,8 +62,8 @@ func parseSub(parentDoc *Document, content []byte) (subdoc *Document) { return subdoc } -// consumeLinesUntil given an element el, consume all lines until we found -// a line with kind match with term or match with one in terms. +// consumeLinesUntil given an element el, consume lines until we found a line +// with kind match with term OR match with one of kind in the terms. func (docp *documentParser) consumeLinesUntil(el *element, term int, terms []int) (line []byte) { var ( logp = `consumeLinesUntil` @@ -1283,8 +1283,14 @@ func (docp *documentParser) parseListOrdered(parent *element, title string, line docp.kind == lineKindAttributeElement || docp.kind == lineKindBlockTitle || docp.kind == lineKindID || - docp.kind == lineKindIDShort || - docp.kind == lineKindText { + docp.kind == lineKindIDShort { + if docp.prevKind == lineKindEmpty || + docp.prevKind == lineKindComment || + docp.prevKind == lineKindBlockComment { + break + } + } + if docp.kind == lineKindText { if docp.prevKind == lineKindEmpty { break } @@ -1531,8 +1537,14 @@ func (docp *documentParser) parseListUnordered(parent, el *element, line []byte, docp.kind == lineKindAdmonition || docp.kind == lineKindBlockTitle || docp.kind == lineKindID || - docp.kind == lineKindIDShort || - docp.kind == lineKindText { + docp.kind == lineKindIDShort { + if docp.prevKind == lineKindEmpty || + docp.prevKind == lineKindComment || + docp.prevKind == lineKindBlockComment { + break + } + } + if docp.kind == lineKindText { if docp.prevKind == lineKindEmpty { break } diff --git a/testdata/comment_test.txt b/testdata/comment_test.txt new file mode 100644 index 0000000..05eec37 --- /dev/null +++ b/testdata/comment_test.txt @@ -0,0 +1,158 @@ +Test comments. + +>>> section after comment += Title + +//{{{ +== Sub 1 + +* List +** Sub list ++ +Sub list content. + +//}}} +//{{{ +== Sub 2 + +. Ordered +.. Sub ordered ++ +Sub 2 content. + +//}}} +//{{{ +== Sub 3 + +//}}} + +<<< section after comment + +
+

Sub 1

+
+
+
    +
  • +

    List

    +
    +
      +
    • +

      Sub list

      +
      +

      Sub list content.

      +
      +
    • +
    +
    +
  • +
+
+
+
+
+

Sub 2

+
+
+
    +
  1. +

    Ordered

    +
    +
      +
    1. +

      Sub ordered

      +
      +

      Sub 2 content.

      +
      +
    2. +
    +
    +
  2. +
+
+
+
+
+

Sub 3

+
+
+
+ +>>> section after block comment += Title + +//{{{ +== Sub 1 + +* List +** Sub list ++ +Sub list content. + +//}}} +//// +Block comment +//// +== Sub 2 + +. Ordered +.. Sub ordered ++ +Sub 2 content. + +//// +Block comment +//// +== Sub 3 + +<<< section after block comment + +
+

Sub 1

+
+
+
    +
  • +

    List

    +
    +
      +
    • +

      Sub list

      +
      +

      Sub list content.

      +
      +
    • +
    +
    +
  • +
+
+
+
+
+

Sub 2

+
+
+
    +
  1. +

    Ordered

    +
    +
      +
    1. +

      Sub ordered

      +
      +

      Sub 2 content.

      +
      +
    2. +
    +
    +
  2. +
+
+
+
+
+

Sub 3

+
+
+
-- cgit v1.3