@@ -78,6 +78,12 @@ def token_line(token: SyntaxTreeNode, default: Optional[int] = None) -> int:
7878 return token .map [0 ] # type: ignore[index]
7979
8080
81+ def create_literal_block (rawsource = '' , text = '' , * children , ** attributes ):
82+ if text .endswith ("\n " ):
83+ text = text [:- 1 ]
84+ return nodes .literal_block (rawsource , text , * children , ** attributes )
85+
86+
8187class DocutilsRenderer (RendererProtocol ):
8288 """A markdown-it-py renderer to populate (in-place) a `docutils.document` AST.
8389
@@ -510,15 +516,18 @@ def create_highlighted_code_block(
510516
511517 Note, this function does not add the literal block to the document.
512518 """
519+ rawsource = text
520+ if text .endswith ("\n " ):
521+ rawsource , text = text , text [:- 1 ]
513522 if self .sphinx_env is not None :
514- node = node_cls (text , text , language = lexer_name or "none" )
523+ node = node_cls (rawsource , text , language = lexer_name or "none" )
515524 if number_lines :
516525 node ["linenos" ] = True
517526 if lineno_start != 1 :
518527 node ["highlight_args" ] = {"linenostart" : lineno_start }
519528 else :
520529 node = node_cls (
521- text , classes = ["code" ] + ([lexer_name ] if lexer_name else [])
530+ rawsource , classes = ["code" ] + ([lexer_name ] if lexer_name else [])
522531 )
523532 try :
524533 lex_tokens = Lexer (
@@ -780,7 +789,7 @@ def render_front_matter(self, token: SyntaxTreeNode) -> None:
780789 msg_node = self .reporter .error (
781790 "Front matter block:\n " + str (error ), line = position
782791 )
783- msg_node += nodes . literal_block (token .content , token .content )
792+ msg_node += create_literal_block (token .content , token .content )
784793 self .current_node .append (msg_node )
785794 return
786795 else :
@@ -801,14 +810,14 @@ def render_front_matter(self, token: SyntaxTreeNode) -> None:
801810 msg_node = self .reporter .error (
802811 "Front-matter 'substitutions' is not a dict" , line = position
803812 )
804- msg_node += nodes . literal_block (token .content , token .content )
813+ msg_node += create_literal_block (token .content , token .content )
805814 self .current_node .append (msg_node )
806815
807816 if not isinstance (html_meta , dict ):
808817 msg_node = self .reporter .error (
809818 "Front-matter 'html_meta' is not a dict" , line = position
810819 )
811- msg_node += nodes . literal_block (token .content , token .content )
820+ msg_node += create_literal_block (token .content , token .content )
812821 self .current_node .append (msg_node )
813822
814823 self .current_node .extend (
@@ -1108,7 +1117,7 @@ def render_dl(self, token: SyntaxTreeNode) -> None:
11081117 "Found a definition in a definition list, "
11091118 "with no preceding term"
11101119 ),
1111- # nodes.literal_block (content, content),
1120+ # create_literal_block (content, content),
11121121 line = token_line (child ),
11131122 )
11141123 self .current_node += [error ]
@@ -1123,7 +1132,7 @@ def render_dl(self, token: SyntaxTreeNode) -> None:
11231132 "Expected a term/definition as a child of a definition list"
11241133 f", but found a: { child .type } "
11251134 ),
1126- # nodes.literal_block (content, content),
1135+ # create_literal_block (content, content),
11271136 line = token_line (child ),
11281137 )
11291138 self .current_node += [error_msg ]
@@ -1143,7 +1152,7 @@ def render_field_list(self, token: SyntaxTreeNode) -> None:
11431152 "Expected a fieldlist_name as a child of a field_list"
11441153 f", but found a: { child .type } "
11451154 ),
1146- # nodes.literal_block (content, content),
1155+ # create_literal_block (content, content),
11471156 line = token_line (child ),
11481157 )
11491158 self .current_node += [error_msg ]
@@ -1214,7 +1223,7 @@ def run_directive(
12141223 if not directive_class :
12151224 error = self .reporter .error (
12161225 'Unknown directive type "{}".\n ' .format (name ),
1217- # nodes.literal_block (content, content),
1226+ # create_literal_block (content, content),
12181227 line = position ,
12191228 )
12201229 return [error ] + messages
@@ -1232,7 +1241,7 @@ def run_directive(
12321241 except DirectiveParsingError as error :
12331242 error = self .reporter .error (
12341243 "Directive '{}': {}" .format (name , error ),
1235- nodes . literal_block (content , content ),
1244+ create_literal_block (content , content ),
12361245 line = position ,
12371246 )
12381247 return [error ]
@@ -1276,14 +1285,14 @@ def run_directive(
12761285 msg_node = self .reporter .system_message (
12771286 error .level , error .msg , line = position
12781287 )
1279- msg_node += nodes . literal_block (content , content )
1288+ msg_node += create_literal_block (content , content )
12801289 result = [msg_node ]
12811290 except MockingError as exc :
12821291 error_msg = self .reporter .error (
12831292 "Directive '{}' cannot be mocked: {}: {}" .format (
12841293 name , exc .__class__ .__name__ , exc
12851294 ),
1286- nodes . literal_block (content , content ),
1295+ create_literal_block (content , content ),
12871296 line = position ,
12881297 )
12891298 return [error_msg ]
0 commit comments