Skip to content

Commit

Permalink
Added ability to parse listItems in hierarchical nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdeep committed Jul 9, 2024
1 parent 81483ae commit 806309b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Proton/Sources/Swift/Core/ListParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,18 @@ public struct ListParser {
}
return items
}


/// Parses NSAttributedString to list items
/// - Parameters:
/// - attributedString: NSAttributedString to convert to list items.
/// - indent: Indentation used in list representation in attributedString. This determines the level of list item.
/// - Returns: Array of list item nodes with hierarchical representation of list
/// - Note: If NSAttributedString passed into the function is non continuous i.e. contains multiple lists, the array will contain items from all the list with the range corresponding to range of text in original attributed string.
public static func parseListHierarchy(attributedString: NSAttributedString, indent: CGFloat = 25) -> [ListItemNode] {
let listItems = parse(attributedString: attributedString, indent: indent).map { $0.listItem }
return createListItemNodes(from: listItems)
}

/// Creates hierarchical representation of `ListItem` from the provided collection based on levels of each of the items
/// - Parameter listItems: ListItems to convert
/// - Returns: Collection of `ListItemNode` with each node having children nodes based on level of individual list items.
Expand All @@ -130,7 +141,7 @@ public struct ListParser {
stack.removeLast()
}

if let last = stack.last {
if stack.last != nil {
// If there's a parent, add this node to its children
stack[stack.count - 1].node.children.append(newNode)
} else {
Expand Down
18 changes: 18 additions & 0 deletions Proton/Tests/Core/ListParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,24 @@ class ListParserTests: XCTestCase {
XCTAssertEqual(list[1].range, NSRange(location: 80, length: 47))
}

func FIXME_testParsesNonContinuousListNodes() {
let paraStyle = NSMutableParagraphStyle.forListLevel(1)
let line1 = NSAttributedString(string: "This is line 1. This is line 1. This is line 1. This is line 1.", attributes: [.listItem: 1, .paragraphStyle: paraStyle])
let line2 = NSAttributedString(string: "This is line 2.")
let line3 = NSAttributedString(string: "This is line 3. This is line 3. This is line 3.", attributes: [.listItem: 1, .paragraphStyle: paraStyle])

let text = NSMutableAttributedString(attributedString: line1)
text.append(NSAttributedString(string: "\n"))
text.append(line2)
text.append(NSAttributedString(string: "\n"))
text.append( line3)

let list = ListParser.parse(attributedString: text)
let listNodes = ListParser.parseListHierarchy(attributedString: text)
XCTFail()
}


func testFullCircleWithSameAttributeValue() {
let listItems1 = [
ListItem(text: NSAttributedString(string: "One"), level: 1, attributeValue: 1),
Expand Down

0 comments on commit 806309b

Please sign in to comment.