@@ -732,26 +732,27 @@ def render_code_block(self, token: SyntaxTreeNode) -> None:
732
732
self .current_node .append (node )
733
733
734
734
def render_fence (self , token : SyntaxTreeNode ) -> None :
735
- text = token .content
736
- # Ensure that we'll have an empty string if info exists but is only spaces
737
- info = token .info .strip () if token .info else token .info
738
- language = info .split ()[0 ] if info else ""
735
+ """Render a fenced code block."""
736
+ # split the info into possible ```name arguments
737
+ parts = (token .info .strip () if token .info else "" ).split (maxsplit = 1 )
738
+ name = parts [0 ] if parts else ""
739
+ arguments = parts [1 ] if len (parts ) > 1 else ""
739
740
740
741
if (not self .md_config .commonmark_only ) and (not self .md_config .gfm_only ):
741
- if language == "{eval-rst}" :
742
+ if name == "{eval-rst}" :
742
743
return self .render_restructuredtext (token )
743
- if language .startswith ("{" ) and language .endswith ("}" ):
744
- return self .render_directive (token )
744
+ if name .startswith ("{" ) and name .endswith ("}" ):
745
+ return self .render_directive (token , name [ 1 : - 1 ], arguments )
745
746
746
- if not language and self .sphinx_env is not None :
747
+ if not name and self .sphinx_env is not None :
747
748
# use the current highlight setting, via the ``highlight`` directive,
748
749
# or ``highlight_language`` configuration.
749
- language = self .sphinx_env .temp_data .get (
750
+ name = self .sphinx_env .temp_data .get (
750
751
"highlight_language" , self .sphinx_env .config .highlight_language
751
752
)
752
753
753
754
lineno_start = 1
754
- number_lines = language in self .md_config .number_code_blocks
755
+ number_lines = name in self .md_config .number_code_blocks
755
756
emphasize_lines = (
756
757
str (token .attrs .get ("emphasize-lines" ))
757
758
if "emphasize-lines" in token .attrs
@@ -763,8 +764,8 @@ def render_fence(self, token: SyntaxTreeNode) -> None:
763
764
number_lines = True
764
765
765
766
node = self .create_highlighted_code_block (
766
- text ,
767
- language ,
767
+ token . content ,
768
+ name ,
768
769
number_lines = number_lines ,
769
770
lineno_start = lineno_start ,
770
771
source = self .document ["source" ],
@@ -1525,10 +1526,11 @@ def render_myst_role(self, token: SyntaxTreeNode) -> None:
1525
1526
self .current_node += _nodes + messages2
1526
1527
1527
1528
def render_colon_fence (self , token : SyntaxTreeNode ) -> None :
1528
- """Render a code fence with ``:`` colon delimiters."""
1529
-
1530
- info = token .info .strip () if token .info else token .info
1531
- name = info .split ()[0 ] if info else ""
1529
+ """Render a div block, with ``:`` colon delimiters."""
1530
+ # split the info into possible :::name arguments
1531
+ parts = (token .info .strip () if token .info else "" ).split (maxsplit = 1 )
1532
+ name = parts [0 ] if parts else ""
1533
+ arguments = parts [1 ] if len (parts ) > 1 else ""
1532
1534
1533
1535
if name .startswith ("{" ) and name .endswith ("}" ):
1534
1536
if token .content .startswith (":::" ):
@@ -1538,7 +1540,7 @@ def render_colon_fence(self, token: SyntaxTreeNode) -> None:
1538
1540
linear_token = token .token .copy ()
1539
1541
linear_token .content = "\n " + linear_token .content
1540
1542
token .token = linear_token
1541
- return self .render_directive (token )
1543
+ return self .render_directive (token , name [ 1 : - 1 ], arguments )
1542
1544
1543
1545
container = nodes .container (is_div = True )
1544
1546
self .add_line_and_source_path (container , token )
@@ -1661,18 +1663,26 @@ def render_restructuredtext(self, token: SyntaxTreeNode) -> None:
1661
1663
self .document .note_explicit_target (node , node )
1662
1664
self .current_node .extend (newdoc .children )
1663
1665
1664
- def render_directive (self , token : SyntaxTreeNode ) -> None :
1665
- """Render special fenced code blocks as directives."""
1666
- first_line = token .info .split (maxsplit = 1 )
1667
- name = first_line [0 ][1 :- 1 ]
1668
- arguments = "" if len (first_line ) == 1 else first_line [1 ]
1669
- content = token .content
1666
+ def render_directive (
1667
+ self , token : SyntaxTreeNode , name : str , arguments : str
1668
+ ) -> None :
1669
+ """Render special fenced code blocks as directives.
1670
+
1671
+ :param token: the token to render
1672
+ :param name: the name of the directive
1673
+ :param arguments: The remaining text on the same line as the directive name.
1674
+ """
1670
1675
position = token_line (token )
1671
- nodes_list = self .run_directive (name , arguments , content , position )
1676
+ nodes_list = self .run_directive (name , arguments , token . content , position )
1672
1677
self .current_node += nodes_list
1673
1678
1674
1679
def run_directive (
1675
- self , name : str , first_line : str , content : str , position : int
1680
+ self ,
1681
+ name : str ,
1682
+ first_line : str ,
1683
+ content : str ,
1684
+ position : int ,
1685
+ additional_options : dict [str , str ] | None = None ,
1676
1686
) -> list [nodes .Element ]:
1677
1687
"""Run a directive and return the generated nodes.
1678
1688
@@ -1681,6 +1691,8 @@ def run_directive(
1681
1691
May be an argument or body text, dependent on the directive
1682
1692
:param content: All text after the first line. Can include options.
1683
1693
:param position: The line number of the first line
1694
+ :param additional_options: Additional options to add to the directive,
1695
+ above those parsed from the content.
1684
1696
1685
1697
"""
1686
1698
self .document .current_line = position
@@ -1706,7 +1718,12 @@ def run_directive(
1706
1718
directive_class .option_spec ["heading-offset" ] = directives .nonnegative_int
1707
1719
1708
1720
try :
1709
- parsed = parse_directive_text (directive_class , first_line , content )
1721
+ parsed = parse_directive_text (
1722
+ directive_class ,
1723
+ first_line ,
1724
+ content ,
1725
+ additional_options = additional_options ,
1726
+ )
1710
1727
except MarkupError as error :
1711
1728
error = self .reporter .error (
1712
1729
f"Directive '{ name } ': { error } " ,
0 commit comments