Artifact Repository (Glare) Specifications
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.

redirect.py 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # A simple sphinx plugin which creates HTML redirections from old names
  2. # to new names. It does this by looking for files named "redirect" in
  3. # the documentation source and using the contents to create simple HTML
  4. # redirection pages for changed filenames.
  5. # Stolen from openstack/nova-specs
  6. import os.path
  7. from sphinx.application import ENV_PICKLE_FILENAME
  8. from sphinx.util.console import bold
  9. def setup(app):
  10. from sphinx import application
  11. if not isinstance(app, application.Sphinx):
  12. return
  13. app.connect('build-finished', emit_redirects)
  14. def process_redirect_file(app, path, ent):
  15. parent_path = path.replace(app.builder.srcdir, app.builder.outdir)
  16. with open(os.path.join(path, ent)) as redirects:
  17. for line in redirects.readlines():
  18. from_path, to_path = line.rstrip().split(' ')
  19. from_path = from_path.replace('.rst', '.html')
  20. to_path = to_path.replace('.rst', '.html')
  21. redirected_filename = os.path.join(parent_path, from_path)
  22. redirected_directory = os.path.dirname(redirected_filename)
  23. if not os.path.exists(redirected_directory):
  24. os.makedirs(redirected_directory)
  25. with open(redirected_filename, 'w') as f:
  26. f.write('<html><head><meta http-equiv="refresh" content="0; '
  27. 'url=%s" /></head></html>'
  28. % to_path)
  29. def emit_redirects(app, exc):
  30. app.builder.info(bold('scanning %s for redirects...') % app.builder.srcdir)
  31. def process_directory(path):
  32. for ent in os.listdir(path):
  33. p = os.path.join(path, ent)
  34. if os.path.isdir(p):
  35. process_directory(p)
  36. elif ent == 'redirects':
  37. app.builder.info(' found redirects at %s' % p)
  38. process_redirect_file(app, path, ent)
  39. process_directory(app.builder.srcdir)
  40. app.builder.info('...done')