- 实现EPUB阅读器搜索功能及选中注释功能 - 优化CFI模块,修复代码审查发现的11个问题 - 实现大书远距目录跳转与后台补全优化方案 - 优化设置面板与章节运行时联动 - 重构及大量改进优化
73 lines
2.1 KiB
Swift
73 lines
2.1 KiB
Swift
|
|
import Foundation
|
|
|
|
enum RDEPUBAsset: String {
|
|
|
|
case rangyCoreScript = "rangy-core"
|
|
|
|
case rangySerializerScript = "rangy-serializer"
|
|
|
|
case cssInjectorScript = "cssInjector"
|
|
|
|
case weReadAPIScript = "WeReadApi"
|
|
|
|
case bridgeScript = "epub-bridge"
|
|
|
|
case fixedLayoutTemplate = "epub-fixed-layout"
|
|
|
|
case wxReadDefaultCSS = "wxread-default"
|
|
|
|
case wxReadReplaceCSS = "wxread-replace"
|
|
|
|
case wxReadDarkCSS = "wxread-dark"
|
|
|
|
case wxReadLatinReplaceCSS = "wxread-replace-latin"
|
|
|
|
var fileExtension: String {
|
|
switch self {
|
|
case .rangyCoreScript, .rangySerializerScript, .cssInjectorScript, .weReadAPIScript, .bridgeScript:
|
|
return "js"
|
|
case .fixedLayoutTemplate:
|
|
return "html"
|
|
case .wxReadDefaultCSS, .wxReadReplaceCSS, .wxReadDarkCSS, .wxReadLatinReplaceCSS:
|
|
return "css"
|
|
}
|
|
}
|
|
}
|
|
|
|
enum RDEPUBAssetRepository {
|
|
|
|
static func string(for asset: RDEPUBAsset, replacements: [String: String] = [:]) -> String {
|
|
guard let url = resourceBundle.url(forResource: asset.rawValue, withExtension: asset.fileExtension),
|
|
var content = try? String(contentsOf: url, encoding: .utf8) else {
|
|
assertionFailure("Missing EPUB asset: \(asset.rawValue).\(asset.fileExtension)")
|
|
return ""
|
|
}
|
|
|
|
for (token, value) in replacements {
|
|
content = content.replacingOccurrences(of: "{{\(token)}}", with: value)
|
|
}
|
|
return content
|
|
}
|
|
|
|
private static var resourceBundle: Bundle {
|
|
if let bundle = resolvedBundle {
|
|
return bundle
|
|
}
|
|
return Bundle(for: RDEPUBAssetBundleToken.self)
|
|
}
|
|
|
|
private static var resolvedBundle: Bundle? = {
|
|
let hostBundles = [Bundle(for: RDEPUBAssetBundleToken.self), Bundle.main] + Bundle.allFrameworks + Bundle.allBundles
|
|
for hostBundle in hostBundles {
|
|
if let url = hostBundle.url(forResource: "RDReaderViewAssets", withExtension: "bundle"),
|
|
let bundle = Bundle(url: url) {
|
|
return bundle
|
|
}
|
|
}
|
|
return nil
|
|
}()
|
|
}
|
|
|
|
private final class RDEPUBAssetBundleToken {}
|