make_rst.py 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614
  1. #!/usr/bin/env python3
  2. # This script makes RST files from the XML class reference for use with the online docs.
  3. from __future__ import annotations
  4. import argparse
  5. import os
  6. import re
  7. import sys
  8. import xml.etree.ElementTree as ET
  9. from collections import OrderedDict
  10. from typing import Any, TextIO
  11. sys.path.insert(0, root_directory := os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../"))
  12. import version
  13. from misc.utility.color import Ansi, force_stderr_color, force_stdout_color
  14. # $DOCS_URL/path/to/page.html(#fragment-tag)
  15. GODOT_DOCS_PATTERN = re.compile(r"^\$DOCS_URL/(.*)\.html(#.*)?$")
  16. # Based on reStructuredText inline markup recognition rules
  17. # https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#inline-markup-recognition-rules
  18. MARKUP_ALLOWED_PRECEDENT = " -:/'\"<([{"
  19. MARKUP_ALLOWED_SUBSEQUENT = " -.,:;!?\\/'\")]}>"
  20. # Used to translate section headings and other hardcoded strings when required with
  21. # the --lang argument. The BASE_STRINGS list should be synced with what we actually
  22. # write in this script (check `translate()` uses), and also hardcoded in
  23. # `scripts/extract_classes.py` (godotengine/godot-editor-l10n repo) to include them in the source POT file.
  24. BASE_STRINGS = [
  25. "All classes",
  26. "Globals",
  27. "Nodes",
  28. "Resources",
  29. "Editor-only",
  30. "Other objects",
  31. "Variant types",
  32. "Description",
  33. "Tutorials",
  34. "Properties",
  35. "Constructors",
  36. "Methods",
  37. "Operators",
  38. "Theme Properties",
  39. "Signals",
  40. "Enumerations",
  41. "Constants",
  42. "Annotations",
  43. "Property Descriptions",
  44. "Constructor Descriptions",
  45. "Method Descriptions",
  46. "Operator Descriptions",
  47. "Theme Property Descriptions",
  48. "Inherits:",
  49. "Inherited By:",
  50. "(overrides %s)",
  51. "Default",
  52. "Setter",
  53. "value",
  54. "Getter",
  55. "This method should typically be overridden by the user to have any effect.",
  56. "This method is required to be overridden when extending its base class.",
  57. "This method has no side effects. It doesn't modify any of the instance's member variables.",
  58. "This method accepts any number of arguments after the ones described here.",
  59. "This method is used to construct a type.",
  60. "This method doesn't need an instance to be called, so it can be called directly using the class name.",
  61. "This method describes a valid operator to use with this type as left-hand operand.",
  62. "This value is an integer composed as a bitmask of the following flags.",
  63. "No return value.",
  64. "There is currently no description for this class. Please help us by `contributing one <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__!",
  65. "There is currently no description for this signal. Please help us by `contributing one <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__!",
  66. "There is currently no description for this enum. Please help us by `contributing one <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__!",
  67. "There is currently no description for this constant. Please help us by `contributing one <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__!",
  68. "There is currently no description for this annotation. Please help us by `contributing one <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__!",
  69. "There is currently no description for this property. Please help us by `contributing one <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__!",
  70. "There is currently no description for this constructor. Please help us by `contributing one <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__!",
  71. "There is currently no description for this method. Please help us by `contributing one <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__!",
  72. "There is currently no description for this operator. Please help us by `contributing one <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__!",
  73. "There is currently no description for this theme property. Please help us by `contributing one <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__!",
  74. "There are notable differences when using this API with C#. See :ref:`doc_c_sharp_differences` for more information.",
  75. "Deprecated:",
  76. "Experimental:",
  77. "This signal may be changed or removed in future versions.",
  78. "This constant may be changed or removed in future versions.",
  79. "This property may be changed or removed in future versions.",
  80. "This constructor may be changed or removed in future versions.",
  81. "This method may be changed or removed in future versions.",
  82. "This operator may be changed or removed in future versions.",
  83. "This theme property may be changed or removed in future versions.",
  84. # See also `make_rst_class()` and `editor/doc/editor_help.cpp`.
  85. "[b]Note:[/b] The returned array is [i]copied[/i] and any changes to it will not update the original property value. See [%s] for more details.",
  86. ]
  87. strings_l10n: dict[str, str] = {}
  88. writing_translation = False
  89. CLASS_GROUPS: dict[str, str] = {
  90. "global": "Globals",
  91. "node": "Nodes",
  92. "resource": "Resources",
  93. "object": "Other objects",
  94. "editor": "Editor-only",
  95. "variant": "Variant types",
  96. }
  97. CLASS_GROUPS_BASE: dict[str, str] = {
  98. "node": "Node",
  99. "resource": "Resource",
  100. "object": "Object",
  101. "variant": "Variant",
  102. }
  103. # Sync with editor\register_editor_types.cpp
  104. EDITOR_CLASSES: list[str] = [
  105. "FileSystemDock",
  106. "ScriptCreateDialog",
  107. "ScriptEditor",
  108. "ScriptEditorBase",
  109. ]
  110. # Sync with the types mentioned in https://docs.godotengine.org/en/stable/tutorials/scripting/c_sharp/c_sharp_differences.html
  111. CLASSES_WITH_CSHARP_DIFFERENCES: list[str] = [
  112. "@GlobalScope",
  113. "String",
  114. "StringName",
  115. "NodePath",
  116. "Signal",
  117. "Callable",
  118. "RID",
  119. "Basis",
  120. "Transform2D",
  121. "Transform3D",
  122. "Rect2",
  123. "Rect2i",
  124. "AABB",
  125. "Quaternion",
  126. "Projection",
  127. "Color",
  128. "Array",
  129. "Dictionary",
  130. "PackedByteArray",
  131. "PackedColorArray",
  132. "PackedFloat32Array",
  133. "PackedFloat64Array",
  134. "PackedInt32Array",
  135. "PackedInt64Array",
  136. "PackedStringArray",
  137. "PackedVector2Array",
  138. "PackedVector3Array",
  139. "PackedVector4Array",
  140. "Variant",
  141. ]
  142. PACKED_ARRAY_TYPES: list[str] = [
  143. "PackedByteArray",
  144. "PackedColorArray",
  145. "PackedFloat32Array",
  146. "PackedFloat64Array",
  147. "PackedInt32Array",
  148. "PackedInt64Array",
  149. "PackedStringArray",
  150. "PackedVector2Array",
  151. "PackedVector3Array",
  152. "PackedVector4Array",
  153. ]
  154. class State:
  155. def __init__(self) -> None:
  156. self.num_errors = 0
  157. self.num_warnings = 0
  158. self.classes: OrderedDict[str, ClassDef] = OrderedDict()
  159. self.current_class: str = ""
  160. # Additional content and structure checks and validators.
  161. self.script_language_parity_check: ScriptLanguageParityCheck = ScriptLanguageParityCheck()
  162. def parse_class(self, class_root: ET.Element, filepath: str) -> None:
  163. class_name = class_root.attrib["name"]
  164. self.current_class = class_name
  165. class_def = ClassDef(class_name)
  166. self.classes[class_name] = class_def
  167. class_def.filepath = filepath
  168. inherits = class_root.get("inherits")
  169. if inherits is not None:
  170. class_def.inherits = inherits
  171. class_def.deprecated = class_root.get("deprecated")
  172. class_def.experimental = class_root.get("experimental")
  173. brief_desc = class_root.find("brief_description")
  174. if brief_desc is not None and brief_desc.text:
  175. class_def.brief_description = brief_desc.text
  176. desc = class_root.find("description")
  177. if desc is not None and desc.text:
  178. class_def.description = desc.text
  179. keywords = class_root.get("keywords")
  180. if keywords is not None:
  181. class_def.keywords = keywords
  182. properties = class_root.find("members")
  183. if properties is not None:
  184. for property in properties:
  185. assert property.tag == "member"
  186. property_name = property.attrib["name"]
  187. if property_name in class_def.properties:
  188. print_error(f'{class_name}.xml: Duplicate property "{property_name}".', self)
  189. continue
  190. type_name = TypeName.from_element(property)
  191. setter = property.get("setter") or None # Use or None so '' gets turned into None.
  192. getter = property.get("getter") or None
  193. default_value = property.get("default") or None
  194. if default_value is not None:
  195. default_value = f"``{default_value}``"
  196. overrides = property.get("overrides") or None
  197. property_def = PropertyDef(
  198. property_name, type_name, setter, getter, property.text, default_value, overrides
  199. )
  200. property_def.deprecated = property.get("deprecated")
  201. property_def.experimental = property.get("experimental")
  202. class_def.properties[property_name] = property_def
  203. constructors = class_root.find("constructors")
  204. if constructors is not None:
  205. for constructor in constructors:
  206. assert constructor.tag == "constructor"
  207. method_name = constructor.attrib["name"]
  208. qualifiers = constructor.get("qualifiers")
  209. return_element = constructor.find("return")
  210. if return_element is not None:
  211. return_type = TypeName.from_element(return_element)
  212. else:
  213. return_type = TypeName("void")
  214. params = self.parse_params(constructor, "constructor")
  215. desc_element = constructor.find("description")
  216. method_desc = None
  217. if desc_element is not None:
  218. method_desc = desc_element.text
  219. method_def = MethodDef(method_name, return_type, params, method_desc, qualifiers)
  220. method_def.definition_name = "constructor"
  221. method_def.deprecated = constructor.get("deprecated")
  222. method_def.experimental = constructor.get("experimental")
  223. if method_name not in class_def.constructors:
  224. class_def.constructors[method_name] = []
  225. class_def.constructors[method_name].append(method_def)
  226. methods = class_root.find("methods")
  227. if methods is not None:
  228. for method in methods:
  229. assert method.tag == "method"
  230. method_name = method.attrib["name"]
  231. qualifiers = method.get("qualifiers")
  232. return_element = method.find("return")
  233. if return_element is not None:
  234. return_type = TypeName.from_element(return_element)
  235. else:
  236. return_type = TypeName("void")
  237. params = self.parse_params(method, "method")
  238. desc_element = method.find("description")
  239. method_desc = None
  240. if desc_element is not None:
  241. method_desc = desc_element.text
  242. method_def = MethodDef(method_name, return_type, params, method_desc, qualifiers)
  243. method_def.deprecated = method.get("deprecated")
  244. method_def.experimental = method.get("experimental")
  245. if method_name not in class_def.methods:
  246. class_def.methods[method_name] = []
  247. class_def.methods[method_name].append(method_def)
  248. operators = class_root.find("operators")
  249. if operators is not None:
  250. for operator in operators:
  251. assert operator.tag == "operator"
  252. method_name = operator.attrib["name"]
  253. qualifiers = operator.get("qualifiers")
  254. return_element = operator.find("return")
  255. if return_element is not None:
  256. return_type = TypeName.from_element(return_element)
  257. else:
  258. return_type = TypeName("void")
  259. params = self.parse_params(operator, "operator")
  260. desc_element = operator.find("description")
  261. method_desc = None
  262. if desc_element is not None:
  263. method_desc = desc_element.text
  264. method_def = MethodDef(method_name, return_type, params, method_desc, qualifiers)
  265. method_def.definition_name = "operator"
  266. method_def.deprecated = operator.get("deprecated")
  267. method_def.experimental = operator.get("experimental")
  268. if method_name not in class_def.operators:
  269. class_def.operators[method_name] = []
  270. class_def.operators[method_name].append(method_def)
  271. constants = class_root.find("constants")
  272. if constants is not None:
  273. for constant in constants:
  274. assert constant.tag == "constant"
  275. constant_name = constant.attrib["name"]
  276. value = constant.attrib["value"]
  277. enum = constant.get("enum")
  278. is_bitfield = constant.get("is_bitfield") == "true"
  279. constant_def = ConstantDef(constant_name, value, constant.text, is_bitfield)
  280. constant_def.deprecated = constant.get("deprecated")
  281. constant_def.experimental = constant.get("experimental")
  282. if enum is None:
  283. if constant_name in class_def.constants:
  284. print_error(f'{class_name}.xml: Duplicate constant "{constant_name}".', self)
  285. continue
  286. class_def.constants[constant_name] = constant_def
  287. else:
  288. if enum in class_def.enums:
  289. enum_def = class_def.enums[enum]
  290. else:
  291. enum_def = EnumDef(enum, TypeName("int", enum), is_bitfield)
  292. class_def.enums[enum] = enum_def
  293. enum_def.values[constant_name] = constant_def
  294. annotations = class_root.find("annotations")
  295. if annotations is not None:
  296. for annotation in annotations:
  297. assert annotation.tag == "annotation"
  298. annotation_name = annotation.attrib["name"]
  299. qualifiers = annotation.get("qualifiers")
  300. params = self.parse_params(annotation, "annotation")
  301. desc_element = annotation.find("description")
  302. annotation_desc = None
  303. if desc_element is not None:
  304. annotation_desc = desc_element.text
  305. annotation_def = AnnotationDef(annotation_name, params, annotation_desc, qualifiers)
  306. if annotation_name not in class_def.annotations:
  307. class_def.annotations[annotation_name] = []
  308. class_def.annotations[annotation_name].append(annotation_def)
  309. signals = class_root.find("signals")
  310. if signals is not None:
  311. for signal in signals:
  312. assert signal.tag == "signal"
  313. signal_name = signal.attrib["name"]
  314. if signal_name in class_def.signals:
  315. print_error(f'{class_name}.xml: Duplicate signal "{signal_name}".', self)
  316. continue
  317. params = self.parse_params(signal, "signal")
  318. desc_element = signal.find("description")
  319. signal_desc = None
  320. if desc_element is not None:
  321. signal_desc = desc_element.text
  322. signal_def = SignalDef(signal_name, params, signal_desc)
  323. signal_def.deprecated = signal.get("deprecated")
  324. signal_def.experimental = signal.get("experimental")
  325. class_def.signals[signal_name] = signal_def
  326. theme_items = class_root.find("theme_items")
  327. if theme_items is not None:
  328. for theme_item in theme_items:
  329. assert theme_item.tag == "theme_item"
  330. theme_item_name = theme_item.attrib["name"]
  331. theme_item_data_name = theme_item.attrib["data_type"]
  332. theme_item_id = "{}_{}".format(theme_item_data_name, theme_item_name)
  333. if theme_item_id in class_def.theme_items:
  334. print_error(
  335. f'{class_name}.xml: Duplicate theme property "{theme_item_name}" of type "{theme_item_data_name}".',
  336. self,
  337. )
  338. continue
  339. default_value = theme_item.get("default") or None
  340. if default_value is not None:
  341. default_value = f"``{default_value}``"
  342. theme_item_def = ThemeItemDef(
  343. theme_item_name,
  344. TypeName.from_element(theme_item),
  345. theme_item_data_name,
  346. theme_item.text,
  347. default_value,
  348. )
  349. class_def.theme_items[theme_item_name] = theme_item_def
  350. tutorials = class_root.find("tutorials")
  351. if tutorials is not None:
  352. for link in tutorials:
  353. assert link.tag == "link"
  354. if link.text is not None:
  355. class_def.tutorials.append((link.text.strip(), link.get("title", "")))
  356. self.current_class = ""
  357. def parse_params(self, root: ET.Element, context: str) -> list[ParameterDef]:
  358. param_elements = root.findall("param")
  359. params: Any = [None] * len(param_elements)
  360. for param_index, param_element in enumerate(param_elements):
  361. param_name = param_element.attrib["name"]
  362. index = int(param_element.attrib["index"])
  363. type_name = TypeName.from_element(param_element)
  364. default = param_element.get("default")
  365. if param_name.strip() == "" or param_name.startswith("_unnamed_arg"):
  366. print_error(
  367. f'{self.current_class}.xml: Empty argument name in {context} "{root.attrib["name"]}" at position {param_index}.',
  368. self,
  369. )
  370. params[index] = ParameterDef(param_name, type_name, default)
  371. cast: list[ParameterDef] = params
  372. return cast
  373. def sort_classes(self) -> None:
  374. self.classes = OrderedDict(sorted(self.classes.items(), key=lambda t: t[0].lower()))
  375. class TagState:
  376. def __init__(self, raw: str, name: str, arguments: str, closing: bool) -> None:
  377. self.raw = raw
  378. self.name = name
  379. self.arguments = arguments
  380. self.closing = closing
  381. class TypeName:
  382. def __init__(self, type_name: str, enum: str | None = None, is_bitfield: bool = False) -> None:
  383. self.type_name = type_name
  384. self.enum = enum
  385. self.is_bitfield = is_bitfield
  386. def to_rst(self, state: State) -> str:
  387. if self.enum is not None:
  388. return make_enum(self.enum, self.is_bitfield, state)
  389. elif self.type_name == "void":
  390. return "|void|"
  391. else:
  392. return make_type(self.type_name, state)
  393. @classmethod
  394. def from_element(cls, element: ET.Element) -> TypeName:
  395. return cls(element.attrib["type"], element.get("enum"), element.get("is_bitfield") == "true")
  396. class DefinitionBase:
  397. def __init__(
  398. self,
  399. definition_name: str,
  400. name: str,
  401. ) -> None:
  402. self.definition_name = definition_name
  403. self.name = name
  404. self.deprecated: str | None = None
  405. self.experimental: str | None = None
  406. class PropertyDef(DefinitionBase):
  407. def __init__(
  408. self,
  409. name: str,
  410. type_name: TypeName,
  411. setter: str | None,
  412. getter: str | None,
  413. text: str | None,
  414. default_value: str | None,
  415. overrides: str | None,
  416. ) -> None:
  417. super().__init__("property", name)
  418. self.type_name = type_name
  419. self.setter = setter
  420. self.getter = getter
  421. self.text = text
  422. self.default_value = default_value
  423. self.overrides = overrides
  424. class ParameterDef(DefinitionBase):
  425. def __init__(self, name: str, type_name: TypeName, default_value: str | None) -> None:
  426. super().__init__("parameter", name)
  427. self.type_name = type_name
  428. self.default_value = default_value
  429. class SignalDef(DefinitionBase):
  430. def __init__(self, name: str, parameters: list[ParameterDef], description: str | None) -> None:
  431. super().__init__("signal", name)
  432. self.parameters = parameters
  433. self.description = description
  434. class AnnotationDef(DefinitionBase):
  435. def __init__(
  436. self,
  437. name: str,
  438. parameters: list[ParameterDef],
  439. description: str | None,
  440. qualifiers: str | None,
  441. ) -> None:
  442. super().__init__("annotation", name)
  443. self.parameters = parameters
  444. self.description = description
  445. self.qualifiers = qualifiers
  446. class MethodDef(DefinitionBase):
  447. def __init__(
  448. self,
  449. name: str,
  450. return_type: TypeName,
  451. parameters: list[ParameterDef],
  452. description: str | None,
  453. qualifiers: str | None,
  454. ) -> None:
  455. super().__init__("method", name)
  456. self.return_type = return_type
  457. self.parameters = parameters
  458. self.description = description
  459. self.qualifiers = qualifiers
  460. class ConstantDef(DefinitionBase):
  461. def __init__(self, name: str, value: str, text: str | None, bitfield: bool) -> None:
  462. super().__init__("constant", name)
  463. self.value = value
  464. self.text = text
  465. self.is_bitfield = bitfield
  466. class EnumDef(DefinitionBase):
  467. def __init__(self, name: str, type_name: TypeName, bitfield: bool) -> None:
  468. super().__init__("enum", name)
  469. self.type_name = type_name
  470. self.values: OrderedDict[str, ConstantDef] = OrderedDict()
  471. self.is_bitfield = bitfield
  472. class ThemeItemDef(DefinitionBase):
  473. def __init__(
  474. self, name: str, type_name: TypeName, data_name: str, text: str | None, default_value: str | None
  475. ) -> None:
  476. super().__init__("theme property", name)
  477. self.type_name = type_name
  478. self.data_name = data_name
  479. self.text = text
  480. self.default_value = default_value
  481. class ClassDef(DefinitionBase):
  482. def __init__(self, name: str) -> None:
  483. super().__init__("class", name)
  484. self.class_group = "variant"
  485. self.editor_class = self._is_editor_class()
  486. self.constants: OrderedDict[str, ConstantDef] = OrderedDict()
  487. self.enums: OrderedDict[str, EnumDef] = OrderedDict()
  488. self.properties: OrderedDict[str, PropertyDef] = OrderedDict()
  489. self.constructors: OrderedDict[str, list[MethodDef]] = OrderedDict()
  490. self.methods: OrderedDict[str, list[MethodDef]] = OrderedDict()
  491. self.operators: OrderedDict[str, list[MethodDef]] = OrderedDict()
  492. self.signals: OrderedDict[str, SignalDef] = OrderedDict()
  493. self.annotations: OrderedDict[str, list[AnnotationDef]] = OrderedDict()
  494. self.theme_items: OrderedDict[str, ThemeItemDef] = OrderedDict()
  495. self.inherits: str | None = None
  496. self.brief_description: str | None = None
  497. self.description: str | None = None
  498. self.tutorials: list[tuple[str, str]] = []
  499. self.keywords: str | None = None
  500. # Used to match the class with XML source for output filtering purposes.
  501. self.filepath: str = ""
  502. def _is_editor_class(self) -> bool:
  503. if self.name.startswith("Editor"):
  504. return True
  505. if self.name in EDITOR_CLASSES:
  506. return True
  507. return False
  508. def update_class_group(self, state: State) -> None:
  509. group_name = "variant"
  510. if self.name.startswith("@"):
  511. group_name = "global"
  512. elif self.inherits:
  513. inherits = self.inherits.strip()
  514. while inherits in state.classes:
  515. if inherits == "Node":
  516. group_name = "node"
  517. break
  518. if inherits == "Resource":
  519. group_name = "resource"
  520. break
  521. if inherits == "Object":
  522. group_name = "object"
  523. break
  524. inode = state.classes[inherits].inherits
  525. if inode:
  526. inherits = inode.strip()
  527. else:
  528. break
  529. self.class_group = group_name
  530. # Checks if code samples have both GDScript and C# variations.
  531. # For simplicity we assume that a GDScript example is always present, and ignore contexts
  532. # which don't necessarily need C# examples.
  533. class ScriptLanguageParityCheck:
  534. def __init__(self) -> None:
  535. self.hit_map: OrderedDict[str, list[tuple[DefinitionBase, str]]] = OrderedDict()
  536. self.hit_count = 0
  537. def add_hit(self, class_name: str, context: DefinitionBase, error: str, state: State) -> None:
  538. if class_name in ["@GDScript", "@GlobalScope"]:
  539. return # We don't expect these contexts to have parity.
  540. class_def = state.classes[class_name]
  541. if class_def.class_group == "variant" and class_def.name != "Object":
  542. return # Variant types are replaced with native types in C#, we don't expect parity.
  543. self.hit_count += 1
  544. if class_name not in self.hit_map:
  545. self.hit_map[class_name] = []
  546. self.hit_map[class_name].append((context, error))
  547. # Entry point for the RST generator.
  548. def main() -> None:
  549. parser = argparse.ArgumentParser()
  550. parser.add_argument("path", nargs="+", help="A path to an XML file or a directory containing XML files to parse.")
  551. parser.add_argument("--filter", default="", help="The filepath pattern for XML files to filter.")
  552. parser.add_argument("--lang", "-l", default="en", help="Language to use for section headings.")
  553. parser.add_argument(
  554. "--color",
  555. action="store_true",
  556. help="If passed, force colored output even if stdout is not a TTY (useful for continuous integration).",
  557. )
  558. group = parser.add_mutually_exclusive_group()
  559. group.add_argument("--output", "-o", default=".", help="The directory to save output .rst files in.")
  560. group.add_argument(
  561. "--dry-run",
  562. action="store_true",
  563. help="If passed, no output will be generated and XML files are only checked for errors.",
  564. )
  565. parser.add_argument(
  566. "--verbose",
  567. action="store_true",
  568. help="If passed, enables verbose printing.",
  569. )
  570. args = parser.parse_args()
  571. if args.color:
  572. force_stdout_color(True)
  573. force_stderr_color(True)
  574. # Retrieve heading translations for the given language.
  575. if not args.dry_run and args.lang != "en":
  576. global writing_translation
  577. writing_translation = True
  578. lang_file = os.path.join(
  579. os.path.dirname(os.path.realpath(__file__)), "..", "translations", "{}.po".format(args.lang)
  580. )
  581. if os.path.exists(lang_file):
  582. try:
  583. import polib # type: ignore
  584. except ImportError:
  585. print("Base template strings localization requires `polib`.")
  586. exit(1)
  587. pofile = polib.pofile(lang_file)
  588. for entry in pofile.translated_entries():
  589. if entry.msgid in BASE_STRINGS:
  590. strings_l10n[entry.msgid] = entry.msgstr
  591. else:
  592. print(f'No PO file at "{lang_file}" for language "{args.lang}".')
  593. print("Checking for errors in the XML class reference...")
  594. file_list: list[str] = []
  595. for path in args.path:
  596. # Cut off trailing slashes so os.path.basename doesn't choke.
  597. if path.endswith("/") or path.endswith("\\"):
  598. path = path[:-1]
  599. if os.path.basename(path) in ["modules", "platform"]:
  600. for subdir, dirs, _ in os.walk(path):
  601. if "doc_classes" in dirs:
  602. doc_dir = os.path.join(subdir, "doc_classes")
  603. class_file_names = (f for f in os.listdir(doc_dir) if f.endswith(".xml"))
  604. file_list += (os.path.join(doc_dir, f) for f in class_file_names)
  605. elif os.path.isdir(path):
  606. file_list += (os.path.join(path, f) for f in os.listdir(path) if f.endswith(".xml"))
  607. elif os.path.isfile(path):
  608. if not path.endswith(".xml"):
  609. print(f'Got non-.xml file "{path}" in input, skipping.')
  610. continue
  611. file_list.append(path)
  612. classes: dict[str, tuple[ET.Element, str]] = {}
  613. state = State()
  614. for cur_file in file_list:
  615. try:
  616. tree = ET.parse(cur_file)
  617. except ET.ParseError as e:
  618. print_error(f"{cur_file}: Parse error while reading the file: {e}", state)
  619. continue
  620. doc = tree.getroot()
  621. name = doc.attrib["name"]
  622. if name in classes:
  623. print_error(f'{cur_file}: Duplicate class "{name}".', state)
  624. continue
  625. classes[name] = (doc, cur_file)
  626. for name, data in classes.items():
  627. try:
  628. state.parse_class(data[0], data[1])
  629. except Exception as e:
  630. print_error(f"{name}.xml: Exception while parsing class: {e}", state)
  631. state.sort_classes()
  632. pattern = re.compile(args.filter)
  633. # Create the output folder recursively if it doesn't already exist.
  634. os.makedirs(args.output, exist_ok=True)
  635. print("Generating the RST class reference...")
  636. grouped_classes: dict[str, list[str]] = {}
  637. for class_name, class_def in state.classes.items():
  638. if args.filter and not pattern.search(class_def.filepath):
  639. continue
  640. state.current_class = class_name
  641. class_def.update_class_group(state)
  642. make_rst_class(class_def, state, args.dry_run, args.output)
  643. if class_def.class_group not in grouped_classes:
  644. grouped_classes[class_def.class_group] = []
  645. grouped_classes[class_def.class_group].append(class_name)
  646. if class_def.editor_class:
  647. if "editor" not in grouped_classes:
  648. grouped_classes["editor"] = []
  649. grouped_classes["editor"].append(class_name)
  650. print("")
  651. print("Generating the index file...")
  652. make_rst_index(grouped_classes, args.dry_run, args.output)
  653. print("")
  654. # Print out checks.
  655. if state.script_language_parity_check.hit_count > 0:
  656. if not args.verbose:
  657. print(
  658. f"{Ansi.YELLOW}{state.script_language_parity_check.hit_count} code samples failed parity check. Use --verbose to get more information.{Ansi.RESET}"
  659. )
  660. else:
  661. print(
  662. f"{Ansi.YELLOW}{state.script_language_parity_check.hit_count} code samples failed parity check:{Ansi.RESET}"
  663. )
  664. for class_name in state.script_language_parity_check.hit_map.keys():
  665. class_hits = state.script_language_parity_check.hit_map[class_name]
  666. print(f'{Ansi.YELLOW}- {len(class_hits)} hits in class "{class_name}"{Ansi.RESET}')
  667. for context, error in class_hits:
  668. print(f" - {error} in {format_context_name(context)}")
  669. print("")
  670. # Print out warnings and errors, or lack thereof, and exit with an appropriate code.
  671. if state.num_warnings >= 2:
  672. print(
  673. f"{Ansi.YELLOW}{state.num_warnings} warnings were found in the class reference XML. Please check the messages above.{Ansi.RESET}"
  674. )
  675. elif state.num_warnings == 1:
  676. print(
  677. f"{Ansi.YELLOW}1 warning was found in the class reference XML. Please check the messages above.{Ansi.RESET}"
  678. )
  679. if state.num_errors >= 2:
  680. print(
  681. f"{Ansi.RED}{state.num_errors} errors were found in the class reference XML. Please check the messages above.{Ansi.RESET}"
  682. )
  683. elif state.num_errors == 1:
  684. print(f"{Ansi.RED}1 error was found in the class reference XML. Please check the messages above.{Ansi.RESET}")
  685. if state.num_warnings == 0 and state.num_errors == 0:
  686. print(f"{Ansi.GREEN}No warnings or errors found in the class reference XML.{Ansi.RESET}")
  687. if not args.dry_run:
  688. print(f"Wrote reStructuredText files for each class to: {args.output}")
  689. else:
  690. exit(1)
  691. # Common helpers.
  692. def print_error(error: str, state: State) -> None:
  693. print(f"{Ansi.RED}{Ansi.BOLD}ERROR:{Ansi.REGULAR} {error}{Ansi.RESET}")
  694. state.num_errors += 1
  695. def print_warning(warning: str, state: State) -> None:
  696. print(f"{Ansi.YELLOW}{Ansi.BOLD}WARNING:{Ansi.REGULAR} {warning}{Ansi.RESET}")
  697. state.num_warnings += 1
  698. def translate(string: str) -> str:
  699. """Translate a string based on translations sourced from `doc/translations/*.po`
  700. for a language if defined via the --lang command line argument.
  701. Returns the original string if no translation exists.
  702. """
  703. return strings_l10n.get(string, string)
  704. def get_git_branch() -> str:
  705. if hasattr(version, "docs") and version.docs != "latest":
  706. return version.docs
  707. return "master"
  708. # Generator methods.
  709. def make_rst_class(class_def: ClassDef, state: State, dry_run: bool, output_dir: str) -> None:
  710. class_name = class_def.name
  711. with open(
  712. os.devnull if dry_run else os.path.join(output_dir, f"class_{sanitize_class_name(class_name, True)}.rst"),
  713. "w",
  714. encoding="utf-8",
  715. newline="\n",
  716. ) as f:
  717. # Remove the "Edit on Github" button from the online docs page.
  718. f.write(":github_url: hide\n\n")
  719. # Add keywords metadata.
  720. if class_def.keywords is not None and class_def.keywords != "":
  721. f.write(f".. meta::\n\t:keywords: {class_def.keywords}\n\n")
  722. if not writing_translation: # Skip for translations to reduce diff.
  723. # Warn contributors not to edit this file directly.
  724. # Also provide links to the source files for reference.
  725. git_branch = get_git_branch()
  726. source_xml_path = os.path.relpath(class_def.filepath, root_directory).replace("\\", "/")
  727. source_github_url = f"https://github.com/godotengine/godot/tree/{git_branch}/{source_xml_path}"
  728. generator_github_url = f"https://github.com/godotengine/godot/tree/{git_branch}/doc/tools/make_rst.py"
  729. f.write(".. DO NOT EDIT THIS FILE!!!\n")
  730. f.write(".. Generated automatically from Godot engine sources.\n")
  731. f.write(f".. Generator: {generator_github_url}.\n")
  732. f.write(f".. XML source: {source_github_url}.\n\n")
  733. # Document reference id and header.
  734. f.write(f".. _class_{sanitize_class_name(class_name)}:\n\n")
  735. f.write(make_heading(class_name, "=", False))
  736. f.write(make_deprecated_experimental(class_def, state))
  737. ### INHERITANCE TREE ###
  738. # Ascendants
  739. if class_def.inherits:
  740. inherits = class_def.inherits.strip()
  741. f.write(f"**{translate('Inherits:')}** ")
  742. first = True
  743. while inherits is not None:
  744. if not first:
  745. f.write(" **<** ")
  746. else:
  747. first = False
  748. f.write(make_type(inherits, state))
  749. if inherits not in state.classes:
  750. break # Parent unknown.
  751. inode = state.classes[inherits].inherits
  752. if inode:
  753. inherits = inode.strip()
  754. else:
  755. break
  756. f.write("\n\n")
  757. # Descendants
  758. inherited: list[str] = []
  759. for c in state.classes.values():
  760. if c.inherits and c.inherits.strip() == class_name:
  761. inherited.append(c.name)
  762. if len(inherited):
  763. f.write(f"**{translate('Inherited By:')}** ")
  764. for i, child in enumerate(inherited):
  765. if i > 0:
  766. f.write(", ")
  767. f.write(make_type(child, state))
  768. f.write("\n\n")
  769. ### INTRODUCTION ###
  770. has_description = False
  771. # Brief description
  772. if class_def.brief_description is not None and class_def.brief_description.strip() != "":
  773. has_description = True
  774. f.write(f"{format_text_block(class_def.brief_description.strip(), class_def, state)}\n\n")
  775. # Class description
  776. if class_def.description is not None and class_def.description.strip() != "":
  777. has_description = True
  778. f.write(".. rst-class:: classref-introduction-group\n\n")
  779. f.write(make_heading("Description", "-"))
  780. f.write(f"{format_text_block(class_def.description.strip(), class_def, state)}\n\n")
  781. if not has_description:
  782. f.write(".. container:: contribute\n\n\t")
  783. f.write(
  784. translate(
  785. "There is currently no description for this class. Please help us by `contributing one <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__!"
  786. )
  787. + "\n\n"
  788. )
  789. if class_def.name in CLASSES_WITH_CSHARP_DIFFERENCES:
  790. f.write(".. note::\n\n\t")
  791. f.write(
  792. translate(
  793. "There are notable differences when using this API with C#. See :ref:`doc_c_sharp_differences` for more information."
  794. )
  795. + "\n\n"
  796. )
  797. # Online tutorials
  798. if len(class_def.tutorials) > 0:
  799. f.write(".. rst-class:: classref-introduction-group\n\n")
  800. f.write(make_heading("Tutorials", "-"))
  801. for url, title in class_def.tutorials:
  802. f.write(f"- {make_link(url, title)}\n\n")
  803. ### REFERENCE TABLES ###
  804. # Reused container for reference tables.
  805. ml: list[tuple[str | None, ...]] = []
  806. # Properties reference table
  807. if len(class_def.properties) > 0:
  808. f.write(".. rst-class:: classref-reftable-group\n\n")
  809. f.write(make_heading("Properties", "-"))
  810. ml = []
  811. for property_def in class_def.properties.values():
  812. type_rst = property_def.type_name.to_rst(state)
  813. default = property_def.default_value
  814. if default is not None and property_def.overrides:
  815. ref = f":ref:`{property_def.overrides}<class_{sanitize_class_name(property_def.overrides)}_property_{property_def.name}>`"
  816. # Not using translate() for now as it breaks table formatting.
  817. ml.append((type_rst, property_def.name, f"{default} (overrides {ref})"))
  818. else:
  819. ref = f":ref:`{property_def.name}<class_{sanitize_class_name(class_name)}_property_{property_def.name}>`"
  820. ml.append((type_rst, ref, default))
  821. format_table(f, ml, True)
  822. # Constructors, Methods, Operators reference tables
  823. if len(class_def.constructors) > 0:
  824. f.write(".. rst-class:: classref-reftable-group\n\n")
  825. f.write(make_heading("Constructors", "-"))
  826. ml = []
  827. for method_list in class_def.constructors.values():
  828. for m in method_list:
  829. ml.append(make_method_signature(class_def, m, "constructor", state))
  830. format_table(f, ml)
  831. if len(class_def.methods) > 0:
  832. f.write(".. rst-class:: classref-reftable-group\n\n")
  833. f.write(make_heading("Methods", "-"))
  834. ml = []
  835. for method_list in class_def.methods.values():
  836. for m in method_list:
  837. ml.append(make_method_signature(class_def, m, "method", state))
  838. format_table(f, ml)
  839. if len(class_def.operators) > 0:
  840. f.write(".. rst-class:: classref-reftable-group\n\n")
  841. f.write(make_heading("Operators", "-"))
  842. ml = []
  843. for method_list in class_def.operators.values():
  844. for m in method_list:
  845. ml.append(make_method_signature(class_def, m, "operator", state))
  846. format_table(f, ml)
  847. # Theme properties reference table
  848. if len(class_def.theme_items) > 0:
  849. f.write(".. rst-class:: classref-reftable-group\n\n")
  850. f.write(make_heading("Theme Properties", "-"))
  851. ml = []
  852. for theme_item_def in class_def.theme_items.values():
  853. ref = f":ref:`{theme_item_def.name}<class_{sanitize_class_name(class_name)}_theme_{theme_item_def.data_name}_{theme_item_def.name}>`"
  854. ml.append((theme_item_def.type_name.to_rst(state), ref, theme_item_def.default_value))
  855. format_table(f, ml, True)
  856. ### DETAILED DESCRIPTIONS ###
  857. # Signal descriptions
  858. if len(class_def.signals) > 0:
  859. f.write(make_separator(True))
  860. f.write(".. rst-class:: classref-descriptions-group\n\n")
  861. f.write(make_heading("Signals", "-"))
  862. index = 0
  863. for signal in class_def.signals.values():
  864. if index != 0:
  865. f.write(make_separator())
  866. # Create signal signature and anchor point.
  867. signal_anchor = f"class_{sanitize_class_name(class_name)}_signal_{signal.name}"
  868. f.write(f".. _{signal_anchor}:\n\n")
  869. self_link = f":ref:`🔗<{signal_anchor}>`"
  870. f.write(".. rst-class:: classref-signal\n\n")
  871. _, signature = make_method_signature(class_def, signal, "", state)
  872. f.write(f"{signature} {self_link}\n\n")
  873. # Add signal description, or a call to action if it's missing.
  874. f.write(make_deprecated_experimental(signal, state))
  875. if signal.description is not None and signal.description.strip() != "":
  876. f.write(f"{format_text_block(signal.description.strip(), signal, state)}\n\n")
  877. elif signal.deprecated is None and signal.experimental is None:
  878. f.write(".. container:: contribute\n\n\t")
  879. f.write(
  880. translate(
  881. "There is currently no description for this signal. Please help us by `contributing one <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__!"
  882. )
  883. + "\n\n"
  884. )
  885. index += 1
  886. # Enumeration descriptions
  887. if len(class_def.enums) > 0:
  888. f.write(make_separator(True))
  889. f.write(".. rst-class:: classref-descriptions-group\n\n")
  890. f.write(make_heading("Enumerations", "-"))
  891. index = 0
  892. for e in class_def.enums.values():
  893. if index != 0:
  894. f.write(make_separator())
  895. # Create enumeration signature and anchor point.
  896. enum_anchor = f"enum_{sanitize_class_name(class_name)}_{e.name}"
  897. f.write(f".. _{enum_anchor}:\n\n")
  898. self_link = f":ref:`🔗<{enum_anchor}>`"
  899. f.write(".. rst-class:: classref-enumeration\n\n")
  900. if e.is_bitfield:
  901. f.write(f"flags **{e.name}**: {self_link}\n\n")
  902. else:
  903. f.write(f"enum **{e.name}**: {self_link}\n\n")
  904. for value in e.values.values():
  905. # Also create signature and anchor point for each enum constant.
  906. f.write(f".. _class_{sanitize_class_name(class_name)}_constant_{value.name}:\n\n")
  907. f.write(".. rst-class:: classref-enumeration-constant\n\n")
  908. f.write(f"{e.type_name.to_rst(state)} **{value.name}** = ``{value.value}``\n\n")
  909. # Add enum constant description.
  910. f.write(make_deprecated_experimental(value, state))
  911. if value.text is not None and value.text.strip() != "":
  912. f.write(f"{format_text_block(value.text.strip(), value, state)}")
  913. elif value.deprecated is None and value.experimental is None:
  914. f.write(".. container:: contribute\n\n\t")
  915. f.write(
  916. translate(
  917. "There is currently no description for this enum. Please help us by `contributing one <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__!"
  918. )
  919. + "\n\n"
  920. )
  921. f.write("\n\n")
  922. index += 1
  923. # Constant descriptions
  924. if len(class_def.constants) > 0:
  925. f.write(make_separator(True))
  926. f.write(".. rst-class:: classref-descriptions-group\n\n")
  927. f.write(make_heading("Constants", "-"))
  928. for constant in class_def.constants.values():
  929. # Create constant signature and anchor point.
  930. constant_anchor = f"class_{sanitize_class_name(class_name)}_constant_{constant.name}"
  931. f.write(f".. _{constant_anchor}:\n\n")
  932. self_link = f":ref:`🔗<{constant_anchor}>`"
  933. f.write(".. rst-class:: classref-constant\n\n")
  934. f.write(f"**{constant.name}** = ``{constant.value}`` {self_link}\n\n")
  935. # Add constant description.
  936. f.write(make_deprecated_experimental(constant, state))
  937. if constant.text is not None and constant.text.strip() != "":
  938. f.write(f"{format_text_block(constant.text.strip(), constant, state)}")
  939. elif constant.deprecated is None and constant.experimental is None:
  940. f.write(".. container:: contribute\n\n\t")
  941. f.write(
  942. translate(
  943. "There is currently no description for this constant. Please help us by `contributing one <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__!"
  944. )
  945. + "\n\n"
  946. )
  947. f.write("\n\n")
  948. # Annotation descriptions
  949. if len(class_def.annotations) > 0:
  950. f.write(make_separator(True))
  951. f.write(".. rst-class:: classref-descriptions-group\n\n")
  952. f.write(make_heading("Annotations", "-"))
  953. index = 0
  954. for method_list in class_def.annotations.values(): # type: ignore
  955. for i, m in enumerate(method_list):
  956. if index != 0:
  957. f.write(make_separator())
  958. # Create annotation signature and anchor point.
  959. self_link = ""
  960. if i == 0:
  961. annotation_anchor = f"class_{sanitize_class_name(class_name)}_annotation_{m.name}"
  962. f.write(f".. _{annotation_anchor}:\n\n")
  963. self_link = f" :ref:`🔗<{annotation_anchor}>`"
  964. f.write(".. rst-class:: classref-annotation\n\n")
  965. _, signature = make_method_signature(class_def, m, "", state)
  966. f.write(f"{signature}{self_link}\n\n")
  967. # Add annotation description, or a call to action if it's missing.
  968. if m.description is not None and m.description.strip() != "":
  969. f.write(f"{format_text_block(m.description.strip(), m, state)}\n\n")
  970. else:
  971. f.write(".. container:: contribute\n\n\t")
  972. f.write(
  973. translate(
  974. "There is currently no description for this annotation. Please help us by `contributing one <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__!"
  975. )
  976. + "\n\n"
  977. )
  978. index += 1
  979. # Property descriptions
  980. if any(not p.overrides for p in class_def.properties.values()) > 0:
  981. f.write(make_separator(True))
  982. f.write(".. rst-class:: classref-descriptions-group\n\n")
  983. f.write(make_heading("Property Descriptions", "-"))
  984. index = 0
  985. for property_def in class_def.properties.values():
  986. if property_def.overrides:
  987. continue
  988. if index != 0:
  989. f.write(make_separator())
  990. # Create property signature and anchor point.
  991. property_anchor = f"class_{sanitize_class_name(class_name)}_property_{property_def.name}"
  992. f.write(f".. _{property_anchor}:\n\n")
  993. self_link = f":ref:`🔗<{property_anchor}>`"
  994. f.write(".. rst-class:: classref-property\n\n")
  995. property_default = ""
  996. if property_def.default_value is not None:
  997. property_default = f" = {property_def.default_value}"
  998. f.write(
  999. f"{property_def.type_name.to_rst(state)} **{property_def.name}**{property_default} {self_link}\n\n"
  1000. )
  1001. # Create property setter and getter records.
  1002. property_setget = ""
  1003. if property_def.setter is not None and not property_def.setter.startswith("_"):
  1004. property_setter = make_setter_signature(class_def, property_def, state)
  1005. property_setget += f"- {property_setter}\n"
  1006. if property_def.getter is not None and not property_def.getter.startswith("_"):
  1007. property_getter = make_getter_signature(class_def, property_def, state)
  1008. property_setget += f"- {property_getter}\n"
  1009. if property_setget != "":
  1010. f.write(".. rst-class:: classref-property-setget\n\n")
  1011. f.write(property_setget)
  1012. f.write("\n")
  1013. # Add property description, or a call to action if it's missing.
  1014. f.write(make_deprecated_experimental(property_def, state))
  1015. if property_def.text is not None and property_def.text.strip() != "":
  1016. f.write(f"{format_text_block(property_def.text.strip(), property_def, state)}\n\n")
  1017. elif property_def.deprecated is None and property_def.experimental is None:
  1018. f.write(".. container:: contribute\n\n\t")
  1019. f.write(
  1020. translate(
  1021. "There is currently no description for this property. Please help us by `contributing one <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__!"
  1022. )
  1023. + "\n\n"
  1024. )
  1025. # Add copy note to built-in properties returning `Packed*Array`.
  1026. if property_def.type_name.type_name in PACKED_ARRAY_TYPES:
  1027. # See also `BASE_STRINGS` and `editor/doc/editor_help.cpp`.
  1028. copy_note = f"[b]Note:[/b] The returned array is [i]copied[/i] and any changes to it will not update the original property value. See [{property_def.type_name.type_name}] for more details."
  1029. f.write(f"{format_text_block(copy_note, property_def, state)}\n\n")
  1030. index += 1
  1031. # Constructor, Method, Operator descriptions
  1032. if len(class_def.constructors) > 0:
  1033. f.write(make_separator(True))
  1034. f.write(".. rst-class:: classref-descriptions-group\n\n")
  1035. f.write(make_heading("Constructor Descriptions", "-"))
  1036. index = 0
  1037. for method_list in class_def.constructors.values():
  1038. for i, m in enumerate(method_list):
  1039. if index != 0:
  1040. f.write(make_separator())
  1041. # Create constructor signature and anchor point.
  1042. self_link = ""
  1043. if i == 0:
  1044. constructor_anchor = f"class_{sanitize_class_name(class_name)}_constructor_{m.name}"
  1045. f.write(f".. _{constructor_anchor}:\n\n")
  1046. self_link = f" :ref:`🔗<{constructor_anchor}>`"
  1047. f.write(".. rst-class:: classref-constructor\n\n")
  1048. ret_type, signature = make_method_signature(class_def, m, "", state)
  1049. f.write(f"{ret_type} {signature}{self_link}\n\n")
  1050. # Add constructor description, or a call to action if it's missing.
  1051. f.write(make_deprecated_experimental(m, state))
  1052. if m.description is not None and m.description.strip() != "":
  1053. f.write(f"{format_text_block(m.description.strip(), m, state)}\n\n")
  1054. elif m.deprecated is None and m.experimental is None:
  1055. f.write(".. container:: contribute\n\n\t")
  1056. f.write(
  1057. translate(
  1058. "There is currently no description for this constructor. Please help us by `contributing one <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__!"
  1059. )
  1060. + "\n\n"
  1061. )
  1062. index += 1
  1063. if len(class_def.methods) > 0:
  1064. f.write(make_separator(True))
  1065. f.write(".. rst-class:: classref-descriptions-group\n\n")
  1066. f.write(make_heading("Method Descriptions", "-"))
  1067. index = 0
  1068. for method_list in class_def.methods.values():
  1069. for i, m in enumerate(method_list):
  1070. if index != 0:
  1071. f.write(make_separator())
  1072. # Create method signature and anchor point.
  1073. self_link = ""
  1074. if i == 0:
  1075. method_qualifier = ""
  1076. if m.name.startswith("_"):
  1077. method_qualifier = "private_"
  1078. method_anchor = f"class_{sanitize_class_name(class_name)}_{method_qualifier}method_{m.name}"
  1079. f.write(f".. _{method_anchor}:\n\n")
  1080. self_link = f" :ref:`🔗<{method_anchor}>`"
  1081. f.write(".. rst-class:: classref-method\n\n")
  1082. ret_type, signature = make_method_signature(class_def, m, "", state)
  1083. f.write(f"{ret_type} {signature}{self_link}\n\n")
  1084. # Add method description, or a call to action if it's missing.
  1085. f.write(make_deprecated_experimental(m, state))
  1086. if m.description is not None and m.description.strip() != "":
  1087. f.write(f"{format_text_block(m.description.strip(), m, state)}\n\n")
  1088. elif m.deprecated is None and m.experimental is None:
  1089. f.write(".. container:: contribute\n\n\t")
  1090. f.write(
  1091. translate(
  1092. "There is currently no description for this method. Please help us by `contributing one <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__!"
  1093. )
  1094. + "\n\n"
  1095. )
  1096. index += 1
  1097. if len(class_def.operators) > 0:
  1098. f.write(make_separator(True))
  1099. f.write(".. rst-class:: classref-descriptions-group\n\n")
  1100. f.write(make_heading("Operator Descriptions", "-"))
  1101. index = 0
  1102. for method_list in class_def.operators.values():
  1103. for i, m in enumerate(method_list):
  1104. if index != 0:
  1105. f.write(make_separator())
  1106. # Create operator signature and anchor point.
  1107. operator_anchor = (
  1108. f"class_{sanitize_class_name(class_name)}_operator_{sanitize_operator_name(m.name, state)}"
  1109. )
  1110. for parameter in m.parameters:
  1111. operator_anchor += f"_{parameter.type_name.type_name}"
  1112. f.write(f".. _{operator_anchor}:\n\n")
  1113. self_link = f":ref:`🔗<{operator_anchor}>`"
  1114. f.write(".. rst-class:: classref-operator\n\n")
  1115. ret_type, signature = make_method_signature(class_def, m, "", state)
  1116. f.write(f"{ret_type} {signature} {self_link}\n\n")
  1117. # Add operator description, or a call to action if it's missing.
  1118. f.write(make_deprecated_experimental(m, state))
  1119. if m.description is not None and m.description.strip() != "":
  1120. f.write(f"{format_text_block(m.description.strip(), m, state)}\n\n")
  1121. elif m.deprecated is None and m.experimental is None:
  1122. f.write(".. container:: contribute\n\n\t")
  1123. f.write(
  1124. translate(
  1125. "There is currently no description for this operator. Please help us by `contributing one <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__!"
  1126. )
  1127. + "\n\n"
  1128. )
  1129. index += 1
  1130. # Theme property descriptions
  1131. if len(class_def.theme_items) > 0:
  1132. f.write(make_separator(True))
  1133. f.write(".. rst-class:: classref-descriptions-group\n\n")
  1134. f.write(make_heading("Theme Property Descriptions", "-"))
  1135. index = 0
  1136. for theme_item_def in class_def.theme_items.values():
  1137. if index != 0:
  1138. f.write(make_separator())
  1139. # Create theme property signature and anchor point.
  1140. theme_item_anchor = (
  1141. f"class_{sanitize_class_name(class_name)}_theme_{theme_item_def.data_name}_{theme_item_def.name}"
  1142. )
  1143. f.write(f".. _{theme_item_anchor}:\n\n")
  1144. self_link = f":ref:`🔗<{theme_item_anchor}>`"
  1145. f.write(".. rst-class:: classref-themeproperty\n\n")
  1146. theme_item_default = ""
  1147. if theme_item_def.default_value is not None:
  1148. theme_item_default = f" = {theme_item_def.default_value}"
  1149. f.write(
  1150. f"{theme_item_def.type_name.to_rst(state)} **{theme_item_def.name}**{theme_item_default} {self_link}\n\n"
  1151. )
  1152. # Add theme property description, or a call to action if it's missing.
  1153. f.write(make_deprecated_experimental(theme_item_def, state))
  1154. if theme_item_def.text is not None and theme_item_def.text.strip() != "":
  1155. f.write(f"{format_text_block(theme_item_def.text.strip(), theme_item_def, state)}\n\n")
  1156. elif theme_item_def.deprecated is None and theme_item_def.experimental is None:
  1157. f.write(".. container:: contribute\n\n\t")
  1158. f.write(
  1159. translate(
  1160. "There is currently no description for this theme property. Please help us by `contributing one <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__!"
  1161. )
  1162. + "\n\n"
  1163. )
  1164. index += 1
  1165. f.write(make_footer())
  1166. def make_type(klass: str, state: State) -> str:
  1167. if klass.find("*") != -1: # Pointer, ignore
  1168. return f"``{klass}``"
  1169. def resolve_type(link_type: str) -> str:
  1170. if link_type in state.classes:
  1171. return f":ref:`{link_type}<class_{sanitize_class_name(link_type)}>`"
  1172. else:
  1173. print_error(f'{state.current_class}.xml: Unresolved type "{link_type}".', state)
  1174. return f"``{link_type}``"
  1175. if klass.endswith("[]"): # Typed array, strip [] to link to contained type.
  1176. return f":ref:`Array<class_Array>`\\[{resolve_type(klass[: -len('[]')])}\\]"
  1177. if klass.startswith("Dictionary["): # Typed dictionary, split elements to link contained types.
  1178. parts = klass[len("Dictionary[") : -len("]")].partition(", ")
  1179. key = parts[0]
  1180. value = parts[2]
  1181. return f":ref:`Dictionary<class_Dictionary>`\\[{resolve_type(key)}, {resolve_type(value)}\\]"
  1182. return resolve_type(klass)
  1183. def make_enum(t: str, is_bitfield: bool, state: State) -> str:
  1184. p = t.rfind(".")
  1185. if p >= 0:
  1186. c = t[0:p]
  1187. e = t[p + 1 :]
  1188. # Variant enums live in GlobalScope but still use periods.
  1189. if c == "Variant":
  1190. c = "@GlobalScope"
  1191. e = "Variant." + e
  1192. else:
  1193. c = state.current_class
  1194. e = t
  1195. if c in state.classes and e not in state.classes[c].enums:
  1196. c = "@GlobalScope"
  1197. if c in state.classes and e in state.classes[c].enums:
  1198. if is_bitfield:
  1199. if not state.classes[c].enums[e].is_bitfield:
  1200. print_error(f'{state.current_class}.xml: Enum "{t}" is not bitfield.', state)
  1201. return f"|bitfield|\\[:ref:`{e}<enum_{sanitize_class_name(c)}_{e}>`\\]"
  1202. else:
  1203. return f":ref:`{e}<enum_{sanitize_class_name(c)}_{e}>`"
  1204. print_error(f'{state.current_class}.xml: Unresolved enum "{t}".', state)
  1205. return t
  1206. def make_method_signature(
  1207. class_def: ClassDef, definition: AnnotationDef | MethodDef | SignalDef, ref_type: str, state: State
  1208. ) -> tuple[str, str]:
  1209. ret_type = ""
  1210. if isinstance(definition, MethodDef):
  1211. ret_type = definition.return_type.to_rst(state)
  1212. qualifiers = None
  1213. if isinstance(definition, (MethodDef, AnnotationDef)):
  1214. qualifiers = definition.qualifiers
  1215. out = ""
  1216. if isinstance(definition, MethodDef) and ref_type != "":
  1217. if ref_type == "operator":
  1218. op_name = definition.name.replace("<", "\\<") # So operator "<" gets correctly displayed.
  1219. out += f":ref:`{op_name}<class_{sanitize_class_name(class_def.name)}_{ref_type}_{sanitize_operator_name(definition.name, state)}"
  1220. for parameter in definition.parameters:
  1221. out += f"_{parameter.type_name.type_name}"
  1222. out += ">`"
  1223. elif ref_type == "method":
  1224. ref_type_qualifier = ""
  1225. if definition.name.startswith("_"):
  1226. ref_type_qualifier = "private_"
  1227. out += f":ref:`{definition.name}<class_{sanitize_class_name(class_def.name)}_{ref_type_qualifier}{ref_type}_{definition.name}>`"
  1228. else:
  1229. out += f":ref:`{definition.name}<class_{sanitize_class_name(class_def.name)}_{ref_type}_{definition.name}>`"
  1230. else:
  1231. out += f"**{definition.name}**"
  1232. out += "\\ ("
  1233. for i, arg in enumerate(definition.parameters):
  1234. if i > 0:
  1235. out += ", "
  1236. else:
  1237. out += "\\ "
  1238. out += f"{arg.name}\\: {arg.type_name.to_rst(state)}"
  1239. if arg.default_value is not None:
  1240. out += f" = {arg.default_value}"
  1241. if qualifiers is not None and "vararg" in qualifiers:
  1242. if len(definition.parameters) > 0:
  1243. out += ", ..."
  1244. else:
  1245. out += "\\ ..."
  1246. out += "\\ )"
  1247. if qualifiers is not None:
  1248. # Use substitutions for abbreviations. This is used to display tooltips on hover.
  1249. # See `make_footer()` for descriptions.
  1250. for qualifier in qualifiers.split():
  1251. out += f" |{qualifier}|"
  1252. return ret_type, out
  1253. def make_setter_signature(class_def: ClassDef, property_def: PropertyDef, state: State) -> str:
  1254. if property_def.setter is None:
  1255. return ""
  1256. # If setter is a method available as a method definition, we use that.
  1257. if property_def.setter in class_def.methods:
  1258. setter = class_def.methods[property_def.setter][0]
  1259. # Otherwise we fake it with the information we have available.
  1260. else:
  1261. setter_params: list[ParameterDef] = []
  1262. setter_params.append(ParameterDef("value", property_def.type_name, None))
  1263. setter = MethodDef(property_def.setter, TypeName("void"), setter_params, None, None)
  1264. ret_type, signature = make_method_signature(class_def, setter, "", state)
  1265. return f"{ret_type} {signature}"
  1266. def make_getter_signature(class_def: ClassDef, property_def: PropertyDef, state: State) -> str:
  1267. if property_def.getter is None:
  1268. return ""
  1269. # If getter is a method available as a method definition, we use that.
  1270. if property_def.getter in class_def.methods:
  1271. getter = class_def.methods[property_def.getter][0]
  1272. # Otherwise we fake it with the information we have available.
  1273. else:
  1274. getter_params: list[ParameterDef] = []
  1275. getter = MethodDef(property_def.getter, property_def.type_name, getter_params, None, None)
  1276. ret_type, signature = make_method_signature(class_def, getter, "", state)
  1277. return f"{ret_type} {signature}"
  1278. def make_deprecated_experimental(item: DefinitionBase, state: State) -> str:
  1279. result = ""
  1280. if item.deprecated is not None:
  1281. deprecated_prefix = translate("Deprecated:")
  1282. if item.deprecated.strip() == "":
  1283. default_message = translate(f"This {item.definition_name} may be changed or removed in future versions.")
  1284. result += f"**{deprecated_prefix}** {default_message}\n\n"
  1285. else:
  1286. result += f"**{deprecated_prefix}** {format_text_block(item.deprecated.strip(), item, state)}\n\n"
  1287. if item.experimental is not None:
  1288. experimental_prefix = translate("Experimental:")
  1289. if item.experimental.strip() == "":
  1290. default_message = translate(f"This {item.definition_name} may be changed or removed in future versions.")
  1291. result += f"**{experimental_prefix}** {default_message}\n\n"
  1292. else:
  1293. result += f"**{experimental_prefix}** {format_text_block(item.experimental.strip(), item, state)}\n\n"
  1294. return result
  1295. def make_heading(title: str, underline: str, l10n: bool = True) -> str:
  1296. if l10n:
  1297. new_title = translate(title)
  1298. if new_title != title:
  1299. title = new_title
  1300. underline *= 2 # Double length to handle wide chars.
  1301. return f"{title}\n{(underline * len(title))}\n\n"
  1302. def make_footer() -> str:
  1303. # Generate reusable abbreviation substitutions.
  1304. # This way, we avoid bloating the generated rST with duplicate abbreviations.
  1305. virtual_msg = translate("This method should typically be overridden by the user to have any effect.")
  1306. required_msg = translate("This method is required to be overridden when extending its base class.")
  1307. const_msg = translate("This method has no side effects. It doesn't modify any of the instance's member variables.")
  1308. vararg_msg = translate("This method accepts any number of arguments after the ones described here.")
  1309. constructor_msg = translate("This method is used to construct a type.")
  1310. static_msg = translate(
  1311. "This method doesn't need an instance to be called, so it can be called directly using the class name."
  1312. )
  1313. operator_msg = translate("This method describes a valid operator to use with this type as left-hand operand.")
  1314. bitfield_msg = translate("This value is an integer composed as a bitmask of the following flags.")
  1315. void_msg = translate("No return value.")
  1316. return (
  1317. f".. |virtual| replace:: :abbr:`virtual ({virtual_msg})`\n"
  1318. f".. |required| replace:: :abbr:`required ({required_msg})`\n"
  1319. f".. |const| replace:: :abbr:`const ({const_msg})`\n"
  1320. f".. |vararg| replace:: :abbr:`vararg ({vararg_msg})`\n"
  1321. f".. |constructor| replace:: :abbr:`constructor ({constructor_msg})`\n"
  1322. f".. |static| replace:: :abbr:`static ({static_msg})`\n"
  1323. f".. |operator| replace:: :abbr:`operator ({operator_msg})`\n"
  1324. f".. |bitfield| replace:: :abbr:`BitField ({bitfield_msg})`\n"
  1325. f".. |void| replace:: :abbr:`void ({void_msg})`\n"
  1326. )
  1327. def make_separator(section_level: bool = False) -> str:
  1328. separator_class = "item"
  1329. if section_level:
  1330. separator_class = "section"
  1331. return f".. rst-class:: classref-{separator_class}-separator\n\n----\n\n"
  1332. def make_link(url: str, title: str) -> str:
  1333. match = GODOT_DOCS_PATTERN.search(url)
  1334. if match:
  1335. groups = match.groups()
  1336. if match.lastindex == 2:
  1337. # Doc reference with fragment identifier: emit direct link to section with reference to page, for example:
  1338. # `#calling-javascript-from-script in Exporting For Web`
  1339. # Or use the title if provided.
  1340. if title != "":
  1341. return f"`{title} <../{groups[0]}.html{groups[1]}>`__"
  1342. return f"`{groups[1]} <../{groups[0]}.html{groups[1]}>`__ in :doc:`../{groups[0]}`"
  1343. elif match.lastindex == 1:
  1344. # Doc reference, for example:
  1345. # `Math`
  1346. if title != "":
  1347. return f":doc:`{title} <../{groups[0]}>`"
  1348. return f":doc:`../{groups[0]}`"
  1349. # External link, for example:
  1350. # `http://enet.bespin.org/usergroup0.html`
  1351. if title != "":
  1352. return f"`{title} <{url}>`__"
  1353. return f"`{url} <{url}>`__"
  1354. def make_rst_index(grouped_classes: dict[str, list[str]], dry_run: bool, output_dir: str) -> None:
  1355. with open(
  1356. os.devnull if dry_run else os.path.join(output_dir, "index.rst"), "w", encoding="utf-8", newline="\n"
  1357. ) as f:
  1358. # Remove the "Edit on Github" button from the online docs page, and disallow user-contributed notes
  1359. # on the index page. User-contributed notes are allowed on individual class pages.
  1360. f.write(":github_url: hide\n:allow_comments: False\n\n")
  1361. # Warn contributors not to edit this file directly.
  1362. # Also provide links to the source files for reference.
  1363. git_branch = get_git_branch()
  1364. generator_github_url = f"https://github.com/godotengine/godot/tree/{git_branch}/doc/tools/make_rst.py"
  1365. f.write(".. DO NOT EDIT THIS FILE!!!\n")
  1366. f.write(".. Generated automatically from Godot engine sources.\n")
  1367. f.write(f".. Generator: {generator_github_url}.\n\n")
  1368. f.write(".. _doc_class_reference:\n\n")
  1369. f.write(make_heading("All classes", "="))
  1370. for group_name in CLASS_GROUPS:
  1371. if group_name in grouped_classes:
  1372. f.write(make_heading(CLASS_GROUPS[group_name], "="))
  1373. f.write(".. toctree::\n")
  1374. f.write(" :maxdepth: 1\n")
  1375. f.write(f" :name: toc-class-ref-{group_name}s\n")
  1376. f.write("\n")
  1377. if group_name in CLASS_GROUPS_BASE:
  1378. f.write(f" class_{sanitize_class_name(CLASS_GROUPS_BASE[group_name], True)}\n")
  1379. for class_name in grouped_classes[group_name]:
  1380. if group_name in CLASS_GROUPS_BASE and sanitize_class_name(
  1381. CLASS_GROUPS_BASE[group_name], True
  1382. ) == sanitize_class_name(class_name, True):
  1383. continue
  1384. f.write(f" class_{sanitize_class_name(class_name, True)}\n")
  1385. f.write("\n")
  1386. # Formatting helpers.
  1387. RESERVED_FORMATTING_TAGS = ["i", "b", "u", "lb", "rb", "code", "kbd", "center", "url", "br"]
  1388. RESERVED_LAYOUT_TAGS = ["codeblocks"]
  1389. RESERVED_CODEBLOCK_TAGS = ["codeblock", "gdscript", "csharp"]
  1390. RESERVED_CROSSLINK_TAGS = [
  1391. "method",
  1392. "constructor",
  1393. "operator",
  1394. "member",
  1395. "signal",
  1396. "constant",
  1397. "enum",
  1398. "annotation",
  1399. "theme_item",
  1400. "param",
  1401. ]
  1402. def is_in_tagset(tag_text: str, tagset: list[str]) -> bool:
  1403. for tag in tagset:
  1404. # Complete match.
  1405. if tag_text == tag:
  1406. return True
  1407. # Tag with arguments.
  1408. if tag_text.startswith(tag + " "):
  1409. return True
  1410. # Tag with arguments, special case for [url], [color], and [font].
  1411. if tag_text.startswith(tag + "="):
  1412. return True
  1413. return False
  1414. def get_tag_and_args(tag_text: str) -> TagState:
  1415. tag_name = tag_text
  1416. arguments: str = ""
  1417. delim_pos = -1
  1418. space_pos = tag_text.find(" ")
  1419. if space_pos >= 0:
  1420. delim_pos = space_pos
  1421. # Special case for [url], [color], and [font].
  1422. assign_pos = tag_text.find("=")
  1423. if assign_pos >= 0 and (delim_pos < 0 or assign_pos < delim_pos):
  1424. delim_pos = assign_pos
  1425. if delim_pos >= 0:
  1426. tag_name = tag_text[:delim_pos]
  1427. arguments = tag_text[delim_pos + 1 :].strip()
  1428. closing = False
  1429. if tag_name.startswith("/"):
  1430. tag_name = tag_name[1:]
  1431. closing = True
  1432. return TagState(tag_text, tag_name, arguments, closing)
  1433. def parse_link_target(link_target: str, state: State, context_name: str) -> list[str]:
  1434. if link_target.find(".") != -1:
  1435. return link_target.split(".")
  1436. else:
  1437. return [state.current_class, link_target]
  1438. def format_text_block(
  1439. text: str,
  1440. context: DefinitionBase,
  1441. state: State,
  1442. ) -> str:
  1443. result = preformat_text_block(text, state)
  1444. if result is None:
  1445. return ""
  1446. text = result
  1447. next_brac_pos = text.find("[")
  1448. text = escape_rst(text, next_brac_pos)
  1449. context_name = format_context_name(context)
  1450. # Handle [tags]
  1451. inside_code = False
  1452. inside_code_tag = ""
  1453. inside_code_tabs = False
  1454. ignore_code_warnings = False
  1455. code_warning_if_intended_string = "If this is intended, use [code skip-lint]...[/code]."
  1456. has_codeblocks_gdscript = False
  1457. has_codeblocks_csharp = False
  1458. pos = 0
  1459. tag_depth = 0
  1460. while True:
  1461. pos = text.find("[", pos)
  1462. if pos == -1:
  1463. break
  1464. endq_pos = text.find("]", pos + 1)
  1465. if endq_pos == -1:
  1466. break
  1467. pre_text = text[:pos]
  1468. post_text = text[endq_pos + 1 :]
  1469. tag_text = text[pos + 1 : endq_pos]
  1470. escape_pre = False
  1471. escape_post = False
  1472. # Tag is a reference to a class.
  1473. if tag_text in state.classes and not inside_code:
  1474. if tag_text == state.current_class:
  1475. # Don't create a link to the same class, format it as strong emphasis.
  1476. tag_text = f"**{tag_text}**"
  1477. else:
  1478. tag_text = make_type(tag_text, state)
  1479. escape_pre = True
  1480. escape_post = True
  1481. # Tag is a cross-reference or a formatting directive.
  1482. else:
  1483. tag_state = get_tag_and_args(tag_text)
  1484. # Anything identified as a tag inside of a code block is valid,
  1485. # unless it's a matching closing tag.
  1486. if inside_code:
  1487. # Exiting codeblocks and inline code tags.
  1488. if tag_state.closing and tag_state.name == inside_code_tag:
  1489. if is_in_tagset(tag_state.name, RESERVED_CODEBLOCK_TAGS):
  1490. tag_text = ""
  1491. tag_depth -= 1
  1492. inside_code = False
  1493. ignore_code_warnings = False
  1494. # Strip newline if the tag was alone on one
  1495. if pre_text[-1] == "\n":
  1496. pre_text = pre_text[:-1]
  1497. elif is_in_tagset(tag_state.name, ["code"]):
  1498. tag_text = "``"
  1499. tag_depth -= 1
  1500. inside_code = False
  1501. ignore_code_warnings = False
  1502. escape_post = True
  1503. else:
  1504. if not ignore_code_warnings and tag_state.closing:
  1505. print_warning(
  1506. f'{state.current_class}.xml: Found a code string that looks like a closing tag "[{tag_state.raw}]" in {context_name}. {code_warning_if_intended_string}',
  1507. state,
  1508. )
  1509. tag_text = f"[{tag_text}]"
  1510. # Entering codeblocks and inline code tags.
  1511. elif tag_state.name == "codeblocks":
  1512. if tag_state.closing:
  1513. if not has_codeblocks_gdscript or not has_codeblocks_csharp:
  1514. state.script_language_parity_check.add_hit(
  1515. state.current_class,
  1516. context,
  1517. "Only one script language sample found in [codeblocks]",
  1518. state,
  1519. )
  1520. has_codeblocks_gdscript = False
  1521. has_codeblocks_csharp = False
  1522. tag_depth -= 1
  1523. tag_text = ""
  1524. inside_code_tabs = False
  1525. else:
  1526. tag_depth += 1
  1527. tag_text = "\n.. tabs::"
  1528. inside_code_tabs = True
  1529. elif is_in_tagset(tag_state.name, RESERVED_CODEBLOCK_TAGS):
  1530. tag_depth += 1
  1531. if tag_state.name == "gdscript":
  1532. if not inside_code_tabs:
  1533. print_error(
  1534. f"{state.current_class}.xml: GDScript code block is used outside of [codeblocks] in {context_name}.",
  1535. state,
  1536. )
  1537. else:
  1538. has_codeblocks_gdscript = True
  1539. tag_text = "\n .. code-tab:: gdscript\n"
  1540. elif tag_state.name == "csharp":
  1541. if not inside_code_tabs:
  1542. print_error(
  1543. f"{state.current_class}.xml: C# code block is used outside of [codeblocks] in {context_name}.",
  1544. state,
  1545. )
  1546. else:
  1547. has_codeblocks_csharp = True
  1548. tag_text = "\n .. code-tab:: csharp\n"
  1549. else:
  1550. state.script_language_parity_check.add_hit(
  1551. state.current_class,
  1552. context,
  1553. "Code sample is formatted with [codeblock] where [codeblocks] should be used",
  1554. state,
  1555. )
  1556. if "lang=text" in tag_state.arguments.split(" "):
  1557. tag_text = "\n.. code:: text\n"
  1558. else:
  1559. tag_text = "\n::\n"
  1560. inside_code = True
  1561. inside_code_tag = tag_state.name
  1562. ignore_code_warnings = "skip-lint" in tag_state.arguments.split(" ")
  1563. elif is_in_tagset(tag_state.name, ["code"]):
  1564. tag_text = "``"
  1565. tag_depth += 1
  1566. inside_code = True
  1567. inside_code_tag = "code"
  1568. ignore_code_warnings = "skip-lint" in tag_state.arguments.split(" ")
  1569. escape_pre = True
  1570. if not ignore_code_warnings:
  1571. endcode_pos = text.find("[/code]", endq_pos + 1)
  1572. if endcode_pos == -1:
  1573. print_error(
  1574. f"{state.current_class}.xml: Tag depth mismatch for [code]: no closing [/code] in {context_name}.",
  1575. state,
  1576. )
  1577. break
  1578. inside_code_text = text[endq_pos + 1 : endcode_pos]
  1579. if inside_code_text.endswith("()"):
  1580. # It's formatted like a call for some reason, may still be a mistake.
  1581. inside_code_text = inside_code_text[:-2]
  1582. if inside_code_text in state.classes:
  1583. print_warning(
  1584. f'{state.current_class}.xml: Found a code string "{inside_code_text}" that matches one of the known classes in {context_name}. {code_warning_if_intended_string}',
  1585. state,
  1586. )
  1587. target_class_name, target_name, *rest = parse_link_target(inside_code_text, state, context_name)
  1588. if len(rest) == 0 and target_class_name in state.classes:
  1589. class_def = state.classes[target_class_name]
  1590. if target_name in class_def.methods:
  1591. print_warning(
  1592. f'{state.current_class}.xml: Found a code string "{inside_code_text}" that matches the {target_class_name}.{target_name} method in {context_name}. {code_warning_if_intended_string}',
  1593. state,
  1594. )
  1595. elif target_name in class_def.constructors:
  1596. print_warning(
  1597. f'{state.current_class}.xml: Found a code string "{inside_code_text}" that matches the {target_class_name}.{target_name} constructor in {context_name}. {code_warning_if_intended_string}',
  1598. state,
  1599. )
  1600. elif target_name in class_def.operators:
  1601. print_warning(
  1602. f'{state.current_class}.xml: Found a code string "{inside_code_text}" that matches the {target_class_name}.{target_name} operator in {context_name}. {code_warning_if_intended_string}',
  1603. state,
  1604. )
  1605. elif target_name in class_def.properties:
  1606. print_warning(
  1607. f'{state.current_class}.xml: Found a code string "{inside_code_text}" that matches the {target_class_name}.{target_name} member in {context_name}. {code_warning_if_intended_string}',
  1608. state,
  1609. )
  1610. elif target_name in class_def.signals:
  1611. print_warning(
  1612. f'{state.current_class}.xml: Found a code string "{inside_code_text}" that matches the {target_class_name}.{target_name} signal in {context_name}. {code_warning_if_intended_string}',
  1613. state,
  1614. )
  1615. elif target_name in class_def.annotations:
  1616. print_warning(
  1617. f'{state.current_class}.xml: Found a code string "{inside_code_text}" that matches the {target_class_name}.{target_name} annotation in {context_name}. {code_warning_if_intended_string}',
  1618. state,
  1619. )
  1620. elif target_name in class_def.theme_items:
  1621. print_warning(
  1622. f'{state.current_class}.xml: Found a code string "{inside_code_text}" that matches the {target_class_name}.{target_name} theme property in {context_name}. {code_warning_if_intended_string}',
  1623. state,
  1624. )
  1625. elif target_name in class_def.constants:
  1626. print_warning(
  1627. f'{state.current_class}.xml: Found a code string "{inside_code_text}" that matches the {target_class_name}.{target_name} constant in {context_name}. {code_warning_if_intended_string}',
  1628. state,
  1629. )
  1630. else:
  1631. for enum in class_def.enums.values():
  1632. if target_name in enum.values:
  1633. print_warning(
  1634. f'{state.current_class}.xml: Found a code string "{inside_code_text}" that matches the {target_class_name}.{target_name} enum value in {context_name}. {code_warning_if_intended_string}',
  1635. state,
  1636. )
  1637. break
  1638. valid_param_context = isinstance(context, (MethodDef, SignalDef, AnnotationDef))
  1639. if valid_param_context:
  1640. context_params: list[ParameterDef] = context.parameters # type: ignore
  1641. for param_def in context_params:
  1642. if param_def.name == inside_code_text:
  1643. print_warning(
  1644. f'{state.current_class}.xml: Found a code string "{inside_code_text}" that matches one of the parameters in {context_name}. {code_warning_if_intended_string}',
  1645. state,
  1646. )
  1647. break
  1648. # Cross-references to items in this or other class documentation pages.
  1649. elif is_in_tagset(tag_state.name, RESERVED_CROSSLINK_TAGS):
  1650. link_target: str = tag_state.arguments
  1651. if link_target == "":
  1652. print_error(
  1653. f'{state.current_class}.xml: Empty cross-reference link "[{tag_state.raw}]" in {context_name}.',
  1654. state,
  1655. )
  1656. tag_text = ""
  1657. else:
  1658. if (
  1659. tag_state.name == "method"
  1660. or tag_state.name == "constructor"
  1661. or tag_state.name == "operator"
  1662. or tag_state.name == "member"
  1663. or tag_state.name == "signal"
  1664. or tag_state.name == "annotation"
  1665. or tag_state.name == "theme_item"
  1666. or tag_state.name == "constant"
  1667. ):
  1668. target_class_name, target_name, *rest = parse_link_target(link_target, state, context_name)
  1669. if len(rest) > 0:
  1670. print_error(
  1671. f'{state.current_class}.xml: Bad reference "{link_target}" in {context_name}.',
  1672. state,
  1673. )
  1674. # Default to the tag command name. This works by default for most tags,
  1675. # but method, member, and theme_item have special cases.
  1676. ref_type = "_{}".format(tag_state.name)
  1677. if target_class_name in state.classes:
  1678. class_def = state.classes[target_class_name]
  1679. if tag_state.name == "method":
  1680. if target_name.startswith("_"):
  1681. ref_type = "_private_method"
  1682. if target_name not in class_def.methods:
  1683. print_error(
  1684. f'{state.current_class}.xml: Unresolved method reference "{link_target}" in {context_name}.',
  1685. state,
  1686. )
  1687. elif tag_state.name == "constructor" and target_name not in class_def.constructors:
  1688. print_error(
  1689. f'{state.current_class}.xml: Unresolved constructor reference "{link_target}" in {context_name}.',
  1690. state,
  1691. )
  1692. elif tag_state.name == "operator" and target_name not in class_def.operators:
  1693. print_error(
  1694. f'{state.current_class}.xml: Unresolved operator reference "{link_target}" in {context_name}.',
  1695. state,
  1696. )
  1697. elif tag_state.name == "member":
  1698. ref_type = "_property"
  1699. if target_name not in class_def.properties:
  1700. print_error(
  1701. f'{state.current_class}.xml: Unresolved member reference "{link_target}" in {context_name}.',
  1702. state,
  1703. )
  1704. elif class_def.properties[target_name].overrides is not None:
  1705. print_error(
  1706. f'{state.current_class}.xml: Invalid member reference "{link_target}" in {context_name}. The reference must point to the original definition, not to the override.',
  1707. state,
  1708. )
  1709. elif tag_state.name == "signal" and target_name not in class_def.signals:
  1710. print_error(
  1711. f'{state.current_class}.xml: Unresolved signal reference "{link_target}" in {context_name}.',
  1712. state,
  1713. )
  1714. elif tag_state.name == "annotation" and target_name not in class_def.annotations:
  1715. print_error(
  1716. f'{state.current_class}.xml: Unresolved annotation reference "{link_target}" in {context_name}.',
  1717. state,
  1718. )
  1719. elif tag_state.name == "theme_item":
  1720. if target_name not in class_def.theme_items:
  1721. print_error(
  1722. f'{state.current_class}.xml: Unresolved theme property reference "{link_target}" in {context_name}.',
  1723. state,
  1724. )
  1725. else:
  1726. # Needs theme data type to be properly linked, which we cannot get without a class.
  1727. name = class_def.theme_items[target_name].data_name
  1728. ref_type = f"_theme_{name}"
  1729. elif tag_state.name == "constant":
  1730. found = False
  1731. # Search in the current class
  1732. search_class_defs = [class_def]
  1733. if link_target.find(".") == -1:
  1734. # Also search in @GlobalScope as a last resort if no class was specified
  1735. search_class_defs.append(state.classes["@GlobalScope"])
  1736. for search_class_def in search_class_defs:
  1737. if target_name in search_class_def.constants:
  1738. target_class_name = search_class_def.name
  1739. found = True
  1740. else:
  1741. for enum in search_class_def.enums.values():
  1742. if target_name in enum.values:
  1743. target_class_name = search_class_def.name
  1744. found = True
  1745. break
  1746. if not found:
  1747. print_error(
  1748. f'{state.current_class}.xml: Unresolved constant reference "{link_target}" in {context_name}.',
  1749. state,
  1750. )
  1751. else:
  1752. print_error(
  1753. f'{state.current_class}.xml: Unresolved type reference "{target_class_name}" in method reference "{link_target}" in {context_name}.',
  1754. state,
  1755. )
  1756. repl_text = target_name
  1757. if target_class_name != state.current_class:
  1758. repl_text = f"{target_class_name}.{target_name}"
  1759. if tag_state.name == "method":
  1760. repl_text = f"{repl_text}()"
  1761. tag_text = f":ref:`{repl_text}<class_{sanitize_class_name(target_class_name)}{ref_type}_{target_name}>`"
  1762. escape_pre = True
  1763. escape_post = True
  1764. elif tag_state.name == "enum":
  1765. tag_text = make_enum(link_target, False, state)
  1766. escape_pre = True
  1767. escape_post = True
  1768. elif tag_state.name == "param":
  1769. valid_param_context = isinstance(context, (MethodDef, SignalDef, AnnotationDef))
  1770. if not valid_param_context:
  1771. print_error(
  1772. f'{state.current_class}.xml: Argument reference "{link_target}" used outside of method, signal, or annotation context in {context_name}.',
  1773. state,
  1774. )
  1775. else:
  1776. context_params: list[ParameterDef] = context.parameters # type: ignore
  1777. found = False
  1778. for param_def in context_params:
  1779. if param_def.name == link_target:
  1780. found = True
  1781. break
  1782. if not found:
  1783. print_error(
  1784. f'{state.current_class}.xml: Unresolved argument reference "{link_target}" in {context_name}.',
  1785. state,
  1786. )
  1787. tag_text = f"``{link_target}``"
  1788. escape_pre = True
  1789. escape_post = True
  1790. # Formatting directives.
  1791. elif is_in_tagset(tag_state.name, ["url"]):
  1792. url_target = tag_state.arguments
  1793. if url_target == "":
  1794. print_error(
  1795. f'{state.current_class}.xml: Misformatted [url] tag "[{tag_state.raw}]" in {context_name}.',
  1796. state,
  1797. )
  1798. else:
  1799. # Unlike other tags, URLs are handled in full here, as we need to extract
  1800. # the optional link title to use `make_link`.
  1801. endurl_pos = text.find("[/url]", endq_pos + 1)
  1802. if endurl_pos == -1:
  1803. print_error(
  1804. f"{state.current_class}.xml: Tag depth mismatch for [url]: no closing [/url] in {context_name}.",
  1805. state,
  1806. )
  1807. break
  1808. link_title = text[endq_pos + 1 : endurl_pos]
  1809. tag_text = make_link(url_target, link_title)
  1810. pre_text = text[:pos]
  1811. post_text = text[endurl_pos + 6 :]
  1812. if pre_text and pre_text[-1] not in MARKUP_ALLOWED_PRECEDENT:
  1813. pre_text += "\\ "
  1814. if post_text and post_text[0] not in MARKUP_ALLOWED_SUBSEQUENT:
  1815. post_text = "\\ " + post_text
  1816. text = pre_text + tag_text + post_text
  1817. pos = len(pre_text) + len(tag_text)
  1818. continue
  1819. elif tag_state.name == "br":
  1820. # Make a new paragraph instead of a linebreak, rst is not so linebreak friendly
  1821. tag_text = "\n\n"
  1822. # Strip potential leading spaces
  1823. while post_text[0] == " ":
  1824. post_text = post_text[1:]
  1825. elif tag_state.name == "center":
  1826. if tag_state.closing:
  1827. tag_depth -= 1
  1828. else:
  1829. tag_depth += 1
  1830. tag_text = ""
  1831. elif tag_state.name == "i":
  1832. if tag_state.closing:
  1833. tag_depth -= 1
  1834. escape_post = True
  1835. else:
  1836. tag_depth += 1
  1837. escape_pre = True
  1838. tag_text = "*"
  1839. elif tag_state.name == "b":
  1840. if tag_state.closing:
  1841. tag_depth -= 1
  1842. escape_post = True
  1843. else:
  1844. tag_depth += 1
  1845. escape_pre = True
  1846. tag_text = "**"
  1847. elif tag_state.name == "u":
  1848. if tag_state.closing:
  1849. tag_depth -= 1
  1850. escape_post = True
  1851. else:
  1852. tag_depth += 1
  1853. escape_pre = True
  1854. tag_text = ""
  1855. elif tag_state.name == "lb":
  1856. tag_text = "\\["
  1857. elif tag_state.name == "rb":
  1858. tag_text = "\\]"
  1859. elif tag_state.name == "kbd":
  1860. tag_text = "`"
  1861. if tag_state.closing:
  1862. tag_depth -= 1
  1863. escape_post = True
  1864. else:
  1865. tag_text = ":kbd:" + tag_text
  1866. tag_depth += 1
  1867. escape_pre = True
  1868. # Invalid syntax.
  1869. else:
  1870. if tag_state.closing:
  1871. print_error(
  1872. f'{state.current_class}.xml: Unrecognized closing tag "[{tag_state.raw}]" in {context_name}.',
  1873. state,
  1874. )
  1875. tag_text = f"[{tag_text}]"
  1876. else:
  1877. print_error(
  1878. f'{state.current_class}.xml: Unrecognized opening tag "[{tag_state.raw}]" in {context_name}.',
  1879. state,
  1880. )
  1881. tag_text = f"``{tag_text}``"
  1882. escape_pre = True
  1883. escape_post = True
  1884. # Properly escape things like `[Node]s`
  1885. if escape_pre and pre_text and pre_text[-1] not in MARKUP_ALLOWED_PRECEDENT:
  1886. pre_text += "\\ "
  1887. if escape_post and post_text and post_text[0] not in MARKUP_ALLOWED_SUBSEQUENT:
  1888. post_text = "\\ " + post_text
  1889. next_brac_pos = post_text.find("[", 0)
  1890. iter_pos = 0
  1891. while not inside_code:
  1892. iter_pos = post_text.find("*", iter_pos, next_brac_pos)
  1893. if iter_pos == -1:
  1894. break
  1895. post_text = f"{post_text[:iter_pos]}\\*{post_text[iter_pos + 1 :]}"
  1896. iter_pos += 2
  1897. iter_pos = 0
  1898. while not inside_code:
  1899. iter_pos = post_text.find("_", iter_pos, next_brac_pos)
  1900. if iter_pos == -1:
  1901. break
  1902. if not post_text[iter_pos + 1].isalnum(): # don't escape within a snake_case word
  1903. post_text = f"{post_text[:iter_pos]}\\_{post_text[iter_pos + 1 :]}"
  1904. iter_pos += 2
  1905. else:
  1906. iter_pos += 1
  1907. text = pre_text + tag_text + post_text
  1908. pos = len(pre_text) + len(tag_text)
  1909. if tag_depth > 0:
  1910. print_error(
  1911. f"{state.current_class}.xml: Tag depth mismatch: too many (or too few) open/close tags in {context_name}.",
  1912. state,
  1913. )
  1914. return text
  1915. def preformat_text_block(text: str, state: State) -> str | None:
  1916. result = ""
  1917. codeblock_tag = ""
  1918. indent_level = 0
  1919. for line in text.splitlines():
  1920. stripped_line = line.lstrip("\t")
  1921. tab_count = len(line) - len(stripped_line)
  1922. if codeblock_tag:
  1923. if line == "":
  1924. result += "\n"
  1925. continue
  1926. if tab_count < indent_level:
  1927. print_error(f"{state.current_class}.xml: Invalid indentation.", state)
  1928. return None
  1929. if stripped_line.startswith("[/" + codeblock_tag):
  1930. result += stripped_line
  1931. codeblock_tag = ""
  1932. else:
  1933. # Remove extraneous tabs and replace remaining tabs with spaces.
  1934. result += "\n" + " " * (tab_count - indent_level + 1) + stripped_line
  1935. else:
  1936. if (
  1937. stripped_line.startswith("[codeblock]")
  1938. or stripped_line.startswith("[codeblock ")
  1939. or stripped_line.startswith("[gdscript]")
  1940. or stripped_line.startswith("[gdscript ")
  1941. or stripped_line.startswith("[csharp]")
  1942. or stripped_line.startswith("[csharp ")
  1943. ):
  1944. if result:
  1945. result += "\n"
  1946. result += stripped_line
  1947. tag_text = stripped_line[1:].split("]", 1)[0]
  1948. tag_state = get_tag_and_args(tag_text)
  1949. codeblock_tag = tag_state.name
  1950. indent_level = tab_count
  1951. else:
  1952. # A line break in XML should become two line breaks (unless in a code block).
  1953. if result:
  1954. result += "\n\n"
  1955. result += stripped_line
  1956. return result
  1957. def format_context_name(context: DefinitionBase | None) -> str:
  1958. context_name: str = "unknown context"
  1959. if context is not None:
  1960. context_name = f'{context.definition_name} "{context.name}" description'
  1961. return context_name
  1962. def escape_rst(text: str, until_pos: int = -1) -> str:
  1963. # Escape \ character, otherwise it ends up as an escape character in rst
  1964. pos = 0
  1965. while True:
  1966. pos = text.find("\\", pos, until_pos)
  1967. if pos == -1:
  1968. break
  1969. text = f"{text[:pos]}\\\\{text[pos + 1 :]}"
  1970. pos += 2
  1971. # Escape * character to avoid interpreting it as emphasis
  1972. pos = 0
  1973. while True:
  1974. pos = text.find("*", pos, until_pos)
  1975. if pos == -1:
  1976. break
  1977. text = f"{text[:pos]}\\*{text[pos + 1 :]}"
  1978. pos += 2
  1979. # Escape _ character at the end of a word to avoid interpreting it as an inline hyperlink
  1980. pos = 0
  1981. while True:
  1982. pos = text.find("_", pos, until_pos)
  1983. if pos == -1:
  1984. break
  1985. if not text[pos + 1].isalnum(): # don't escape within a snake_case word
  1986. text = f"{text[:pos]}\\_{text[pos + 1 :]}"
  1987. pos += 2
  1988. else:
  1989. pos += 1
  1990. return text
  1991. def format_table(f: TextIO, data: list[tuple[str | None, ...]], remove_empty_columns: bool = False) -> None:
  1992. if len(data) == 0:
  1993. return
  1994. f.write(".. table::\n")
  1995. f.write(" :widths: auto\n\n")
  1996. # Calculate the width of each column first, we will use this information
  1997. # to properly format RST-style tables.
  1998. column_sizes = [0] * len(data[0])
  1999. for row in data:
  2000. for i, text in enumerate(row):
  2001. text_length = len(text or "")
  2002. if text_length > column_sizes[i]:
  2003. column_sizes[i] = text_length
  2004. # Each table row is wrapped in two separators, consecutive rows share the same separator.
  2005. # All separators, or rather borders, have the same shape and content. We compose it once,
  2006. # then reuse it.
  2007. sep = ""
  2008. for size in column_sizes:
  2009. if size == 0 and remove_empty_columns:
  2010. continue
  2011. sep += "+" + "-" * (size + 2) # Content of each cell is padded by 1 on each side.
  2012. sep += "+\n"
  2013. # Draw the first separator.
  2014. f.write(f" {sep}")
  2015. # Draw each row and close it with a separator.
  2016. for row in data:
  2017. row_text = "|"
  2018. for i, text in enumerate(row):
  2019. if column_sizes[i] == 0 and remove_empty_columns:
  2020. continue
  2021. row_text += f" {(text or '').ljust(column_sizes[i])} |"
  2022. row_text += "\n"
  2023. f.write(f" {row_text}")
  2024. f.write(f" {sep}")
  2025. f.write("\n")
  2026. def sanitize_class_name(dirty_name: str, is_file_name: bool = False) -> str:
  2027. if is_file_name:
  2028. return dirty_name.lower().replace('"', "").replace("/", "--")
  2029. else:
  2030. return dirty_name.replace('"', "").replace("/", "_").replace(".", "_")
  2031. def sanitize_operator_name(dirty_name: str, state: State) -> str:
  2032. clear_name = dirty_name.replace("operator ", "")
  2033. if clear_name == "!=":
  2034. clear_name = "neq"
  2035. elif clear_name == "==":
  2036. clear_name = "eq"
  2037. elif clear_name == "<":
  2038. clear_name = "lt"
  2039. elif clear_name == "<=":
  2040. clear_name = "lte"
  2041. elif clear_name == ">":
  2042. clear_name = "gt"
  2043. elif clear_name == ">=":
  2044. clear_name = "gte"
  2045. elif clear_name == "+":
  2046. clear_name = "sum"
  2047. elif clear_name == "-":
  2048. clear_name = "dif"
  2049. elif clear_name == "*":
  2050. clear_name = "mul"
  2051. elif clear_name == "/":
  2052. clear_name = "div"
  2053. elif clear_name == "%":
  2054. clear_name = "mod"
  2055. elif clear_name == "**":
  2056. clear_name = "pow"
  2057. elif clear_name == "unary+":
  2058. clear_name = "unplus"
  2059. elif clear_name == "unary-":
  2060. clear_name = "unminus"
  2061. elif clear_name == "<<":
  2062. clear_name = "bwsl"
  2063. elif clear_name == ">>":
  2064. clear_name = "bwsr"
  2065. elif clear_name == "&":
  2066. clear_name = "bwand"
  2067. elif clear_name == "|":
  2068. clear_name = "bwor"
  2069. elif clear_name == "^":
  2070. clear_name = "bwxor"
  2071. elif clear_name == "~":
  2072. clear_name = "bwnot"
  2073. elif clear_name == "[]":
  2074. clear_name = "idx"
  2075. else:
  2076. clear_name = "xxx"
  2077. print_error(f'Unsupported operator type "{dirty_name}", please add the missing rule.', state)
  2078. return clear_name
  2079. if __name__ == "__main__":
  2080. main()