-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from iwasrobbed/downview
DownView rendering to close #3
- Loading branch information
Showing
13 changed files
with
205 additions
and
8 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
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,14 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=640"/> | ||
<link charset="utf-8" href="css/down.min.css" rel="stylesheet"> | ||
<script charset="utf-8" src="js/highlight.min.js" type="text/javascript"></script> | ||
<script charset="utf-8" src="js/down.js" type="text/javascript"></script> | ||
<title></title> | ||
</head> | ||
<body> | ||
DOWN_HTML | ||
</body> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
hljs.initHighlightingOnLoad(); |
Large diffs are not rendered by default.
Oops, something went wrong.
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,30 @@ | ||
// | ||
// String+ToHTML.swift | ||
// Down | ||
// | ||
// Created by Rob Phillips on 6/1/16. | ||
// Copyright © 2016 Glazed Donut, LLC. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import libcmark | ||
|
||
extension String { | ||
|
||
/** | ||
Generates an HTML string from the contents of the string (self), which should contain CommonMark Markdown | ||
- parameter options: `DownOptions` to modify parsing or rendering, defaulting to `.Default` | ||
- throws: `DownErrors` depending on the scenario | ||
- returns: HTML string | ||
*/ | ||
public func toHTML(options: DownOptions = .Default) throws -> String { | ||
let ast = try DownASTRenderer.stringToAST(self, options: options) | ||
let html = try DownHTMLRenderer.astToHTML(ast, options: options) | ||
cmark_node_free(ast) | ||
return 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
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,78 @@ | ||
// | ||
// DownView.swift | ||
// Down | ||
// | ||
// Created by Rob Phillips on 6/1/16. | ||
// Copyright © 2016 Glazed Donut, LLC. All rights reserved. | ||
// | ||
|
||
import WebKit | ||
|
||
// MARK: - Public API | ||
|
||
public class DownView: WKWebView { | ||
|
||
/** | ||
Initializes a web view with the results of rendering a CommonMark Markdown string | ||
- parameter frame: The frame size of the web view | ||
- parameter markdownString: A string containing CommonMark Markdown | ||
- parameter openLinksInBrowser: Whether or not to open links using an external browser | ||
- returns: An instance of Self | ||
*/ | ||
@warn_unused_result | ||
public init(frame: CGRect, markdownString: String, openLinksInBrowser: Bool = true) throws { | ||
super.init(frame: frame, configuration: WKWebViewConfiguration()) | ||
|
||
if openLinksInBrowser { navigationDelegate = self } | ||
try loadHTMLView(markdownString) | ||
} | ||
|
||
// MARK: - Private Properties | ||
|
||
private let bundle: NSBundle = { | ||
let bundle = NSBundle(forClass: DownView.self) | ||
let url = bundle.URLForResource("DownView", withExtension: "bundle")! | ||
return NSBundle(URL: url)! | ||
}() | ||
|
||
private lazy var baseURL: NSURL = { | ||
return self.bundle.URLForResource("index", withExtension: "html")! | ||
}() | ||
} | ||
|
||
// MARK: - Private API | ||
|
||
private extension DownView { | ||
|
||
func loadHTMLView(markdownString: String) throws { | ||
let htmlString = try markdownString.toHTML() | ||
let pageHTMLString = try htmlFromTemplate(htmlString) | ||
loadHTMLString(pageHTMLString, baseURL: baseURL) | ||
} | ||
|
||
func htmlFromTemplate(htmlString: String) throws -> String { | ||
let template = try NSString(contentsOfURL: baseURL, encoding: NSUTF8StringEncoding) | ||
return template.stringByReplacingOccurrencesOfString("DOWN_HTML", withString: htmlString) | ||
} | ||
|
||
} | ||
|
||
// MARK: - WKNavigationDelegate | ||
|
||
extension DownView: WKNavigationDelegate { | ||
|
||
public func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) { | ||
guard let url = navigationAction.request.URL else { return } | ||
|
||
switch navigationAction.navigationType { | ||
case .LinkActivated: | ||
decisionHandler(.Cancel) | ||
UIApplication.sharedApplication().openURL(url) | ||
default: | ||
decisionHandler(.Allow) | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.