updated the makefile, added new org files (2026), and started working on adding a search feature for the website.
This commit is contained in:
64
search-index-json.py
Normal file
64
search-index-json.py
Normal file
@@ -0,0 +1,64 @@
|
||||
import os
|
||||
import json
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
def strip_html_tags(html):
|
||||
soup = BeautifulSoup(html, "html.parser")
|
||||
|
||||
# Remove script and style elements completely
|
||||
for tag in soup(["script", "style"]):
|
||||
tag.decompose()
|
||||
|
||||
# Extract plain text
|
||||
text = soup.get_text(separator="\n")
|
||||
|
||||
# Clean up whitespace
|
||||
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)
|
||||
|
||||
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',
|
||||
'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)
|
||||
Reference in New Issue
Block a user