1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import os.path
20 from docutils import nodes
21
22 -def api_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
23 """
24 Role `:api:` bridges generated API documentation by tool like EpyDoc
25 with Sphinx Python Documentation Generator.
26
27 Other tools, other than EpyDoc, can be easily supported as well.
28
29 First generate the documentation to be referenced, i.e. with EpyDoc::
30
31 $ mkdir -p doc/_build/html/api
32 $ epydoc -o doc/_build/html/api ...
33
34 Next step is to generate documentation with Sphinx::
35
36 $ sphinx-build doc doc/_build/html
37
38 """
39 basedir = 'api'
40 prefix = 'build/html/generated/'
41 exists = lambda f: os.path.exists(prefix + f)
42
43
44 name = '%s' % text
45 uri = file = '%s/%s-module.html' % (basedir, text)
46 chunks = text.split('.')
47
48
49 if not exists(file):
50 name = text.split('.')[-1]
51 uri = file = '%s/%s-class.html' % (basedir, text)
52
53
54 if not exists(file):
55 method = chunks[-1]
56 fprefix = '.'.join(chunks[:-1])
57
58 file = '%s/%s-module.html' % (basedir, fprefix)
59 if exists(file):
60 uri = '%s#%s' % (file, method)
61 else:
62
63 file = '%s/%s-class.html' % (basedir, fprefix)
64 if exists(file):
65 name = '.'.join(chunks[-2:])
66 uri = '%s/%s-class.html#%s' % (basedir, fprefix, method)
67
68 if exists(file):
69 name = os.path.basename(uri)
70 name = name[:name.rfind('-')]
71 node = nodes.reference(rawtext, "Epydoc: %s" % name, refuri=uri, **options)
72 else:
73
74 node = nodes.literal(rawtext, text)
75
76 return [node], []
77
78
81