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+
}

0 commit comments

Comments
 (0)