makerst.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import sys
  4. import xml.etree.ElementTree as ET
  5. input_list = []
  6. for arg in sys.argv[1:]:
  7. input_list.append(arg)
  8. if len(input_list) < 1:
  9. print 'usage: makerst.py <classes.xml>'
  10. sys.exit(0)
  11. def validate_tag(elem, tag):
  12. if elem.tag != tag:
  13. print "Tag mismatch, expected '" + tag + "', got " + elem.tag
  14. sys.exit(255)
  15. class_names = []
  16. classes = {}
  17. def ul_string(str,ul):
  18. str+="\n"
  19. for i in range(len(str)-1):
  20. str+=ul
  21. str+="\n"
  22. return str
  23. def make_class_list(class_list, columns):
  24. f = open('class_list.rst', 'wb')
  25. prev = 0
  26. col_max = len(class_list) / columns + 1
  27. print ('col max is ', col_max)
  28. col_count = 0
  29. row_count = 0
  30. last_initial = ''
  31. fit_columns = []
  32. for n in range(0, columns):
  33. fit_columns += [[]]
  34. indexers = []
  35. last_initial = ''
  36. idx = 0
  37. for n in class_list:
  38. col = idx / col_max
  39. if col >= columns:
  40. col = columns - 1
  41. fit_columns[col] += [n]
  42. idx += 1
  43. if n[:1] != last_initial:
  44. indexers += [n]
  45. last_initial = n[:1]
  46. row_max = 0
  47. f.write("\n")
  48. for n in range(0, columns):
  49. if len(fit_columns[n]) > row_max:
  50. row_max = len(fit_columns[n])
  51. f.write("| ")
  52. for n in range(0, columns):
  53. f.write(" | |")
  54. f.write("\n")
  55. f.write("+")
  56. for n in range(0, columns):
  57. f.write("--+-------+")
  58. f.write("\n")
  59. for r in range(0, row_max):
  60. s = '+ '
  61. for c in range(0, columns):
  62. if r >= len(fit_columns[c]):
  63. continue
  64. classname = fit_columns[c][r]
  65. initial = classname[0]
  66. if classname in indexers:
  67. s += '**' + initial + '** | '
  68. else:
  69. s += ' | '
  70. s += '[' + classname + '](class_'+ classname.lower()+') | '
  71. s += '\n'
  72. f.write(s)
  73. for n in range(0, columns):
  74. f.write("--+-------+")
  75. f.write("\n")
  76. def rstize_text(text,cclass):
  77. # Linebreak + tabs in the XML should become two line breaks
  78. pos = 0
  79. while True:
  80. pos = text.find('\n', pos)
  81. if pos == -1:
  82. break
  83. pre_text = text[:pos]
  84. while text[pos+1] == '\t':
  85. pos += 1
  86. post_text = text[pos+1:]
  87. text = pre_text + "\n\n" + post_text
  88. pos += 2
  89. pos = 0
  90. while True:
  91. pos = text.find('[', pos)
  92. if pos == -1:
  93. break
  94. endq_pos = text.find(']', pos + 1)
  95. if endq_pos == -1:
  96. break
  97. pre_text = text[:pos]
  98. post_text = text[endq_pos + 1:]
  99. tag_text = text[pos + 1:endq_pos]
  100. if tag_text in class_names:
  101. tag_text = make_type(tag_text)
  102. else:
  103. # command
  104. cmd = tag_text
  105. space_pos = tag_text.find(' ')
  106. if cmd.find('html') == 0:
  107. cmd = tag_text[:space_pos]
  108. param = tag_text[space_pos + 1:]
  109. tag_text = param
  110. elif cmd.find('method') == 0:
  111. cmd = tag_text[:space_pos]
  112. param = tag_text[space_pos + 1:]
  113. if param.find('.') != -1:
  114. (class_param, method_param) = param.split('.')
  115. tag_text = ':ref:`'+class_param+'.'+method_param+'<' + class_param.lower() + '_' + method_param + '>`'
  116. else:
  117. tag_text = ':ref:`' + param + '<' + cclass +"_"+ param + '>`'
  118. elif cmd.find('image=') == 0:
  119. tag_text = "" #'![](' + cmd[6:] + ')'
  120. elif cmd.find('url=') == 0:
  121. tag_text = ':ref:`' + cmd[4:] + '<'+cmd[4:]+">`"
  122. elif cmd == '/url':
  123. tag_text = ')'
  124. elif cmd == 'center':
  125. tag_text = ''
  126. elif cmd == '/center':
  127. tag_text = ''
  128. elif cmd == 'br':
  129. # Make a new paragraph instead of a linebreak, rst is not so linebreak friendly
  130. tag_text = '\n\n'
  131. # Strip potential leading spaces
  132. while post_text[0] == ' ':
  133. post_text = post_text[1:]
  134. elif cmd == 'i' or cmd == '/i':
  135. tag_text = '*'
  136. elif cmd == 'b' or cmd == '/b':
  137. tag_text = '**'
  138. elif cmd == 'u' or cmd == '/u':
  139. tag_text = ''
  140. else:
  141. tag_text = ':ref:`' + tag_text + '<class_'+tag_text.lower()+'>`'
  142. text = pre_text + tag_text + post_text
  143. pos = len(pre_text) + len(tag_text)
  144. # tnode = ET.SubElement(parent,"div")
  145. # tnode.text=text
  146. return text
  147. def make_type(t):
  148. global class_names
  149. if t in class_names:
  150. return ':ref:`'+t+'<class_' + t.lower()+'>`'
  151. return t
  152. def make_method(
  153. f,
  154. name,
  155. m,
  156. declare,
  157. cname,
  158. event=False,
  159. pp=None
  160. ):
  161. if (declare or pp==None):
  162. t = '- '
  163. else:
  164. t = ""
  165. ret_type = 'void'
  166. args = list(m)
  167. mdata = {}
  168. mdata['argidx'] = []
  169. for a in args:
  170. if a.tag == 'return':
  171. idx = -1
  172. elif a.tag == 'argument':
  173. idx = int(a.attrib['index'])
  174. else:
  175. continue
  176. mdata['argidx'].append(idx)
  177. mdata[idx] = a
  178. if not event:
  179. if -1 in mdata['argidx']:
  180. t += make_type(mdata[-1].attrib['type'])
  181. else:
  182. t += 'void'
  183. t += ' '
  184. if declare or pp==None:
  185. # span.attrib["class"]="funcdecl"
  186. # a=ET.SubElement(span,"a")
  187. # a.attrib["name"]=name+"_"+m.attrib["name"]
  188. # a.text=name+"::"+m.attrib["name"]
  189. s = ' **'+m.attrib['name']+'** '
  190. else:
  191. s = ':ref:`'+ m.attrib['name']+'<class_' + cname+"_"+m.attrib['name'] + '>` '
  192. s += ' **(**'
  193. argfound = False
  194. for a in mdata['argidx']:
  195. arg = mdata[a]
  196. if a < 0:
  197. continue
  198. if a > 0:
  199. s += ', '
  200. else:
  201. s += ' '
  202. s += make_type(arg.attrib['type'])
  203. if 'name' in arg.attrib:
  204. s += ' ' + arg.attrib['name']
  205. else:
  206. s += ' arg' + str(a)
  207. if 'default' in arg.attrib:
  208. s += '=' + arg.attrib['default']
  209. argfound = True
  210. if argfound:
  211. s += ' '
  212. s += ' **)**'
  213. if 'qualifiers' in m.attrib:
  214. s += ' ' + m.attrib['qualifiers']
  215. # f.write(s)
  216. if (not declare):
  217. if (pp!=None):
  218. pp.append( (t,s) )
  219. else:
  220. f.write("- "+t+" "+s+"\n")
  221. else:
  222. f.write(t+s+"\n")
  223. def make_heading(title, underline):
  224. return title + '\n' + underline*len(title) + "\n\n"
  225. def make_rst_class(node):
  226. name = node.attrib['name']
  227. f = open("class_"+name.lower() + '.rst', 'wb')
  228. f.write(".. _class_"+name+":\n\n")
  229. f.write(make_heading(name, '='))
  230. if 'inherits' in node.attrib:
  231. inh = node.attrib['inherits'].strip()
  232. f.write(make_heading('Inherits: ' + make_type(inh), '-'))
  233. if 'category' in node.attrib:
  234. f.write(make_heading('Category: ' + node.attrib['category'].strip(), '-'))
  235. briefd = node.find('brief_description')
  236. if briefd != None:
  237. f.write(make_heading('Brief Description', '-'))
  238. f.write(rstize_text(briefd.text.strip(),name) + "\n\n")
  239. methods = node.find('methods')
  240. if methods != None and len(list(methods)) > 0:
  241. f.write(make_heading('Member Functions', '-'))
  242. ml=[]
  243. for m in list(methods):
  244. make_method(f, node.attrib['name'], m, False,name,False,ml)
  245. longest_t = 0
  246. longest_s = 0
  247. for s in ml:
  248. sl = len(s[0])
  249. if (sl>longest_s):
  250. longest_s=sl
  251. tl = len(s[1])
  252. if (tl>longest_t):
  253. longest_t=tl
  254. sep="+"
  255. for i in range(longest_s+2):
  256. sep+="-"
  257. sep+="+"
  258. for i in range(longest_t+2):
  259. sep+="-"
  260. sep+="+\n"
  261. f.write(sep)
  262. for s in ml:
  263. rt = s[0]
  264. while( len(rt) < longest_s ):
  265. rt+=" "
  266. st = s[1]
  267. while( len(st) < longest_t ):
  268. st+=" "
  269. f.write("| "+rt+" | "+st+" |\n")
  270. f.write(sep)
  271. f.write('\n')
  272. events = node.find('signals')
  273. if events != None and len(list(events)) > 0:
  274. f.write(make_heading('Signals', '-'))
  275. for m in list(events):
  276. make_method(f, node.attrib['name'], m, True,name, True)
  277. f.write('\n')
  278. members = node.find('members')
  279. if members != None and len(list(members)) > 0:
  280. f.write(make_heading('Member Variables', '-'))
  281. for c in list(members):
  282. s = '- '
  283. s += make_type(c.attrib['type']) + ' '
  284. s += '**' + c.attrib['name'] + '**'
  285. if c.text.strip() != '':
  286. s += ' - ' + c.text.strip()
  287. f.write(s + '\n')
  288. f.write('\n')
  289. constants = node.find('constants')
  290. if constants != None and len(list(constants)) > 0:
  291. f.write(make_heading('Numeric Constants', '-'))
  292. for c in list(constants):
  293. s = '- '
  294. s += '**' + c.attrib['name'] + '**'
  295. if 'value' in c.attrib:
  296. s += ' = **' + c.attrib['value'] + '**'
  297. if c.text.strip() != '':
  298. s += ' --- ' + rstize_text(c.text.strip(),name)
  299. f.write(s + '\n')
  300. f.write('\n')
  301. descr = node.find('description')
  302. if descr != None and descr.text.strip() != '':
  303. f.write(make_heading('Description', '-'))
  304. f.write(rstize_text(descr.text.strip(),name) + "\n\n")
  305. methods = node.find('methods')
  306. if methods != None and len(list(methods)) > 0:
  307. f.write(make_heading('Member Function Description', '-'))
  308. for m in list(methods):
  309. f.write(".. _class_"+name+"_"+m.attrib['name']+":\n\n")
  310. # f.write(ul_string(m.attrib['name'],"^"))
  311. #f.write('\n<a name="'+m.attrib['name']+'">' + m.attrib['name'] + '</a>\n------\n')
  312. make_method(f, node.attrib['name'], m, True,name)
  313. f.write('\n')
  314. d = m.find('description')
  315. if d == None or d.text.strip() == '':
  316. continue
  317. f.write(rstize_text(d.text.strip(),name))
  318. f.write("\n\n")
  319. f.write('\n')
  320. for file in input_list:
  321. tree = ET.parse(file)
  322. doc = tree.getroot()
  323. if 'version' not in doc.attrib:
  324. print "Version missing from 'doc'"
  325. sys.exit(255)
  326. version = doc.attrib['version']
  327. for c in list(doc):
  328. if c.attrib['name'] in class_names:
  329. continue
  330. class_names.append(c.attrib['name'])
  331. classes[c.attrib['name']] = c
  332. class_names.sort()
  333. make_class_list(class_names, 2)
  334. for cn in class_names:
  335. c = classes[cn]
  336. make_rst_class(c)