2 # Copyright 2012-2015 Bronto Software, Inc. and contributors
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
19 from docutils import nodes, utils
20 from sphinx.util.nodes import split_explicit_title
22 def get_javadoc_ref(app, rawtext, text):
23 javadoc_url_map = app.config.javadoc_url_map
25 # Add default Java SE sources
26 if not javadoc_url_map.get("java"):
27 javadoc_url_map["java"] = ("http://docs.oracle.com/javase/8/docs/api", 'javadoc8')
28 if not javadoc_url_map.get("javax"):
29 javadoc_url_map["javax"] = ("http://docs.oracle.com/javase/8/docs/api", 'javadoc8')
30 if not javadoc_url_map.get("org.xml"):
31 javadoc_url_map["org.xml"] = ("http://docs.oracle.com/javase/8/docs/api", 'javadoc8')
32 if not javadoc_url_map.get("org.w3c"):
33 javadoc_url_map["org.w3c"] = ("http://docs.oracle.com/javase/8/docs/api", 'javadoc8')
40 # If the javadoc contains a line like this:
42 # there is no package so the text.rindex will fail
44 split_point = text.rindex('.', 0, text.index('('))
45 method = text[split_point + 1:]
46 text = text[:split_point]
50 for pkg, (baseurl, ext_type) in javadoc_url_map.items():
51 if text.startswith(pkg + '.') and len(pkg) > len(package):
52 source = baseurl, ext_type
58 baseurl, ext_type = source
63 for part in text.split('.'):
64 if cls_parts or part[0].isupper():
65 cls_parts.append(part)
67 package_parts.append(part)
69 package = '.'.join(package_parts)
70 cls = '.'.join(cls_parts)
72 if not baseurl.endswith('/'):
73 baseurl = baseurl + '/'
75 if ext_type == 'javadoc':
77 cls = 'package-summary'
78 source = baseurl + package.replace('.', '/') + '/' + cls + '.html'
80 source = source + '#' + method
81 elif ext_type == 'javadoc8':
83 cls = 'package-summary'
84 source = baseurl + package.replace('.', '/') + '/' + cls + '.html'
86 source = source + '#' + re.sub(r'[()]', '-', method)
87 elif ext_type == 'sphinx':
90 source = baseurl + package.replace('.', '/') + '/' + cls.replace('.', '-') + '.html'
92 source = source + '#' + package + '.' + cls + '.' + method
94 raise ValueError('invalid target specifier ' + ext_type)
96 title = '.'.join(filter(None, (package, cls, method)))
97 node = nodes.reference(rawtext, '')
98 node['refuri'] = source
99 node['reftitle'] = title
103 def javadoc_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
104 """ Role for linking to external Javadoc """
106 has_explicit_title, title, target = split_explicit_title(text)
107 title = utils.unescape(title)
108 target = utils.unescape(target)
110 if not has_explicit_title:
111 target = target.lstrip('~')
114 title = title[1:].rpartition('.')[2]
116 app = inliner.document.settings.env.app
117 ref = get_javadoc_ref(app, rawtext, target)
120 raise ValueError("no Javadoc source found for %s in javadoc_url_map" % (target,))
122 ref.append(nodes.Text(title, title))