"""Build a static documentation page using a documented Markdown subset.""" from pathlib import Path import html import re import shutil ROOT = Path(__file__).resolve().parent def inline(value): # Escape HTML first; only explicitly supported markup becomes HTML. value = html.escape(value) tokens = [] def token(markup): tokens.append(markup) return f'\x00{len(tokens) - 1}\x00' value = re.sub(r'`([^`]+)`', lambda m: token(f'{m[1]}'), value) def link(match): label, url = match.groups() if not re.match(r'^(https?://|mailto:|#)', html.unescape(url), re.I): return label return token(f'{label}') value = re.sub(r'\[([^\]]+)\]\(([^\s)]+)\)', link, value) value = re.sub(r'\*\*([^*]+)\*\*', r'\1', value) for i in reversed(range(len(tokens))): value = value.replace(f'\x00{i}\x00', tokens[i]) return value def render(source): lines = source.splitlines() output, navigation = [], [] index, section, heading = 0, False, 0 while index < len(lines): line = lines[index].strip() index += 1 if not line: continue if line.startswith('```'): code = [] while index < len(lines) and not lines[index].strip().startswith('```'): code.append(lines[index]) index += 1 index += 1 output.append('
' + html.escape('\n'.join(code)) + '
') elif re.match(r'^#{1,3} ', line): level = len(line.split(' ', 1)[0]) title = line[level + 1:] if level == 2: if section: output.append('') heading += 1 anchor = f'section-{heading}' navigation.append(f'{heading:02}{html.escape(title)}') output.append(f'
') section = True output.append(f'{inline(title)}') elif line.startswith('|') and index < len(lines) and re.match(r'^\|[\s:|\-]+\|$', lines[index].strip()): cells = lambda row: [inline(cell.strip()) for cell in row.strip().strip('|').split('|')] output.append('
' + ''.join(f'' for cell in cells(line)) + '') index += 1 while index < len(lines) and lines[index].strip().startswith('|'): output.append('' + ''.join(f'' for cell in cells(lines[index])) + '') index += 1 output.append('
{cell}
{cell}
') elif re.match(r'^(\d+\. |[-*] )', line): ordered = bool(re.match(r'^\d+\.', line)) tag, pattern = ('ol', r'^\d+\. ') if ordered else ('ul', r'^[-*] ') items = [re.sub(pattern, '', line)] while index < len(lines) and re.match(pattern, lines[index].strip()): items.append(re.sub(pattern, '', lines[index].strip())) index += 1 output.append(f'<{tag}>' + ''.join(f'
  • {inline(item)}
  • ' for item in items) + f'') elif line.startswith('> '): output.append(f'
    {inline(line[2:])}
    ') else: output.append(f'

    {inline(line)}

    ') if section: output.append('
    ') return '\n'.join(output), '\n'.join(navigation) def build(): version = (ROOT / 'VERSION').read_text(encoding='utf-8').strip() if not re.fullmatch(r'\d+\.\d+\.\d+', version): raise ValueError('VERSION must contain major.minor.patch') content, navigation = render((ROOT / 'docs/content.md').read_text()) template = (ROOT / 'web/index.html').read_text() target = ROOT / 'dist' target.mkdir(exist_ok=True) (target / 'index.html').write_text(template.replace('{{version}}', version).replace('{{navigation}}', navigation).replace('{{content}}', content)) for name in ('style.css', 'app.js'): shutil.copyfile(ROOT / 'web' / name, target / name) print('Built dist/index.html') if __name__ == '__main__': build()