"""Controlled cell-break experiment; no network, package installation or UI claims."""
from __future__ import annotations
import hashlib
import importlib.metadata
import inspect
import json
import platform
import subprocess
from collections import Counter
from datetime import datetime, timezone
from html.parser import HTMLParser
from pathlib import Path
import markdown
from markdown_it import MarkdownIt
from markdown_it.rules_block import table as table_rule
from markdown_it.rules_inline import html_inline as html_rule
HERE = Path(__file__).resolve().parent
SAMPLES = {
"cell-br": "| Service | Contact |\n| --- | --- |\n| Support | Email
Phone |\n",
"cell-br-selfclosing": "| Service | Contact |\n| --- | --- |\n| Support | Email
Phone |\n",
"cell-newline": "| Service | Contact |\n| --- | --- |\n| Support | Email\nPhone |\n",
"cell-two-spaces": "| Service | Contact |\n| --- | --- |\n| Support | Email \nPhone |\n",
"cell-backslash": "| Service | Contact |\n| --- | --- |\n| Support | Email\\\nPhone |\n",
"cell-code-literal": "| Service | Contact |\n| --- | --- |\n| Support | `Email
Phone` |\n",
"cell-code-separated": "| Step | Commands |\n| --- | --- |\n| Install | `npm install`
`npm run build` |\n",
}
CONFIGS = {
"markdown-it-commonmark": {"preset": "commonmark", "html": True, "breaks": False, "table": False},
"markdown-it-breaks": {"preset": "commonmark", "html": True, "breaks": True, "table": False},
"markdown-it-table": {"preset": "commonmark", "html": True, "breaks": False, "table": True},
"markdown-it-table-breaks": {"preset": "commonmark", "html": True, "breaks": True, "table": True},
"markdown-it-table-nohtml": {"preset": "commonmark", "html": False, "breaks": False, "table": True},
"python-markdown": {"extensions": ["tables", "fenced_code"]},
"pandoc-gfm": {"argv": ["pandoc", "-f", "gfm", "-t", "html"]},
}
class Structure(HTMLParser):
def __init__(self):
super().__init__(convert_charrefs=True)
self.tags = Counter()
self.in_tbody = False
self.cell = None
self.cells = []
self.body_rows = 0
self.br_in_cells = 0
def handle_starttag(self, tag, attrs):
self.tags[tag] += 1
if tag == "tbody":
self.in_tbody = True
if tag == "tr" and self.in_tbody:
self.body_rows += 1
if tag == "td":
self.cell = ""
if tag == "br" and self.cell is not None:
self.br_in_cells += 1
def handle_endtag(self, tag):
if tag == "td" and self.cell is not None:
self.cells.append(self.cell)
self.cell = None
if tag == "tbody":
self.in_tbody = False
def handle_startendtag(self, tag, attrs):
self.handle_starttag(tag, attrs)
self.handle_endtag(tag)
def handle_data(self, data):
if self.cell is not None:
self.cell += data
def run():
rows = []
for sample, source in SAMPLES.items():
source_path = HERE / f"{sample}.md"
source_path.write_text(source, encoding="utf-8")
for renderer, config in CONFIGS.items():
if renderer.startswith("markdown-it"):
engine = MarkdownIt(config["preset"], {"html": config["html"], "breaks": config["breaks"]})
if config["table"]:
engine.enable("table")
output = engine.render(source)
elif renderer == "python-markdown":
output = markdown.markdown(source, extensions=config["extensions"])
else:
output = subprocess.run(config["argv"], input=source, text=True, capture_output=True, check=True).stdout
output_path = HERE / f"{sample}--{renderer}.txt"
output_path.write_text(output, encoding="utf-8")
parsed = Structure()
parsed.feed(output)
rows.append({
"sample": sample, "renderer": renderer,
"source": f"lab/renderers/{source_path.name}",
"output": f"lab/renderers/{output_path.name}",
"tags": dict(parsed.tags), "table_count": parsed.tags["table"],
"body_rows": parsed.body_rows, "data_cells": len(parsed.cells),
"br_in_cells": parsed.br_in_cells,
"literal_br_text_cells": sum("