Skip to content

Commit 3d15685

Browse files
authored
Merge pull request #21 from ZhgChgLi/feature/support-new-tags
[Feat] add support s/blockquote/em/pre/code
2 parents 998abff + 3778a88 commit 3d15685

25 files changed

+259
-4
lines changed

Package.resolved

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/ZMarkupParser/Core/Markup/MarkupVisitor.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ protocol MarkupVisitor {
3232
func visit(_ markup: ImageMarkup) -> Result
3333
func visit(_ markup: TableMarkup) -> Result
3434
func visit(_ markup: HeadMarkup) -> Result
35+
func visit(_ markup: BlockQuoteMarkup) -> Result
36+
func visit(_ markup: CodeMarkup) -> Result
3537
}
3638

3739
extension MarkupVisitor {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// BlockQuoteMarkup.swift
3+
//
4+
//
5+
// Created by zhgchgli on 2023/4/11.
6+
//
7+
8+
import Foundation
9+
10+
final class BlockQuoteMarkup: Markup {
11+
weak var parentMarkup: Markup? = nil
12+
var childMarkups: [Markup] = []
13+
14+
func accept<V>(_ visitor: V) -> V.Result where V : MarkupVisitor {
15+
return visitor.visit(self)
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// CodeMarkup.swift
3+
//
4+
//
5+
// Created by zhgchgli on 2023/4/11.
6+
//
7+
8+
import Foundation
9+
10+
final class CodeMarkup: Markup {
11+
weak var parentMarkup: Markup? = nil
12+
var childMarkups: [Markup] = []
13+
14+
func accept<V>(_ visitor: V) -> V.Result where V : MarkupVisitor {
15+
return visitor.visit(self)
16+
}
17+
}

Sources/ZMarkupParser/Core/MarkupStyle/MarkupStyle+Extension.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ public extension MarkupStyle {
2020
static let h4 = MarkupStyle(font: MarkupStyleFont(size: 18))
2121
static let h5 = MarkupStyle(font: MarkupStyleFont(size: 16))
2222
static let h6 = MarkupStyle(font: MarkupStyleFont(size: 14))
23+
static let blockQuote = MarkupStyle(paragraphStyle: .init(headIndent: 10, firstLineHeadIndent: 10))
24+
static let code = MarkupStyle(paragraphStyle: .init(lineSpacing: 0, headIndent: 10, firstLineHeadIndent: 10), backgroundColor: .init(color: .lightGray))
2325
}

Sources/ZMarkupParser/Core/Processor/MarkupNSAttributedStringVisitor.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import Foundation
99

1010
struct MarkupNSAttributedStringVisitor: MarkupVisitor {
11+
1112
typealias Result = NSAttributedString
1213

1314
let components: [MarkupStyleComponent]
@@ -155,6 +156,18 @@ struct MarkupNSAttributedStringVisitor: MarkupVisitor {
155156
attributedString.insert(NSAttributedString(attachment: markup.attachment), at: 0)
156157
return attributedString
157158
}
159+
160+
func visit(_ markup: BlockQuoteMarkup) -> NSAttributedString {
161+
let attributedString = collectAttributedString(markup)
162+
attributedString.append(makeBreakLine(in: markup))
163+
attributedString.insert(makeBreakLine(in: markup), at: 0)
164+
return attributedString
165+
}
166+
167+
func visit(_ markup: CodeMarkup) -> NSAttributedString {
168+
let attributedString = collectAttributedString(markup)
169+
return attributedString
170+
}
158171
}
159172

160173
extension MarkupNSAttributedStringVisitor {

Sources/ZMarkupParser/HTML/HTMLTag/HTMLTagNameVisitor.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public protocol HTMLTagNameVisitor {
3232
func visit(_ tagName: TH_HTMLTagName) -> Result
3333
func visit(_ tagName: IMG_HTMLTagName) -> Result
3434
func visit(_ tagName: TABLE_HTMLTagName) -> Result
35+
func visit(_ tagName: S_HTMLTagName) -> Result
36+
func visit(_ tagName: PRE_HTMLTagName) -> Result
37+
func visit(_ tagName: BLOCKQUOTE_HTMLTagName) -> Result
38+
func visit(_ tagName: CODE_HTMLTagName) -> Result
39+
func visit(_ tagName: EM_HTMLTagName) -> Result
3540

3641
func visit(_ tagName: H1_HTMLTagName) -> Result
3742
func visit(_ tagName: H2_HTMLTagName) -> Result
@@ -74,6 +79,11 @@ public extension ZHTMLParserBuilder {
7479
H4_HTMLTagName(),
7580
H5_HTMLTagName(),
7681
H6_HTMLTagName(),
82+
S_HTMLTagName(),
83+
PRE_HTMLTagName(),
84+
CODE_HTMLTagName(),
85+
EM_HTMLTagName(),
86+
BLOCKQUOTE_HTMLTagName(),
7787
IMG_HTMLTagName(handler: nil)
7888
]
7989
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// BLOCKQUOTE_HTMLTagName.swift
3+
//
4+
//
5+
// Created by zhgchgli on 2023/4/11.
6+
//
7+
8+
import Foundation
9+
10+
public struct BLOCKQUOTE_HTMLTagName: HTMLTagName {
11+
public let string: String = WC3HTMLTagName.blockquote.rawValue
12+
13+
public init() {
14+
15+
}
16+
17+
public func accept<V>(_ visitor: V) -> V.Result where V : HTMLTagNameVisitor {
18+
return visitor.visit(self)
19+
}
20+
}
21+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// CODE_HTMLTagName.swift
3+
//
4+
//
5+
// Created by zhgchgli on 2023/4/11.
6+
//
7+
8+
import Foundation
9+
10+
public struct CODE_HTMLTagName: HTMLTagName {
11+
public let string: String = WC3HTMLTagName.code.rawValue
12+
13+
public init() {
14+
15+
}
16+
17+
public func accept<V>(_ visitor: V) -> V.Result where V : HTMLTagNameVisitor {
18+
return visitor.visit(self)
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// EM_HTMLTagName.swift
3+
//
4+
//
5+
// Created by zhgchgli on 2023/4/11.
6+
//
7+
8+
import Foundation
9+
10+
public struct EM_HTMLTagName: HTMLTagName {
11+
public let string: String = WC3HTMLTagName.em.rawValue
12+
13+
public init() {
14+
15+
}
16+
17+
public func accept<V>(_ visitor: V) -> V.Result where V : HTMLTagNameVisitor {
18+
return visitor.visit(self)
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// PRE_HTMLTagName.swift
3+
//
4+
//
5+
// Created by zhgchgli on 2023/4/11.
6+
//
7+
8+
import Foundation
9+
10+
public struct PRE_HTMLTagName: HTMLTagName {
11+
public let string: String = WC3HTMLTagName.pre.rawValue
12+
13+
public init() {
14+
15+
}
16+
17+
public func accept<V>(_ visitor: V) -> V.Result where V : HTMLTagNameVisitor {
18+
return visitor.visit(self)
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// S_HTMLTagName.swift
3+
//
4+
//
5+
// Created by zhgchgli on 2023/4/11.
6+
//
7+
8+
import Foundation
9+
10+
public struct S_HTMLTagName: HTMLTagName {
11+
public let string: String = WC3HTMLTagName.s.rawValue
12+
13+
public init() {
14+
15+
}
16+
17+
public func accept<V>(_ visitor: V) -> V.Result where V : HTMLTagNameVisitor {
18+
return visitor.visit(self)
19+
}
20+
}

Sources/ZMarkupParser/HTML/Processor/HTMLElementMarkupComponentMarkupStyleVisitor.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ struct HTMLElementMarkupComponentMarkupStyleVisitor: MarkupVisitor {
121121
func visit(_ markup: ImageMarkup) -> MarkupStyle? {
122122
return defaultVisit(components.value(markup: markup))
123123
}
124+
125+
func visit(_ markup: BlockQuoteMarkup) -> MarkupStyle? {
126+
return defaultVisit(components.value(markup: markup), defaultStyle: .blockQuote)
127+
}
128+
129+
func visit(_ markup: CodeMarkup) -> MarkupStyle? {
130+
return defaultVisit(components.value(markup: markup), defaultStyle: .code)
131+
}
124132
}
125133

126134
extension HTMLElementMarkupComponentMarkupStyleVisitor {

Sources/ZMarkupParser/HTML/Processor/HTMLTagNameToHTMLMarkupVisitor.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,26 @@ struct HTMLTagNameToMarkupVisitor: HTMLTagNameVisitor {
129129
return HeadMarkup(levle: .h6)
130130
}
131131

132+
func visit(_ tagName: S_HTMLTagName) -> Result {
133+
return DeletelineMarkup()
134+
}
135+
136+
func visit(_ tagName: PRE_HTMLTagName) -> Result {
137+
return BlockQuoteMarkup()
138+
}
139+
140+
func visit(_ tagName: BLOCKQUOTE_HTMLTagName) -> Result {
141+
return BlockQuoteMarkup()
142+
}
143+
144+
func visit(_ tagName: CODE_HTMLTagName) -> Result {
145+
return CodeMarkup()
146+
}
147+
148+
func visit(_ tagName: EM_HTMLTagName) -> Result {
149+
return ItalicMarkup()
150+
}
151+
132152
func visit(_ tagName: IMG_HTMLTagName) -> Result {
133153
guard let srcString = attributes?["src"],
134154
let srcURL = URL(string: srcString) else {

Tests/ZMarkupParserSnapshotTests/ZMarkupParserSnapshotTests.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,18 @@ final class ZHTMLToNSAttributedStringSnapshotTests: XCTestCase {
5151
<h4>H4</h4>
5252
<h5>H5</h5>
5353
<h6>H6</h6>
54-
<p>Before <span style="color:green;background-color:blue;font-size:18px;font-weight:bold;line-height:10;word-spacing:8px">12/26</span>, place an order and draw a round-trip ticket for two to Japan!</p>
54+
<p>Before <span style="color:green;background-color:blue;font-size:18px;font-weight:bold;line-height:10;word-spacing:8px">12/26</span>, place an order and <s>draw</s> a round-trip ticket <em>for</em> two to Japan!</p>
5555
你好你好<span style="background-color:red">你好你好</span>你好你好 <br />
56-
안녕하세요안녕하세<span style="color:red">요안녕하세</span>요안녕하세요안녕하세요안녕하세요 <br />
56+
안녕<code>하세요</code>안녕하세<span style="color:red">요안녕하세</span>요안녕하세요안녕하세요안녕하세요 <br />
5757
<span style="color:red">こんにちは</span>こんにちはこんにちは <br />
58+
<blockquote>
59+
blocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktextblocktext
60+
blocktext2
61+
</blockquote>
62+
<pre><code>
63+
precode
64+
precode2
65+
</code></pre>
5866
"""
5967

6068
var attributedHTMLString: NSAttributedString {

Tests/ZMarkupParserTests/HTML/AllMarkupsHasAddToBuilderDefaultListTests.swift

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ final class AllMarkupsHasAddToBuilderDefaultListTests: XCTestCase {
1616
let _ = visitor.visit(tagName: tagName)
1717
}
1818

19-
XCTAssertEqual(visitor.count, 25, "Must added new pre-defined HTMLTagName to ZHTMLParserBuilder.htmlTagNames")
19+
XCTAssertEqual(visitor.count, 30, "Must added new pre-defined HTMLTagName to ZHTMLParserBuilder.htmlTagNames")
2020
}
2121
}
2222

2323
private class StubVisitor: HTMLTagNameVisitor {
24-
24+
2525
typealias Result = Int
2626

2727
private(set) var count: Int = 0
@@ -155,4 +155,29 @@ private class StubVisitor: HTMLTagNameVisitor {
155155
return count
156156
}
157157

158+
func visit(_ tagName: S_HTMLTagName) -> Int {
159+
count += 1
160+
return count
161+
}
162+
163+
func visit(_ tagName: PRE_HTMLTagName) -> Int {
164+
count += 1
165+
return count
166+
}
167+
168+
func visit(_ tagName: BLOCKQUOTE_HTMLTagName) -> Int {
169+
count += 1
170+
return count
171+
}
172+
173+
func visit(_ tagName: CODE_HTMLTagName) -> Int {
174+
count += 1
175+
return count
176+
}
177+
178+
func visit(_ tagName: EM_HTMLTagName) -> Int {
179+
count += 1
180+
return count
181+
}
182+
158183
}

0 commit comments

Comments
 (0)