@@ -600,6 +600,43 @@ def escape_rst(text, until_pos=-1): # type: (str) -> str
600
600
return text
601
601
602
602
603
+ def format_codeblock (code_type , post_text , indent_level , state ): # types: str, str, int, state
604
+ end_pos = post_text .find ("[/" + code_type + "]" )
605
+ if end_pos == - 1 :
606
+ print_error ("[" + code_type + "] without a closing tag, file: {}" .format (state .current_class ), state )
607
+ return None
608
+
609
+ code_text = post_text [len ("[" + code_type + "]" ) : end_pos ]
610
+ post_text = post_text [end_pos :]
611
+
612
+ # Remove extraneous tabs
613
+ code_pos = 0
614
+ while True :
615
+ code_pos = code_text .find ("\n " , code_pos )
616
+ if code_pos == - 1 :
617
+ break
618
+
619
+ to_skip = 0
620
+ while code_pos + to_skip + 1 < len (code_text ) and code_text [code_pos + to_skip + 1 ] == "\t " :
621
+ to_skip += 1
622
+
623
+ if to_skip > indent_level :
624
+ print_error (
625
+ "Four spaces should be used for indentation within ["
626
+ + code_type
627
+ + "], file: {}" .format (state .current_class ),
628
+ state ,
629
+ )
630
+
631
+ if len (code_text [code_pos + to_skip + 1 :]) == 0 :
632
+ code_text = code_text [:code_pos ] + "\n "
633
+ code_pos += 1
634
+ else :
635
+ code_text = code_text [:code_pos ] + "\n " + code_text [code_pos + to_skip + 1 :]
636
+ code_pos += 5 - to_skip
637
+ return ["\n [" + code_type + "]" + code_text + post_text , len ("\n [" + code_type + "]" + code_text )]
638
+
639
+
603
640
def rstize_text (text , state ): # type: (str, State) -> str
604
641
# Linebreak + tabs in the XML should become two line breaks unless in a "codeblock"
605
642
pos = 0
@@ -616,43 +653,17 @@ def rstize_text(text, state): # type: (str, State) -> str
616
653
post_text = text [pos + 1 :]
617
654
618
655
# Handle codeblocks
619
- if post_text .startswith ("[codeblock]" ):
620
- end_pos = post_text .find ("[/codeblock]" )
621
- if end_pos == - 1 :
622
- print_error ("[codeblock] without a closing tag, file: {}" .format (state .current_class ), state )
656
+ if (
657
+ post_text .startswith ("[codeblock]" )
658
+ or post_text .startswith ("[gdscript]" )
659
+ or post_text .startswith ("[csharp]" )
660
+ ):
661
+ block_type = post_text [1 :].split ("]" )[0 ]
662
+ result = format_codeblock (block_type , post_text , indent_level , state )
663
+ if result is None :
623
664
return ""
624
-
625
- code_text = post_text [len ("[codeblock]" ) : end_pos ]
626
- post_text = post_text [end_pos :]
627
-
628
- # Remove extraneous tabs
629
- code_pos = 0
630
- while True :
631
- code_pos = code_text .find ("\n " , code_pos )
632
- if code_pos == - 1 :
633
- break
634
-
635
- to_skip = 0
636
- while code_pos + to_skip + 1 < len (code_text ) and code_text [code_pos + to_skip + 1 ] == "\t " :
637
- to_skip += 1
638
-
639
- if to_skip > indent_level :
640
- print_error (
641
- "Four spaces should be used for indentation within [codeblock], file: {}" .format (
642
- state .current_class
643
- ),
644
- state ,
645
- )
646
-
647
- if len (code_text [code_pos + to_skip + 1 :]) == 0 :
648
- code_text = code_text [:code_pos ] + "\n "
649
- code_pos += 1
650
- else :
651
- code_text = code_text [:code_pos ] + "\n " + code_text [code_pos + to_skip + 1 :]
652
- code_pos += 5 - to_skip
653
-
654
- text = pre_text + "\n [codeblock]" + code_text + post_text
655
- pos += len ("\n [codeblock]" + code_text )
665
+ text = pre_text + result [0 ]
666
+ pos += result [1 ]
656
667
657
668
# Handle normal text
658
669
else :
@@ -697,7 +708,7 @@ def rstize_text(text, state): # type: (str, State) -> str
697
708
else : # command
698
709
cmd = tag_text
699
710
space_pos = tag_text .find (" " )
700
- if cmd == "/codeblock" :
711
+ if cmd == "/codeblock" or cmd == "/gdscript" or cmd == "/csharp" :
701
712
tag_text = ""
702
713
tag_depth -= 1
703
714
inside_code = False
@@ -813,6 +824,20 @@ def rstize_text(text, state): # type: (str, State) -> str
813
824
tag_depth += 1
814
825
tag_text = "\n ::\n "
815
826
inside_code = True
827
+ elif cmd == "gdscript" :
828
+ tag_depth += 1
829
+ tag_text = "\n .. code-tab:: gdscript GDScript\n "
830
+ inside_code = True
831
+ elif cmd == "csharp" :
832
+ tag_depth += 1
833
+ tag_text = "\n .. code-tab:: csharp\n "
834
+ inside_code = True
835
+ elif cmd == "codeblocks" :
836
+ tag_depth += 1
837
+ tag_text = "\n .. tabs::"
838
+ elif cmd == "/codeblocks" :
839
+ tag_depth -= 1
840
+ tag_text = ""
816
841
elif cmd == "br" :
817
842
# Make a new paragraph instead of a linebreak, rst is not so linebreak friendly
818
843
tag_text = "\n \n "
0 commit comments