link-roles.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # based on http://protips.readthedocs.io/link-roles.html
  2. import re
  3. import os
  4. from docutils import nodes
  5. from local_util import run_cmd_get_output
  6. def get_github_rev():
  7. path = run_cmd_get_output('git rev-parse --short HEAD')
  8. tag = run_cmd_get_output('git describe --exact-match')
  9. print ('Git commit ID: ', path)
  10. if len(tag):
  11. print ('Git tag: ', tag)
  12. path = tag
  13. return path
  14. def setup(app):
  15. rev = get_github_rev()
  16. # links to files or folders on the GitHub
  17. baseurl = 'https://github.com/espressif/esp-idf'
  18. app.add_role('idf', autolink('{}/tree/{}/%s'.format(baseurl, rev)))
  19. app.add_role('idf_file', autolink('{}/blob/{}/%s'.format(baseurl, rev)))
  20. app.add_role('idf_raw', autolink('{}/raw/{}/%s'.format(baseurl, rev)))
  21. app.add_role('component', autolink('{}/tree/{}/components/%s'.format(baseurl, rev)))
  22. app.add_role('component_file', autolink('{}/blob/{}/components/%s'.format(baseurl, rev)))
  23. app.add_role('component_raw', autolink('{}/raw/{}/components/%s'.format(baseurl, rev)))
  24. app.add_role('example', autolink('{}/tree/{}/examples/%s'.format(baseurl, rev)))
  25. app.add_role('example_file', autolink('{}/blob/{}/examples/%s'.format(baseurl, rev)))
  26. app.add_role('example_raw', autolink('{}/raw/{}/examples/%s'.format(baseurl, rev)))
  27. # link to the current documentation file in specific language version
  28. on_rtd = os.environ.get('READTHEDOCS', None) == 'True'
  29. if on_rtd:
  30. # provide RTD specific commit identification to be included in the link
  31. tag_rev = 'latest'
  32. if (run_cmd_get_output('git rev-parse --short HEAD') != rev):
  33. tag_rev = rev
  34. else:
  35. # if not on the RTD then provide generic identification
  36. tag_rev = run_cmd_get_output('git describe --always')
  37. app.add_role('link_to_translation', crosslink('%s../../%s/{}/%s.html'.format(tag_rev)))
  38. def autolink(pattern):
  39. def role(name, rawtext, text, lineno, inliner, options={}, content=[]):
  40. m = re.search('(.*)\s*<(.*)>', text)
  41. if m:
  42. link_text = m.group(1)
  43. link = m.group(2)
  44. else:
  45. link_text = text
  46. link = text
  47. url = pattern % (link,)
  48. node = nodes.reference(rawtext, link_text, refuri=url, **options)
  49. return [node], []
  50. return role
  51. def crosslink(pattern):
  52. def role(name, rawtext, text, lineno, inliner, options={}, content=[]):
  53. (language, link_text) = text.split(':')
  54. docname = inliner.document.settings.env.docname
  55. doc_path = inliner.document.settings.env.doc2path(docname, None, None)
  56. return_path = '../' * doc_path.count('/')
  57. url = pattern % (return_path, language, docname)
  58. node = nodes.reference(rawtext, link_text, refuri=url, **options)
  59. return [node], []
  60. return role