Skip to content

Commit d71980e

Browse files
authored
Merge pull request #63 from ZhgChgLi/fix/harry/list-indent
[Fix] list item indent with fancy way
2 parents 7525275 + dfacf29 commit d71980e

File tree

2 files changed

+16
-41
lines changed

2 files changed

+16
-41
lines changed

Sources/ZMarkupParser/Core/Processor/MarkupNSAttributedStringVisitor.swift

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,31 +68,14 @@ struct MarkupNSAttributedStringVisitor: MarkupVisitor {
6868
// We don't set NSTextList to NSParagraphStyle directly, because NSTextList have abnormal extra spaces.
6969
// ref: https://stackoverflow.com/questions/66714650/nstextlist-formatting
7070

71-
var level = 0
72-
var parentMarkup: Markup? = markup.parentMarkup as? ListMarkup
73-
while (parentMarkup != nil) {
74-
if parentMarkup is ListMarkup {
75-
level += 1
76-
}
77-
parentMarkup = parentMarkup?.parentMarkup
78-
}
79-
let indent = String(repeating: "\t", count: level - 1)
80-
8171
if let parentMarkup = markup.parentMarkup as? ListMarkup {
8272
let thisAttributedString: NSMutableAttributedString
8373
if parentMarkup.styleList.type.isOrder() {
8474
let siblingListItems = markup.parentMarkup?.childMarkups.filter({ $0 is ListItemMarkup }) ?? []
8575
let position = (siblingListItems.firstIndex(where: { $0 === markup }) ?? 0) + parentMarkup.styleList.startingItemNumber
86-
thisAttributedString = NSMutableAttributedString(attributedString: makeString(in: markup, string:indent+parentMarkup.styleList.marker(forItemNumber: position)))
76+
thisAttributedString = NSMutableAttributedString(attributedString: makeString(in: markup, string:parentMarkup.styleList.marker(forItemNumber: position)))
8777
} else {
88-
thisAttributedString = NSMutableAttributedString(attributedString: makeString(in: markup, string:indent+parentMarkup.styleList.marker(forItemNumber: parentMarkup.styleList.startingItemNumber)))
89-
}
90-
91-
// Since we use \t as an indentation character, the list text cannot contain \t, otherwise it will cause formatting issues, so we replace \t with spaces directly.
92-
var tabRange = attributedString.mutableString.range(of: "\t", options: .caseInsensitive)
93-
while tabRange.location != NSNotFound {
94-
attributedString.replaceCharacters(in: tabRange, with: " ") // the default width of a \t character is typically equivalent to 8 spaces
95-
tabRange = attributedString.mutableString.range(of: "\t", options: .caseInsensitive)
78+
thisAttributedString = NSMutableAttributedString(attributedString: makeString(in: markup, string:parentMarkup.styleList.marker(forItemNumber: parentMarkup.styleList.startingItemNumber)))
9679
}
9780

9881
attributedString.insert(thisAttributedString, at: 0)

Sources/ZMarkupParser/HTML/Processor/HTMLElementMarkupComponentMarkupStyleVisitor.swift

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,35 +76,27 @@ struct HTMLElementMarkupComponentMarkupStyleVisitor: MarkupVisitor {
7676

7777
func visit(_ markup: ListMarkup) -> Result {
7878
var style = defaultVisit(components.value(markup: markup)) ?? MarkupStyle()
79-
80-
var level = 1
81-
var parentMarkup: Markup? = markup.parentMarkup as? ListMarkup
82-
while (parentMarkup != nil) {
83-
if parentMarkup is ListMarkup {
84-
level += 1
85-
}
86-
parentMarkup = parentMarkup?.parentMarkup
87-
}
79+
8880

8981
let headIndent = (style.font.size ?? 16) * markup.styleList.headIndentMultiply
9082
let indent = (style.font.size ?? 16) * markup.styleList.indentMultiply
91-
92-
style.paragraphStyle.headIndent = CGFloat(level) * (style.paragraphStyle.headIndent ?? indent) + headIndent + (indent * CGFloat(level))
83+
let dotIndent: CGFloat = (markup.styleList.type.isOrder()) ? (indent) : (0) // for 1. -> "."
9384

94-
if style.paragraphStyle.tabStops == nil {
95-
var tabStops: [NSTextTab] = [.init(textAlignment: .left, location: headIndent)]
96-
for i in 1...level {
97-
let dotWidth: CGFloat
98-
if markup.styleList.type.isOrder() {
99-
dotWidth = indent * CGFloat(i)
100-
} else {
101-
dotWidth = 0
102-
}
103-
tabStops.append(.init(textAlignment: .left, location: CGFloat(i) * indent + headIndent + dotWidth))
85+
var parentIndent: CGFloat = 0
86+
var parentMarkup: Markup? = markup.parentMarkup
87+
while (parentMarkup != nil) {
88+
if let thisParentMarkup = parentMarkup as? ListMarkup {
89+
parentIndent += visit(markup: thisParentMarkup)?.paragraphStyle.headIndent ?? 0
10490
}
105-
style.paragraphStyle.tabStops = tabStops
91+
parentMarkup = parentMarkup?.parentMarkup
10692
}
93+
94+
var tabStops: [NSTextTab] = [.init(textAlignment: .left, location: headIndent + parentIndent)]
95+
tabStops.append(.init(textAlignment: .left, location: headIndent + parentIndent + dotIndent + indent))
96+
10797

98+
style.paragraphStyle.tabStops = style.paragraphStyle.tabStops ?? tabStops
99+
style.paragraphStyle.headIndent = style.paragraphStyle.headIndent ?? style.paragraphStyle.tabStops?.last?.location
108100
return style
109101
}
110102

0 commit comments

Comments
 (0)