Skip to content

Commit

Permalink
馃悰 FIX: Spurious newline in generated literal_block nodes
Browse files Browse the repository at this point in the history
Closes #407.
  • Loading branch information
cpitclaudel committed Apr 1, 2022
1 parent dc45a1f commit 135e77f
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions myst_parser/docutils_renderer.py
Expand Up @@ -78,6 +78,12 @@ def token_line(token: SyntaxTreeNode, default: Optional[int] = None) -> int:
return token.map[0] # type: ignore[index]


def create_literal_block(rawsource='', text='', *children, **attributes):
if text.endswith("\n"):
text = text[:-1]
return nodes.literal_block(rawsource, text, *children, **kwargs)


class DocutilsRenderer(RendererProtocol):
"""A markdown-it-py renderer to populate (in-place) a `docutils.document` AST.
Expand Down Expand Up @@ -510,15 +516,18 @@ def create_highlighted_code_block(
Note, this function does not add the literal block to the document.
"""
rawsource = text
if text.endswith("\n"):
rawsource, text = text, text[:-1]
if self.sphinx_env is not None:
node = node_cls(text, text, language=lexer_name or "none")
node = node_cls(rawsource, text, language=lexer_name or "none")
if number_lines:
node["linenos"] = True
if lineno_start != 1:
node["highlight_args"] = {"linenostart": lineno_start}
else:
node = node_cls(
text, classes=["code"] + ([lexer_name] if lexer_name else [])
rawsource, classes=["code"] + ([lexer_name] if lexer_name else [])
)
try:
lex_tokens = Lexer(
Expand Down Expand Up @@ -780,7 +789,7 @@ def render_front_matter(self, token: SyntaxTreeNode) -> None:
msg_node = self.reporter.error(
"Front matter block:\n" + str(error), line=position
)
msg_node += nodes.literal_block(token.content, token.content)
msg_node += create_literal_block(token.content, token.content)
self.current_node.append(msg_node)
return
else:
Expand All @@ -801,14 +810,14 @@ def render_front_matter(self, token: SyntaxTreeNode) -> None:
msg_node = self.reporter.error(
"Front-matter 'substitutions' is not a dict", line=position
)
msg_node += nodes.literal_block(token.content, token.content)
msg_node += create_literal_block(token.content, token.content)
self.current_node.append(msg_node)

if not isinstance(html_meta, dict):
msg_node = self.reporter.error(
"Front-matter 'html_meta' is not a dict", line=position
)
msg_node += nodes.literal_block(token.content, token.content)
msg_node += create_literal_block(token.content, token.content)
self.current_node.append(msg_node)

self.current_node.extend(
Expand Down Expand Up @@ -1108,7 +1117,7 @@ def render_dl(self, token: SyntaxTreeNode) -> None:
"Found a definition in a definition list, "
"with no preceding term"
),
# nodes.literal_block(content, content),
# create_literal_block(content, content),
line=token_line(child),
)
self.current_node += [error]
Expand All @@ -1123,7 +1132,7 @@ def render_dl(self, token: SyntaxTreeNode) -> None:
"Expected a term/definition as a child of a definition list"
f", but found a: {child.type}"
),
# nodes.literal_block(content, content),
# create_literal_block(content, content),
line=token_line(child),
)
self.current_node += [error_msg]
Expand All @@ -1143,7 +1152,7 @@ def render_field_list(self, token: SyntaxTreeNode) -> None:
"Expected a fieldlist_name as a child of a field_list"
f", but found a: {child.type}"
),
# nodes.literal_block(content, content),
# create_literal_block(content, content),
line=token_line(child),
)
self.current_node += [error_msg]
Expand Down Expand Up @@ -1214,7 +1223,7 @@ def run_directive(
if not directive_class:
error = self.reporter.error(
'Unknown directive type "{}".\n'.format(name),
# nodes.literal_block(content, content),
# create_literal_block(content, content),
line=position,
)
return [error] + messages
Expand All @@ -1232,7 +1241,7 @@ def run_directive(
except DirectiveParsingError as error:
error = self.reporter.error(
"Directive '{}': {}".format(name, error),
nodes.literal_block(content, content),
create_literal_block(content, content),
line=position,
)
return [error]
Expand Down Expand Up @@ -1276,14 +1285,14 @@ def run_directive(
msg_node = self.reporter.system_message(
error.level, error.msg, line=position
)
msg_node += nodes.literal_block(content, content)
msg_node += create_literal_block(content, content)
result = [msg_node]
except MockingError as exc:
error_msg = self.reporter.error(
"Directive '{}' cannot be mocked: {}: {}".format(
name, exc.__class__.__name__, exc
),
nodes.literal_block(content, content),
create_literal_block(content, content),
line=position,
)
return [error_msg]
Expand Down

0 comments on commit 135e77f

Please sign in to comment.