Skip to content

Commit 3b9c7f8

Browse files
committed
Support building line blocks with markdown-it hardbreaks
Provides the ability to build docutils' line-block nodes if an inline element contains hardbreaks. When a hardbreak is detected as a child of an inline token, a line-block will be created and child tokens will be rendered into a prepared line node. Child nodes are placed into a line node until the next hardbreak is detected, where a new line node is created to hold the next child set. The process repeats until all children are processed. Signed-off-by: James Knight <[email protected]>
1 parent d3d7fbb commit 3b9c7f8

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
lines changed

myst_parser/mdit_to_docutils/base.py

+43-10
Original file line numberDiff line numberDiff line change
@@ -392,15 +392,19 @@ def current_node_context(
392392
def render_children(self, token: SyntaxTreeNode) -> None:
393393
"""Render the children of a token."""
394394
for child in token.children or []:
395-
if f"render_{child.type}" in self.rules:
396-
self.rules[f"render_{child.type}"](child)
397-
else:
398-
self.create_warning(
399-
f"No render method for: {child.type}",
400-
MystWarnings.RENDER_METHOD,
401-
line=token_line(child, default=0),
402-
append_to=self.current_node,
403-
)
395+
self.render_token(child)
396+
397+
def render_token(self, token: SyntaxTreeNode) -> None:
398+
"""Render a token."""
399+
if f"render_{token.type}" in self.rules:
400+
self.rules[f"render_{token.type}"](token)
401+
else:
402+
self.create_warning(
403+
f"No render method for: {token.type}",
404+
MystWarnings.RENDER_METHOD,
405+
line=token_line(token, default=0),
406+
append_to=self.current_node,
407+
)
404408

405409
def add_line_and_source_path(self, node, token: SyntaxTreeNode) -> None:
406410
"""Copy the line number and document source path to the docutils node."""
@@ -526,7 +530,36 @@ def render_paragraph(self, token: SyntaxTreeNode) -> None:
526530
self.render_children(token)
527531

528532
def render_inline(self, token: SyntaxTreeNode) -> None:
529-
self.render_children(token)
533+
lineblock = False
534+
for child in token.children or []:
535+
if child.type == 'hardbreak':
536+
lineblock = True
537+
break
538+
539+
if not lineblock:
540+
self.render_children(token)
541+
return
542+
543+
# if we have any hard breaks, we will build a line block and
544+
# prepare line nodes for each group of children between these
545+
# breaks
546+
lineblock = nodes.line_block()
547+
self.add_line_and_source_path(lineblock, token)
548+
self.current_node.append(lineblock)
549+
550+
current_line = nodes.line()
551+
self.add_line_and_source_path(current_line, token)
552+
lineblock.append(current_line)
553+
554+
for child in token.children or []:
555+
if child.type == 'hardbreak':
556+
current_line = nodes.line()
557+
self.add_line_and_source_path(current_line, token)
558+
lineblock.append(current_line)
559+
continue
560+
561+
with self.current_node_context(current_line):
562+
self.render_token(child)
530563

531564
def render_text(self, token: SyntaxTreeNode) -> None:
532565
self.current_node.append(nodes.Text(token.content))

tests/test_renderers/fixtures/docutil_syntax_elements.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ bar
1414
.
1515
<document source="notset">
1616
<paragraph>
17-
foo
18-
<raw format="html" xml:space="preserve">
19-
<br />
20-
<raw format="latex" xml:space="preserve">
21-
\\
22-
bar
17+
<line_block>
18+
<line>
19+
foo
20+
<line>
21+
bar
2322
.
2423

2524
Strong:

tests/test_renderers/fixtures/sphinx_syntax_elements.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ bar
1414
.
1515
<document source="<src>/index.md">
1616
<paragraph>
17-
foo
18-
<raw format="html" xml:space="preserve">
19-
<br />
20-
<raw format="latex" xml:space="preserve">
21-
\\
22-
bar
17+
<line_block>
18+
<line>
19+
foo
20+
<line>
21+
bar
2322
.
2423

2524
Strong:

0 commit comments

Comments
 (0)