makerst.py 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. #!/usr/bin/env python3
  2. import argparse
  3. import os
  4. import re
  5. import xml.etree.ElementTree as ET
  6. from collections import OrderedDict
  7. # Uncomment to do type checks. I have it commented out so it works below Python 3.5
  8. #from typing import List, Dict, TextIO, Tuple, Iterable, Optional, DefaultDict, Any, Union
  9. # http(s)://docs.godotengine.org/<langcode>/<tag>/path/to/page.html(#fragment-tag)
  10. GODOT_DOCS_PATTERN = re.compile(r'^http(?:s)?://docs\.godotengine\.org/(?:[a-zA-Z0-9.\-_]*)/(?:[a-zA-Z0-9.\-_]*)/(.*)\.html(#.*)?$')
  11. def print_error(error, state): # type: (str, State) -> None
  12. print(error)
  13. state.errored = True
  14. class TypeName:
  15. def __init__(self, type_name, enum=None): # type: (str, Optional[str]) -> None
  16. self.type_name = type_name
  17. self.enum = enum
  18. def to_rst(self, state): # type: ("State") -> str
  19. if self.enum is not None:
  20. return make_enum(self.enum, state)
  21. elif self.type_name == "void":
  22. return "void"
  23. else:
  24. return make_type(self.type_name, state)
  25. @classmethod
  26. def from_element(cls, element): # type: (ET.Element) -> "TypeName"
  27. return cls(element.attrib["type"], element.get("enum"))
  28. class PropertyDef:
  29. def __init__(self, name, type_name, setter, getter, text): # type: (str, TypeName, Optional[str], Optional[str], Optional[str]) -> None
  30. self.name = name
  31. self.type_name = type_name
  32. self.setter = setter
  33. self.getter = getter
  34. self.text = text
  35. class ParameterDef:
  36. def __init__(self, name, type_name, default_value): # type: (str, TypeName, Optional[str]) -> None
  37. self.name = name
  38. self.type_name = type_name
  39. self.default_value = default_value
  40. class SignalDef:
  41. def __init__(self, name, parameters, description): # type: (str, List[ParameterDef], Optional[str]) -> None
  42. self.name = name
  43. self.parameters = parameters
  44. self.description = description
  45. class MethodDef:
  46. def __init__(self, name, return_type, parameters, description, qualifiers): # type: (str, TypeName, List[ParameterDef], Optional[str], Optional[str]) -> None
  47. self.name = name
  48. self.return_type = return_type
  49. self.parameters = parameters
  50. self.description = description
  51. self.qualifiers = qualifiers
  52. class ConstantDef:
  53. def __init__(self, name, value, text): # type: (str, str, Optional[str]) -> None
  54. self.name = name
  55. self.value = value
  56. self.text = text
  57. class EnumDef:
  58. def __init__(self, name): # type: (str) -> None
  59. self.name = name
  60. self.values = OrderedDict() # type: OrderedDict[str, ConstantDef]
  61. class ThemeItemDef:
  62. def __init__(self, name, type_name): # type: (str, TypeName) -> None
  63. self.name = name
  64. self.type_name = type_name
  65. class ClassDef:
  66. def __init__(self, name): # type: (str) -> None
  67. self.name = name
  68. self.constants = OrderedDict() # type: OrderedDict[str, ConstantDef]
  69. self.enums = OrderedDict() # type: OrderedDict[str, EnumDef]
  70. self.properties = OrderedDict() # type: OrderedDict[str, PropertyDef]
  71. self.methods = OrderedDict() # type: OrderedDict[str, List[MethodDef]]
  72. self.signals = OrderedDict() # type: OrderedDict[str, SignalDef]
  73. self.inherits = None # type: Optional[str]
  74. self.category = None # type: Optional[str]
  75. self.brief_description = None # type: Optional[str]
  76. self.description = None # type: Optional[str]
  77. self.theme_items = None # type: Optional[OrderedDict[str, List[ThemeItemDef]]]
  78. self.tutorials = [] # type: List[str]
  79. class State:
  80. def __init__(self): # type: () -> None
  81. # Has any error been reported?
  82. self.errored = False
  83. self.classes = OrderedDict() # type: OrderedDict[str, ClassDef]
  84. self.current_class = "" # type: str
  85. def parse_class(self, class_root): # type: (ET.Element) -> None
  86. class_name = class_root.attrib["name"]
  87. class_def = ClassDef(class_name)
  88. self.classes[class_name] = class_def
  89. inherits = class_root.get("inherits")
  90. if inherits is not None:
  91. class_def.inherits = inherits
  92. category = class_root.get("category")
  93. if category is not None:
  94. class_def.category = category
  95. brief_desc = class_root.find("brief_description")
  96. if brief_desc is not None and brief_desc.text:
  97. class_def.brief_description = brief_desc.text
  98. desc = class_root.find("description")
  99. if desc is not None and desc.text:
  100. class_def.description = desc.text
  101. properties = class_root.find("members")
  102. if properties is not None:
  103. for property in properties:
  104. assert property.tag == "member"
  105. property_name = property.attrib["name"]
  106. if property_name in class_def.properties:
  107. print_error("Duplicate property '{}', file: {}".format(property_name, class_name), self)
  108. continue
  109. type_name = TypeName.from_element(property)
  110. setter = property.get("setter") or None # Use or None so '' gets turned into None.
  111. getter = property.get("getter") or None
  112. property_def = PropertyDef(property_name, type_name, setter, getter, property.text)
  113. class_def.properties[property_name] = property_def
  114. methods = class_root.find("methods")
  115. if methods is not None:
  116. for method in methods:
  117. assert method.tag == "method"
  118. method_name = method.attrib["name"]
  119. qualifiers = method.get("qualifiers")
  120. return_element = method.find("return")
  121. if return_element is not None:
  122. return_type = TypeName.from_element(return_element)
  123. else:
  124. return_type = TypeName("void")
  125. params = parse_arguments(method)
  126. desc_element = method.find("description")
  127. method_desc = None
  128. if desc_element is not None:
  129. method_desc = desc_element.text
  130. method_def = MethodDef(method_name, return_type, params, method_desc, qualifiers)
  131. if method_name not in class_def.methods:
  132. class_def.methods[method_name] = []
  133. class_def.methods[method_name].append(method_def)
  134. constants = class_root.find("constants")
  135. if constants is not None:
  136. for constant in constants:
  137. assert constant.tag == "constant"
  138. constant_name = constant.attrib["name"]
  139. value = constant.attrib["value"]
  140. enum = constant.get("enum")
  141. constant_def = ConstantDef(constant_name, value, constant.text)
  142. if enum is None:
  143. if constant_name in class_def.constants:
  144. print_error("Duplicate constant '{}', file: {}".format(constant_name, class_name), self)
  145. continue
  146. class_def.constants[constant_name] = constant_def
  147. else:
  148. if enum in class_def.enums:
  149. enum_def = class_def.enums[enum]
  150. else:
  151. enum_def = EnumDef(enum)
  152. class_def.enums[enum] = enum_def
  153. enum_def.values[constant_name] = constant_def
  154. signals = class_root.find("signals")
  155. if signals is not None:
  156. for signal in signals:
  157. assert signal.tag == "signal"
  158. signal_name = signal.attrib["name"]
  159. if signal_name in class_def.signals:
  160. print_error("Duplicate signal '{}', file: {}".format(signal_name, class_name), self)
  161. continue
  162. params = parse_arguments(signal)
  163. desc_element = signal.find("description")
  164. signal_desc = None
  165. if desc_element is not None:
  166. signal_desc = desc_element.text
  167. signal_def = SignalDef(signal_name, params, signal_desc)
  168. class_def.signals[signal_name] = signal_def
  169. theme_items = class_root.find("theme_items")
  170. if theme_items is not None:
  171. class_def.theme_items = OrderedDict()
  172. for theme_item in theme_items:
  173. assert theme_item.tag == "theme_item"
  174. theme_item_name = theme_item.attrib["name"]
  175. theme_item_def = ThemeItemDef(theme_item_name, TypeName.from_element(theme_item))
  176. if theme_item_name not in class_def.theme_items:
  177. class_def.theme_items[theme_item_name] = []
  178. class_def.theme_items[theme_item_name].append(theme_item_def)
  179. tutorials = class_root.find("tutorials")
  180. if tutorials is not None:
  181. for link in tutorials:
  182. assert link.tag == "link"
  183. if link.text is not None:
  184. class_def.tutorials.append(link.text)
  185. def sort_classes(self): # type: () -> None
  186. self.classes = OrderedDict(sorted(self.classes.items(), key=lambda t: t[0]))
  187. def parse_arguments(root): # type: (ET.Element) -> List[ParameterDef]
  188. param_elements = root.findall("argument")
  189. params = [None] * len(param_elements) # type: Any
  190. for param_element in param_elements:
  191. param_name = param_element.attrib["name"]
  192. index = int(param_element.attrib["index"])
  193. type_name = TypeName.from_element(param_element)
  194. default = param_element.get("default")
  195. params[index] = ParameterDef(param_name, type_name, default)
  196. cast = params # type: List[ParameterDef]
  197. return cast
  198. def main(): # type: () -> None
  199. parser = argparse.ArgumentParser()
  200. parser.add_argument("path", nargs="+", help="A path to an XML file or a directory containing XML files to parse.")
  201. group = parser.add_mutually_exclusive_group()
  202. group.add_argument("--output", "-o", default=".", help="The directory to save output .rst files in.")
  203. group.add_argument("--dry-run", action="store_true", help="If passed, no output will be generated and XML files are only checked for errors.")
  204. args = parser.parse_args()
  205. file_list = [] # type: List[str]
  206. for path in args.path:
  207. # Cut off trailing slashes so os.path.basename doesn't choke.
  208. if path.endswith(os.sep):
  209. path = path[:-1]
  210. if os.path.basename(path) == 'modules':
  211. for subdir, dirs, _ in os.walk(path):
  212. if 'doc_classes' in dirs:
  213. doc_dir = os.path.join(subdir, 'doc_classes')
  214. class_file_names = (f for f in os.listdir(doc_dir) if f.endswith('.xml'))
  215. file_list += (os.path.join(doc_dir, f) for f in class_file_names)
  216. elif os.path.isdir(path):
  217. file_list += (os.path.join(path, f) for f in os.listdir(path) if f.endswith('.xml'))
  218. elif os.path.isfile(path):
  219. if not path.endswith(".xml"):
  220. print("Got non-.xml file '{}' in input, skipping.".format(path))
  221. continue
  222. file_list.append(path)
  223. classes = {} # type: Dict[str, ET.Element]
  224. state = State()
  225. for cur_file in file_list:
  226. try:
  227. tree = ET.parse(cur_file)
  228. except ET.ParseError as e:
  229. print_error("Parse error reading file '{}': {}".format(cur_file, e), state)
  230. continue
  231. doc = tree.getroot()
  232. if 'version' not in doc.attrib:
  233. print_error("Version missing from 'doc', file: {}".format(cur_file), state)
  234. continue
  235. name = doc.attrib["name"]
  236. if name in classes:
  237. print_error("Duplicate class '{}'".format(name), state)
  238. continue
  239. classes[name] = doc
  240. for name, data in classes.items():
  241. try:
  242. state.parse_class(data)
  243. except Exception as e:
  244. print_error("Exception while parsing class '{}': {}".format(name, e), state)
  245. state.sort_classes()
  246. for class_name, class_def in state.classes.items():
  247. state.current_class = class_name
  248. make_rst_class(class_def, state, args.dry_run, args.output)
  249. if state.errored:
  250. exit(1)
  251. def make_rst_class(class_def, state, dry_run, output_dir): # type: (ClassDef, State, bool, str) -> None
  252. class_name = class_def.name
  253. if dry_run:
  254. f = open(os.devnull, "w")
  255. else:
  256. f = open(os.path.join(output_dir, "class_" + class_name.lower() + '.rst'), 'w', encoding='utf-8')
  257. # Warn contributors not to edit this file directly
  258. f.write(".. Generated automatically by doc/tools/makerst.py in Godot's source tree.\n")
  259. f.write(".. DO NOT EDIT THIS FILE, but the " + class_name + ".xml source instead.\n")
  260. f.write(".. The source is found in doc/classes or modules/<name>/doc_classes.\n\n")
  261. f.write(".. _class_" + class_name + ":\n\n")
  262. f.write(make_heading(class_name, '='))
  263. # Inheritance tree
  264. # Ascendants
  265. if class_def.inherits:
  266. inh = class_def.inherits.strip()
  267. f.write('**Inherits:** ')
  268. first = True
  269. while inh in state.classes:
  270. if not first:
  271. f.write(" **<** ")
  272. else:
  273. first = False
  274. f.write(make_type(inh, state))
  275. inode = state.classes[inh].inherits
  276. if inode:
  277. inh = inode.strip()
  278. else:
  279. break
  280. f.write("\n\n")
  281. # Descendents
  282. inherited = []
  283. for c in state.classes.values():
  284. if c.inherits and c.inherits.strip() == class_name:
  285. inherited.append(c.name)
  286. if len(inherited):
  287. f.write('**Inherited By:** ')
  288. for i, child in enumerate(inherited):
  289. if i > 0:
  290. f.write(", ")
  291. f.write(make_type(child, state))
  292. f.write("\n\n")
  293. # Category
  294. if class_def.category is not None:
  295. f.write('**Category:** ' + class_def.category.strip() + "\n\n")
  296. # Brief description
  297. f.write(make_heading('Brief Description', '-'))
  298. if class_def.brief_description is not None:
  299. f.write(rstize_text(class_def.brief_description.strip(), state) + "\n\n")
  300. # Properties overview
  301. if len(class_def.properties) > 0:
  302. f.write(make_heading('Properties', '-'))
  303. ml = [] # type: List[Tuple[str, str]]
  304. for property_def in class_def.properties.values():
  305. type_rst = property_def.type_name.to_rst(state)
  306. ref = ":ref:`{0}<class_{1}_property_{0}>`".format(property_def.name, class_name)
  307. ml.append((type_rst, ref))
  308. format_table(f, ml)
  309. # Methods overview
  310. if len(class_def.methods) > 0:
  311. f.write(make_heading('Methods', '-'))
  312. ml = []
  313. for method_list in class_def.methods.values():
  314. for m in method_list:
  315. ml.append(make_method_signature(class_def, m, True, state))
  316. format_table(f, ml)
  317. # Theme properties
  318. if class_def.theme_items is not None and len(class_def.theme_items) > 0:
  319. f.write(make_heading('Theme Properties', '-'))
  320. ml = []
  321. for theme_item_list in class_def.theme_items.values():
  322. for theme_item in theme_item_list:
  323. ml.append((theme_item.type_name.to_rst(state), theme_item.name))
  324. format_table(f, ml)
  325. # Signals
  326. if len(class_def.signals) > 0:
  327. f.write(make_heading('Signals', '-'))
  328. for signal in class_def.signals.values():
  329. #f.write(".. _class_{}_{}:\n\n".format(class_name, signal.name))
  330. f.write(".. _class_{}_signal_{}:\n\n".format(class_name, signal.name))
  331. _, signature = make_method_signature(class_def, signal, False, state)
  332. f.write("- {}\n\n".format(signature))
  333. if signal.description is None or signal.description.strip() == '':
  334. continue
  335. f.write(rstize_text(signal.description.strip(), state))
  336. f.write("\n\n")
  337. # Enums
  338. if len(class_def.enums) > 0:
  339. f.write(make_heading('Enumerations', '-'))
  340. for e in class_def.enums.values():
  341. f.write(".. _enum_{}_{}:\n\n".format(class_name, e.name))
  342. # Sphinx seems to divide the bullet list into individual <ul> tags if we weave the labels into it.
  343. # As such I'll put them all above the list. Won't be perfect but better than making the list visually broken.
  344. # As to why I'm not modifying the reference parser to directly link to the _enum label:
  345. # If somebody gets annoyed enough to fix it, all existing references will magically improve.
  346. for value in e.values.values():
  347. f.write(".. _class_{}_constant_{}:\n\n".format(class_name, value.name))
  348. f.write("enum **{}**:\n\n".format(e.name))
  349. for value in e.values.values():
  350. f.write("- **{}** = **{}**".format(value.name, value.value))
  351. if value.text is not None and value.text.strip() != '':
  352. f.write(' --- ' + rstize_text(value.text.strip(), state))
  353. f.write('\n\n')
  354. # Constants
  355. if len(class_def.constants) > 0:
  356. f.write(make_heading('Constants', '-'))
  357. # Sphinx seems to divide the bullet list into individual <ul> tags if we weave the labels into it.
  358. # As such I'll put them all above the list. Won't be perfect but better than making the list visually broken.
  359. for constant in class_def.constants.values():
  360. f.write(".. _class_{}_constant_{}:\n\n".format(class_name, constant.name))
  361. for constant in class_def.constants.values():
  362. f.write("- **{}** = **{}**".format(constant.name, constant.value))
  363. if constant.text is not None and constant.text.strip() != '':
  364. f.write(' --- ' + rstize_text(constant.text.strip(), state))
  365. f.write('\n\n')
  366. # Class description
  367. if class_def.description is not None and class_def.description.strip() != '':
  368. f.write(make_heading('Description', '-'))
  369. f.write(rstize_text(class_def.description.strip(), state) + "\n\n")
  370. # Online tutorials
  371. if len(class_def.tutorials) > 0:
  372. f.write(make_heading('Tutorials', '-'))
  373. for t in class_def.tutorials:
  374. link = t.strip()
  375. match = GODOT_DOCS_PATTERN.search(link)
  376. if match:
  377. groups = match.groups()
  378. if match.lastindex == 2:
  379. # Doc reference with fragment identifier: emit direct link to section with reference to page, for example:
  380. # `#calling-javascript-from-script in Exporting For Web`
  381. f.write("- `" + groups[1] + " <../" + groups[0] + ".html" + groups[1] + ">`_ in :doc:`../" + groups[0] + "`\n\n")
  382. # Commented out alternative: Instead just emit:
  383. # `Subsection in Exporting For Web`
  384. # f.write("- `Subsection <../" + groups[0] + ".html" + groups[1] + ">`_ in :doc:`../" + groups[0] + "`\n\n")
  385. elif match.lastindex == 1:
  386. # Doc reference, for example:
  387. # `Math`
  388. f.write("- :doc:`../" + groups[0] + "`\n\n")
  389. else:
  390. # External link, for example:
  391. # `http://enet.bespin.org/usergroup0.html`
  392. f.write("- `" + link + " <" + link + ">`_\n\n")
  393. # Property descriptions
  394. if len(class_def.properties) > 0:
  395. f.write(make_heading('Property Descriptions', '-'))
  396. for property_def in class_def.properties.values():
  397. #f.write(".. _class_{}_{}:\n\n".format(class_name, property_def.name))
  398. f.write(".. _class_{}_property_{}:\n\n".format(class_name, property_def.name))
  399. f.write('- {} **{}**\n\n'.format(property_def.type_name.to_rst(state), property_def.name))
  400. setget = []
  401. if property_def.setter is not None and not property_def.setter.startswith("_"):
  402. setget.append(("*Setter*", property_def.setter + '(value)'))
  403. if property_def.getter is not None and not property_def.getter.startswith("_"):
  404. setget.append(('*Getter*', property_def.getter + '()'))
  405. if len(setget) > 0:
  406. format_table(f, setget)
  407. if property_def.text is not None and property_def.text.strip() != '':
  408. f.write(rstize_text(property_def.text.strip(), state))
  409. f.write('\n\n')
  410. # Method descriptions
  411. if len(class_def.methods) > 0:
  412. f.write(make_heading('Method Descriptions', '-'))
  413. for method_list in class_def.methods.values():
  414. for i, m in enumerate(method_list):
  415. if i == 0:
  416. #f.write(".. _class_{}_{}:\n\n".format(class_name, m.name))
  417. f.write(".. _class_{}_method_{}:\n\n".format(class_name, m.name))
  418. ret_type, signature = make_method_signature(class_def, m, False, state)
  419. f.write("- {} {}\n\n".format(ret_type, signature))
  420. if m.description is None or m.description.strip() == '':
  421. continue
  422. f.write(rstize_text(m.description.strip(), state))
  423. f.write("\n\n")
  424. def make_class_list(class_list, columns): # type: (List[str], int) -> None
  425. # This function is no longer used.
  426. f = open('class_list.rst', 'w', encoding='utf-8')
  427. col_max = len(class_list) // columns + 1
  428. print(('col max is ', col_max))
  429. fit_columns = [] # type: List[List[str]]
  430. for _ in range(0, columns):
  431. fit_columns.append([])
  432. indexers = [] # type List[str]
  433. last_initial = ''
  434. for idx, name in enumerate(class_list):
  435. col = idx // col_max
  436. if col >= columns:
  437. col = columns - 1
  438. fit_columns[col].append(name)
  439. idx += 1
  440. if name[:1] != last_initial:
  441. indexers.append(name)
  442. last_initial = name[:1]
  443. row_max = 0
  444. f.write("\n")
  445. for n in range(0, columns):
  446. if len(fit_columns[n]) > row_max:
  447. row_max = len(fit_columns[n])
  448. f.write("| ")
  449. for n in range(0, columns):
  450. f.write(" | |")
  451. f.write("\n")
  452. f.write("+")
  453. for n in range(0, columns):
  454. f.write("--+-------+")
  455. f.write("\n")
  456. for r in range(0, row_max):
  457. s = '+ '
  458. for c in range(0, columns):
  459. if r >= len(fit_columns[c]):
  460. continue
  461. classname = fit_columns[c][r]
  462. initial = classname[0]
  463. if classname in indexers:
  464. s += '**' + initial + '** | '
  465. else:
  466. s += ' | '
  467. s += '[' + classname + '](class_' + classname.lower() + ') | '
  468. s += '\n'
  469. f.write(s)
  470. for n in range(0, columns):
  471. f.write("--+-------+")
  472. f.write("\n")
  473. f.close()
  474. def rstize_text(text, state): # type: (str, State) -> str
  475. # Linebreak + tabs in the XML should become two line breaks unless in a "codeblock"
  476. pos = 0
  477. while True:
  478. pos = text.find('\n', pos)
  479. if pos == -1:
  480. break
  481. pre_text = text[:pos]
  482. while text[pos + 1] == '\t':
  483. pos += 1
  484. post_text = text[pos + 1:]
  485. # Handle codeblocks
  486. if post_text.startswith("[codeblock]"):
  487. end_pos = post_text.find("[/codeblock]")
  488. if end_pos == -1:
  489. print_error("[codeblock] without a closing tag, file: {}".format(state.current_class), state)
  490. return ""
  491. code_text = post_text[len("[codeblock]"):end_pos]
  492. post_text = post_text[end_pos:]
  493. # Remove extraneous tabs
  494. code_pos = 0
  495. while True:
  496. code_pos = code_text.find('\n', code_pos)
  497. if code_pos == -1:
  498. break
  499. to_skip = 0
  500. while code_pos + to_skip + 1 < len(code_text) and code_text[code_pos + to_skip + 1] == '\t':
  501. to_skip += 1
  502. if len(code_text[code_pos + to_skip + 1:]) == 0:
  503. code_text = code_text[:code_pos] + "\n"
  504. code_pos += 1
  505. else:
  506. code_text = code_text[:code_pos] + "\n " + code_text[code_pos + to_skip + 1:]
  507. code_pos += 5 - to_skip
  508. text = pre_text + "\n[codeblock]" + code_text + post_text
  509. pos += len("\n[codeblock]" + code_text)
  510. # Handle normal text
  511. else:
  512. text = pre_text + "\n\n" + post_text
  513. pos += 2
  514. next_brac_pos = text.find('[')
  515. # Escape \ character, otherwise it ends up as an escape character in rst
  516. pos = 0
  517. while True:
  518. pos = text.find('\\', pos, next_brac_pos)
  519. if pos == -1:
  520. break
  521. text = text[:pos] + "\\\\" + text[pos + 1:]
  522. pos += 2
  523. # Escape * character to avoid interpreting it as emphasis
  524. pos = 0
  525. while True:
  526. pos = text.find('*', pos, next_brac_pos)
  527. if pos == -1:
  528. break
  529. text = text[:pos] + "\*" + text[pos + 1:]
  530. pos += 2
  531. # Escape _ character at the end of a word to avoid interpreting it as an inline hyperlink
  532. pos = 0
  533. while True:
  534. pos = text.find('_', pos, next_brac_pos)
  535. if pos == -1:
  536. break
  537. if not text[pos + 1].isalnum(): # don't escape within a snake_case word
  538. text = text[:pos] + "\_" + text[pos + 1:]
  539. pos += 2
  540. else:
  541. pos += 1
  542. # Handle [tags]
  543. inside_code = False
  544. inside_url = False
  545. url_has_name = False
  546. url_link = ""
  547. pos = 0
  548. tag_depth = 0
  549. previous_pos = 0
  550. while True:
  551. pos = text.find('[', pos)
  552. if inside_url and (pos > previous_pos):
  553. url_has_name = True
  554. if pos == -1:
  555. break
  556. endq_pos = text.find(']', pos + 1)
  557. if endq_pos == -1:
  558. break
  559. pre_text = text[:pos]
  560. post_text = text[endq_pos + 1:]
  561. tag_text = text[pos + 1:endq_pos]
  562. escape_post = False
  563. if tag_text in state.classes:
  564. if tag_text == state.current_class:
  565. # We don't want references to the same class
  566. tag_text = '``{}``'.format(tag_text)
  567. else:
  568. tag_text = make_type(tag_text, state)
  569. escape_post = True
  570. else: # command
  571. cmd = tag_text
  572. space_pos = tag_text.find(' ')
  573. if cmd == '/codeblock':
  574. tag_text = ''
  575. tag_depth -= 1
  576. inside_code = False
  577. # Strip newline if the tag was alone on one
  578. if pre_text[-1] == '\n':
  579. pre_text = pre_text[:-1]
  580. elif cmd == '/code':
  581. tag_text = '``'
  582. tag_depth -= 1
  583. inside_code = False
  584. escape_post = True
  585. elif inside_code:
  586. tag_text = '[' + tag_text + ']'
  587. elif cmd.find('html') == 0:
  588. param = tag_text[space_pos + 1:]
  589. tag_text = param
  590. elif cmd.startswith('method') or cmd.startswith('member') or cmd.startswith('signal') or cmd.startswith('constant'):
  591. param = tag_text[space_pos + 1:]
  592. if param.find('.') != -1:
  593. ss = param.split('.')
  594. if len(ss) > 2:
  595. print_error("Bad reference: '{}', file: {}".format(param, state.current_class), state)
  596. class_param, method_param = ss
  597. else:
  598. class_param = state.current_class
  599. method_param = param
  600. ref_type = ""
  601. if class_param in state.classes:
  602. class_def = state.classes[class_param]
  603. if cmd.startswith("method"):
  604. if method_param not in class_def.methods:
  605. print_error("Unresolved method '{}', file: {}".format(param, state.current_class), state)
  606. ref_type = "_method"
  607. elif cmd.startswith("member"):
  608. if method_param not in class_def.properties:
  609. print_error("Unresolved member '{}', file: {}".format(param, state.current_class), state)
  610. ref_type = "_property"
  611. elif cmd.startswith("signal"):
  612. if method_param not in class_def.signals:
  613. print_error("Unresolved signal '{}', file: {}".format(param, state.current_class), state)
  614. ref_type = "_signal"
  615. elif cmd.startswith("constant"):
  616. found = False
  617. # Search in the current class
  618. search_class_defs = [class_def]
  619. if param.find('.') == -1:
  620. # Also search in @GlobalScope as a last resort if no class was specified
  621. search_class_defs.append(state.classes["@GlobalScope"])
  622. for search_class_def in search_class_defs:
  623. if method_param in search_class_def.constants:
  624. class_param = search_class_def.name
  625. found = True
  626. else:
  627. for enum in search_class_def.enums.values():
  628. if method_param in enum.values:
  629. class_param = search_class_def.name
  630. found = True
  631. break
  632. if not found:
  633. print_error("Unresolved constant '{}', file: {}".format(param, state.current_class), state)
  634. ref_type = "_constant"
  635. else:
  636. print_error("Unresolved type reference '{}' in method reference '{}', file: {}".format(class_param, param, state.current_class), state)
  637. repl_text = method_param
  638. if class_param != state.current_class:
  639. repl_text = "{}.{}".format(class_param, method_param)
  640. tag_text = ':ref:`{}<class_{}{}_{}>`'.format(repl_text, class_param, ref_type, method_param)
  641. escape_post = True
  642. elif cmd.find('image=') == 0:
  643. tag_text = "" # '![](' + cmd[6:] + ')'
  644. elif cmd.find('url=') == 0:
  645. url_link = cmd[4:]
  646. tag_text = ':ref:`'
  647. tag_depth += 1
  648. url_has_name = False
  649. inside_url = True
  650. elif cmd == '/url':
  651. tag_text = ('' if url_has_name else url_link) + '<' + url_link + ">`"
  652. tag_depth -= 1
  653. escape_post = True
  654. inside_url = False
  655. elif cmd == 'center':
  656. tag_depth += 1
  657. tag_text = ''
  658. elif cmd == '/center':
  659. tag_depth -= 1
  660. tag_text = ''
  661. elif cmd == 'codeblock':
  662. tag_depth += 1
  663. tag_text = '\n::\n'
  664. inside_code = True
  665. elif cmd == 'br':
  666. # Make a new paragraph instead of a linebreak, rst is not so linebreak friendly
  667. tag_text = '\n\n'
  668. # Strip potential leading spaces
  669. while post_text[0] == ' ':
  670. post_text = post_text[1:]
  671. elif cmd == 'i' or cmd == '/i':
  672. if cmd == "/i":
  673. tag_depth -= 1
  674. else:
  675. tag_depth += 1
  676. tag_text = '*'
  677. elif cmd == 'b' or cmd == '/b':
  678. if cmd == "/b":
  679. tag_depth -= 1
  680. else:
  681. tag_depth += 1
  682. tag_text = '**'
  683. elif cmd == 'u' or cmd == '/u':
  684. if cmd == "/u":
  685. tag_depth -= 1
  686. else:
  687. tag_depth += 1
  688. tag_text = ''
  689. elif cmd == 'code':
  690. tag_text = '``'
  691. tag_depth += 1
  692. inside_code = True
  693. elif cmd.startswith('enum '):
  694. tag_text = make_enum(cmd[5:], state)
  695. else:
  696. tag_text = make_type(tag_text, state)
  697. escape_post = True
  698. # Properly escape things like `[Node]s`
  699. if escape_post and post_text and (post_text[0].isalnum() or post_text[0] == "("): # not punctuation, escape
  700. post_text = '\ ' + post_text
  701. next_brac_pos = post_text.find('[', 0)
  702. iter_pos = 0
  703. while not inside_code:
  704. iter_pos = post_text.find('*', iter_pos, next_brac_pos)
  705. if iter_pos == -1:
  706. break
  707. post_text = post_text[:iter_pos] + "\*" + post_text[iter_pos + 1:]
  708. iter_pos += 2
  709. iter_pos = 0
  710. while not inside_code:
  711. iter_pos = post_text.find('_', iter_pos, next_brac_pos)
  712. if iter_pos == -1:
  713. break
  714. if not post_text[iter_pos + 1].isalnum(): # don't escape within a snake_case word
  715. post_text = post_text[:iter_pos] + "\_" + post_text[iter_pos + 1:]
  716. iter_pos += 2
  717. else:
  718. iter_pos += 1
  719. text = pre_text + tag_text + post_text
  720. pos = len(pre_text) + len(tag_text)
  721. previous_pos = pos
  722. if tag_depth > 0:
  723. print_error("Tag depth mismatch: too many/little open/close tags, file: {}".format(state.current_class), state)
  724. return text
  725. def format_table(f, pp): # type: (TextIO, Iterable[Tuple[str, ...]]) -> None
  726. longest_t = 0
  727. longest_s = 0
  728. for s in pp:
  729. sl = len(s[0])
  730. if sl > longest_s:
  731. longest_s = sl
  732. tl = len(s[1])
  733. if tl > longest_t:
  734. longest_t = tl
  735. sep = "+"
  736. for i in range(longest_s + 2):
  737. sep += "-"
  738. sep += "+"
  739. for i in range(longest_t + 2):
  740. sep += "-"
  741. sep += "+\n"
  742. f.write(sep)
  743. for s in pp:
  744. rt = s[0]
  745. while len(rt) < longest_s:
  746. rt += " "
  747. st = s[1]
  748. while len(st) < longest_t:
  749. st += " "
  750. f.write("| " + rt + " | " + st + " |\n")
  751. f.write(sep)
  752. f.write('\n')
  753. def make_type(t, state): # type: (str, State) -> str
  754. if t in state.classes:
  755. return ':ref:`{0}<class_{0}>`'.format(t)
  756. print_error("Unresolved type '{}', file: {}".format(t, state.current_class), state)
  757. return t
  758. def make_enum(t, state): # type: (str, State) -> str
  759. p = t.find(".")
  760. if p >= 0:
  761. c = t[0:p]
  762. e = t[p + 1:]
  763. # Variant enums live in GlobalScope but still use periods.
  764. if c == "Variant":
  765. c = "@GlobalScope"
  766. e = "Variant." + e
  767. else:
  768. c = state.current_class
  769. e = t
  770. if c in state.classes and e not in state.classes[c].enums:
  771. c = "@GlobalScope"
  772. if not c in state.classes and c.startswith("_"):
  773. c = c[1:] # Remove the underscore prefix
  774. if c in state.classes and e in state.classes[c].enums:
  775. return ":ref:`{0}<enum_{1}_{0}>`".format(e, c)
  776. print_error("Unresolved enum '{}', file: {}".format(t, state.current_class), state)
  777. return t
  778. def make_method_signature(class_def, method_def, make_ref, state): # type: (ClassDef, Union[MethodDef, SignalDef], bool, State) -> Tuple[str, str]
  779. ret_type = " "
  780. ref_type = "signal"
  781. if isinstance(method_def, MethodDef):
  782. ret_type = method_def.return_type.to_rst(state)
  783. ref_type = "method"
  784. out = ""
  785. if make_ref:
  786. out += ":ref:`{0}<class_{1}_{2}_{0}>` ".format(method_def.name, class_def.name, ref_type)
  787. else:
  788. out += "**{}** ".format(method_def.name)
  789. out += '**(**'
  790. for i, arg in enumerate(method_def.parameters):
  791. if i > 0:
  792. out += ', '
  793. else:
  794. out += ' '
  795. out += "{} {}".format(arg.type_name.to_rst(state), arg.name)
  796. if arg.default_value is not None:
  797. out += '=' + arg.default_value
  798. if isinstance(method_def, MethodDef) and method_def.qualifiers is not None and 'vararg' in method_def.qualifiers:
  799. if len(method_def.parameters) > 0:
  800. out += ', ...'
  801. else:
  802. out += ' ...'
  803. out += ' **)**'
  804. if isinstance(method_def, MethodDef) and method_def.qualifiers is not None:
  805. out += ' ' + method_def.qualifiers
  806. return ret_type, out
  807. def make_heading(title, underline): # type: (str, str) -> str
  808. return title + '\n' + (underline * len(title)) + "\n\n"
  809. if __name__ == '__main__':
  810. main()