Skip to content

Commit 948117a

Browse files
committed
DOP-2620: Coalesce adjacent inline-in-block-context substitutions into a single paragraph
1 parent a8fc9ad commit 948117a

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

snooty/postprocess.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,8 +1165,23 @@ def search_block(
11651165
if result is None:
11661166
return None
11671167

1168-
# If we're injecting inline nodes, wrap them in a paragraph
1169-
return [n.Paragraph(node.span, [el]) if isinstance(el, n.InlineNode) else el for el in result] # type: ignore
1168+
# If we're injecting inline nodes, wrap them in a paragraph. Coalesce adjacent
1169+
# inline elements into a single paragraph.
1170+
output: List[n.Node] = []
1171+
current_paragraph: List[n.InlineNode] = []
1172+
for element in result:
1173+
if isinstance(element, n.InlineNode):
1174+
current_paragraph.append(element)
1175+
else:
1176+
if current_paragraph:
1177+
output.append(n.Paragraph(node.span, current_paragraph)) # type: ignore
1178+
current_paragraph = []
1179+
output.append(element)
1180+
1181+
if current_paragraph:
1182+
output.append(n.Paragraph(node.span, current_paragraph)) # type: ignore
1183+
1184+
return output
11701185

11711186
def _search(
11721187
self,

snooty/test_postprocess.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,58 @@ def test_replacements() -> None:
15811581
""",
15821582
)
15831583

1584+
# Test replacements that have multiple inline elements: DOP-2620
1585+
with make_test(
1586+
{
1587+
Path(
1588+
"source/index.txt"
1589+
): """
1590+
.. binary:: mongod
1591+
1592+
.. |both| replace:: Available for :binary:`~bin.mongod` only.
1593+
1594+
|both|
1595+
1596+
"""
1597+
}
1598+
) as result:
1599+
check_ast_testing_string(
1600+
result.pages[FileId("index.txt")].ast,
1601+
"""
1602+
<root fileid="index.txt">
1603+
<target domain="mongodb" name="binary" html_id="mongodb-binary-bin.mongod">
1604+
<directive_argument>
1605+
<literal>
1606+
<text>mongod</text>
1607+
</literal>
1608+
</directive_argument>
1609+
<target_identifier ids="['bin.mongod']">
1610+
<text>mongod</text>
1611+
</target_identifier>
1612+
</target>
1613+
<substitution_definition name="both">
1614+
<text>Available for </text>
1615+
<ref_role domain="mongodb" name="binary" target="bin.mongod" flag="~" fileid="['index', 'mongodb-binary-bin.mongod']">
1616+
<literal>
1617+
<text>mongod</text>
1618+
</literal>
1619+
</ref_role>
1620+
<text> only.</text>
1621+
</substitution_definition>
1622+
<substitution_reference name="both">
1623+
<paragraph>
1624+
<text>Available for </text>
1625+
<ref_role domain="mongodb" name="binary" target="bin.mongod" flag="~" fileid="['index', 'mongodb-binary-bin.mongod']">
1626+
<literal>
1627+
<text>mongod</text>
1628+
</literal>
1629+
</ref_role>
1630+
<text> only.</text>
1631+
</paragraph>
1632+
</substitution_reference>
1633+
</root>""",
1634+
)
1635+
15841636

15851637
def test_replacements_scope() -> None:
15861638
with make_test(

0 commit comments

Comments
 (0)