doc-generator.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python
  2. #
  3. # doc-generator.py
  4. #
  5. # Generates HTML documentation from the parsed spec using Cheetah templates.
  6. #
  7. # Copyright (C) 2009 Collabora Ltd.
  8. #
  9. # This library is free software; you can redistribute it and/or modify it
  10. # under the terms of the GNU Lesser General Public License as published by
  11. # the Free Software Foundation; either version 2.1 of the License, or (at
  12. # your option) any later version.
  13. #
  14. # This library is distributed in the hope that it will be useful, but WITHOUT
  15. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  17. # for more details.
  18. #
  19. # You should have received a copy of the GNU Lesser General Public License
  20. # along with this library; if not, write to the Free Software Foundation,
  21. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. #
  23. # Authors: Davyd Madeley <davyd.madeley@collabora.co.uk>
  24. #
  25. import sys
  26. import os
  27. import os.path
  28. import shutil
  29. try:
  30. from Cheetah.Template import Template
  31. except ImportError, e:
  32. print >> sys.stderr, e
  33. print >> sys.stderr, "Install `python-cheetah'?"
  34. sys.exit(-1)
  35. import specparser
  36. program, spec_file, output_path, project, namespace = sys.argv
  37. template_path = os.path.join(os.path.dirname(program), '../doc/templates')
  38. # make the output path
  39. try:
  40. os.mkdir(output_path)
  41. except OSError:
  42. pass
  43. # copy in the CSS
  44. shutil.copy(os.path.join(template_path, 'style.css'), output_path)
  45. def load_template(filename):
  46. try:
  47. file = open(os.path.join(template_path, filename))
  48. template_def = file.read()
  49. file.close()
  50. except IOError, e:
  51. print >> sys.stderr, "Could not load template file `%s'" % filename
  52. print >> sys.stderr, e
  53. sys.exit(-1)
  54. return template_def
  55. spec = specparser.parse(spec_file, namespace)
  56. # write out HTML files for each of the interfaces
  57. # Not using render_template here to avoid recompiling it n times.
  58. namespace = {}
  59. template_def = load_template('interface.html')
  60. t = Template(template_def, namespaces = [namespace])
  61. for interface in spec.interfaces:
  62. namespace['interface'] = interface
  63. # open the output file
  64. out = open(os.path.join(output_path, '%s.html' % interface.name), 'w')
  65. print >> out, unicode(t).encode('utf-8')
  66. out.close()
  67. def render_template(name, namespaces, target=None):
  68. if target is None:
  69. target = name
  70. namespace = { 'spec': spec }
  71. template_def = load_template(name)
  72. t = Template(template_def, namespaces=namespaces)
  73. out = open(os.path.join(output_path, target), 'w')
  74. print >> out, unicode(t).encode('utf-8')
  75. out.close()
  76. namespaces = { 'spec': spec }
  77. render_template('generic-types.html', namespaces)
  78. render_template('errors.html', namespaces)
  79. render_template('interfaces.html', namespaces)
  80. render_template('fullindex.html', namespaces)
  81. dh_namespaces = { 'spec': spec, 'name': project }
  82. render_template('devhelp.devhelp2', dh_namespaces,
  83. target=('%s.devhelp2' % project))
  84. # write out the TOC last, because this is the file used as the target in the
  85. # Makefile.
  86. render_template('index.html', namespaces)