Skip to content

Commit 8f0977f

Browse files
committed
Exposed the throwing parser function as public and updated error descriptions
1 parent 5a90e9d commit 8f0977f

File tree

3 files changed

+64
-15
lines changed

3 files changed

+64
-15
lines changed

Marker/Classes/Marker.swift

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,51 @@
88

99
import Foundation
1010

11-
/// Returns formatted Markdown text as an attributed string.
11+
/// Parses Markdown text and eturns formatted text as an attributed string with custom markup attributes applied.
12+
/// If the parsing failed, specified Markdown tags are ignored. Yet, the rest of the style information is still applied.
1213
///
1314
/// - Parameters:
14-
/// - markdownText: String with Markdown tags.
15+
/// - text: Text.
1516
/// - textStyle: Text style object containing style information.
16-
/// - Returns: Formatted Markdown text.
17+
/// - markups: Markup information.
18+
/// - Returns: Formatted text.
1719
public func attributedMarkdownString(from markdownText: String,
1820
using textStyle: TextStyle) -> NSAttributedString {
19-
guard let (parsedString, elements) = try? MarkdownParser.parse(markdownText) else {
21+
do {
22+
return try parsedMarkdownString(from: markdownText, using: textStyle)
23+
} catch {
2024
return NSAttributedString(string: markdownText, textStyle: textStyle)
2125
}
26+
}
27+
28+
/// Parses custom mark up information and returns formatted text as an attributed string with custom markup attributes applied.
29+
/// If the parsing failed, custom markup attributes are ignored. Yet, style information from `textStyle` parameter is still applied.
30+
///
31+
/// - Parameters:
32+
/// - text: Text.
33+
/// - textStyle: Text style object containing style information.
34+
/// - markups: Markup information.
35+
/// - Returns: Formatted text.
36+
public func attributedMarkupString(from text: String,
37+
using textStyle: TextStyle,
38+
customMarkup markups: Markup) -> NSAttributedString {
39+
do {
40+
return try parsedMarkupString(from: text, using: textStyle, customMarkup: markups)
41+
} catch {
42+
return NSAttributedString(string: text, textStyle: textStyle)
43+
}
44+
}
45+
46+
/// Returns formatted Markdown text as an attributed string.
47+
///
48+
/// - Parameters:
49+
/// - markdownText: String with Markdown tags.
50+
/// - textStyle: Text style object containing style information.
51+
/// - Returns: Formatted Markdown text.
52+
/// - Throws: Parser error.
53+
public func parsedMarkdownString(from markdownText: String,
54+
using textStyle: TextStyle) throws -> NSAttributedString {
55+
let (parsedString, elements) = try MarkdownParser.parse(markdownText)
2256

2357
let attributedString = NSMutableAttributedString(string: textStyle.textTransform.applied(to: parsedString))
2458
attributedString.addAttributes(textStyle.attributes,
@@ -62,14 +96,15 @@ public func attributedMarkdownString(from markdownText: String,
6296
/// - textStyle: Text style object containing style information.
6397
/// - markups: Markup information.
6498
/// - Returns: Formatted text.
65-
public func attributedMarkupString(from text: String,
66-
using textStyle: TextStyle,
67-
customMarkup markups: Markup) -> NSAttributedString {
68-
guard
69-
markups.count > 0,
70-
let (parsedString, elements) = try? ElementParser.parse(text, for: markups.map { Symbol(character: $0.0) }) else {
71-
return NSAttributedString(string: text, textStyle: textStyle)
99+
/// - Throws: Parser error.
100+
public func parsedMarkupString(from text: String,
101+
using textStyle: TextStyle,
102+
customMarkup markups: Markup) throws -> NSAttributedString {
103+
guard markups.count > 0 else {
104+
return NSAttributedString(string: text, textStyle: textStyle)
72105
}
106+
107+
let (parsedString, elements) = try ElementParser.parse(text, for: markups.map { Symbol(character: $0.0) })
73108

74109
let attributedString = NSMutableAttributedString(string: textStyle.textTransform.applied(to: parsedString))
75110
attributedString.addAttributes(textStyle.attributes,

Marker/Classes/Parser/ElementParser.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,20 @@ internal struct ElementParser {
1515
///
1616
/// - tagMismatch: Opening tag doesn't match closing tag.
1717
/// - unclosedTags: A tag was left unclosed.
18-
enum ParserError: Error {
18+
enum ParserError: LocalizedError {
19+
1920
case tagMismatch
2021
case unclosedTags
22+
23+
var errorDescription: String? {
24+
switch self {
25+
case .tagMismatch:
26+
return "Opening tag doesn't match closing tag."
27+
case .unclosedTags:
28+
return "A tag was left unclosed."
29+
}
30+
}
31+
2132
}
2233

2334
// MARK: - Static functions

Marker/Classes/Parser/MarkdownParser.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ internal struct MarkdownParser {
1111

1212
/// Parser error.
1313
///
14-
/// - tagMismatch: Opening tag doesn't match closing tag.
15-
/// - unclosedTags: A tag was left unclosed.
16-
enum ParserError: Error {
14+
/// - invalidTagSymbol: Tag symbol is not a Markdown symbol.
15+
enum ParserError: LocalizedError {
1716
case invalidTagSymbol
17+
18+
var errorDescription: String? {
19+
return "Invalid Markdown tag."
20+
}
1821
}
1922

2023
// MARK: - Private properties

0 commit comments

Comments
 (0)