import parsel import httpx from pathlib import Path from textwrap import indent VERSION = '0.31.2' CACHE = Path('.spec', VERSION) URL = f'https://spec.commonmark.org/{VERSION}/' if CACHE.exists(): sel = parsel.Selector(CACHE.read_text()) else: response = httpx.get('https://spec.commonmark.org/0.31.2/') response.raise_for_status() CACHE.parent.mkdir(parents=True, exist_ok=True) CACHE.write_text(response.text) sel = parsel.Selector(response.text) print( '''const std = @import("std"); const convert = @import("main.zig").convert; ''' ) for example in sel.css('.example'): name = example.css('.examplenum > a::text').get() md = example.css('.language-markdown::text').get() html = example.css('.language-html::text').get() assert name is not None print(f''' test "{name}" {{ const alloc = std.testing.allocator; const md = ( {indent(md, r' \\', lambda _: True) if md else '""'} ); const html = ( {indent(html, r' \\', lambda _: True) if html else '""'} ); const output = try convert(alloc, md); defer alloc.free(output); try std.testing.expectEqualStrings(html, output); }} ''') # mdtext = textwrap.indent("md", '\\')