Files
rocky-man/old_scripts/generate_index.py
Stephen Simpson 2287678798 Init
2025-01-09 15:39:20 -06:00

95 lines
3.2 KiB
Python

import os
import json
import gzip
from string import Template
from collections import defaultdict
from fnmatch import fnmatch
from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template('templates/index.j2')
directory = '/data/html_data' # Change this to your directory path
rocky_version = "8.10"
def generate_sitemap(directory):
links = defaultdict(lambda: defaultdict(dict))
for root, _, files in os.walk(directory):
for file in files:
full_filepath = os.path.join(root, file)
filepath = full_filepath.split(rocky_version, 1)[-1]
if any(fnmatch(filepath, pattern) for pattern in ['/index.html', '/links.html','/list.json*', '/sitemap*']):
continue
filepath_parts = filepath.split('/')
package_name = filepath_parts[1]
man_type = filepath_parts[2]
man_type_number = man_type.lstrip('man') if man_type.startswith('man') else man_type
command_file = filepath_parts[3]
command = command_file.split('.html', 1)[0]
if filepath.startswith('/'):
filepath = filepath[1:]
fullname = f"{package_name} - {command}({man_type_number})"
links[package_name][command] = {
"url": filepath,
"man_type": man_type,
"man_type_number": man_type_number,
"fullname": fullname
}
return links
def generate_links_html(links):
links_html = ""
for package_name in links.keys():
links_html += f"<h2>package_name</h2>"
links_html += "<ul>"
for command in links[package_name]:
url = links[package_name][command]['url']
man_type_number = links[package_name][command]['man_type_number']
links_html += f"<li><a href='{url}'>{command}</a>({man_type_number})</li>"
links_html += "</ul>"
data = {
'title': f"Rocky Man Page - {rocky_version}",
'header_title': f"Rocky Man Page - {rocky_version}",
'main_content': f"{links_html}"
}
return template.render(data)
def convert_sitemap_to_json(links, minify=False):
# data
# for package_name in links.keys():
# for command in links[package_name]:
# # Add command details to sitemap
# sitemap[package_name][command] = {
# "url": filepath,
# "mantype": man_type,
# "fullname": fullname
# }
if minify:
return json.dumps(links, separators=(',', ':'))
return json.dumps(links, indent=4)
if __name__ == "__main__":
sitemap = generate_sitemap(directory)
# Output the links HTML page to a file
with open(f"{directory}/{rocky_version}/links.html", "w") as file:
file.write(generate_links_html(sitemap))
# Output the list JSON to a file
with open(f"{directory}/{rocky_version}/list.json", "w") as file:
file.write(convert_sitemap_to_json(sitemap, minify=True))
# Gzip the JSON file
with gzip.open(f"{directory}/{rocky_version}/list.json.gz", "wb") as f_out:
f_out.write(convert_sitemap_to_json(sitemap, minify=True).encode('utf-8'))