php.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # -*- coding: utf-8 -*-
  2. """
  3. :copyright: (c) 2010-2012 Fabien Potencier
  4. :license: MIT, see LICENSE for more details.
  5. """
  6. from sphinx import addnodes
  7. from sphinx.domains import Domain, ObjType
  8. from sphinx.locale import l_, _
  9. from sphinx.directives import ObjectDescription
  10. from sphinx.domains.python import py_paramlist_re as js_paramlist_re
  11. from sphinx.roles import XRefRole
  12. from sphinx.util.nodes import make_refnode
  13. from sphinx.util.docfields import Field, GroupedField, TypedField
  14. def setup(app):
  15. app.add_domain(PHPDomain)
  16. class PHPXRefRole(XRefRole):
  17. def process_link(self, env, refnode, has_explicit_title, title, target):
  18. # basically what sphinx.domains.python.PyXRefRole does
  19. refnode['php:object'] = env.temp_data.get('php:object')
  20. if not has_explicit_title:
  21. title = title.lstrip('\\')
  22. target = target.lstrip('~')
  23. if title[0:1] == '~':
  24. title = title[1:]
  25. ns = title.rfind('\\')
  26. if ns != -1:
  27. title = title[ns+1:]
  28. if target[0:1] == '\\':
  29. target = target[1:]
  30. refnode['refspecific'] = True
  31. return title, target
  32. class PHPDomain(Domain):
  33. """PHP language domain."""
  34. name = 'php'
  35. label = 'PHP'
  36. # if you add a new object type make sure to edit JSObject.get_index_string
  37. object_types = {
  38. }
  39. directives = {
  40. }
  41. roles = {
  42. 'func': PHPXRefRole(fix_parens=True),
  43. 'class': PHPXRefRole(),
  44. 'data': PHPXRefRole(),
  45. 'attr': PHPXRefRole(),
  46. }
  47. initial_data = {
  48. 'objects': {}, # fullname -> docname, objtype
  49. }
  50. def clear_doc(self, docname):
  51. for fullname, (fn, _) in self.data['objects'].items():
  52. if fn == docname:
  53. del self.data['objects'][fullname]
  54. def find_obj(self, env, obj, name, typ, searchorder=0):
  55. if name[-2:] == '()':
  56. name = name[:-2]
  57. objects = self.data['objects']
  58. newname = None
  59. if searchorder == 1:
  60. if obj and obj + '\\' + name in objects:
  61. newname = obj + '\\' + name
  62. else:
  63. newname = name
  64. else:
  65. if name in objects:
  66. newname = name
  67. elif obj and obj + '\\' + name in objects:
  68. newname = obj + '\\' + name
  69. return newname, objects.get(newname)
  70. def resolve_xref(self, env, fromdocname, builder, typ, target, node,
  71. contnode):
  72. objectname = node.get('php:object')
  73. searchorder = node.hasattr('refspecific') and 1 or 0
  74. name, obj = self.find_obj(env, objectname, target, typ, searchorder)
  75. if not obj:
  76. return None
  77. return make_refnode(builder, fromdocname, obj[0], name, contnode, name)
  78. def get_objects(self):
  79. for refname, (docname, type) in self.data['objects'].iteritems():
  80. yield refname, refname, type, docname, refname, 1