html_redirects.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # Mechanism to generate static HTML redirect pages in the output
  16. #
  17. # Uses redirect_template.html and the list of pages given in
  18. # the file conf.html_redirect_pages
  19. #
  20. # Redirections can be internal or absolute (i.e., external).
  21. # - Internal redirects are supplied without quotation marks and must be relative to the document root
  22. # - External redirects are wrapped in doulbe quotation marks and are used verbatim
  23. #
  24. # Adapted from ideas in https://tech.signavio.com/2017/managing-sphinx-redirects
  25. import os.path
  26. from sphinx.builders.html import StandaloneHTMLBuilder
  27. REDIRECT_TEMPLATE = """
  28. <html>
  29. <head>
  30. <meta http-equiv="refresh" content="0; url=$NEWURL" />
  31. <script>
  32. window.location.href = "$NEWURL"
  33. </script>
  34. </head>
  35. <body>
  36. <p>Page has moved <a href="$NEWURL">here</a>.</p>
  37. </body>
  38. </html>
  39. """
  40. def setup(app):
  41. app.add_config_value('html_redirect_pages', [], 'html')
  42. # attaching to this event is a hack, but it's a convenient stage in the build
  43. # to create HTML redirects
  44. app.connect('html-collect-pages', create_redirect_pages)
  45. return {'parallel_read_safe': True, 'parallel_write_safe': True, 'version': '0.1'}
  46. def create_redirect_pages(app):
  47. if not isinstance(app.builder, StandaloneHTMLBuilder):
  48. return # only relevant for standalone HTML output
  49. for (old_url, new_url) in app.config.html_redirect_pages:
  50. if old_url.startswith('/'):
  51. print('Stripping leading / from URL in config file...')
  52. old_url = old_url[1:]
  53. out_file = app.builder.get_outfilename(old_url)
  54. if new_url.startswith('\"') and new_url.endswith('\"'):
  55. # This is an absolute redirect. Slice away the surrouding quotation marks and copy the url verbatim
  56. new_url = new_url[1:-1]
  57. else:
  58. # This is an internal redirect. Find the relative url to the target document
  59. new_url = app.builder.get_relative_uri(old_url, new_url)
  60. print('HTML file %s redirects to URL %s' % (out_file, new_url))
  61. out_dir = os.path.dirname(out_file)
  62. if not os.path.exists(out_dir):
  63. os.makedirs(out_dir)
  64. content = REDIRECT_TEMPLATE.replace('$NEWURL', new_url)
  65. with open(out_file, 'w') as rp:
  66. rp.write(content)
  67. return []