Tools used by OpenStack Documentation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.0KB

  1. #!/usr/bin/env python
  2. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  3. # not use this file except in compliance with the License. You may obtain
  4. # a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  10. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  11. # License for the specific language governing permissions and limitations
  12. # under the License.
  13. import argparse
  14. import glob
  15. import os
  16. import sys
  17. def get_pdf_link(root, publish_path):
  18. p = '%s/*.pdf' % root
  19. re = glob.glob(p)
  20. if len(re) == 0:
  21. return ''
  22. filename = os.path.basename(re[0])
  23. path = os.path.relpath(root, publish_path)
  24. return ' <a href="%s/%s">(pdf)</a>' % (path, filename)
  25. def generate_index_file(publish_path):
  26. """Generate index.html file in publish_path."""
  27. if not os.path.isdir(publish_path):
  28. os.mkdir(publish_path)
  29. index_file = open(os.path.join(publish_path, 'index.html'), 'w')
  30. index_file.write(
  31. '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n'
  32. '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n'
  33. '<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">\n'
  34. '<body>\n'
  35. '<h1>Generated documents</h1>\n')
  36. links = {}
  37. for root, dirs, files in os.walk(publish_path):
  38. dirs[:] = [d for d in dirs if d not in ['common', 'webapp', 'content',
  39. 'www', 'samples']]
  40. # Ignore top-level index.html files
  41. if root == publish_path:
  42. continue
  43. pdf_link = get_pdf_link(root, publish_path)
  44. if os.path.isfile(os.path.join(root, 'index.html')):
  45. path = os.path.relpath(root, publish_path)
  46. links[path] = ('<a href="%s/index.html">%s</a>%s\n' %
  47. (path, path.replace('draft/', ''), pdf_link))
  48. for entry in sorted([s for s in links if not s.startswith('draft/')]):
  49. index_file.write(links[entry])
  50. index_file.write('<br/>\n')
  51. drafts = [s for s in links if s.startswith('draft/')]
  52. if drafts:
  53. index_file.write('<h2>Draft guides</h2>\n')
  54. for entry in sorted(drafts):
  55. index_file.write(links[entry])
  56. index_file.write('<br/>\n')
  57. if os.path.isfile(os.path.join(publish_path, 'www-index.html')):
  58. index_file.write('<h2>WWW index pages</h2>\n')
  59. index_file.write('<a href="www-index.html">List of generated '
  60. 'WWW pages</a>\n')
  61. index_file.write('</body>\n'
  62. '</html>\n')
  63. index_file.close()
  64. def main():
  65. parser = argparse.ArgumentParser(description="Generate index file.")
  66. parser.add_argument('directory', metavar='DIR',
  67. help="Directory to search.")
  68. args = parser.parse_args()
  69. generate_index_file(args.directory)
  70. if __name__ == "__main__":
  71. sys.exit(main())