nixos-render-docs: calculate list end indices

that'll be useful to calculate the width of list item heads, which we'll
ned to render manpages.
This commit is contained in:
pennae 2023-02-01 20:30:53 +01:00 committed by pennae
parent 8fe19590c3
commit 5a5255983b
2 changed files with 14 additions and 5 deletions

View file

@ -1,7 +1,7 @@
from abc import ABC
from collections.abc import Mapping, MutableMapping, Sequence
from frozendict import frozendict # type: ignore[attr-defined]
from typing import Any, Callable, Optional
from typing import Any, Callable, cast, Optional
import re
@ -364,14 +364,21 @@ class Converter(ABC):
# of each item to hidden. this is not useful for our stylesheets, which
# signify this with a special css class on list elements instead.
wide_stack = []
end_stack = []
for i in range(0, len(tokens)):
if tokens[i].type in [ 'bullet_list_open', 'ordered_list_open' ]:
wide_stack.append([i, True])
end_stack.append([i, cast(int, tokens[i].attrs.get('start', 1))])
elif tokens[i].type in [ 'bullet_list_close', 'ordered_list_close' ]:
(idx, compact) = wide_stack.pop()
tokens[idx].attrs['compact'] = compact
(idx, end) = end_stack.pop()
if tokens[i].type == 'ordered_list_close':
tokens[idx].meta['end'] = end - 1
elif len(wide_stack) > 0 and tokens[i].type == 'paragraph_open' and not tokens[i].hidden:
wide_stack[-1][1] = False
elif tokens[i].type == 'list_item_open':
end_stack[-1][1] += 1
return tokens

View file

@ -13,9 +13,10 @@ def test_list_wide(ordered: bool) -> None:
("ordered", "ol", ".", "1.", "2.", "1", "2") if ordered else ("bullet", "ul", "-", "-", "-", "", "")
)
c = Converter({})
meta = { 'end': int(e2[:-1]) } if ordered else {}
assert c._parse(f"{e1} a\n\n{e2} b") == [
Token(type=f'{t}_list_open', tag=tag, nesting=1, attrs={'compact': False}, map=[0, 3], level=0,
children=None, content='', markup=m, info='', meta={}, block=True, hidden=False),
children=None, content='', markup=m, info='', meta=meta, block=True, hidden=False),
Token(type='list_item_open', tag='li', nesting=1, attrs={}, map=[0, 2], level=1, children=None,
content='', markup=m, info=i1, meta={}, block=True, hidden=False),
Token(type='paragraph_open', tag='p', nesting=1, attrs={}, map=[0, 1], level=2, children=None,
@ -54,9 +55,10 @@ def test_list_narrow(ordered: bool) -> None:
("ordered", "ol", ".", "1.", "2.", "1", "2") if ordered else ("bullet", "ul", "-", "-", "-", "", "")
)
c = Converter({})
meta = { 'end': int(e2[:-1]) } if ordered else {}
assert c._parse(f"{e1} a\n{e2} b") == [
Token(type=f'{t}_list_open', tag=tag, nesting=1, attrs={'compact': True}, map=[0, 2], level=0,
children=None, content='', markup=m, info='', meta={}, block=True, hidden=False),
children=None, content='', markup=m, info='', meta=meta, block=True, hidden=False),
Token(type='list_item_open', tag='li', nesting=1, attrs={}, map=[0, 1], level=1, children=None,
content='', markup=m, info=i1, meta={}, block=True, hidden=False),
Token(type='paragraph_open', tag='p', nesting=1, attrs={}, map=[0, 1], level=2, children=None,
@ -90,7 +92,7 @@ def test_list_narrow(ordered: bool) -> None:
]
assert c._parse(f"{e1} - a\n{e2} b") == [
Token(type=f'{t}_list_open', tag=tag, nesting=1, attrs={'compact': True}, map=[0, 2], level=0,
children=None, content='', markup=m, info='', meta={}, block=True, hidden=False),
children=None, content='', markup=m, info='', meta=meta, block=True, hidden=False),
Token(type='list_item_open', tag='li', nesting=1, attrs={}, map=[0, 1], level=1, children=None,
content='', markup=m, info=i1, meta={}, block=True, hidden=False),
Token(type='bullet_list_open', tag='ul', nesting=1, attrs={'compact': True}, map=[0, 1], level=2,
@ -132,7 +134,7 @@ def test_list_narrow(ordered: bool) -> None:
]
assert c._parse(f"{e1} - a\n{e2} - b") == [
Token(type=f'{t}_list_open', tag=tag, nesting=1, attrs={'compact': True}, map=[0, 2], level=0,
children=None, content='', markup=m, info='', meta={}, block=True, hidden=False),
children=None, content='', markup=m, info='', meta=meta, block=True, hidden=False),
Token(type='list_item_open', tag='li', nesting=1, attrs={}, map=[0, 1], level=1, children=None,
content='', markup=m, info=i1, meta={}, block=True, hidden=False),
Token(type='bullet_list_open', tag='ul', nesting=1, attrs={'compact': True}, map=[0, 1], level=2,