Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve multiple depth levels list parsing (second attempt!) #71

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions Sources/Ink/Internal/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ internal struct List: Fragment {
continue
}

let fallbackIndex = reader.currentIndex
let itemIndentationLength = try reader.readWhitespaces().count

if itemIndentationLength < indentationLength {
reader.moveToIndex(fallbackIndex)
return list
} else if itemIndentationLength == indentationLength {
continue
}

let fallbackIndex = reader.currentIndex

do {
let nestedList = try List.read(
using: &reader, indentationLength:
Expand All @@ -80,6 +80,22 @@ internal struct List: Fragment {
var lastItem = list.items.removeLast()
lastItem.nestedList = nestedList
list.items.append(lastItem)

if !reader.didReachEnd {
switch (indentationLength, reader.currentCharacter) {
case (0, _): continue
case (_, \.isWhitespace):
let fallbackIndex = reader.currentIndex
let itemIndentationLength = try reader.readWhitespaces().count
if itemIndentationLength == indentationLength {
continue
} else {
reader.moveToIndex(fallbackIndex)
return list
}
case (_, _): return list
}
}
} catch {
reader.moveToIndex(fallbackIndex)
try addTextToLastItem()
Expand Down Expand Up @@ -122,6 +138,14 @@ internal struct List: Fragment {
reader.advanceIndex()
try reader.readWhitespaces()
list.items.append(Item(text: .readLine(using: &reader)))

if !reader.didReachEnd {
switch (indentationLength, reader.currentCharacter) {
case (0, _): continue
case (_, \.isWhitespace): continue
case (_, _): return list
}
}
default:
try addTextToLastItem()
}
Expand Down
200 changes: 200 additions & 0 deletions Tests/InkTests/ListTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,200 @@ final class ListTests: XCTestCase {
XCTAssertEqual(html, expectedComponents.joined())
}

func testUnorderedListWithNestedListWithTwoLevelsGap() {
let html = MarkdownParser().html(from: """
- A
- B
- B1
- B11
- C
""")

let expectedComponents: [String] = [
"<ul>",
"<li>A</li>",
"<li>B",
"<ul>",
"<li>B1",
"<ul>",
"<li>B11</li>",
"</ul>",
"</li>",
"</ul>",
"</li>",
"<li>C</li>",
"</ul>"
]

XCTAssertEqual(html, expectedComponents.joined())
}

func testUnorderedListWithNestedListSnakeCase() {
let html = MarkdownParser().html(from: """
- A
- A1
- B
- B1
- C
- C1
""")

let expectedComponents: [String] = [
"<ul>",
"<li>A",
"<ul>",
"<li>A1</li>",
"</ul>",
"</li>",
"<li>B",
"<ul>",
"<li>B1</li>",
"</ul>",
"</li>",
"<li>C",
"<ul>",
"<li>C1</li>",
"</ul>",
"</li>",
"</ul>"
]

XCTAssertEqual(html, expectedComponents.joined())
}

func testUnorderedListWithFourLevelsNestedList() {
let html = MarkdownParser().html(from: """
- A
- A1
- A11
- A111
- B1
- B11
- C1
- C11
""")

let expectedComponents: [String] = [
"<ul>",
"<li>A",
"<ul>",
"<li>A1",
"<ul>",
"<li>A11",
"<ul>",
"<li>A111</li>",
"</ul>",
"</li>",
"</ul>",
"</li>",
"<li>B1",
"<ul>",
"<li>B11</li>",
"</ul>",
"</li>",
"<li>C1",
"<ul>",
"<li>C11</li>",
"</ul>",
"</li>",
"</ul>",
"</li>",
"</ul>"
]

XCTAssertEqual(html, expectedComponents.joined())
}

func testUnorderedListWithTripleGrowNestedList() {
let html = MarkdownParser().html(from: """
- A
- A1
- A11
- A111
- A12
- A121
- A2
- A21
- A211
""")

let expectedComponents: [String] = [
"<ul>",
"<li>A",
"<ul>",
"<li>A1",
"<ul>",
"<li>A11",
"<ul>",
"<li>A111</li>",
"</ul>",
"</li>",
"<li>A12",
"<ul>",
"<li>A121</li>",
"</ul>",
"</li>",
"</ul>",
"</li>",
"<li>A2",
"<ul>",
"<li>A21",
"<ul>",
"<li>A211</li>",
"</ul>",
"</li>",
"</ul>",
"</li>",
"</ul>",
"</li>",
"</ul>"
]

XCTAssertEqual(html, expectedComponents.joined())
}

func testUnorderedListWithDoubleSymmetricGrowNestedList() {
let html = MarkdownParser().html(from: """
- A
- A1
- A11
- A111
- A12
- A121
- A13
- A2
- B
""")

let expectedComponents: [String] = [
"<ul>",
"<li>A",
"<ul>",
"<li>A1",
"<ul>",
"<li>A11",
"<ul>",
"<li>A111</li>",
"</ul>",
"</li>",
"<li>A12",
"<ul>",
"<li>A121</li>",
"</ul>",
"</li>",
"<li>A13</li>",
"</ul>",
"</li>",
"<li>A2</li>",
"</ul>",
"</li>",
"<li>B</li>",
"</ul>"
]

XCTAssertEqual(html, expectedComponents.joined())
}

func testUnorderedListWithInvalidMarker() {
let html = MarkdownParser().html(from: """
- One
Expand Down Expand Up @@ -170,6 +364,12 @@ extension ListTests {
("testUnorderedListWithInvalidMarker", testUnorderedListWithInvalidMarker),
("testOrderedIndentedList", testUnorderedIndentedList),
("testUnorderedIndentedList", testUnorderedIndentedList),
("testUnorderedListWithNestedListSnakeCase", testUnorderedListWithNestedListSnakeCase),
("testUnorderedListWithNestedListWithTwoLevelsGap", testUnorderedListWithNestedListWithTwoLevelsGap),
("testUnorderedListWithFourLevelsNestedList", testUnorderedListWithFourLevelsNestedList),
("testUnorderedListWithTripleGrowNestedList", testUnorderedListWithTripleGrowNestedList),
("testUnorderedListWithDoubleSymmetricGrowNestedList", testUnorderedListWithDoubleSymmetricGrowNestedList),
("testUnorderedListWithInvalidMarker", testUnorderedListWithInvalidMarker)
]
}
}