bitdoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (C) 2005 Holger Hans Peter Freyther
  4. #
  5. # SPDX-License-Identifier: GPL-2.0-only
  6. #
  7. import optparse, os, sys
  8. # bitbake
  9. sys.path.append(os.path.join(os.path.dirname(os.path.dirname(__file__), 'lib'))
  10. import bb
  11. import bb.parse
  12. from string import split, join
  13. __version__ = "0.0.2"
  14. class HTMLFormatter:
  15. """
  16. Simple class to help to generate some sort of HTML files. It is
  17. quite inferior solution compared to docbook, gtkdoc, doxygen but it
  18. should work for now.
  19. We've a global introduction site (index.html) and then one site for
  20. the list of keys (alphabetical sorted) and one for the list of groups,
  21. one site for each key with links to the relations and groups.
  22. index.html
  23. all_keys.html
  24. all_groups.html
  25. groupNAME.html
  26. keyNAME.html
  27. """
  28. def replace(self, text, *pairs):
  29. """
  30. From pydoc... almost identical at least
  31. """
  32. while pairs:
  33. (a, b) = pairs[0]
  34. text = join(split(text, a), b)
  35. pairs = pairs[1:]
  36. return text
  37. def escape(self, text):
  38. """
  39. Escape string to be conform HTML
  40. """
  41. return self.replace(text,
  42. ('&', '&'),
  43. ('<', '&lt;' ),
  44. ('>', '&gt;' ) )
  45. def createNavigator(self):
  46. """
  47. Create the navgiator
  48. """
  49. return """<table class="navigation" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2">
  50. <tr valign="middle">
  51. <td><a accesskey="g" href="index.html">Home</a></td>
  52. <td><a accesskey="n" href="all_groups.html">Groups</a></td>
  53. <td><a accesskey="u" href="all_keys.html">Keys</a></td>
  54. </tr></table>
  55. """
  56. def relatedKeys(self, item):
  57. """
  58. Create HTML to link to foreign keys
  59. """
  60. if len(item.related()) == 0:
  61. return ""
  62. txt = "<p><b>See also:</b><br>"
  63. txts = []
  64. for it in item.related():
  65. txts.append("""<a href="key%(it)s.html">%(it)s</a>""" % vars() )
  66. return txt + ",".join(txts)
  67. def groups(self, item):
  68. """
  69. Create HTML to link to related groups
  70. """
  71. if len(item.groups()) == 0:
  72. return ""
  73. txt = "<p><b>See also:</b><br>"
  74. txts = []
  75. for group in item.groups():
  76. txts.append( """<a href="group%s.html">%s</a> """ % (group, group) )
  77. return txt + ",".join(txts)
  78. def createKeySite(self, item):
  79. """
  80. Create a site for a key. It contains the header/navigator, a heading,
  81. the description, links to related keys and to the groups.
  82. """
  83. return """<!doctype html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  84. <html><head><title>Key %s</title></head>
  85. <link rel="stylesheet" href="style.css" type="text/css">
  86. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  87. %s
  88. <h2><span class="refentrytitle">%s</span></h2>
  89. <div class="refsynopsisdiv">
  90. <h2>Synopsis</h2>
  91. <p>
  92. %s
  93. </p>
  94. </div>
  95. <div class="refsynopsisdiv">
  96. <h2>Related Keys</h2>
  97. <p>
  98. %s
  99. </p>
  100. </div>
  101. <div class="refsynopsisdiv">
  102. <h2>Groups</h2>
  103. <p>
  104. %s
  105. </p>
  106. </div>
  107. </body>
  108. """ % (item.name(), self.createNavigator(), item.name(),
  109. self.escape(item.description()), self.relatedKeys(item), self.groups(item))
  110. def createGroupsSite(self, doc):
  111. """
  112. Create the Group Overview site
  113. """
  114. groups = ""
  115. sorted_groups = sorted(doc.groups())
  116. for group in sorted_groups:
  117. groups += """<a href="group%s.html">%s</a><br>""" % (group, group)
  118. return """<!doctype html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  119. <html><head><title>Group overview</title></head>
  120. <link rel="stylesheet" href="style.css" type="text/css">
  121. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  122. %s
  123. <h2>Available Groups</h2>
  124. %s
  125. </body>
  126. """ % (self.createNavigator(), groups)
  127. def createIndex(self):
  128. """
  129. Create the index file
  130. """
  131. return """<!doctype html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  132. <html><head><title>Bitbake Documentation</title></head>
  133. <link rel="stylesheet" href="style.css" type="text/css">
  134. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  135. %s
  136. <h2>Documentation Entrance</h2>
  137. <a href="all_groups.html">All available groups</a><br>
  138. <a href="all_keys.html">All available keys</a><br>
  139. </body>
  140. """ % self.createNavigator()
  141. def createKeysSite(self, doc):
  142. """
  143. Create Overview of all avilable keys
  144. """
  145. keys = ""
  146. sorted_keys = sorted(doc.doc_keys())
  147. for key in sorted_keys:
  148. keys += """<a href="key%s.html">%s</a><br>""" % (key, key)
  149. return """<!doctype html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  150. <html><head><title>Key overview</title></head>
  151. <link rel="stylesheet" href="style.css" type="text/css">
  152. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  153. %s
  154. <h2>Available Keys</h2>
  155. %s
  156. </body>
  157. """ % (self.createNavigator(), keys)
  158. def createGroupSite(self, gr, items, _description = None):
  159. """
  160. Create a site for a group:
  161. Group the name of the group, items contain the name of the keys
  162. inside this group
  163. """
  164. groups = ""
  165. description = ""
  166. # create a section with the group descriptions
  167. if _description:
  168. description += "<h2 Description of Grozp %s</h2>" % gr
  169. description += _description
  170. items.sort(lambda x, y:cmp(x.name(), y.name()))
  171. for group in items:
  172. groups += """<a href="key%s.html">%s</a><br>""" % (group.name(), group.name())
  173. return """<!doctype html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  174. <html><head><title>Group %s</title></head>
  175. <link rel="stylesheet" href="style.css" type="text/css">
  176. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  177. %s
  178. %s
  179. <div class="refsynopsisdiv">
  180. <h2>Keys in Group %s</h2>
  181. <pre class="synopsis">
  182. %s
  183. </pre>
  184. </div>
  185. </body>
  186. """ % (gr, self.createNavigator(), description, gr, groups)
  187. def createCSS(self):
  188. """
  189. Create the CSS file
  190. """
  191. return """.synopsis, .classsynopsis
  192. {
  193. background: #eeeeee;
  194. border: solid 1px #aaaaaa;
  195. padding: 0.5em;
  196. }
  197. .programlisting
  198. {
  199. background: #eeeeff;
  200. border: solid 1px #aaaaff;
  201. padding: 0.5em;
  202. }
  203. .variablelist
  204. {
  205. padding: 4px;
  206. margin-left: 3em;
  207. }
  208. .variablelist td:first-child
  209. {
  210. vertical-align: top;
  211. }
  212. table.navigation
  213. {
  214. background: #ffeeee;
  215. border: solid 1px #ffaaaa;
  216. margin-top: 0.5em;
  217. margin-bottom: 0.5em;
  218. }
  219. .navigation a
  220. {
  221. color: #770000;
  222. }
  223. .navigation a:visited
  224. {
  225. color: #550000;
  226. }
  227. .navigation .title
  228. {
  229. font-size: 200%;
  230. }
  231. div.refnamediv
  232. {
  233. margin-top: 2em;
  234. }
  235. div.gallery-float
  236. {
  237. float: left;
  238. padding: 10px;
  239. }
  240. div.gallery-float img
  241. {
  242. border-style: none;
  243. }
  244. div.gallery-spacer
  245. {
  246. clear: both;
  247. }
  248. a
  249. {
  250. text-decoration: none;
  251. }
  252. a:hover
  253. {
  254. text-decoration: underline;
  255. color: #FF0000;
  256. }
  257. """
  258. class DocumentationItem:
  259. """
  260. A class to hold information about a configuration
  261. item. It contains the key name, description, a list of related names,
  262. and the group this item is contained in.
  263. """
  264. def __init__(self):
  265. self._groups = []
  266. self._related = []
  267. self._name = ""
  268. self._desc = ""
  269. def groups(self):
  270. return self._groups
  271. def name(self):
  272. return self._name
  273. def description(self):
  274. return self._desc
  275. def related(self):
  276. return self._related
  277. def setName(self, name):
  278. self._name = name
  279. def setDescription(self, desc):
  280. self._desc = desc
  281. def addGroup(self, group):
  282. self._groups.append(group)
  283. def addRelation(self, relation):
  284. self._related.append(relation)
  285. def sort(self):
  286. self._related.sort()
  287. self._groups.sort()
  288. class Documentation:
  289. """
  290. Holds the documentation... with mappings from key to items...
  291. """
  292. def __init__(self):
  293. self.__keys = {}
  294. self.__groups = {}
  295. def insert_doc_item(self, item):
  296. """
  297. Insert the Doc Item into the internal list
  298. of representation
  299. """
  300. item.sort()
  301. self.__keys[item.name()] = item
  302. for group in item.groups():
  303. if not group in self.__groups:
  304. self.__groups[group] = []
  305. self.__groups[group].append(item)
  306. self.__groups[group].sort()
  307. def doc_item(self, key):
  308. """
  309. Return the DocumentationInstance describing the key
  310. """
  311. try:
  312. return self.__keys[key]
  313. except KeyError:
  314. return None
  315. def doc_keys(self):
  316. """
  317. Return the documented KEYS (names)
  318. """
  319. return self.__keys.keys()
  320. def groups(self):
  321. """
  322. Return the names of available groups
  323. """
  324. return self.__groups.keys()
  325. def group_content(self, group_name):
  326. """
  327. Return a list of keys/names that are in a specefic
  328. group or the empty list
  329. """
  330. try:
  331. return self.__groups[group_name]
  332. except KeyError:
  333. return []
  334. def parse_cmdline(args):
  335. """
  336. Parse the CMD line and return the result as a n-tuple
  337. """
  338. parser = optparse.OptionParser( version = "Bitbake Documentation Tool Core version %s, %%prog version %s" % (bb.__version__, __version__))
  339. usage = """%prog [options]
  340. Create a set of html pages (documentation) for a bitbake.conf....
  341. """
  342. # Add the needed options
  343. parser.add_option( "-c", "--config", help = "Use the specified configuration file as source",
  344. action = "store", dest = "config", default = os.path.join("conf", "documentation.conf") )
  345. parser.add_option( "-o", "--output", help = "Output directory for html files",
  346. action = "store", dest = "output", default = "html/" )
  347. parser.add_option( "-D", "--debug", help = "Increase the debug level",
  348. action = "count", dest = "debug", default = 0 )
  349. parser.add_option( "-v", "--verbose", help = "output more chit-char to the terminal",
  350. action = "store_true", dest = "verbose", default = False )
  351. options, args = parser.parse_args( sys.argv )
  352. bb.msg.init_msgconfig(options.verbose, options.debug)
  353. return options.config, options.output
  354. def main():
  355. """
  356. The main Method
  357. """
  358. (config_file, output_dir) = parse_cmdline( sys.argv )
  359. # right to let us load the file now
  360. try:
  361. documentation = bb.parse.handle( config_file, bb.data.init() )
  362. except IOError:
  363. bb.fatal( "Unable to open %s" % config_file )
  364. except bb.parse.ParseError:
  365. bb.fatal( "Unable to parse %s" % config_file )
  366. if isinstance(documentation, dict):
  367. documentation = documentation[""]
  368. # Assuming we've the file loaded now, we will initialize the 'tree'
  369. doc = Documentation()
  370. # defined states
  371. state_begin = 0
  372. state_see = 1
  373. state_group = 2
  374. for key in bb.data.keys(documentation):
  375. data = documentation.getVarFlag(key, "doc", False)
  376. if not data:
  377. continue
  378. # The Documentation now starts
  379. doc_ins = DocumentationItem()
  380. doc_ins.setName(key)
  381. tokens = data.split(' ')
  382. state = state_begin
  383. string= ""
  384. for token in tokens:
  385. token = token.strip(',')
  386. if not state == state_see and token == "@see":
  387. state = state_see
  388. continue
  389. elif not state == state_group and token == "@group":
  390. state = state_group
  391. continue
  392. if state == state_begin:
  393. string += " %s" % token
  394. elif state == state_see:
  395. doc_ins.addRelation(token)
  396. elif state == state_group:
  397. doc_ins.addGroup(token)
  398. # set the description
  399. doc_ins.setDescription(string)
  400. doc.insert_doc_item(doc_ins)
  401. # let us create the HTML now
  402. bb.utils.mkdirhier(output_dir)
  403. os.chdir(output_dir)
  404. # Let us create the sites now. We do it in the following order
  405. # Start with the index.html. It will point to sites explaining all
  406. # keys and groups
  407. html_slave = HTMLFormatter()
  408. f = file('style.css', 'w')
  409. print >> f, html_slave.createCSS()
  410. f = file('index.html', 'w')
  411. print >> f, html_slave.createIndex()
  412. f = file('all_groups.html', 'w')
  413. print >> f, html_slave.createGroupsSite(doc)
  414. f = file('all_keys.html', 'w')
  415. print >> f, html_slave.createKeysSite(doc)
  416. # now for each group create the site
  417. for group in doc.groups():
  418. f = file('group%s.html' % group, 'w')
  419. print >> f, html_slave.createGroupSite(group, doc.group_content(group))
  420. # now for the keys
  421. for key in doc.doc_keys():
  422. f = file('key%s.html' % doc.doc_item(key).name(), 'w')
  423. print >> f, html_slave.createKeySite(doc.doc_item(key))
  424. if __name__ == "__main__":
  425. main()