at 23.11-beta 6.4 kB view raw
1import nixos_render_docs as nrd 2 3from markdown_it.token import Token 4 5class Converter(nrd.md.Converter[nrd.docbook.DocBookRenderer]): 6 # actual renderer doesn't matter, we're just parsing. 7 def __init__(self, manpage_urls: dict[str, str]) -> None: 8 super().__init__() 9 self._renderer = nrd.docbook.DocBookRenderer(manpage_urls) 10 11def test_heading_id_absent() -> None: 12 c = Converter({}) 13 assert c._parse("# foo") == [ 14 Token(type='heading_open', tag='h1', nesting=1, attrs={}, map=[0, 1], level=0, children=None, 15 content='', markup='#', info='', meta={}, block=True, hidden=False), 16 Token(type='inline', tag='', nesting=0, attrs={}, map=[0, 1], level=1, 17 children=[ 18 Token(type='text', tag='', nesting=0, attrs={}, map=None, level=0, children=None, 19 content='foo', markup='', info='', meta={}, block=False, hidden=False) 20 ], 21 content='foo', markup='', info='', meta={}, block=True, hidden=False), 22 Token(type='heading_close', tag='h1', nesting=-1, attrs={}, map=None, level=0, children=None, 23 content='', markup='#', info='', meta={}, block=True, hidden=False) 24 ] 25 26def test_heading_id_present() -> None: 27 c = Converter({}) 28 assert c._parse("# foo {#foo}\n## bar { #bar}\n### bal { #bal} ") == [ 29 Token(type='heading_open', tag='h1', nesting=1, attrs={'id': 'foo'}, map=[0, 1], level=0, 30 children=None, content='', markup='#', info='', meta={}, block=True, hidden=False), 31 Token(type='inline', tag='', nesting=0, attrs={}, map=[0, 1], level=1, 32 content='foo {#foo}', markup='', info='', meta={}, block=True, hidden=False, 33 children=[ 34 Token(type='text', tag='', nesting=0, attrs={}, map=None, level=0, children=None, 35 content='foo', markup='', info='', meta={}, block=False, hidden=False) 36 ]), 37 Token(type='heading_close', tag='h1', nesting=-1, attrs={}, map=None, level=0, children=None, 38 content='', markup='#', info='', meta={}, block=True, hidden=False), 39 Token(type='heading_open', tag='h2', nesting=1, attrs={'id': 'bar'}, map=[1, 2], level=0, 40 children=None, content='', markup='##', info='', meta={}, block=True, hidden=False), 41 Token(type='inline', tag='', nesting=0, attrs={}, map=[1, 2], level=1, 42 content='bar { #bar}', markup='', info='', meta={}, block=True, hidden=False, 43 children=[ 44 Token(type='text', tag='', nesting=0, attrs={}, map=None, level=0, children=None, 45 content='bar', markup='', info='', meta={}, block=False, hidden=False) 46 ]), 47 Token(type='heading_close', tag='h2', nesting=-1, attrs={}, map=None, level=0, children=None, 48 content='', markup='##', info='', meta={}, block=True, hidden=False), 49 Token(type='heading_open', tag='h3', nesting=1, attrs={'id': 'bal'}, map=[2, 3], level=0, 50 children=None, content='', markup='###', info='', meta={}, block=True, hidden=False), 51 Token(type='inline', tag='', nesting=0, attrs={}, map=[2, 3], level=1, 52 content='bal { #bal}', markup='', info='', meta={}, block=True, hidden=False, 53 children=[ 54 Token(type='text', tag='', nesting=0, attrs={}, map=None, level=0, children=None, 55 content='bal', markup='', info='', meta={}, block=False, hidden=False) 56 ]), 57 Token(type='heading_close', tag='h3', nesting=-1, attrs={}, map=None, level=0, children=None, 58 content='', markup='###', info='', meta={}, block=True, hidden=False) 59 ] 60 61def test_heading_id_incomplete() -> None: 62 c = Converter({}) 63 assert c._parse("# foo {#}") == [ 64 Token(type='heading_open', tag='h1', nesting=1, attrs={}, map=[0, 1], level=0, children=None, 65 content='', markup='#', info='', meta={}, block=True, hidden=False), 66 Token(type='inline', tag='', nesting=0, attrs={}, map=[0, 1], level=1, 67 content='foo {#}', markup='', info='', meta={}, block=True, hidden=False, 68 children=[ 69 Token(type='text', tag='', nesting=0, attrs={}, map=None, level=0, children=None, 70 content='foo {#}', markup='', info='', meta={}, block=False, hidden=False) 71 ]), 72 Token(type='heading_close', tag='h1', nesting=-1, attrs={}, map=None, level=0, children=None, 73 content='', markup='#', info='', meta={}, block=True, hidden=False) 74 ] 75 76def test_heading_id_double() -> None: 77 c = Converter({}) 78 assert c._parse("# foo {#a} {#b}") == [ 79 Token(type='heading_open', tag='h1', nesting=1, attrs={'id': 'b'}, map=[0, 1], level=0, 80 children=None, content='', markup='#', info='', meta={}, block=True, hidden=False), 81 Token(type='inline', tag='', nesting=0, attrs={}, map=[0, 1], level=1, 82 content='foo {#a} {#b}', markup='', info='', meta={}, block=True, hidden=False, 83 children=[ 84 Token(type='text', tag='', nesting=0, attrs={}, map=None, level=0, children=None, 85 content='foo {#a}', markup='', info='', meta={}, block=False, hidden=False) 86 ]), 87 Token(type='heading_close', tag='h1', nesting=-1, attrs={}, map=None, level=0, children=None, 88 content='', markup='#', info='', meta={}, block=True, hidden=False) 89 ] 90 91def test_heading_id_suffixed() -> None: 92 c = Converter({}) 93 assert c._parse("# foo {#a} s") == [ 94 Token(type='heading_open', tag='h1', nesting=1, attrs={}, map=[0, 1], level=0, 95 children=None, content='', markup='#', info='', meta={}, block=True, hidden=False), 96 Token(type='inline', tag='', nesting=0, attrs={}, map=[0, 1], level=1, 97 content='foo {#a} s', markup='', info='', meta={}, block=True, hidden=False, 98 children=[ 99 Token(type='text', tag='', nesting=0, attrs={}, map=None, level=0, children=None, 100 content='foo {#a} s', markup='', info='', meta={}, block=False, hidden=False) 101 ]), 102 Token(type='heading_close', tag='h1', nesting=-1, attrs={}, map=None, level=0, children=None, 103 content='', markup='#', info='', meta={}, block=True, hidden=False) 104 ]