#!/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 = ''' {title} {content} ''' lines = md_content.split('\n') html_lines = [] i = 0 in_list = False list_type = None while i < len(lines): line = lines[i] # Empty line - close any open list if not line.strip(): if in_list: html_lines.append(f'') in_list = False list_type = None i += 1 continue # Headers if line.startswith('# '): if in_list: html_lines.append(f'') in_list = False text = process_inline(line[2:].strip()) anchor = slugify(line[2:].strip()) html_lines.append(f'

{text}

') i += 1 elif line.startswith('## '): if in_list: html_lines.append(f'') in_list = False text = process_inline(line[3:].strip()) anchor = slugify(line[3:].strip()) html_lines.append(f'

{text}

') i += 1 elif line.startswith('### '): if in_list: html_lines.append(f'') in_list = False text = process_inline(line[4:].strip()) anchor = slugify(line[4:].strip()) html_lines.append(f'

{text}

') i += 1 elif line.startswith('#### '): if in_list: html_lines.append(f'') in_list = False text = process_inline(line[5:].strip()) anchor = slugify(line[5:].strip()) html_lines.append(f'

{text}

') i += 1 # Horizontal rule elif line.strip() == '---': if in_list: html_lines.append(f'') in_list = False html_lines.append('
') i += 1 # Code blocks elif line.strip().startswith('```'): if in_list: html_lines.append(f'') in_list = False lang = line.strip()[3:] code_lines = [] i += 1 while i < len(lines) and not lines[i].strip().startswith('```'): code_lines.append(html.escape(lines[i])) i += 1 code_content = '\n'.join(code_lines) if lang: html_lines.append(f'
{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'') in_list = False html_lines.append('') # Header row cells = [c.strip() for c in line.split('|')[1:-1]] html_lines.append('') for cell in cells: html_lines.append(f'') html_lines.append('') i += 2 # Skip header and separator html_lines.append('') while i < len(lines) and '|' in lines[i]: cells = [c.strip() for c in lines[i].split('|')[1:-1]] html_lines.append('') for cell in cells: html_lines.append(f'') html_lines.append('') i += 1 html_lines.append('
{process_inline(cell)}
{process_inline(cell)}
') # Bullet lists elif line.strip().startswith('- ') or line.strip().startswith('* '): if not in_list or list_type != 'ul': if in_list: html_lines.append(f'') html_lines.append('