bitdoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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(sys.argv[0])), '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 = doc.groups()
  128. sorted_groups.sort()
  129. for group in sorted_groups:
  130. groups += """<a href="group%s.html">%s</a><br>""" % (group, group)
  131. return """<!doctype html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  132. <html><head><title>Group overview</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>Available Groups</h2>
  137. %s
  138. </body>
  139. """ % (self.createNavigator(), groups)
  140. def createIndex(self):
  141. """
  142. Create the index file
  143. """
  144. return """<!doctype html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  145. <html><head><title>Bitbake Documentation</title></head>
  146. <link rel="stylesheet" href="style.css" type="text/css">
  147. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  148. %s
  149. <h2>Documentation Entrance</h2>
  150. <a href="all_groups.html">All available groups</a><br>
  151. <a href="all_keys.html">All available keys</a><br>
  152. </body>
  153. """ % self.createNavigator()
  154. def createKeysSite(self, doc):
  155. """
  156. Create Overview of all avilable keys
  157. """
  158. keys = ""
  159. sorted_keys = doc.doc_keys()
  160. sorted_keys.sort()
  161. for key in sorted_keys:
  162. keys += """<a href="key%s.html">%s</a><br>""" % (key, key)
  163. return """<!doctype html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  164. <html><head><title>Key overview</title></head>
  165. <link rel="stylesheet" href="style.css" type="text/css">
  166. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  167. %s
  168. <h2>Available Keys</h2>
  169. %s
  170. </body>
  171. """ % (self.createNavigator(), keys)
  172. def createGroupSite(self, gr, items, _description = None):
  173. """
  174. Create a site for a group:
  175. Group the name of the group, items contain the name of the keys
  176. inside this group
  177. """
  178. groups = ""
  179. description = ""
  180. # create a section with the group descriptions
  181. if _description:
  182. description += "<h2 Description of Grozp %s</h2>" % gr
  183. description += _description
  184. items.sort(lambda x,y:cmp(x.name(),y.name()))
  185. for group in items:
  186. groups += """<a href="key%s.html">%s</a><br>""" % (group.name(), group.name())
  187. return """<!doctype html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  188. <html><head><title>Group %s</title></head>
  189. <link rel="stylesheet" href="style.css" type="text/css">
  190. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  191. %s
  192. %s
  193. <div class="refsynopsisdiv">
  194. <h2>Keys in Group %s</h2>
  195. <pre class="synopsis">
  196. %s
  197. </pre>
  198. </div>
  199. </body>
  200. """ % (gr, self.createNavigator(), description, gr, groups)
  201. def createCSS(self):
  202. """
  203. Create the CSS file
  204. """
  205. return """.synopsis, .classsynopsis
  206. {
  207. background: #eeeeee;
  208. border: solid 1px #aaaaaa;
  209. padding: 0.5em;
  210. }
  211. .programlisting
  212. {
  213. background: #eeeeff;
  214. border: solid 1px #aaaaff;
  215. padding: 0.5em;
  216. }
  217. .variablelist
  218. {
  219. padding: 4px;
  220. margin-left: 3em;
  221. }
  222. .variablelist td:first-child
  223. {
  224. vertical-align: top;
  225. }
  226. table.navigation
  227. {
  228. background: #ffeeee;
  229. border: solid 1px #ffaaaa;
  230. margin-top: 0.5em;
  231. margin-bottom: 0.5em;
  232. }
  233. .navigation a
  234. {
  235. color: #770000;
  236. }
  237. .navigation a:visited
  238. {
  239. color: #550000;
  240. }
  241. .navigation .title
  242. {
  243. font-size: 200%;
  244. }
  245. div.refnamediv
  246. {
  247. margin-top: 2em;
  248. }
  249. div.gallery-float
  250. {
  251. float: left;
  252. padding: 10px;
  253. }
  254. div.gallery-float img
  255. {
  256. border-style: none;
  257. }
  258. div.gallery-spacer
  259. {
  260. clear: both;
  261. }
  262. a
  263. {
  264. text-decoration: none;
  265. }
  266. a:hover
  267. {
  268. text-decoration: underline;
  269. color: #FF0000;
  270. }
  271. """
  272. class DocumentationItem:
  273. """
  274. A class to hold information about a configuration
  275. item. It contains the key name, description, a list of related names,
  276. and the group this item is contained in.
  277. """
  278. def __init__(self):
  279. self._groups = []
  280. self._related = []
  281. self._name = ""
  282. self._desc = ""
  283. def groups(self):
  284. return self._groups
  285. def name(self):
  286. return self._name
  287. def description(self):
  288. return self._desc
  289. def related(self):
  290. return self._related
  291. def setName(self, name):
  292. self._name = name
  293. def setDescription(self, desc):
  294. self._desc = desc
  295. def addGroup(self, group):
  296. self._groups.append(group)
  297. def addRelation(self,relation):
  298. self._related.append(relation)
  299. def sort(self):
  300. self._related.sort()
  301. self._groups.sort()
  302. class Documentation:
  303. """
  304. Holds the documentation... with mappings from key to items...
  305. """
  306. def __init__(self):
  307. self.__keys = {}
  308. self.__groups = {}
  309. def insert_doc_item(self, item):
  310. """
  311. Insert the Doc Item into the internal list
  312. of representation
  313. """
  314. item.sort()
  315. self.__keys[item.name()] = item
  316. for group in item.groups():
  317. if not group in self.__groups:
  318. self.__groups[group] = []
  319. self.__groups[group].append(item)
  320. self.__groups[group].sort()
  321. def doc_item(self, key):
  322. """
  323. Return the DocumentationInstance describing the key
  324. """
  325. try:
  326. return self.__keys[key]
  327. except KeyError:
  328. return None
  329. def doc_keys(self):
  330. """
  331. Return the documented KEYS (names)
  332. """
  333. return self.__keys.keys()
  334. def groups(self):
  335. """
  336. Return the names of available groups
  337. """
  338. return self.__groups.keys()
  339. def group_content(self,group_name):
  340. """
  341. Return a list of keys/names that are in a specefic
  342. group or the empty list
  343. """
  344. try:
  345. return self.__groups[group_name]
  346. except KeyError:
  347. return []
  348. def parse_cmdline(args):
  349. """
  350. Parse the CMD line and return the result as a n-tuple
  351. """
  352. parser = optparse.OptionParser( version = "Bitbake Documentation Tool Core version %s, %%prog version %s" % (bb.__version__,__version__))
  353. usage = """%prog [options]
  354. Create a set of html pages (documentation) for a bitbake.conf....
  355. """
  356. # Add the needed options
  357. parser.add_option( "-c", "--config", help = "Use the specified configuration file as source",
  358. action = "store", dest = "config", default = os.path.join("conf", "documentation.conf") )
  359. parser.add_option( "-o", "--output", help = "Output directory for html files",
  360. action = "store", dest = "output", default = "html/" )
  361. parser.add_option( "-D", "--debug", help = "Increase the debug level",
  362. action = "count", dest = "debug", default = 0 )
  363. parser.add_option( "-v","--verbose", help = "output more chit-char to the terminal",
  364. action = "store_true", dest = "verbose", default = False )
  365. options, args = parser.parse_args( sys.argv )
  366. if options.debug:
  367. bb.msg.set_debug_level(options.debug)
  368. return options.config, options.output
  369. def main():
  370. """
  371. The main Method
  372. """
  373. (config_file,output_dir) = parse_cmdline( sys.argv )
  374. # right to let us load the file now
  375. try:
  376. documentation = bb.parse.handle( config_file, bb.data.init() )
  377. except IOError:
  378. bb.fatal( "Unable to open %s" % config_file )
  379. except bb.parse.ParseError:
  380. bb.fatal( "Unable to parse %s" % config_file )
  381. # Assuming we've the file loaded now, we will initialize the 'tree'
  382. doc = Documentation()
  383. # defined states
  384. state_begin = 0
  385. state_see = 1
  386. state_group = 2
  387. for key in bb.data.keys(documentation):
  388. data = bb.data.getVarFlag(key, "doc", documentation)
  389. if not data:
  390. continue
  391. # The Documentation now starts
  392. doc_ins = DocumentationItem()
  393. doc_ins.setName(key)
  394. tokens = data.split(' ')
  395. state = state_begin
  396. string= ""
  397. for token in tokens:
  398. token = token.strip(',')
  399. if not state == state_see and token == "@see":
  400. state = state_see
  401. continue
  402. elif not state == state_group and token == "@group":
  403. state = state_group
  404. continue
  405. if state == state_begin:
  406. string += " %s" % token
  407. elif state == state_see:
  408. doc_ins.addRelation(token)
  409. elif state == state_group:
  410. doc_ins.addGroup(token)
  411. # set the description
  412. doc_ins.setDescription(string)
  413. doc.insert_doc_item(doc_ins)
  414. # let us create the HTML now
  415. bb.mkdirhier(output_dir)
  416. os.chdir(output_dir)
  417. # Let us create the sites now. We do it in the following order
  418. # Start with the index.html. It will point to sites explaining all
  419. # keys and groups
  420. html_slave = HTMLFormatter()
  421. f = file('style.css', 'w')
  422. print >> f, html_slave.createCSS()
  423. f = file('index.html', 'w')
  424. print >> f, html_slave.createIndex()
  425. f = file('all_groups.html', 'w')
  426. print >> f, html_slave.createGroupsSite(doc)
  427. f = file('all_keys.html', 'w')
  428. print >> f, html_slave.createKeysSite(doc)
  429. # now for each group create the site
  430. for group in doc.groups():
  431. f = file('group%s.html' % group, 'w')
  432. print >> f, html_slave.createGroupSite(group, doc.group_content(group))
  433. # now for the keys
  434. for key in doc.doc_keys():
  435. f = file('key%s.html' % doc.doc_item(key).name(), 'w')
  436. print >> f, html_slave.createKeySite(doc.doc_item(key))
  437. if __name__ == "__main__":
  438. main()