Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DownView] Add parameter so bundle is writable #200

Merged
merged 6 commits into from
Apr 25, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions Source/Views/DownView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ open class DownView: WKWebView {
/// - configuration: Optional custom web view configuration.
/// - options: `DownOptions` to modify parsing or rendering, defaulting to `.default`
/// - didLoadSuccessfully: Optional callback for when the web content has loaded successfully
/// - writableBundle: Whether or not the bundle folder is writable.
/// - Throws: `DownErrors` depending on the scenario
public init(frame: CGRect, markdownString: String, openLinksInBrowser: Bool = true, templateBundle: Bundle? = nil, configuration: WKWebViewConfiguration? = nil, options: DownOptions = .default, didLoadSuccessfully: DownViewClosure? = nil) throws {
public init(frame: CGRect, markdownString: String, openLinksInBrowser: Bool = true, templateBundle: Bundle? = nil, writableBundle: Bool = false, configuration: WKWebViewConfiguration? = nil, options: DownOptions = .default, didLoadSuccessfully: DownViewClosure? = nil) throws {
self.options = options
self.didLoadSuccessfully = didLoadSuccessfully
self.writableBundle = writableBundle

if let templateBundle = templateBundle {
self.bundle = templateBundle
Expand Down Expand Up @@ -86,6 +88,7 @@ open class DownView: WKWebView {
// MARK: - Private Properties

let bundle: Bundle
let writableBundle: Bool
var options: DownOptions

private lazy var baseURL: URL = {
Expand Down Expand Up @@ -113,7 +116,12 @@ private extension DownView {
let pageHTMLString = try htmlFromTemplate(htmlString)

#if os(iOS)
loadHTMLString(pageHTMLString, baseURL: baseURL)
if writableBundle {
let newIndexUrl = try writeTempIndexFile(pageHTMLString: pageHTMLString)
loadFileURL(newIndexUrl, allowingReadAccessTo: newIndexUrl.deletingLastPathComponent())
} else {
loadHTMLString(pageHTMLString, baseURL: baseURL)
}
#elseif os(macOS)
let indexURL = try createTemporaryBundle(pageHTMLString: pageHTMLString)
loadFileURL(indexURL, allowingReadAccessTo: indexURL.deletingLastPathComponent())
Expand All @@ -125,6 +133,14 @@ private extension DownView {
return template.replacingOccurrences(of: "DOWN_HTML", with: htmlString)
}

#if os(iOS)
func writeTempIndexFile(pageHTMLString: String) throws -> URL {
let newIndexUrl = bundle.resourceURL!.appendingPathComponent("tmp_index.html")
try pageHTMLString.write(to: newIndexUrl, atomically: true, encoding: .utf8)
return newIndexUrl
}
#endif

#if os(macOS)
func createTemporaryBundle(pageHTMLString: String) throws -> URL {
guard let bundleResourceURL = bundle.resourceURL
Expand Down
34 changes: 34 additions & 0 deletions Tests/DownViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,40 @@ class DownViewTests: XCTestCase {
}
}

func testInstantiationWithCustomWritableTemplateBundle() {
let expect1 = expectation(description: "DownView accepts and loads custom bundle files from a user writable location")

guard
let bundle = Bundle(for: type(of: self)).url(forResource: "TestDownView", withExtension: "bundle"),
let templateBundle = Bundle(url: bundle)
else {
XCTFail("Test template bundle not found in test target!")
return
}

let markdownString = """
```swift
let x = 1
```
"""
var downView: DownView?
downView = try? DownView(frame: .zero, markdownString: markdownString, templateBundle: templateBundle, writableBundle: true, didLoadSuccessfully: {
self._pageContents(for: downView!) { htmlString in
XCTAssertTrue(htmlString!.contains("css/down.min.css"))
XCTAssertTrue(htmlString!.contains("hljs-keyword"))
XCTAssertTrue(htmlString!.contains("But also, custom HTML!"))

expect1.fulfill()
}
})

waitForExpectations(timeout: 10) { (error: Error?) in
if let error = error {
XCTFail("waitForExpectationsWithTimeout errored: \(error)")
}
}
}

func testDownOptions() {
let markdownString = "## [Down](https://github.com/iwasrobbed/Down)\n\n<strong>I'm strong!</strong>"
let renderedHTML = "<strong>I'm strong!</strong>"
Expand Down