96 lines
2.7 KiB
Python
96 lines
2.7 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
import argparse
|
|
import sys
|
|
|
|
|
|
def iter_astro_files(root: Path) -> list[Path]:
|
|
"""Zwraca listę plików .astro w root (rekursywnie), posortowaną stabilnie."""
|
|
files = [p for p in root.rglob("*.astro") if p.is_file()]
|
|
# sortowanie po ścieżce względnej dla powtarzalności
|
|
files.sort(key=lambda p: str(p.as_posix()).lower())
|
|
return files
|
|
|
|
|
|
def read_text_fallback(p: Path) -> str:
|
|
"""
|
|
Czyta plik jako tekst:
|
|
- najpierw UTF-8
|
|
- jeśli się nie da, to z BOM/latin-1 jako awaryjne (bez crasha)
|
|
"""
|
|
try:
|
|
return p.read_text(encoding="utf-8")
|
|
except UnicodeDecodeError:
|
|
# Spróbuj UTF-8 z BOM
|
|
try:
|
|
return p.read_text(encoding="utf-8-sig")
|
|
except UnicodeDecodeError:
|
|
# Ostatecznie: latin-1 (nie idealne, ale nie przerwie działania)
|
|
return p.read_text(encoding="latin-1")
|
|
|
|
|
|
def build_output(pages_dir: Path, files: list[Path]) -> str:
|
|
rel_root = pages_dir.parent.parent # zwykle ./src -> parent parent? NIEPEWNE, więc liczymy inaczej
|
|
# lepiej: relatywnie do katalogu projektu (cwd)
|
|
cwd = Path.cwd()
|
|
|
|
lines: list[str] = []
|
|
lines.append(f"ASTRO DUMP (root: {pages_dir.resolve()})")
|
|
lines.append(f"Found files: {len(files)}")
|
|
lines.append("=" * 80)
|
|
lines.append("")
|
|
|
|
for f in files:
|
|
rel = f.relative_to(cwd) if f.is_relative_to(cwd) else f
|
|
content = read_text_fallback(f)
|
|
|
|
lines.append(f"FILE: {rel.as_posix()}")
|
|
lines.append("-" * 80)
|
|
lines.append(content.rstrip("\n"))
|
|
lines.append("")
|
|
lines.append("=" * 80)
|
|
lines.append("")
|
|
|
|
return "\n".join(lines)
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(
|
|
description="Zrzuca wszystkie pliki .astro z ./src/pages do jednego pliku txt (ścieżka + zawartość)."
|
|
)
|
|
parser.add_argument(
|
|
"--pages",
|
|
default="src/pages",
|
|
help="Ścieżka do katalogu pages (domyślnie: src/pages)",
|
|
)
|
|
parser.add_argument(
|
|
"--out",
|
|
default="astro-pages-dump.txt",
|
|
help="Plik wyjściowy (domyślnie: astro-pages-dump.txt)",
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
pages_dir = Path(args.pages).resolve()
|
|
out_file = Path(args.out).resolve()
|
|
|
|
if not pages_dir.exists() or not pages_dir.is_dir():
|
|
print(f"[ERR] Nie znaleziono katalogu: {pages_dir}", file=sys.stderr)
|
|
return 2
|
|
|
|
files = iter_astro_files(pages_dir)
|
|
|
|
dump = build_output(pages_dir, files)
|
|
|
|
out_file.write_text(dump, encoding="utf-8")
|
|
print(f"[OK] Zapisano: {out_file} (files: {len(files)})")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|