Reorganize docs and update reader search flows
This commit is contained in:
@@ -81,4 +81,51 @@ final class RDEPUBTextSearchEngine: RDEPUBSearchEngine {
|
||||
let range = NSRange(location: start, length: max(end - start, 0))
|
||||
return text.substring(with: range).trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
}
|
||||
|
||||
/// 无 publication 时的搜索(用于 txt 等外部文本文件)
|
||||
static func searchWithoutPublication(textBook: RDEPUBTextBook, keyword: String) -> [RDEPUBSearchMatch] {
|
||||
let normalizedKeyword = keyword.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard !normalizedKeyword.isEmpty else { return [] }
|
||||
|
||||
var matches: [RDEPUBSearchMatch] = []
|
||||
for chapter in textBook.chapters {
|
||||
let source = chapter.attributedContent.string as NSString
|
||||
let fullLength = source.length
|
||||
guard fullLength > 0 else { continue }
|
||||
|
||||
var localMatchIndex = 0
|
||||
var searchRange = NSRange(location: 0, length: fullLength)
|
||||
|
||||
while searchRange.length > 0 {
|
||||
let foundRange = source.range(of: normalizedKeyword, options: [.caseInsensitive], range: searchRange)
|
||||
guard foundRange.location != NSNotFound else { break }
|
||||
|
||||
let progressionDenominator = max(fullLength - 1, 1)
|
||||
let progression = Double(foundRange.location) / Double(progressionDenominator)
|
||||
let previewRadius = 12
|
||||
let previewStart = max(foundRange.location - previewRadius, 0)
|
||||
let previewEnd = min(foundRange.location + foundRange.length + previewRadius, fullLength)
|
||||
let previewRange = NSRange(location: previewStart, length: max(previewEnd - previewStart, 0))
|
||||
let previewText = source.substring(with: previewRange).trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
|
||||
matches.append(
|
||||
RDEPUBSearchMatch(
|
||||
href: chapter.href,
|
||||
progression: progression,
|
||||
previewText: previewText,
|
||||
localMatchIndex: localMatchIndex,
|
||||
rangeLocation: foundRange.location,
|
||||
rangeLength: foundRange.length,
|
||||
rangeAnchor: nil
|
||||
)
|
||||
)
|
||||
|
||||
localMatchIndex += 1
|
||||
let nextLocation = foundRange.location + max(foundRange.length, 1)
|
||||
if nextLocation >= fullLength { break }
|
||||
searchRange = NSRange(location: nextLocation, length: fullLength - nextLocation)
|
||||
}
|
||||
}
|
||||
return matches
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user