8dae91c2ca
Protect pending edits on window close, keep save-as paths and drafts consistent, copy relative image resources, reject oversized table imports without truncation, and preserve independent preview scrolling during refresh. Archive rendered MHTML resources with portable references and embedded local fonts. Preserve existing outputs on export cancellation or failure. Convert editor match positions and filter directory results in linear passes. Include the previously completed editor image/table/history improvements, undoable replacement, export snapshots, shared page settings, and cancellable PDF/Word export workflow. Update documentation and version to 0.3.1. Validation: 142 pytest cases passed; GUI smoke, improvements integration, and offline MHTML/scroll reliability smoke checks passed.
191 lines
6.4 KiB
Python
191 lines
6.4 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
from .document_tree import CODE_DOCUMENT_EXTENSIONS, DocumentEntry
|
|
from .text_io import DEFAULT_SEARCH_INDEX_BYTES, is_within_size_limit, read_text_with_fallback
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class SearchMatch:
|
|
entry: DocumentEntry
|
|
snippet: str
|
|
match_count: int = 0
|
|
match_kind: str = "正文"
|
|
|
|
|
|
class SearchIndex:
|
|
def __init__(self, entries: list[DocumentEntry] | None = None) -> None:
|
|
self._records: list[tuple[DocumentEntry, str, str]] = []
|
|
self._records_by_path: dict[Path, tuple[DocumentEntry, str, str]] = {}
|
|
self._body_cache: dict[Path, tuple[int, int, str]] = {}
|
|
if entries:
|
|
self.rebuild(entries)
|
|
|
|
def rebuild(self, entries: list[DocumentEntry]) -> None:
|
|
self._records = [(entry, entry.title, entry.relative_path) for entry in entries]
|
|
self._records_by_path = {record[0].path: record for record in self._records}
|
|
current_paths = {entry.path for entry in entries}
|
|
self._body_cache = {path: value for path, value in self._body_cache.items() if path in current_paths}
|
|
|
|
def matches_entry(self, entry: DocumentEntry, query: str) -> bool:
|
|
normalized = query.strip().casefold()
|
|
if not normalized:
|
|
return True
|
|
|
|
entry, title, relative_path = self._records_by_path.get(entry.path, (entry, entry.title, entry.relative_path))
|
|
if normalized in self._haystack(title, relative_path, "").casefold():
|
|
return True
|
|
return normalized in self._body_for(entry).casefold()
|
|
|
|
def search(self, query: str, limit: int = 80) -> list[SearchMatch]:
|
|
return self.filter(query, limit, all_paths=False)[1]
|
|
|
|
def filter(self, query: str, limit: int = 80, *, all_paths: bool = True) -> tuple[set[Path], list[SearchMatch]]:
|
|
"""Find tree visibility and bounded result details without scanning twice."""
|
|
normalized = query.strip().casefold()
|
|
if not normalized:
|
|
return ({entry.path for entry, _, _ in self._records} if all_paths else set()), []
|
|
|
|
visible: set[Path] = set()
|
|
matches: list[SearchMatch] = []
|
|
for entry, title, relative_path in self._records:
|
|
result = _match_details(title, relative_path, "", normalized)
|
|
if result is None:
|
|
result = _match_details(title, relative_path, self._body_for(entry), normalized)
|
|
if result is None:
|
|
continue
|
|
|
|
visible.add(entry.path)
|
|
if len(matches) >= limit:
|
|
if not all_paths:
|
|
break
|
|
continue
|
|
snippet, match_count, match_kind = result
|
|
matches.append(
|
|
SearchMatch(
|
|
entry=entry,
|
|
snippet=snippet,
|
|
match_count=match_count,
|
|
match_kind=match_kind,
|
|
)
|
|
)
|
|
if not all_paths and len(matches) >= limit:
|
|
break
|
|
return visible, matches
|
|
|
|
def _haystack(self, title: str, relative_path: str, body: str) -> str:
|
|
return f"{title}\n{relative_path}\n{body}"
|
|
|
|
def _body_for(self, entry: DocumentEntry) -> str:
|
|
signature = _file_signature(entry.path)
|
|
if signature is None:
|
|
return ""
|
|
cached = self._body_cache.get(entry.path)
|
|
if cached is not None and cached[:2] == signature:
|
|
return cached[2]
|
|
body = _read_text(entry)
|
|
self._body_cache[entry.path] = (*signature, body)
|
|
return body
|
|
|
|
|
|
def matches_entry(entry: DocumentEntry, query: str) -> bool:
|
|
normalized = query.strip().casefold()
|
|
if not normalized:
|
|
return True
|
|
haystack = f"{entry.title}\n{entry.relative_path}\n{_read_text(entry)}".casefold()
|
|
return normalized in haystack
|
|
|
|
|
|
def search_entries(entries: list[DocumentEntry], query: str, limit: int = 80) -> list[SearchMatch]:
|
|
normalized = query.strip().casefold()
|
|
if not normalized:
|
|
return []
|
|
|
|
matches: list[SearchMatch] = []
|
|
for entry in entries:
|
|
text = _read_text(entry)
|
|
result = _match_details(entry.title, entry.relative_path, text, normalized)
|
|
if result is None:
|
|
continue
|
|
|
|
snippet, match_count, match_kind = result
|
|
matches.append(
|
|
SearchMatch(
|
|
entry=entry,
|
|
snippet=snippet,
|
|
match_count=match_count,
|
|
match_kind=match_kind,
|
|
)
|
|
)
|
|
if len(matches) >= limit:
|
|
break
|
|
return matches
|
|
|
|
|
|
def _read_text(entry: DocumentEntry) -> str:
|
|
supported_text_extensions = {
|
|
".md",
|
|
".markdown",
|
|
".txt",
|
|
".html",
|
|
".htm",
|
|
".xhtml",
|
|
".css",
|
|
".js",
|
|
".json",
|
|
".yaml",
|
|
".yml",
|
|
} | CODE_DOCUMENT_EXTENSIONS
|
|
if entry.path.suffix.lower() not in supported_text_extensions:
|
|
return ""
|
|
if not is_within_size_limit(entry.path, DEFAULT_SEARCH_INDEX_BYTES):
|
|
return ""
|
|
text = read_text_with_fallback(entry.path, max_bytes=DEFAULT_SEARCH_INDEX_BYTES)
|
|
return "" if text.startswith("读取失败:") else text
|
|
|
|
|
|
def _file_signature(path: Path) -> tuple[int, int] | None:
|
|
try:
|
|
stat = path.stat()
|
|
except OSError:
|
|
return None
|
|
return stat.st_mtime_ns, stat.st_size
|
|
|
|
|
|
def _snippet(text: str, index: int, length: int) -> str:
|
|
start = max(0, index - 42)
|
|
end = min(len(text), index + length + 86)
|
|
snippet = text[start:end].replace("\r", " ").replace("\n", " ")
|
|
snippet = " ".join(snippet.split())
|
|
if start > 0:
|
|
snippet = "..." + snippet
|
|
if end < len(text):
|
|
snippet += "..."
|
|
return snippet
|
|
|
|
|
|
def _match_details(title: str, relative_path: str, body: str, normalized_query: str) -> tuple[str, int, str] | None:
|
|
fields = (
|
|
("标题", title),
|
|
("路径", relative_path),
|
|
("正文", body),
|
|
)
|
|
total = sum(_count_matches(text, normalized_query) for _, text in fields)
|
|
if total <= 0:
|
|
return None
|
|
|
|
for kind, text in fields:
|
|
index = text.casefold().find(normalized_query)
|
|
if index >= 0:
|
|
return _snippet(text, index, len(normalized_query)), total, kind
|
|
|
|
return None
|
|
|
|
|
|
def _count_matches(text: str, normalized_query: str) -> int:
|
|
if not normalized_query:
|
|
return 0
|
|
return text.casefold().count(normalized_query)
|