65 lines
1.8 KiB
Python
Executable File
65 lines
1.8 KiB
Python
Executable File
import os
|
|
import json
|
|
from bs4 import BeautifulSoup
|
|
|
|
OUTPUT_DIR = os.path.normpath(os.environ.get("SITE_OUTPUT_DIR", "output"))
|
|
|
|
def strip_html_tags(html):
|
|
soup = BeautifulSoup(html, "html.parser")
|
|
for tag in soup(["script", "style"]):
|
|
tag.decompose()
|
|
|
|
text = soup.get_text(separator="\n")
|
|
|
|
lines = [line.strip() for line in text.splitlines() if line.strip()]
|
|
return "\n".join(lines)
|
|
|
|
def create_folder_structure_json(path):
|
|
result = {
|
|
'name': os.path.basename(path),
|
|
'type': 'folder',
|
|
'children': []
|
|
}
|
|
|
|
if not os.path.isdir(path):
|
|
return result
|
|
|
|
for entry in os.listdir(path):
|
|
|
|
entry_path = os.path.join(path, entry)
|
|
base_url = os.path.relpath(entry_path, OUTPUT_DIR)
|
|
if os.path.isdir(entry_path):
|
|
result['children'].append(
|
|
create_folder_structure_json(entry_path)
|
|
)
|
|
|
|
elif entry.lower().endswith('.html'):
|
|
file_entry = {
|
|
'name': entry,
|
|
'type': 'file',
|
|
'url': base_url,
|
|
'content': None
|
|
}
|
|
|
|
try:
|
|
with open(entry_path, 'r', encoding='utf-8') as f:
|
|
html = f.read()
|
|
file_entry['content'] = strip_html_tags(html)
|
|
|
|
except Exception as e:
|
|
file_entry['content'] = f"<unreadable: {e}>"
|
|
|
|
result['children'].append(file_entry)
|
|
|
|
return result
|
|
|
|
|
|
folder_path = OUTPUT_DIR
|
|
folder_json = create_folder_structure_json(folder_path)
|
|
|
|
output_file = os.path.join(OUTPUT_DIR, 'search-index.json')
|
|
with open(output_file, 'w', encoding='utf-8') as f:
|
|
json.dump(folder_json, f, indent=4, ensure_ascii=False)
|
|
|
|
print("JSON saved to", output_file)
|