Add some documentation

This commit is contained in:
Indragie Karunaratne
2015-04-28 23:05:30 -06:00
parent 6601ac338a
commit c0e7f92746
3 changed files with 51 additions and 21 deletions

View File

@@ -8,9 +8,13 @@
import UIKit
public typealias TextAttributes = [String: AnyObject]
/**
* Encapsulates the attributes to use for styling various types
* of Markdown elements.
*/
public struct MarkdownAttributes {
public typealias TextAttributes = [String: AnyObject]
public var defaultAttributes: TextAttributes = [
NSFontAttributeName: UIFont.preferredFontForTextStyle(UIFontTextStyleBody)
]
@@ -78,7 +82,7 @@ public struct MarkdownAttributes {
NSForegroundColorAttributeName: UIColor.darkGrayColor()
]
public func attributesForHeaderLevel(level: Int) -> TextAttributes? {
internal func attributesForHeaderLevel(level: Int) -> TextAttributes? {
switch level {
case 1: return h1Attributes
case 2: return h2Attributes

View File

@@ -8,19 +8,9 @@
import UIKit
private func regexFromPattern(pattern: String) -> NSRegularExpression {
var error: NSError?
if let regex = NSRegularExpression(pattern: pattern, options: .AnchorsMatchLines, error: &error) {
return regex
} else {
fatalError("Failed to initialize regular expression with pattern \(pattern): \(error)")
}
}
private func listItemRegexWithMarkerPattern(pattern: String) -> NSRegularExpression {
return regexFromPattern("(?:[ ]{0,3}(?:\(pattern))[ \t]+)(.+)")
}
/**
* Text storage with support for highlighting Markdown.
*/
public class MarkdownTextStorage: RegularExpressionTextStorage {
// Regular expressions are from John Gruber's original Markdown.pl
// implementation (v1.0.1): http://daringfireball.net/projects/markdown/
@@ -36,6 +26,13 @@ public class MarkdownTextStorage: RegularExpressionTextStorage {
// MARK: Initialization
/**
Creates a new instance of the receiver.
:param: attributes Attributes used to style the text.
:returns: An initialized instance of `MarkdownTextStorage`
*/
public init(attributes: MarkdownAttributes = MarkdownAttributes()) {
self.attributes = attributes
super.init()
@@ -70,7 +67,7 @@ public class MarkdownTextStorage: RegularExpressionTextStorage {
// MARK: RegularExpressionTextStorage
override func highlightRange(range: NSRange) {
override internal func highlightRange(range: NSRange) {
highlightHeadersInRange(range)
highlightLinksInRange(range)
highlightListsInRange(range,
@@ -105,7 +102,7 @@ public class MarkdownTextStorage: RegularExpressionTextStorage {
}
}
private func highlightListsInRange(range: NSRange, regex: NSRegularExpression, attributes: MarkdownAttributes.TextAttributes?, itemAttributes: MarkdownAttributes.TextAttributes?) {
private func highlightListsInRange(range: NSRange, regex: NSRegularExpression, attributes: TextAttributes?, itemAttributes: TextAttributes?) {
if (attributes == nil && itemAttributes == nil) { return }
regex.enumerateMatchesInString(string, options: nil, range: range) { (result, _, _) in
@@ -120,13 +117,13 @@ public class MarkdownTextStorage: RegularExpressionTextStorage {
// MARK: Helpers
private func addPattern(pattern: String, _ attributes: MarkdownAttributes.TextAttributes?) {
private func addPattern(pattern: String, _ attributes: TextAttributes?) {
if let attributes = attributes {
self.addRegularExpression(regexFromPattern(pattern), withAttributes: attributes)
}
}
private func attributesForTraits(traits: UIFontDescriptorSymbolicTraits, var _ attributes: MarkdownAttributes.TextAttributes?) -> MarkdownAttributes.TextAttributes? {
private func attributesForTraits(traits: UIFontDescriptorSymbolicTraits, var _ attributes: TextAttributes?) -> TextAttributes? {
if let defaultFont = defaultAttributes[NSFontAttributeName] as? UIFont where attributes == nil {
attributes = [
NSFontAttributeName: fontWithTraits(traits, defaultFont)
@@ -135,3 +132,16 @@ public class MarkdownTextStorage: RegularExpressionTextStorage {
return attributes
}
}
private func regexFromPattern(pattern: String) -> NSRegularExpression {
var error: NSError?
if let regex = NSRegularExpression(pattern: pattern, options: .AnchorsMatchLines, error: &error) {
return regex
} else {
fatalError("Failed to initialize regular expression with pattern \(pattern): \(error)")
}
}
private func listItemRegexWithMarkerPattern(pattern: String) -> NSRegularExpression {
return regexFromPattern("(?:[ ]{0,3}(?:\(pattern))[ \t]+)(.+)")
}

View File

@@ -8,6 +8,10 @@
import UIKit
/**
* Text storage with support for automatically styling text
* by matching a set of regular expressions.
*/
public class RegularExpressionTextStorage: NSTextStorage {
private struct Expression {
let regex: NSRegularExpression
@@ -16,6 +20,8 @@ public class RegularExpressionTextStorage: NSTextStorage {
private let backingStore: NSMutableAttributedString
private var expressions = [Expression]()
/// Default attributes to use for styling text.
public var defaultAttributes: [String: AnyObject] = [
NSFontAttributeName: UIFont.preferredFontForTextStyle(UIFontTextStyleBody)
] {
@@ -24,6 +30,16 @@ public class RegularExpressionTextStorage: NSTextStorage {
// MARK: API
/**
Adds a regular expression to use for matching text to style.
Regular expressions are evaluated in the order in which they
are added.
:param: expression The regular expression.
:param: attributes The text attributes to apply to range(s) of
text that match the regular expression.
*/
public func addRegularExpression(expression: NSRegularExpression, withAttributes attributes: [String: AnyObject]) {
expressions.append(Expression(regex: expression, attributes: attributes))
editedAll(.EditedAttributes)
@@ -83,7 +99,7 @@ public class RegularExpressionTextStorage: NSTextStorage {
backingStore.endEditing()
}
func highlightRange(range: NSRange) {
internal func highlightRange(range: NSRange) {
for expression in expressions {
expression.regex.enumerateMatchesInString(string, options: nil, range: range) { (result, _, _) in
self.addAttributes(expression.attributes, range: result.range)