forked from chrisdhaan/CDMarkdownKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDMarkdownParser.swift
213 lines (197 loc) · 9.29 KB
/
CDMarkdownParser.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// CDMarkdownParser.swift
// CDMarkdownKit
//
// Created by Christopher de Haan on 11/7/16.
//
// Copyright © 2016-2018 Christopher de Haan <contact@christopherdehaan.me>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#if os(iOS) || os(tvOS) || os(watchOS)
import UIKit
#elseif os(macOS)
import Cocoa
#endif
open class CDMarkdownParser {
// MARK: - Element Arrays
fileprivate var escapingElements: [CDMarkdownElement]
fileprivate var defaultElements: [CDMarkdownElement]
fileprivate var unescapingElements: [CDMarkdownElement]
open var customElements: [CDMarkdownElement]
// MARK: - Basic Elements
public let header: CDMarkdownHeader
public let list: CDMarkdownList
public let quote: CDMarkdownQuote
public let link: CDMarkdownLink
public let automaticLink: CDMarkdownAutomaticLink
public let bold: CDMarkdownBold
public let italic: CDMarkdownItalic
public let code: CDMarkdownCode
public let syntax: CDMarkdownSyntax
#if os(iOS) || os(macOS) || os(tvOS)
public let image: CDMarkdownImage
#endif
// MARK: - Escaping Elements
fileprivate var codeEscaping = CDMarkdownCodeEscaping()
fileprivate var escaping = CDMarkdownEscaping()
fileprivate var unescaping = CDMarkdownUnescaping()
// MARK: - Configuration
// Enables or disables detection of URLs even without Markdown format
open var automaticLinkDetectionEnabled: Bool = true
public let font: CDFont
public let fontColor: CDColor
public let backgroundColor: CDColor
public let paragraphStyle: NSParagraphStyle
// MARK: - Initializer
public init(font: CDFont = CDFont.systemFont(ofSize: 12),
boldFont: CDFont? = nil,
italicFont: CDFont? = nil,
fontColor: CDColor = CDColor.black,
backgroundColor: CDColor = CDColor.clear,
paragraphStyle: NSParagraphStyle? = nil,
imageSize: CGSize? = nil,
automaticLinkDetectionEnabled: Bool = true,
customElements: [CDMarkdownElement] = []) {
self.font = font
self.fontColor = fontColor
self.backgroundColor = backgroundColor
if let paragraphStyle = paragraphStyle {
self.paragraphStyle = paragraphStyle
} else {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.paragraphSpacing = 3
paragraphStyle.paragraphSpacingBefore = 0
paragraphStyle.lineSpacing = 1.38
self.paragraphStyle = paragraphStyle
}
header = CDMarkdownHeader(font: font,
color: fontColor,
backgroundColor: backgroundColor,
paragraphStyle: paragraphStyle)
list = CDMarkdownList(font: font,
color: fontColor,
backgroundColor: backgroundColor,
paragraphStyle: paragraphStyle)
quote = CDMarkdownQuote(font: font,
color: fontColor,
backgroundColor: backgroundColor,
paragraphStyle: paragraphStyle)
link = CDMarkdownLink(font: font,
color: fontColor,
backgroundColor: backgroundColor,
paragraphStyle: paragraphStyle)
automaticLink = CDMarkdownAutomaticLink(font: font,
color: fontColor,
backgroundColor: backgroundColor,
paragraphStyle: paragraphStyle)
bold = CDMarkdownBold(font: font,
customBoldFont: boldFont,
color: fontColor,
backgroundColor: backgroundColor,
paragraphStyle: paragraphStyle)
italic = CDMarkdownItalic(font: font,
customItalicFont: italicFont,
color: fontColor,
backgroundColor: backgroundColor,
paragraphStyle: paragraphStyle)
code = CDMarkdownCode(font: font,
color: fontColor,
backgroundColor: backgroundColor,
paragraphStyle: paragraphStyle)
syntax = CDMarkdownSyntax(font: font,
color: fontColor,
backgroundColor: backgroundColor,
paragraphStyle: paragraphStyle)
#if os(iOS) || os(macOS) || os(tvOS)
image = CDMarkdownImage(font: font,
color: fontColor,
backgroundColor: backgroundColor,
paragraphStyle: paragraphStyle,
size: imageSize)
#endif
self.automaticLinkDetectionEnabled = automaticLinkDetectionEnabled
self.escapingElements = [codeEscaping, escaping]
#if os(iOS) || os(macOS) || os(tvOS)
self.defaultElements = [header, list, quote, link, automaticLink, bold, italic, image]
#else
self.defaultElements = [header, list, quote, link, automaticLink, bold, italic]
#endif
self.unescapingElements = [code, syntax, unescaping]
self.customElements = customElements
}
// MARK: - Element Extensibility
open func addCustomElement(_ element: CDMarkdownElement) {
customElements.append(element)
}
open func removeCustomElement(_ element: CDMarkdownElement) {
guard let index = customElements.firstIndex(where: { someElement -> Bool in
return element === someElement
}) else {
return
}
customElements.remove(at: index)
}
// MARK: - Parsing
open func parse(_ markdown: String) -> NSAttributedString {
return parse(NSAttributedString(string: markdown))
}
open func parse(_ markdown: NSAttributedString) -> NSAttributedString {
let attributedString = NSMutableAttributedString(attributedString: markdown)
let mutableString = attributedString.mutableString
mutableString.replaceOccurrences(of: "\n\n+",
with: "\n",
options: .regularExpression,
range: NSRange(location: 0,
length: mutableString.length))
mutableString.replaceOccurrences(of: " ",
with: " ",
range: NSRange(location: 0,
length: mutableString.length))
let regExp = try? NSRegularExpression(pattern: "^\\s+",
options: .anchorsMatchLines)
if let regExp = regExp {
regExp.replaceMatches(in: mutableString,
options: [],
range: NSRange(location: 0,
length: mutableString.length),
withTemplate: "")
}
let range = NSRange(location: 0,
length: attributedString.length)
attributedString.addFont(font,
toRange: range)
attributedString.addForegroundColor(fontColor,
toRange: range)
attributedString.addBackgroundColor(backgroundColor,
toRange: range)
attributedString.addParagraphStyle(paragraphStyle,
toRange: range)
var elements: [CDMarkdownElement] = escapingElements
elements.append(contentsOf: defaultElements)
elements.append(contentsOf: customElements)
elements.append(contentsOf: unescapingElements)
elements.forEach { element in
if automaticLinkDetectionEnabled || type(of: element) != CDMarkdownAutomaticLink.self {
element.parse(attributedString)
}
}
return attributedString
}
}