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