"""Small, offline syntax matrix for a printable reference, not a hosted UI test.""" from pathlib import Path import argparse, hashlib, importlib.metadata, json, platform, subprocess, sys from datetime import datetime, timezone from html.parser import HTMLParser HERE = Path(__file__).resolve().parent ROOT = HERE.parents[3] # Reuse installed, read-only packages. No pip install and no environment edits. sys.path.append(str(ROOT / '.venv/lib/python3.11/site-packages')) import markdown from markdown_it import MarkdownIt SAMPLES = { 'core': '# Reference\n\nUse **bold** and *italic*.\n\n- Draft\n- Review\n\n[Reference](https://example.com)\n', 'extensions': '| State | Meaning |\n| --- | --- |\n| ~~old~~ | replaced |\n\n- [x] Draft ready\n- [ ] Review pending\n', 'fence-safe': '````markdown\n```python\nprint("hello")\n```\n````\n', 'fence-short': '```markdown\n```python\nprint("hello")\n```\n```\n', 'fence-long-close': '```text\nliteral **bold**\n````\n\nAfter the example.\n', } OPTIONS = { 'markdown-it-commonmark': "MarkdownIt('commonmark'); no plugins; no highlight callback", 'python-markdown': "markdown.markdown(source, extensions=['tables', 'fenced_code']); no other extensions", 'pandoc-gfm': 'pandoc --from=gfm --to=html5 --wrap=none --no-highlight', } class Structure(HTMLParser): def __init__(self): super().__init__(); self.tags = {}; self.pre = 0; self.pre_text = [] def handle_starttag(self, tag, attrs): self.tags[tag] = self.tags.get(tag, 0) + 1 if tag == 'pre': self.pre += 1 def handle_endtag(self, tag): if tag == 'pre': self.pre -= 1 def handle_data(self, data): if self.pre: self.pre_text.append(data) def run(): rows = [] for sample, source in SAMPLES.items(): (HERE / f'{sample}.md').write_text(source) rendered = { 'markdown-it-commonmark': MarkdownIt('commonmark').render(source), 'python-markdown': markdown.markdown(source, extensions=['tables', 'fenced_code']), 'pandoc-gfm': subprocess.run(['pandoc', '--from=gfm', '--to=html5', '--wrap=none', '--no-highlight'], input=source, text=True, capture_output=True, check=True).stdout, } for renderer, output in rendered.items(): name = f'{sample}--{renderer}.txt' (HERE / name).write_text(output) p = Structure(); p.feed(output) rows.append({'sample': sample, 'renderer': renderer, 'tags': p.tags, 'pre_text': ''.join(p.pre_text), 'source': f'lab/renderers/{sample}.md', 'output': f'lab/renderers/{name}', 'sha256': hashlib.sha256(output.encode()).hexdigest()}) source_records = [] for label, p, url in [ ('markdown-it-py fence()', Path(sys.modules['markdown_it'].__file__).parent / 'rules_block/fence.py', 'https://raw.githubusercontent.com/executablebooks/markdown-it-py/v4.0.0/markdown_it/rules_block/fence.py'), ('Python-Markdown FencedBlockPreprocessor', Path(markdown.__file__).parent / 'extensions/fenced_code.py', 'https://raw.githubusercontent.com/Python-Markdown/markdown/3.10.2/markdown/extensions/fenced_code.py'), ]: source_records.append({'object': label, 'installed_path': str(p.relative_to(ROOT)), 'sha256': hashlib.sha256(p.read_bytes()).hexdigest(), 'upstream': url}) report = {'kind': 'markdown-rendering', 'date': datetime.now(timezone.utc).isoformat(), 'python': platform.python_version(), 'versions': {'markdown-it-py': importlib.metadata.version('markdown-it-py'), 'Markdown': importlib.metadata.version('Markdown'), 'pandoc': subprocess.check_output(['pandoc','--version'],text=True).splitlines()[0]}, 'options': OPTIONS, 'command': 'tools/lab-venv/bin/python runs/2026-11-02-markovo-markdown-cheat-sheet-pdf/lab/renderers/run.py', 'dependency_note': 'Python interpreter from tools/lab-venv; installed Markdown and markdown-it-py read from repository .venv through an appended sys.path. Neither environment modified.', 'scope': 'Local HTML implementations only. Not GitHub.com, VS Code, Jupyter, browser printing, performance, OCR, PDF extraction, or a full conformance suite.', 'metric_definition': 'HTMLParser counts actual start tags. pre_text collects text nested inside pre. Counts describe structure, not visual fidelity or an accuracy score.', 'source_records': source_records, 'results': rows} (HERE/'results.json').write_text(json.dumps(report,indent=2)+'\n') lines = ['# Markdown renderer results','',report['scope'],'','| Sample | Renderer | h1 | strong | table | del | input | pre | p |','| --- | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: |'] for row in rows: lines.append('| '+row['sample']+' | '+row['renderer']+' | '+' | '.join(str(row['tags'].get(k,0)) for k in ['h1','strong','table','del','input','pre','p'])+' |') (HERE/'results.md').write_text('\n'.join(lines)+'\n') print(json.dumps({'kind':report['kind'],'rows':len(rows),'versions':report['versions']})) if __name__ == '__main__': run()