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

134 lines
3.4 KiB
Python

#!/usr/bin/env python3
import sys
import argparse
import re
from bs4 import BeautifulSoup
# Simplified CSS with meaningful class names
FILTERED_CSS = """
/* General Styles */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #0D0A09;
color: white;
}
/* Header Styles */
.header {
background-color: #0FB981;
color: white;
padding: 1rem;
text-align: center;
}
/* Main Content Styles */
.main-content {
margin: 2rem auto;
padding: 1rem;
background-color: #282828;
color: white;
max-width: 800px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.main-content a {
color: #0FB981;
}
.head-vol {
color: white;
}
/* Responsive Adjustments */
@media (max-width: 600px) {
.main-content {
margin: 1rem;
padding: 0.5rem;
}
}
"""
# Define the HTML template with placeholders for title, nav, left pane, content, and right pane
HTML_TEMPLATE = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{file_name} - {rpm_name} - Rocky Man Page</title>
<style>
{css}
</style>
</head>
<body>
<header class="header">
<h1>{file_name}</h1>
</header>
<main class="main-content">
{content}
</main>
</body>
</html>
"""
def clean_html(html_content):
"""
Removes existing <html>, <head>, and <body> tags from the HTML content.
"""
html_content = re.sub(r'</?html[^>]*>', '', html_content, flags=re.IGNORECASE)
html_content = re.sub(r'</?head[^>]*>', '', html_content, flags=re.IGNORECASE)
html_content = re.sub(r'</?body[^>]*>', '', html_content, flags=re.IGNORECASE)
return html_content.strip()
def add_see_also_links(html_content):
"""
Adds hyperlinks to existing See Also sections in the HTML content.
"""
soup = BeautifulSoup(html_content, 'html.parser')
# Locate the section
sections = soup.find_all('section', class_='Sh')
# Loop through sections to find the one with "SEE ALSO"
for section in sections:
heading = section.find('h1', id="SEE_ALSO") # Look for the specific "SEE ALSO" heading
if heading: # If the heading exists in this section
extracted_content = []
for b_tag in section.find_all('b'):
text_with_parentheses = b_tag.get_text() + b_tag.next_sibling.strip() # Combine <b> text and next sibling
extracted_content.append(text_with_parentheses)
print(extracted_content)
def main():
parser = argparse.ArgumentParser(description="Wrap HTML content with a consistent theme including nav, left pane, and right pane.")
parser.add_argument('--rpm_name', type=str, help="RPM Name")
parser.add_argument('--file_name', type=str, help="File Name")
args = parser.parse_args()
# Read HTML content from stdin
input_html = sys.stdin.read()
# Extract or set the title
rpm_name = args.rpm_name
file_name = args.file_name
# Clean the HTML content
cleaned_content = clean_html(input_html)
# Add See Also links
content_with_links = add_see_also_links(cleaned_content)
# Fill the HTML template
themed_html = HTML_TEMPLATE.format(
rpm_name=rpm_name,
css=FILTERED_CSS,
file_name=file_name,
content=content_with_links
)
# Output the themed HTML to stdout
print(themed_html)
if __name__ == "__main__":
main()