bitdoc 14 KB

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