1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-28 02:54:31 +03:00
vimr/VimR/PreviewUtils.swift

67 lines
2.1 KiB
Swift
Raw Normal View History

2017-02-12 13:24:15 +03:00
/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Foundation
fileprivate let markdownPath = "tools/preview/markdown"
class PreviewUtils {
static func state(for status: PreviewState.Status, baseUrl: URL) -> PreviewState {
switch status {
case .none:
return PreviewState(status: .none,
2017-03-29 21:10:11 +03:00
server: self.simpleServerUrl(with: PreviewReducer.nonePath, baseUrl: baseUrl))
2017-02-12 13:24:15 +03:00
case .error:
return PreviewState(status: .error,
2017-03-29 21:10:11 +03:00
server: self.simpleServerUrl(with: PreviewReducer.errorPath, baseUrl: baseUrl))
2017-02-12 13:24:15 +03:00
case .notSaved:
return PreviewState(status: .notSaved,
2017-03-29 21:10:11 +03:00
server: self.simpleServerUrl(with: PreviewReducer.saveFirstPath, baseUrl: baseUrl))
2017-02-12 13:24:15 +03:00
case .markdown:
preconditionFailure("ERROR Use the other previewState()!")
}
}
static func state(for uuid: String, baseUrl: URL, buffer: NeoVimBuffer?) -> PreviewState {
guard let url = buffer?.url else {
return self.state(for: .notSaved, baseUrl: baseUrl)
}
guard FileUtils.fileExists(at: url) else {
return self.state(for: .error, baseUrl: baseUrl)
}
guard self.extensions.contains(url.pathExtension) else {
return self.state(for: .none, baseUrl: baseUrl)
}
return PreviewState(status: .markdown,
buffer: url,
html: self.htmlUrl(with: uuid),
server: self.serverUrl(for: uuid, baseUrl: baseUrl, lastComponent: "index.html"))
}
fileprivate static func serverUrl(for uuid: String, baseUrl: URL, lastComponent: String) -> URL {
return baseUrl.appendingPathComponent("\(uuid)/\(markdownPath)/\(lastComponent)")
}
fileprivate static func htmlUrl(with uuid: String) -> URL {
return self.tempDir.appendingPathComponent("\(uuid)-markdown-index.html")
}
fileprivate static func simpleServerUrl(with path: String, baseUrl: URL) -> URL {
return baseUrl.appendingPathComponent(path)
}
fileprivate static let extensions = Set(["md", "markdown"])
fileprivate static let tempDir = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
}