Skip to content

Commit

Permalink
Merge pull request #21 from prolificinteractive/feature/factory_funct…
Browse files Browse the repository at this point in the history
…ions_and_underline_support

Added factory functions and underline support
  • Loading branch information
htinlinn authored Jul 19, 2017
2 parents d314237 + e24b23d commit 82664ed
Show file tree
Hide file tree
Showing 12 changed files with 1,198 additions and 8 deletions.
40 changes: 40 additions & 0 deletions Marker/Marker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@
27C4E66B1ED60C7700DDE387 /* UILabelExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27C4E6691ED60C7700DDE387 /* UILabelExtension.swift */; };
480C7A5F1F15832A0094E4EA /* ParserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 271C85C81ED4E37700F8BBBB /* ParserTests.swift */; };
480C7A601F15832A0094E4EA /* ParserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 271C85C81ED4E37700F8BBBB /* ParserTests.swift */; };
48F232751F1FB7B600E86D5D /* TextTransform+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F232741F1FB7B600E86D5D /* TextTransform+Extensions.swift */; };
48F232761F1FB7B600E86D5D /* TextTransform+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F232741F1FB7B600E86D5D /* TextTransform+Extensions.swift */; };
48F232771F1FB7B600E86D5D /* TextTransform+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F232741F1FB7B600E86D5D /* TextTransform+Extensions.swift */; };
48F2327C1F1FBA4400E86D5D /* TextTransformEquatableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F232781F1FBA2200E86D5D /* TextTransformEquatableTests.swift */; };
48F2327D1F1FBA4400E86D5D /* TextTransformEquatableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F232781F1FBA2200E86D5D /* TextTransformEquatableTests.swift */; };
48F2327E1F1FBA4500E86D5D /* TextTransformEquatableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F232781F1FBA2200E86D5D /* TextTransformEquatableTests.swift */; };
48F232801F1FBA7800E86D5D /* TextStyle+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F2327F1F1FBA7800E86D5D /* TextStyle+Extensions.swift */; };
48F232811F1FBA7800E86D5D /* TextStyle+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F2327F1F1FBA7800E86D5D /* TextStyle+Extensions.swift */; };
48F232821F1FBA7800E86D5D /* TextStyle+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F2327F1F1FBA7800E86D5D /* TextStyle+Extensions.swift */; };
48F232841F1FBD1300E86D5D /* TextStyleEquatableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F232831F1FBD1300E86D5D /* TextStyleEquatableTests.swift */; };
48F232851F1FBD1300E86D5D /* TextStyleEquatableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F232831F1FBD1300E86D5D /* TextStyleEquatableTests.swift */; };
48F232861F1FBD1300E86D5D /* TextStyleEquatableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F232831F1FBD1300E86D5D /* TextStyleEquatableTests.swift */; };
48F232881F1FC2F900E86D5D /* TextStyleFactoryFunctionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F232871F1FC2F900E86D5D /* TextStyleFactoryFunctionTests.swift */; };
48F232891F1FC2F900E86D5D /* TextStyleFactoryFunctionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F232871F1FC2F900E86D5D /* TextStyleFactoryFunctionTests.swift */; };
48F2328A1F1FC2F900E86D5D /* TextStyleFactoryFunctionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F232871F1FC2F900E86D5D /* TextStyleFactoryFunctionTests.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -132,6 +147,11 @@
27C4E6601ED6080B00DDE387 /* UITextViewExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UITextViewExtension.swift; path = TextView/UITextViewExtension.swift; sourceTree = "<group>"; };
27C4E6661ED6082800DDE387 /* NSTextViewExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NSTextViewExtension.swift; path = TextView/NSTextViewExtension.swift; sourceTree = "<group>"; };
27C4E6691ED60C7700DDE387 /* UILabelExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UILabelExtension.swift; path = Label/UILabelExtension.swift; sourceTree = "<group>"; };
48F232741F1FB7B600E86D5D /* TextTransform+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "TextTransform+Extensions.swift"; sourceTree = "<group>"; };
48F232781F1FBA2200E86D5D /* TextTransformEquatableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TextTransformEquatableTests.swift; sourceTree = "<group>"; };
48F2327F1F1FBA7800E86D5D /* TextStyle+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "TextStyle+Extensions.swift"; sourceTree = "<group>"; };
48F232831F1FBD1300E86D5D /* TextStyleEquatableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TextStyleEquatableTests.swift; sourceTree = "<group>"; };
48F232871F1FC2F900E86D5D /* TextStyleFactoryFunctionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TextStyleFactoryFunctionTests.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -216,7 +236,9 @@
271C85AB1ED4E2B700F8BBBB /* Parser */,
271C85B31ED4E2B700F8BBBB /* String+Extensions.swift */,
271C85B41ED4E2B700F8BBBB /* TextStyle.swift */,
48F2327F1F1FBA7800E86D5D /* TextStyle+Extensions.swift */,
271C85B51ED4E2B700F8BBBB /* TextTransform.swift */,
48F232741F1FB7B600E86D5D /* TextTransform+Extensions.swift */,
27C4E61E1ED5EE3000DDE387 /* Utility */,
);
path = Marker;
Expand All @@ -226,6 +248,9 @@
isa = PBXGroup;
children = (
271C85C81ED4E37700F8BBBB /* ParserTests.swift */,
48F232831F1FBD1300E86D5D /* TextStyleEquatableTests.swift */,
48F232871F1FC2F900E86D5D /* TextStyleFactoryFunctionTests.swift */,
48F232781F1FBA2200E86D5D /* TextTransformEquatableTests.swift */,
271C859A1ED4E2A000F8BBBB /* Info.plist */,
271C85C71ED4E37600F8BBBB /* MarkerTests-Bridging-Header.h */,
);
Expand Down Expand Up @@ -574,13 +599,15 @@
buildActionMask = 2147483647;
files = (
271C85C51ED4E2B700F8BBBB /* TextStyle.swift in Sources */,
48F232751F1FB7B600E86D5D /* TextTransform+Extensions.swift in Sources */,
271C85C01ED4E2B700F8BBBB /* MarkdownParser.swift in Sources */,
271C85BB1ED4E2B700F8BBBB /* Markup.swift in Sources */,
27C4E66A1ED60C7700DDE387 /* UILabelExtension.swift in Sources */,
27C4E6551ED6064400DDE387 /* Font.swift in Sources */,
271C85BD1ED4E2B700F8BBBB /* Element.swift in Sources */,
271C85BF1ED4E2B700F8BBBB /* MarkdownElement.swift in Sources */,
271C85C31ED4E2B700F8BBBB /* TagParser.swift in Sources */,
48F232801F1FBA7800E86D5D /* TextStyle+Extensions.swift in Sources */,
27C4E64F1ED6064400DDE387 /* UIButtonExtension.swift in Sources */,
271C85C11ED4E2B700F8BBBB /* Symbol.swift in Sources */,
271C85BE1ED4E2B700F8BBBB /* ElementParser.swift in Sources */,
Expand All @@ -599,6 +626,9 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
48F2327C1F1FBA4400E86D5D /* TextTransformEquatableTests.swift in Sources */,
48F232881F1FC2F900E86D5D /* TextStyleFactoryFunctionTests.swift in Sources */,
48F232841F1FBD1300E86D5D /* TextStyleEquatableTests.swift in Sources */,
271C85C91ED4E37700F8BBBB /* ParserTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -608,13 +638,15 @@
buildActionMask = 2147483647;
files = (
27C4E6091ED5ED6400DDE387 /* TextStyle.swift in Sources */,
48F232761F1FB7B600E86D5D /* TextTransform+Extensions.swift in Sources */,
27C4E6041ED5ED6400DDE387 /* MarkdownParser.swift in Sources */,
27C4E5FF1ED5ED6400DDE387 /* Markup.swift in Sources */,
27C4E66B1ED60C7700DDE387 /* UILabelExtension.swift in Sources */,
27C4E6561ED6064400DDE387 /* Font.swift in Sources */,
27C4E6011ED5ED6400DDE387 /* Element.swift in Sources */,
27C4E6031ED5ED6400DDE387 /* MarkdownElement.swift in Sources */,
27C4E6071ED5ED6400DDE387 /* TagParser.swift in Sources */,
48F232811F1FBA7800E86D5D /* TextStyle+Extensions.swift in Sources */,
27C4E6501ED6064400DDE387 /* UIButtonExtension.swift in Sources */,
27C4E6051ED5ED6400DDE387 /* Symbol.swift in Sources */,
27C4E6021ED5ED6400DDE387 /* ElementParser.swift in Sources */,
Expand All @@ -633,6 +665,9 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
48F2327D1F1FBA4400E86D5D /* TextTransformEquatableTests.swift in Sources */,
48F232891F1FC2F900E86D5D /* TextStyleFactoryFunctionTests.swift in Sources */,
48F232851F1FBD1300E86D5D /* TextStyleEquatableTests.swift in Sources */,
480C7A601F15832A0094E4EA /* ParserTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -643,9 +678,11 @@
files = (
27C4E61A1ED5ED6500DDE387 /* TextStyle.swift in Sources */,
27C4E64E1ED6064400DDE387 /* NSButtonExtension.swift in Sources */,
48F232821F1FBA7800E86D5D /* TextStyle+Extensions.swift in Sources */,
27C4E6151ED5ED6500DDE387 /* MarkdownParser.swift in Sources */,
27C4E65A1ED6064400DDE387 /* NSTextFieldExtension.swift in Sources */,
27C4E6541ED6064400DDE387 /* Color.swift in Sources */,
48F232771F1FB7B600E86D5D /* TextTransform+Extensions.swift in Sources */,
27C4E6101ED5ED6500DDE387 /* Markup.swift in Sources */,
27C4E6121ED5ED6500DDE387 /* Element.swift in Sources */,
27C4E6141ED5ED6500DDE387 /* MarkdownElement.swift in Sources */,
Expand All @@ -666,6 +703,9 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
48F2327E1F1FBA4500E86D5D /* TextTransformEquatableTests.swift in Sources */,
48F2328A1F1FC2F900E86D5D /* TextStyleFactoryFunctionTests.swift in Sources */,
48F232861F1FBD1300E86D5D /* TextStyleEquatableTests.swift in Sources */,
480C7A5F1F15832A0094E4EA /* ParserTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
11 changes: 11 additions & 0 deletions Marker/Marker/Marker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public func parsedMarkdownString(from markdownText: String,
elements.forEach { (element) in
var font: Font? = nil
var strikethroughStyle: NSUnderlineStyle? = nil
var underlineStyle: NSUnderlineStyle? = nil

switch element {
case .em(_):
Expand All @@ -73,6 +74,8 @@ public func parsedMarkdownString(from markdownText: String,
font = textStyle.strongFont
case .strikethrough(_):
strikethroughStyle = textStyle.strikethroughStyle
case .underline(_):
underlineStyle = textStyle.underlineStyle
}

if let font = font {
Expand All @@ -88,6 +91,14 @@ public func parsedMarkdownString(from markdownText: String,
range: parsedString.range(from: element.range))
}
}

if let underlineStyle = underlineStyle {
attributedString.addAttributes([NSUnderlineStyleAttributeName: underlineStyle.rawValue], range: parsedString.range(from: element.range))

if let underlineColor = textStyle.underlineColor {
attributedString.addAttributes([NSUnderlineColorAttributeName: underlineColor], range: parsedString.range(from: element.range))
}
}
}

return attributedString
Expand Down
4 changes: 4 additions & 0 deletions Marker/Marker/Parser/MarkdownElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import Foundation
/// - em: Emphasis element.
/// - strong: Strong element.
/// - strikethrough: Strikethrough element.
/// - underline: Underline element.
internal enum MarkdownElement {

case em(Range<Index>)
case strong(Range<Index>)
case strikethrough(Range<Index>)
case underline(Range<Index>)

/// Range of characters that the elements apply to.
var range: Range<Index> {
Expand All @@ -28,6 +30,8 @@ internal enum MarkdownElement {
return range
case .strikethrough(let range):
return range
case .underline(let range):
return range
}
}

Expand Down
9 changes: 7 additions & 2 deletions Marker/Marker/Parser/MarkdownParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ internal struct MarkdownParser {
private static let underscoreStrongSymbol = Symbol(rawValue: "__")
private static let asteriskStrongSymbol = Symbol(rawValue: "**")
private static let tildeStrikethroughSymbol = Symbol(rawValue: "~~")
private static let equalUnderlineSymbol = Symbol(rawValue: "==")

// MARK: - Static functions

Expand All @@ -41,7 +42,8 @@ internal struct MarkdownParser {
let asteriskEmSymbol = asteriskEmSymbol,
let underscoreStrongSymbol = underscoreStrongSymbol,
let asteriskStrongSymbol = asteriskStrongSymbol,
let tildeStrikethroughSymbol = tildeStrikethroughSymbol else {
let tildeStrikethroughSymbol = tildeStrikethroughSymbol,
let equalUnderlineSymbol = equalUnderlineSymbol else {
return (string, [])
}

Expand All @@ -53,6 +55,8 @@ internal struct MarkdownParser {
return .strong(element.range)
case tildeStrikethroughSymbol:
return .strikethrough(element.range)
case equalUnderlineSymbol:
return .underline(element.range)
default:
throw ParserError.invalidTagSymbol
}
Expand All @@ -63,7 +67,8 @@ internal struct MarkdownParser {
asteriskEmSymbol,
underscoreStrongSymbol,
asteriskStrongSymbol,
tildeStrikethroughSymbol])
tildeStrikethroughSymbol,
equalUnderlineSymbol])
return try (strippedString, elements.map(transformToMarkdownElement))
}

Expand Down
Loading

0 comments on commit 82664ed

Please sign in to comment.