Files
org_web/search-index-json.py
2026-02-22 22:09:44 +00:00

63 lines
1.6 KiB
Python
Executable File

import os
import json
from bs4 import BeautifulSoup
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 = entry_path[6:]
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/'
folder_json = create_folder_structure_json(folder_path)
output_file = 'output/test.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)