init: llm-wiki-web con FastAPI + Ollama
This commit is contained in:
commit
08291b578c
19 files changed
+1030
No files matched your search
@@ -0,0 +1,147 @@
|
||||
"""
|
||||
Operazioni sui file della wiki
|
||||
"""
|
||||
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import List, Dict, Optional
|
||||
import frontmatter
|
||||
|
||||
|
||||
def list_wiki_pages(wiki_root: Path) -> Dict[str, List[Dict]]:
|
||||
"""Lista tutte le pagine wiki organizzate per categoria."""
|
||||
categories = {
|
||||
"entities": [],
|
||||
"concepts": [],
|
||||
"sources": [],
|
||||
"comparisons": [],
|
||||
"topics": [],
|
||||
}
|
||||
|
||||
for category in categories.keys():
|
||||
category_dir = wiki_root / "wiki" / category
|
||||
if category_dir.exists():
|
||||
for md_file in category_dir.glob("*.md"):
|
||||
categories[category].append({
|
||||
"name": md_file.stem,
|
||||
"file": str(md_file),
|
||||
"category": category
|
||||
})
|
||||
|
||||
# Anche le pagine root
|
||||
for md_file in (wiki_root / "wiki").glob("*.md"):
|
||||
if md_file.name not in ["index.md", "log.md", "overview.md"]:
|
||||
categories["topics"].append({
|
||||
"name": md_file.stem,
|
||||
"file": str(md_file),
|
||||
"category": "topics"
|
||||
})
|
||||
|
||||
return categories
|
||||
|
||||
|
||||
def read_page(file_path: Path) -> Dict:
|
||||
"""Legge una pagina wiki e restituisce contenuto + metadata."""
|
||||
if not file_path.exists():
|
||||
return {"error": "File not found"}
|
||||
|
||||
try:
|
||||
post = frontmatter.parse(file_path)
|
||||
content = post.content
|
||||
metadata = post.metadata
|
||||
except Exception:
|
||||
# Fallback: leggi solo contenuto
|
||||
content = file_path.read_text(encoding="utf-8")
|
||||
metadata = {}
|
||||
|
||||
return {
|
||||
"content": content,
|
||||
"metadata": metadata,
|
||||
"file": str(file_path)
|
||||
}
|
||||
|
||||
|
||||
def write_page(file_path: Path, content: str, metadata: Optional[Dict] = None):
|
||||
"""Scrive una pagina wiki con frontmatter."""
|
||||
# Assicura che la directory esista
|
||||
file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
if metadata:
|
||||
# Usa frontmatter per scrivere con metadata
|
||||
post = frontmatter.Post(content, **metadata)
|
||||
with open(file_path, "w", encoding="utf-8") as f:
|
||||
frontmatter.dump(post, f)
|
||||
else:
|
||||
file_path.write_text(content, encoding="utf-8")
|
||||
|
||||
|
||||
def update_index(wiki_root: Path, new_pages: List[str], category: str):
|
||||
"""Aggiunge pagine all'indice."""
|
||||
index_file = wiki_root / "wiki" / "index.md"
|
||||
|
||||
# Leggi indice esistente
|
||||
if index_file.exists():
|
||||
content = index_file.read_text(encoding="utf-8")
|
||||
else:
|
||||
content = "# Wiki Index\n\n> Master catalog of all wiki pages.\n\n"
|
||||
|
||||
# Aggiungi nuovo entries
|
||||
for page in new_pages:
|
||||
# Check se gia presente
|
||||
link = f"[[{page}]]"
|
||||
if link not in content:
|
||||
section = f"## {category.capitalize()}"
|
||||
if section not in content:
|
||||
content += f"\n{section}\n"
|
||||
content += f"- [[{page}]]\n"
|
||||
|
||||
index_file.write_text(content, encoding="utf-8")
|
||||
|
||||
|
||||
def append_log(wiki_root: Path, source_file: str, created: List[str], updated: List[str]):
|
||||
"""Appende un entry al log."""
|
||||
log_file = wiki_root / "wiki" / "log.md"
|
||||
from datetime import datetime
|
||||
now = datetime.now().strftime("%Y-%m-%d %H:%M")
|
||||
|
||||
entry = f"\n## {now} — {Path(source_file).name}\n"
|
||||
if created:
|
||||
entry += f"- Created: {', '.join(created)}\n"
|
||||
if updated:
|
||||
entry += f"- Updated: {', '.join(updated)}\n"
|
||||
|
||||
if log_file.exists():
|
||||
content = log_file.read_text(encoding="utf-8")
|
||||
# Insert dopo l'header
|
||||
lines = content.split("\n")
|
||||
insert_idx = 2 # dopo # Wiki Log e ---
|
||||
lines.insert(insert_idx, entry)
|
||||
content = "\n".join(lines)
|
||||
else:
|
||||
content = f"# Wiki Log\n\n> Append-only record.\n{entry}\n"
|
||||
|
||||
log_file.write_text(content, encoding="utf-8")
|
||||
|
||||
|
||||
def extract_wikilinks(content: str) -> List[str]:
|
||||
"""Estrae tutti i [[wikilinks]] dal contenuto."""
|
||||
pattern = r'\[\[([^\]]+)\]\]'
|
||||
return re.findall(pattern, content)
|
||||
|
||||
|
||||
def get_all_wikilinks(wiki_root: Path) -> Dict[str, List[str]]:
|
||||
"""Trova tutti i wikilinks in tutte le pagine."""
|
||||
links = {}
|
||||
|
||||
for md_file in wiki_root.glob("wiki/**/*.md"):
|
||||
if md_file.name == "index.md":
|
||||
continue
|
||||
try:
|
||||
content = md_file.read_text(encoding="utf-8")
|
||||
page_links = extract_wikilinks(content)
|
||||
if page_links:
|
||||
links[str(md_file.relative_to(wiki_root))] = page_links
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
return links
|
||||
Reference in new issue
Block a user