forked from mirrors/nixpkgs
nixos-render-docs: add html comment plugins
options do not use comments, but a number of manual chapters do. since we don't want to enable html just so we can then inspect the html and figure out whether it's a comment we'll instead add a plugin that detects comments natively.
This commit is contained in:
parent
6829c6c335
commit
00a1b41c3b
|
@ -261,6 +261,45 @@ def _inline_anchor_plugin(md: markdown_it.MarkdownIt) -> None:
|
|||
|
||||
md.inline.ruler.before("link", "inline_anchor", inline_anchor)
|
||||
|
||||
def _inline_comment_plugin(md: markdown_it.MarkdownIt) -> None:
|
||||
def inline_comment(state: markdown_it.rules_inline.StateInline, silent: bool) -> bool:
|
||||
if state.src[state.pos : state.pos + 4] != '<!--':
|
||||
return False
|
||||
if _is_escaped(state.src, state.pos - 1):
|
||||
return False
|
||||
for i in range(state.pos + 4, state.posMax - 2):
|
||||
if state.src[i : i + 3] == '-->': # -->
|
||||
state.pos = i + 3
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
md.inline.ruler.after("autolink", "inline_comment", inline_comment)
|
||||
|
||||
def _block_comment_plugin(md: markdown_it.MarkdownIt) -> None:
|
||||
def block_comment(state: markdown_it.rules_block.StateBlock, startLine: int, endLine: int,
|
||||
silent: bool) -> bool:
|
||||
pos = state.bMarks[startLine] + state.tShift[startLine]
|
||||
posMax = state.eMarks[startLine]
|
||||
|
||||
if state.src[pos : pos + 4] != '<!--':
|
||||
return False
|
||||
|
||||
nextLine = startLine
|
||||
while nextLine < endLine:
|
||||
pos = state.bMarks[nextLine] + state.tShift[nextLine]
|
||||
posMax = state.eMarks[nextLine]
|
||||
|
||||
if state.src[posMax - 3 : posMax] == '-->':
|
||||
state.line = nextLine + 1
|
||||
return True
|
||||
|
||||
nextLine += 1
|
||||
|
||||
return False
|
||||
|
||||
md.block.ruler.after("code", "block_comment", block_comment)
|
||||
|
||||
class Converter(ABC):
|
||||
__renderer__: Callable[[Mapping[str, str], markdown_it.MarkdownIt], Renderer]
|
||||
|
||||
|
@ -286,6 +325,8 @@ class Converter(ABC):
|
|||
self._md.use(deflist_plugin)
|
||||
self._md.use(myst_role_plugin)
|
||||
self._md.use(_inline_anchor_plugin)
|
||||
self._md.use(_inline_comment_plugin)
|
||||
self._md.use(_block_comment_plugin)
|
||||
self._md.enable(["smartquotes", "replacements"])
|
||||
|
||||
def _post_parse(self, tokens: list[Token]) -> list[Token]:
|
||||
|
|
|
@ -169,3 +169,111 @@ def test_inline_anchor_escaping() -> None:
|
|||
Token(type='paragraph_close', tag='p', nesting=-1, attrs={}, map=None, level=0, children=None,
|
||||
content='', markup='', info='', meta={}, block=True, hidden=False)
|
||||
]
|
||||
|
||||
def test_inline_comment_basic() -> None:
|
||||
c = Converter({})
|
||||
assert c._parse("a <!-- foo --><!----> b") == [
|
||||
Token(type='paragraph_open', tag='p', nesting=1, attrs={}, map=[0, 1], level=0, children=None,
|
||||
content='', markup='', info='', meta={}, block=True, hidden=False),
|
||||
Token(type='inline', tag='', nesting=0, attrs={}, map=[0, 1], level=1,
|
||||
content='a <!-- foo --><!----> b', markup='', info='', meta={}, block=True, hidden=False,
|
||||
children=[
|
||||
Token(type='text', tag='', nesting=0, attrs={}, map=None, level=0, children=None,
|
||||
content='a b', markup='', info='', meta={}, block=False, hidden=False)
|
||||
]),
|
||||
Token(type='paragraph_close', tag='p', nesting=-1, attrs={}, map=None, level=0, children=None,
|
||||
content='', markup='', info='', meta={}, block=True, hidden=False)
|
||||
]
|
||||
assert c._parse("a<!-- b -->") == [
|
||||
Token(type='paragraph_open', tag='p', nesting=1, attrs={}, map=[0, 1], level=0, children=None,
|
||||
content='', markup='', info='', meta={}, block=True, hidden=False),
|
||||
Token(type='inline', tag='', nesting=0, attrs={}, map=[0, 1], level=1,
|
||||
content='a<!-- b -->', markup='', info='', meta={}, block=True, hidden=False,
|
||||
children=[
|
||||
Token(type='text', tag='', nesting=0, attrs={}, map=None, level=0, children=None,
|
||||
content='a', markup='', info='', meta={}, block=False, hidden=False)
|
||||
]),
|
||||
Token(type='paragraph_close', tag='p', nesting=-1, attrs={}, map=None, level=0, children=None,
|
||||
content='', markup='', info='', meta={}, block=True, hidden=False)
|
||||
]
|
||||
|
||||
def test_inline_comment_does_not_nest_in_code() -> None:
|
||||
c = Converter({})
|
||||
assert c._parse("`a<!-- b -->c`") == [
|
||||
Token(type='paragraph_open', tag='p', nesting=1, attrs={}, map=[0, 1], level=0, children=None,
|
||||
content='', markup='', info='', meta={}, block=True, hidden=False),
|
||||
Token(type='inline', tag='', nesting=0, attrs={}, map=[0, 1], level=1,
|
||||
content='`a<!-- b -->c`', markup='', info='', meta={}, block=True, hidden=False,
|
||||
children=[
|
||||
Token(type='code_inline', tag='code', nesting=0, attrs={}, map=None, level=0, children=None,
|
||||
content='a<!-- b -->c', markup='`', info='', meta={}, block=False, hidden=False)
|
||||
]),
|
||||
Token(type='paragraph_close', tag='p', nesting=-1, attrs={}, map=None, level=0, children=None,
|
||||
content='', markup='', info='', meta={}, block=True, hidden=False)
|
||||
]
|
||||
|
||||
def test_inline_comment_does_not_nest_elsewhere() -> None:
|
||||
c = Converter({})
|
||||
assert c._parse("*a<!-- b -->c*") == [
|
||||
Token(type='paragraph_open', tag='p', nesting=1, attrs={}, map=[0, 1], level=0, children=None,
|
||||
content='', markup='', info='', meta={}, block=True, hidden=False),
|
||||
Token(type='inline', tag='', nesting=0, attrs={}, map=[0, 1], level=1,
|
||||
content='*a<!-- b -->c*', markup='', info='', meta={}, block=True, hidden=False,
|
||||
children=[
|
||||
Token(type='em_open', tag='em', nesting=1, attrs={}, map=None, level=0, children=None,
|
||||
content='', markup='*', info='', meta={}, block=False, hidden=False),
|
||||
Token(type='text', tag='', nesting=0, attrs={}, map=None, level=1, children=None,
|
||||
content='ac', markup='', info='', meta={}, block=False, hidden=False),
|
||||
Token(type='em_close', tag='em', nesting=-1, attrs={}, map=None, level=0, children=None,
|
||||
content='', markup='*', info='', meta={}, block=False, hidden=False)
|
||||
]),
|
||||
Token(type='paragraph_close', tag='p', nesting=-1, attrs={}, map=None, level=0, children=None,
|
||||
content='', markup='', info='', meta={}, block=True, hidden=False)
|
||||
]
|
||||
|
||||
def test_inline_comment_can_be_escaped() -> None:
|
||||
c = Converter({})
|
||||
assert c._parse("a\\<!-- b -->c") == [
|
||||
Token(type='paragraph_open', tag='p', nesting=1, attrs={}, map=[0, 1], level=0, children=None,
|
||||
content='', markup='', info='', meta={}, block=True, hidden=False),
|
||||
Token(type='inline', tag='', nesting=0, attrs={}, map=[0, 1], level=1,
|
||||
content='a\\<!-- b -->c', markup='', info='', meta={}, block=True, hidden=False,
|
||||
children=[
|
||||
Token(type='text', tag='', nesting=0, attrs={}, map=None, level=0, children=None,
|
||||
content='a<!-- b -->c', markup='', info='', meta={}, block=False, hidden=False)
|
||||
]),
|
||||
Token(type='paragraph_close', tag='p', nesting=-1, attrs={}, map=None, level=0, children=None,
|
||||
content='', markup='', info='', meta={}, block=True, hidden=False)
|
||||
]
|
||||
assert c._parse("a\\\\<!-- b -->c") == [
|
||||
Token(type='paragraph_open', tag='p', nesting=1, attrs={}, map=[0, 1], level=0, children=None,
|
||||
content='', markup='', info='', meta={}, block=True, hidden=False),
|
||||
Token(type='inline', tag='', nesting=0, attrs={}, map=[0, 1], level=1,
|
||||
children=[
|
||||
Token(type='text', tag='', nesting=0, attrs={}, map=None, level=0, children=None,
|
||||
content='a\\c', markup='', info='', meta={}, block=False, hidden=False)
|
||||
],
|
||||
content='a\\\\<!-- b -->c', markup='', info='', meta={}, block=True, hidden=False),
|
||||
Token(type='paragraph_close', tag='p', nesting=-1, attrs={}, map=None, level=0, children=None,
|
||||
content='', markup='', info='', meta={}, block=True, hidden=False)
|
||||
]
|
||||
assert c._parse("a\\\\\\<!-- b -->c") == [
|
||||
Token(type='paragraph_open', tag='p', nesting=1, attrs={}, map=[0, 1], level=0, children=None,
|
||||
content='', markup='', info='', meta={}, block=True, hidden=False),
|
||||
Token(type='inline', tag='', nesting=0, attrs={}, map=[0, 1], level=1,
|
||||
children=[
|
||||
Token(type='text', tag='', nesting=0, attrs={}, map=None, level=0, children=None,
|
||||
content='a\\<!-- b -->c', markup='', info='', meta={}, block=False, hidden=False)
|
||||
],
|
||||
content='a\\\\\\<!-- b -->c', markup='', info='', meta={}, block=True, hidden=False),
|
||||
Token(type='paragraph_close', tag='p', nesting=-1, attrs={}, map=None, level=0, children=None,
|
||||
content='', markup='', info='', meta={}, block=True, hidden=False)
|
||||
]
|
||||
|
||||
def test_block_comment() -> None:
|
||||
c = Converter({})
|
||||
assert c._parse("<!-- a -->") == []
|
||||
assert c._parse("<!-- a\n-->") == []
|
||||
assert c._parse("<!--\na\n-->") == []
|
||||
assert c._parse("<!--\n\na\n\n-->") == []
|
||||
assert c._parse("<!--\n\n```\n\n\n```\n\n-->") == []
|
||||
|
|
Loading…
Reference in a new issue