#!/usr/bin/env python3 """ Convert Markdown documentation to styled HTML """ import re import os import html def convert_md_to_html(md_content, title="Documentation"): """Convert markdown content to styled HTML.""" # HTML template with CSS styling html_template = '''
{code_content}')
else:
html_lines.append(f'{code_content}')
i += 1 # Skip closing ```
# Tables
elif '|' in line and i + 1 < len(lines) and '---' in lines[i + 1]:
if in_list:
html_lines.append(f'{list_type}>')
in_list = False
html_lines.append('| {process_inline(cell)} | ') html_lines.append('
|---|
| {process_inline(cell)} | ') html_lines.append('
{text}') i += 1 # Regular paragraph else: if in_list: html_lines.append(f'{list_type}>') in_list = False para_lines = [line.strip()] i += 1 while i < len(lines) and lines[i].strip() and not lines[i].startswith('#') and not lines[i].startswith('```') and not lines[i].strip().startswith('- ') and not lines[i].strip().startswith('* ') and '|' not in lines[i] and not re.match(r'^\d+\.\s', lines[i].strip()) and lines[i].strip() != '---': para_lines.append(lines[i].strip()) i += 1 text = process_inline(' '.join(para_lines)) html_lines.append(f'
{text}
') # Close any remaining list if in_list: html_lines.append(f'{list_type}>') content = '\n'.join(html_lines) return html_template.format(title=html.escape(title), content=content) def process_inline(text): """Process inline markdown formatting.""" # Escape HTML first # But we need to be careful not to double-escape # Bold text = re.sub(r'\*\*([^*]+)\*\*', r'\1', text) # Italic text = re.sub(r'\*([^*]+)\*', r'\1', text) # Inline code (before links to avoid conflicts) text = re.sub(r'`([^`]+)`', lambda m: f'{html.escape(m.group(1))}', text)
# Links
text = re.sub(r'\[([^\]]+)\]\(([^)]+)\)', r'\1', text)
# Checkmarks and X marks
text = text.replace('✓', '✓')
text = text.replace('✗', '✗')
return text
def slugify(text):
"""Convert text to URL-friendly slug."""
text = text.lower()
text = re.sub(r'[^a-z0-9\s-]', '', text)
text = re.sub(r'[\s]+', '-', text)
return text
def convert_file(md_path, html_path):
"""Convert a markdown file to HTML."""
print(f"Converting {os.path.basename(md_path)} -> {os.path.basename(html_path)}")
with open(md_path, 'r', encoding='utf-8') as f:
content = f.read()
# Extract title from first h1
title_match = re.search(r'^# (.+)$', content, re.MULTILINE)
title = title_match.group(1) if title_match else os.path.basename(md_path)
html_content = convert_md_to_html(content, title)
with open(html_path, 'w', encoding='utf-8') as f:
f.write(html_content)
def main():
docs_dir = '/home/camp/projects/powershell/docs'
md_files = [
'Update-ShopfloorPCs-Remote.md',
'Invoke-RemoteMaintenance.md',
'Update-PC-CompleteAsset.md',
'DATA_COLLECTION_PARITY.md',
'ShopDB-API.md'
]
for md_file in md_files:
md_path = os.path.join(docs_dir, md_file)
html_path = os.path.join(docs_dir, md_file.replace('.md', '.html'))
if os.path.exists(md_path):
convert_file(md_path, html_path)
else:
print(f"Warning: {md_path} not found")
print("\nConversion complete!")
print(f"HTML files saved to: {docs_dir}")
if __name__ == '__main__':
main()