bitdoc 13 KB

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