feat: EPUB阅读器搜索、注释、CFI模块及大书远距跳转优化
- 实现EPUB阅读器搜索功能及选中注释功能 - 优化CFI模块,修复代码审查发现的11个问题 - 实现大书远距目录跳转与后台补全优化方案 - 优化设置面板与章节运行时联动 - 重构及大量改进优化
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import Foundation
|
||||
|
||||
public enum RDEPUBNoteDetector {
|
||||
|
||||
private static let noteTokens = [
|
||||
"noteref",
|
||||
"footnote",
|
||||
"endnote",
|
||||
"rearnote",
|
||||
"fnref",
|
||||
"fn-",
|
||||
"fn_",
|
||||
"note",
|
||||
"annotation"
|
||||
]
|
||||
|
||||
public static func makeReferenceIfLikelyNote(
|
||||
sourceHref: String,
|
||||
sourceCFI: String?,
|
||||
targetHref: String,
|
||||
targetFragment: String?,
|
||||
targetCFI: String?,
|
||||
targetElementHTML: String?
|
||||
) -> RDEPUBNoteReference? {
|
||||
|
||||
let haystack = [
|
||||
targetHref,
|
||||
targetFragment ?? "",
|
||||
targetElementHTML ?? ""
|
||||
].joined(separator: " ").lowercased()
|
||||
|
||||
guard noteTokens.contains(where: { haystack.contains($0) }) ||
|
||||
hasEPUBType("footnote", in: targetElementHTML) ||
|
||||
hasEPUBType("endnote", in: targetElementHTML) ||
|
||||
hasEPUBType("rearnote", in: targetElementHTML) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return RDEPUBNoteReference(
|
||||
sourceHref: sourceHref,
|
||||
sourceCFI: sourceCFI,
|
||||
targetHref: targetHref,
|
||||
targetFragment: targetFragment,
|
||||
targetCFI: targetCFI,
|
||||
label: targetFragment,
|
||||
kind: kind(from: haystack)
|
||||
)
|
||||
}
|
||||
|
||||
public static func kind(from text: String) -> RDEPUBNoteKind {
|
||||
let lowered = text.lowercased()
|
||||
if lowered.contains("endnote") { return .endnote }
|
||||
if lowered.contains("rearnote") { return .rearnote }
|
||||
if lowered.contains("footnote") || lowered.contains("fn") { return .footnote }
|
||||
return .unknown
|
||||
}
|
||||
|
||||
private static func hasEPUBType(_ type: String, in html: String?) -> Bool {
|
||||
guard let html else { return false }
|
||||
let pattern = #"epub:type\s*=\s*['"][^'"]*\b"# + NSRegularExpression.escapedPattern(for: type) + #"\b[^'"]*['"]"#
|
||||
return html.range(of: pattern, options: [.regularExpression, .caseInsensitive]) != nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import Foundation
|
||||
|
||||
public enum RDEPUBNoteKind: String, Codable, Equatable {
|
||||
|
||||
case footnote
|
||||
|
||||
case endnote
|
||||
|
||||
case rearnote
|
||||
|
||||
case unknown
|
||||
}
|
||||
|
||||
public struct RDEPUBNoteReference: Codable, Equatable {
|
||||
|
||||
public var sourceHref: String
|
||||
|
||||
public var sourceCFI: String?
|
||||
|
||||
public var targetHref: String
|
||||
|
||||
public var targetFragment: String?
|
||||
|
||||
public var targetCFI: String?
|
||||
|
||||
public var label: String?
|
||||
|
||||
public var kind: RDEPUBNoteKind
|
||||
|
||||
public init(
|
||||
sourceHref: String,
|
||||
sourceCFI: String? = nil,
|
||||
targetHref: String,
|
||||
targetFragment: String? = nil,
|
||||
targetCFI: String? = nil,
|
||||
label: String? = nil,
|
||||
kind: RDEPUBNoteKind = .unknown
|
||||
) {
|
||||
self.sourceHref = sourceHref
|
||||
self.sourceCFI = sourceCFI
|
||||
self.targetHref = targetHref
|
||||
self.targetFragment = targetFragment
|
||||
self.targetCFI = targetCFI
|
||||
self.label = label?.nilIfEmpty
|
||||
self.kind = kind
|
||||
}
|
||||
}
|
||||
|
||||
public struct RDEPUBResolvedNote: Equatable {
|
||||
|
||||
public var reference: RDEPUBNoteReference
|
||||
|
||||
public var sourceLocation: RDEPUBLocation?
|
||||
|
||||
public var targetLocation: RDEPUBLocation
|
||||
|
||||
public var title: String?
|
||||
|
||||
public var html: String
|
||||
|
||||
public var plainText: String
|
||||
|
||||
public init(
|
||||
reference: RDEPUBNoteReference,
|
||||
sourceLocation: RDEPUBLocation? = nil,
|
||||
targetLocation: RDEPUBLocation,
|
||||
title: String? = nil,
|
||||
html: String,
|
||||
plainText: String
|
||||
) {
|
||||
self.reference = reference
|
||||
self.sourceLocation = sourceLocation
|
||||
self.targetLocation = targetLocation
|
||||
self.title = title?.nilIfEmpty
|
||||
self.html = html
|
||||
self.plainText = plainText
|
||||
}
|
||||
}
|
||||
|
||||
private extension String {
|
||||
var nilIfEmpty: String? {
|
||||
let trimmed = trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
return trimmed.isEmpty ? nil : trimmed
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
import Foundation
|
||||
|
||||
public final class RDEPUBNoteResolver {
|
||||
|
||||
private let resourceResolver: RDEPUBResourceResolver
|
||||
|
||||
public init(resourceResolver: RDEPUBResourceResolver) {
|
||||
self.resourceResolver = resourceResolver
|
||||
}
|
||||
|
||||
public func resolveInternalLink(
|
||||
sourceHref: String,
|
||||
sourceCFI: String?,
|
||||
targetLocation: RDEPUBLocation,
|
||||
sourceLocation: RDEPUBLocation? = nil,
|
||||
relativeToSpineIndex spineIndex: Int?
|
||||
) -> RDEPUBResolvedNote? {
|
||||
guard let normalizedTarget = resourceResolver.normalizedLocation(
|
||||
targetLocation,
|
||||
relativeToSpineIndex: spineIndex,
|
||||
bookIdentifier: targetLocation.bookIdentifier
|
||||
) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let fragment = normalizedTarget.fragment
|
||||
guard let elementHTML = extractElementHTML(href: normalizedTarget.href, fragment: fragment) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
guard let reference = RDEPUBNoteDetector.makeReferenceIfLikelyNote(
|
||||
sourceHref: sourceHref,
|
||||
sourceCFI: sourceCFI,
|
||||
targetHref: normalizedTarget.href,
|
||||
targetFragment: fragment,
|
||||
targetCFI: normalizedTarget.cfi,
|
||||
targetElementHTML: elementHTML
|
||||
) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let sanitizedHTML = stripBacklinks(from: elementHTML)
|
||||
let plainText = plainText(from: sanitizedHTML)
|
||||
guard !plainText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return RDEPUBResolvedNote(
|
||||
reference: reference,
|
||||
sourceLocation: sourceLocation,
|
||||
targetLocation: normalizedTarget,
|
||||
title: title(for: reference),
|
||||
html: sanitizedHTML,
|
||||
plainText: plainText
|
||||
)
|
||||
}
|
||||
|
||||
public func extractElementHTML(href: String, fragment: String?) -> String? {
|
||||
guard let fragment,
|
||||
!fragment.isEmpty,
|
||||
let fileURL = resourceResolver.fileURL(forRelativePath: href),
|
||||
let html = try? String(contentsOf: fileURL, encoding: .utf8) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return elementHTML(withID: fragment, in: html)
|
||||
}
|
||||
|
||||
private func elementHTML(withID id: String, in html: String) -> String? {
|
||||
let escapedID = NSRegularExpression.escapedPattern(for: id)
|
||||
let pattern = #"<([A-Za-z][A-Za-z0-9:_-]*)(?=[^>]*(?:id|xml:id)\s*=\s*(['"])"# + escapedID + #"\2)[^>]*>"#
|
||||
guard let regex = try? NSRegularExpression(pattern: pattern, options: [.caseInsensitive]) else {
|
||||
return nil
|
||||
}
|
||||
let nsRange = NSRange(html.startIndex..<html.endIndex, in: html)
|
||||
guard let match = regex.firstMatch(in: html, options: [], range: nsRange),
|
||||
let startRange = Range(match.range, in: html),
|
||||
let tagRange = Range(match.range(at: 1), in: html) else {
|
||||
return nil
|
||||
}
|
||||
let tagName = String(html[tagRange])
|
||||
guard !String(html[startRange]).hasSuffix("/>") else {
|
||||
return String(html[startRange])
|
||||
}
|
||||
guard let endRange = matchingEndTagRange(
|
||||
tagName: tagName,
|
||||
in: html,
|
||||
after: startRange.upperBound
|
||||
) else {
|
||||
return String(html[startRange])
|
||||
}
|
||||
return String(html[startRange.lowerBound..<endRange.upperBound])
|
||||
}
|
||||
|
||||
private func stripBacklinks(from html: String) -> String {
|
||||
let backlinkPattern = #"<a\b[^>]*(?:rev\s*=\s*['"]footnote['"]|epub:type\s*=\s*['"][^'"]*(?:backlink|return)[^'"]*['"]|class\s*=\s*['"][^'"]*(?:backlink|return)[^'"]*['"])[^>]*>[\s\S]*?</a>"#
|
||||
guard let regex = try? NSRegularExpression(pattern: backlinkPattern, options: [.caseInsensitive]) else {
|
||||
return html
|
||||
}
|
||||
let nsRange = NSRange(html.startIndex..<html.endIndex, in: html)
|
||||
return regex.stringByReplacingMatches(in: html, options: [], range: nsRange, withTemplate: "")
|
||||
}
|
||||
|
||||
private func plainText(from html: String) -> String {
|
||||
let withoutScripts = html
|
||||
.replacingOccurrences(of: #"<script[\s\S]*?</script>"#, with: "", options: .regularExpression)
|
||||
.replacingOccurrences(of: #"<style[\s\S]*?</style>"#, with: "", options: .regularExpression)
|
||||
let withoutTags = withoutScripts.replacingOccurrences(of: #"<[^>]+>"#, with: " ", options: .regularExpression)
|
||||
return withoutTags
|
||||
.replacingOccurrences(of: " ", with: " ")
|
||||
.replacingOccurrences(of: "<", with: "<")
|
||||
.replacingOccurrences(of: ">", with: ">")
|
||||
.replacingOccurrences(of: "&", with: "&")
|
||||
.components(separatedBy: .whitespacesAndNewlines)
|
||||
.filter { !$0.isEmpty }
|
||||
.joined(separator: " ")
|
||||
}
|
||||
|
||||
private func title(for reference: RDEPUBNoteReference) -> String {
|
||||
switch reference.kind {
|
||||
case .footnote:
|
||||
return "脚注"
|
||||
case .endnote:
|
||||
return "尾注"
|
||||
case .rearnote:
|
||||
return "后注"
|
||||
case .unknown:
|
||||
return "注释"
|
||||
}
|
||||
}
|
||||
|
||||
private func matchingEndTagRange(
|
||||
tagName: String,
|
||||
in html: String,
|
||||
after startIndex: String.Index
|
||||
) -> Range<String.Index>? {
|
||||
let escapedTagName = NSRegularExpression.escapedPattern(for: tagName)
|
||||
let pattern = #"</?\s*"# + escapedTagName + #"\b[^>]*>"#
|
||||
guard let regex = try? NSRegularExpression(pattern: pattern, options: [.caseInsensitive]) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let searchRange = NSRange(startIndex..<html.endIndex, in: html)
|
||||
var depth = 1
|
||||
for match in regex.matches(in: html, options: [], range: searchRange) {
|
||||
guard let range = Range(match.range, in: html) else { continue }
|
||||
let tag = String(html[range])
|
||||
if tag.hasPrefix("</") {
|
||||
depth -= 1
|
||||
} else if !tag.hasSuffix("/>") {
|
||||
depth += 1
|
||||
}
|
||||
if depth == 0 {
|
||||
return range
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user