-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Links and inline code styles
- Loading branch information
Showing
6 changed files
with
206 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
Markdowner/Classes/Default Elements/InlineCodeElement.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// | ||
// InlineCodeElement.swift | ||
// Markdowner | ||
// | ||
// Created by Reynaldo Aguilar on 7/26/18. | ||
// | ||
|
||
import Foundation | ||
|
||
open class InlineCodeElement: MarkdownElement { | ||
let symbolsColor: UIColor | ||
let font: UIFont | ||
|
||
public init(symbolsColor: UIColor, font: UIFont) { | ||
self.symbolsColor = symbolsColor | ||
self.font = font | ||
|
||
let pattern = "(?<!`)`(?![ `]).*?(?<![ `])`(?!`)" | ||
|
||
guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else { | ||
fatalError() | ||
} | ||
|
||
super.init(regex: regex) | ||
} | ||
|
||
open override func styles(forMatch match: String) -> [MarkdownElement.Style] { | ||
let fontStyle = Style( | ||
attributeKey: .font, | ||
value: font.dynamic(), | ||
startIndex: 0, | ||
length: match.count | ||
) | ||
|
||
let indicatorRanges = [ | ||
NSRange(location: 0, length: 1), | ||
NSRange(location: match.count - 1, length: 1) | ||
] | ||
|
||
let foregroundStyles = indicatorRanges.map { | ||
Style( | ||
attributeKey: .foregroundColor, | ||
value: symbolsColor, | ||
startIndex: $0.location, | ||
length: $0.length | ||
) | ||
} | ||
|
||
return foregroundStyles + [fontStyle] | ||
} | ||
|
||
open override func applying(stylesConfiguration: StylesConfiguration) -> InlineCodeElement { | ||
return InlineCodeElement( | ||
symbolsColor: stylesConfiguration.symbolsColor, | ||
font: font | ||
) | ||
} | ||
|
||
open override func replacementRanges(forMatch match: String) -> [ReplacementRange] { | ||
let ranges = [NSRange(location: 0, length: 1), NSRange(location: match.count - 1, length: 1)] | ||
return ranges.map { ReplacementRange(range: $0, replacementValue: NSAttributedString()) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// | ||
// LinkElement.swift | ||
// Markdowner | ||
// | ||
// Created by Reynaldo Aguilar on 7/24/18. | ||
// | ||
|
||
import Foundation | ||
|
||
open class LinkElement: MarkdownElement { | ||
let symbolsColor: UIColor | ||
let textFont: UIFont | ||
let linksColor: UIColor | ||
|
||
init(symbolsColor: UIColor, font: UIFont, linksColor: UIColor) { | ||
self.symbolsColor = symbolsColor | ||
self.textFont = font | ||
self.linksColor = linksColor | ||
|
||
// the url regex was taken from: https://stackoverflow.com/a/3809435/3385517 | ||
// specifically from the link http://regexr.com/3e6m0 inside that question | ||
let urlRegexPattern = "(http(s)?:\\/\\/.)?(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)" | ||
|
||
let linkPattern = "(?<!\\[)(\\[).+(\\])(\\()\(urlRegexPattern)(\\))(?!\\))" | ||
|
||
guard let regex = try? NSRegularExpression(pattern: linkPattern, options: []) else { | ||
fatalError() | ||
} | ||
|
||
super.init(regex: regex) | ||
} | ||
|
||
open override func styles(forMatch match: String) -> [MarkdownElement.Style] { | ||
let ranges = symbolRanges(forMatch: match) | ||
|
||
let symbolsColor = ranges.map { range in | ||
Style( | ||
attributeKey: .foregroundColor, | ||
value: self.symbolsColor, | ||
startIndex: range.location, | ||
length: range.length | ||
) | ||
} | ||
|
||
let textRange = NSRange( | ||
location: ranges[0].location + 1, | ||
length: ranges[1].location - ranges[0].location | ||
) | ||
|
||
let urlRange = NSRange( | ||
location: ranges[2].location + 1, | ||
length: ranges[3].location - ranges[2].location | ||
) | ||
|
||
let textStyles = [ | ||
MarkdownElement.Style( | ||
attributeKey: .link, | ||
value: (match as NSString).substring(with: urlRange), | ||
startIndex: textRange.location, | ||
length: textRange.length | ||
), | ||
MarkdownElement.Style( | ||
attributeKey: .foregroundColor, | ||
value: linksColor, | ||
startIndex: textRange.location, | ||
length: textRange.length | ||
) | ||
] | ||
|
||
return symbolsColor + textStyles | ||
} | ||
|
||
open override func replacementRanges(forMatch match: String) -> [ReplacementRange] { | ||
let symbolRanges = self.symbolRanges(forMatch: match) | ||
|
||
let urlPortionRange = NSRange( | ||
location: symbolRanges[2].location, | ||
length: match.count - symbolRanges[2].location | ||
) | ||
|
||
return [symbolRanges[0], symbolRanges[1], urlPortionRange].map { | ||
ReplacementRange(range: $0, replacementValue: NSAttributedString()) | ||
} | ||
} | ||
|
||
open override func applying(stylesConfiguration: StylesConfiguration) -> LinkElement { | ||
return LinkElement( | ||
symbolsColor: stylesConfiguration.symbolsColor, | ||
font: stylesConfiguration.baseFont, | ||
linksColor: linksColor | ||
) | ||
} | ||
|
||
private func symbolRanges(forMatch match: String) -> [NSRange] { | ||
let fullRange = NSRange(location: 0, length: match.count) | ||
|
||
guard let regexMatch = regex.matches(in: match, options: [], range: fullRange).first else { | ||
fatalError("ERROR: Unable to find a match for the given ouput") | ||
} | ||
|
||
let lastRange = regexMatch.range(at: regexMatch.numberOfRanges - 1) | ||
let ranges = (1...3).map { regexMatch.range(at: $0) } + [lastRange] | ||
return ranges | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters