gh_links.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python3
  2. # Sphinx extension to add roles for some GitHub features
  3. # Copyright © 2013-2015 Luca Wehrstedt <luca.wehrstedt@gmail.com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. from docutils import nodes, utils
  18. from sphinx.util.nodes import split_explicit_title
  19. def gh_issue(typ, rawtext, text, lineno, inliner, options={}, content=[]):
  20. text = utils.unescape(text)
  21. has_explicit_title, title, part = split_explicit_title(text)
  22. if not has_explicit_title:
  23. title = 'issue #%s' % part
  24. full_url = 'https://github.com/cms-dev/cms/issues/%s' % part
  25. retnode = nodes.reference(title, title, internal=False, refuri=full_url, **options)
  26. return [retnode], []
  27. def make_gh_download(app):
  28. def gh_download(typ, rawtext, text, lineno, inliner, options={}, content=[]):
  29. title = utils.unescape(text)
  30. full_url = 'https://github.com/cms-dev/cms/releases/download/v%(ver)s/v%(ver)s.tar.gz' % {"ver": app.config.release}
  31. retnode = nodes.reference(title, title, internal=False, refuri=full_url, **options)
  32. return [retnode], []
  33. return gh_download
  34. def make_gh_tree(app):
  35. def gh_tree(typ, rawtext, text, lineno, inliner, options={}, content=[]):
  36. text = utils.unescape(text)
  37. has_explicit_title, title, part = split_explicit_title(text)
  38. if not has_explicit_title:
  39. title = part
  40. full_url = 'https://github.com/cms-dev/cms/tree/v%s/%s' % (app.config.release, part)
  41. refnode = nodes.reference(title, title, internal=False, refuri=full_url, **options)
  42. return [refnode], []
  43. return gh_tree
  44. def make_gh_blob(app):
  45. def gh_blob(typ, rawtext, text, lineno, inliner, options={}, content=[]):
  46. text = utils.unescape(text)
  47. has_explicit_title, title, part = split_explicit_title(text)
  48. if not has_explicit_title:
  49. title = part
  50. full_url = 'https://github.com/cms-dev/cms/blob/v%s/%s' % (app.config.release, part)
  51. refnode = nodes.reference(title, title, internal=False, refuri=full_url, **options)
  52. return [refnode], []
  53. return gh_blob
  54. def setup_roles(app):
  55. app.add_role("gh_issue", gh_issue)
  56. app.add_role("gh_download", make_gh_download(app))
  57. app.add_role("gh_tree", make_gh_tree(app))
  58. app.add_role("gh_blob", make_gh_blob(app))
  59. def setup(app):
  60. app.connect('builder-inited', setup_roles)