phpcode.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- coding: utf-8 -*-
  2. """
  3. :copyright: (c) 2010-2012 Fabien Potencier
  4. :license: MIT, see LICENSE for more details.
  5. """
  6. from docutils import nodes, utils
  7. from sphinx.util.nodes import split_explicit_title
  8. from string import lower
  9. def php_namespace_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
  10. text = utils.unescape(text)
  11. env = inliner.document.settings.env
  12. base_url = env.app.config.api_url
  13. has_explicit_title, title, namespace = split_explicit_title(text)
  14. try:
  15. full_url = base_url % namespace.replace('\\', '/') + '.html'
  16. except (TypeError, ValueError):
  17. env.warn(env.docname, 'unable to expand %s api_url with base '
  18. 'URL %r, please make sure the base contains \'%%s\' '
  19. 'exactly once' % (typ, base_url))
  20. full_url = base_url + utils.escape(full_class)
  21. if not has_explicit_title:
  22. name = namespace.lstrip('\\')
  23. ns = name.rfind('\\')
  24. if ns != -1:
  25. name = name[ns+1:]
  26. title = name
  27. list = [nodes.reference(title, title, internal=False, refuri=full_url, reftitle=namespace)]
  28. pnode = nodes.literal('', '', *list)
  29. return [pnode], []
  30. def php_class_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
  31. text = utils.unescape(text)
  32. env = inliner.document.settings.env
  33. base_url = env.app.config.api_url
  34. has_explicit_title, title, full_class = split_explicit_title(text)
  35. try:
  36. full_url = base_url % full_class.replace('\\', '/') + '.html'
  37. except (TypeError, ValueError):
  38. env.warn(env.docname, 'unable to expand %s api_url with base '
  39. 'URL %r, please make sure the base contains \'%%s\' '
  40. 'exactly once' % (typ, base_url))
  41. full_url = base_url + utils.escape(full_class)
  42. if not has_explicit_title:
  43. class_name = full_class.lstrip('\\')
  44. ns = class_name.rfind('\\')
  45. if ns != -1:
  46. class_name = class_name[ns+1:]
  47. title = class_name
  48. list = [nodes.reference(title, title, internal=False, refuri=full_url, reftitle=full_class)]
  49. pnode = nodes.literal('', '', *list)
  50. return [pnode], []
  51. def php_method_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
  52. text = utils.unescape(text)
  53. env = inliner.document.settings.env
  54. base_url = env.app.config.api_url
  55. has_explicit_title, title, class_and_method = split_explicit_title(text)
  56. ns = class_and_method.rfind('::')
  57. full_class = class_and_method[:ns]
  58. method = class_and_method[ns+2:]
  59. try:
  60. full_url = base_url % full_class.replace('\\', '/') + '.html' + '#method_' + method
  61. except (TypeError, ValueError):
  62. env.warn(env.docname, 'unable to expand %s api_url with base '
  63. 'URL %r, please make sure the base contains \'%%s\' '
  64. 'exactly once' % (typ, base_url))
  65. full_url = base_url + utils.escape(full_class)
  66. if not has_explicit_title:
  67. title = method + '()'
  68. list = [nodes.reference(title, title, internal=False, refuri=full_url, reftitle=full_class + '::' + method + '()')]
  69. pnode = nodes.literal('', '', *list)
  70. return [pnode], []
  71. def php_phpclass_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
  72. text = utils.unescape(text)
  73. has_explicit_title, title, full_class = split_explicit_title(text)
  74. full_url = 'http://php.net/manual/en/class.%s.php' % lower(full_class)
  75. if not has_explicit_title:
  76. title = full_class
  77. list = [nodes.reference(title, title, internal=False, refuri=full_url, reftitle=full_class)]
  78. pnode = nodes.literal('', '', *list)
  79. return [pnode], []
  80. def php_phpfunction_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
  81. text = utils.unescape(text)
  82. has_explicit_title, title, full_function = split_explicit_title(text)
  83. full_url = 'http://php.net/manual/en/function.%s.php' % lower(full_function.replace('_', '-'))
  84. if not has_explicit_title:
  85. title = full_function
  86. list = [nodes.reference(title, title, internal=False, refuri=full_url, reftitle=full_function)]
  87. pnode = nodes.literal('', '', *list)
  88. return [pnode], []
  89. def setup(app):
  90. app.add_config_value('api_url', {}, 'env')
  91. app.add_role('namespace', php_namespace_role)
  92. app.add_role('class', php_class_role)
  93. app.add_role('method', php_method_role)
  94. app.add_role('phpclass', php_phpclass_role)
  95. app.add_role('phpfunction', php_phpfunction_role)