-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds attributed string render for #2
- Loading branch information
rob phillips
committed
Jun 2, 2016
1 parent
9c28de3
commit 01858a2
Showing
7 changed files
with
108 additions
and
1 deletion.
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
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
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,32 @@ | ||
// | ||
// NSAttributedString+HTML.swift | ||
// Down | ||
// | ||
// Created by Rob Phillips on 6/1/16. | ||
// Copyright © 2016 Glazed Donut, LLC. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
extension NSAttributedString { | ||
|
||
/** | ||
Instantiates an attributed string with the given HTML string | ||
|
||
- parameter htmlString: An HTML string | ||
|
||
- throws: `HTMLDataConversionError` or an instantiation error | ||
|
||
- returns: An attributed string | ||
*/ | ||
convenience init(htmlString: String) throws { | ||
guard let data = htmlString.dataUsingEncoding(NSUTF8StringEncoding) else { | ||
throw DownErrors.HTMLDataConversionError | ||
} | ||
|
||
let options = [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType, | ||
NSCharacterEncodingDocumentAttribute: NSNumber(unsignedInteger:NSUTF8StringEncoding)] | ||
try self.init(data: data, options: options, documentAttributes: nil) | ||
} | ||
|
||
} |
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,41 @@ | ||
// | ||
// DownAttributedStringRenderable.swift | ||
// Down | ||
// | ||
// Created by Rob Phillips on 6/1/16. | ||
// Copyright © 2016 Glazed Donut, LLC. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import libcmark | ||
|
||
public protocol DownAttributedStringRenderable: DownHTMLRenderable { | ||
/** | ||
Generates an `NSAttributedString` from the `markdownString` property | ||
|
||
- parameter options: `DownOptions` to modify parsing or rendering | ||
|
||
- throws: `DownErrors` depending on the scenario | ||
|
||
- returns: An `NSAttributedString` | ||
*/ | ||
@warn_unused_result | ||
func toAttributedString(options: DownOptions) throws -> NSAttributedString | ||
} | ||
|
||
public extension DownAttributedStringRenderable { | ||
/** | ||
Generates an `NSAttributedString` from the `markdownString` property | ||
|
||
- parameter options: `DownOptions` to modify parsing or rendering, defaulting to `.Default` | ||
|
||
- throws: `DownErrors` depending on the scenario | ||
|
||
- returns: An `NSAttributedString` | ||
*/ | ||
@warn_unused_result | ||
public func toAttributedString(options: DownOptions = .Default) throws -> NSAttributedString { | ||
let html = try self.toHTML(options) | ||
return try NSAttributedString(htmlString: html) | ||
} | ||
} |
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