Files

94 lines
2.6 KiB
Python

"""
Ricerca nei file wiki
"""
import re
from pathlib import Path
from typing import List, Dict
import config
def search_wiki(query: str, limit: int = 20) -> List[Dict]:
"""Cerca nei contenuti delle pagine wiki."""
results = []
wiki_root = config.WIKI_ROOT
query_lower = query.lower()
for md_file in wiki_root.glob("wiki/**/*.md"):
if md_file.name in ["index.md", "log.md"]:
continue
try:
content = md_file.read_text(encoding="utf-8").lower()
if query_lower in content:
# Trova le linee con il match
lines = md_file.read_text(encoding="utf-8").split("\n")
matches = []
for i, line in enumerate(lines):
if query_lower in line.lower():
matches.append({
"line": i + 1,
"text": line.strip()[:200]
})
results.append({
"file": str(md_file.relative_to(wiki_root)),
"name": md_file.stem,
"category": md_file.parent.name,
"matches": matches[:5] # max 5 match per file
})
if len(results) >= limit:
break
except Exception:
continue
return results
def search_by_name(query: str) -> List[Dict]:
"""Cerca per nome file/titolo."""
results = []
wiki_root = config.WIKI_ROOT
query_lower = query.lower()
for md_file in wiki_root.glob("wiki/**/*.md"):
if md_file.name in ["index.md", "log.md"]:
continue
name_lower = md_file.stem.lower()
if query_lower in name_lower:
results.append({
"file": str(md_file.relative_to(wiki_root)),
"name": md_file.stem,
"category": md_file.parent.name
})
return results
def get_recent_pages(limit: int = 10) -> List[Dict]:
"""Returns the most recently modified pages."""
results = []
wiki_root = config.WIKI_ROOT
files_with_mtime = []
for md_file in wiki_root.glob("wiki/**/*.md"):
if md_file.name in ["index.md", "log.md"]:
continue
mtime = md_file.stat().st_mtime
files_with_mtime.append((mtime, md_file))
files_with_mtime.sort(reverse=True)
for mtime, md_file in files_with_mtime[:limit]:
results.append({
"file": str(md_file.relative_to(wiki_root)),
"name": md_file.stem,
"category": md_file.parent.name,
"mtime": mtime
})
return results