OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 from file_system import FileNotFoundError | 5 from file_system import FileNotFoundError |
6 import logging | 6 import logging |
7 import object_store | 7 import object_store |
8 import string | 8 import string |
9 | 9 |
10 def _ClassifySchemaNode(node_name, api): | 10 def _ClassifySchemaNode(node_name, api): |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 self._api_data_source_factory.Create(None, disable_refs=True), | 60 self._api_data_source_factory.Create(None, disable_refs=True), |
61 self._api_list_data_source_factory.Create(), | 61 self._api_list_data_source_factory.Create(), |
62 self._object_store) | 62 self._object_store) |
63 | 63 |
64 def __init__(self, api_data_source, api_list_data_source, object_store): | 64 def __init__(self, api_data_source, api_list_data_source, object_store): |
65 self._api_data_source = api_data_source | 65 self._api_data_source = api_data_source |
66 self._api_list_data_source = api_list_data_source | 66 self._api_list_data_source = api_list_data_source |
67 self._object_store = object_store | 67 self._object_store = object_store |
68 | 68 |
69 def _GetRefLink(self, ref, api_list, namespace, title): | 69 def _GetRefLink(self, ref, api_list, namespace, title): |
| 70 # Check nodes within each API the ref might refer to. |
70 parts = ref.split('.') | 71 parts = ref.split('.') |
71 for i, part in enumerate(parts): | 72 for i, part in enumerate(parts): |
72 api_name = '.'.join(parts[:i]) | 73 api_name = '.'.join(parts[:i]) |
73 if api_name not in api_list: | 74 if api_name not in api_list: |
74 continue | 75 continue |
75 try: | 76 try: |
76 api = self._api_data_source.get(api_name) | 77 api = self._api_data_source.get(api_name) |
77 except FileNotFoundError: | 78 except FileNotFoundError: |
78 continue | 79 continue |
79 name = '.'.join(parts[i:]) | 80 name = '.'.join(parts[i:]) |
(...skipping 17 matching lines...) Expand all Loading... |
97 else: | 98 else: |
98 text = ref | 99 text = ref |
99 category, node_name = node_info | 100 category, node_name = node_info |
100 if namespace is not None and text.startswith('%s.' % namespace): | 101 if namespace is not None and text.startswith('%s.' % namespace): |
101 text = text[len('%s.' % namespace):] | 102 text = text[len('%s.' % namespace):] |
102 return { | 103 return { |
103 'href': '%s.html#%s-%s' % (api_name, category, name.replace('.', '-')), | 104 'href': '%s.html#%s-%s' % (api_name, category, name.replace('.', '-')), |
104 'text': title if title else text, | 105 'text': title if title else text, |
105 'name': node_name | 106 'name': node_name |
106 } | 107 } |
| 108 |
| 109 # If it's not a reference to an API node it might just be a reference to an |
| 110 # API. Check this last so that links within APIs take precedence over links |
| 111 # to other APIs. |
| 112 if ref in api_list: |
| 113 return { |
| 114 'href': '%s.html' % ref, |
| 115 'text': title if title else ref, |
| 116 'name': ref |
| 117 } |
| 118 |
107 return None | 119 return None |
108 | 120 |
109 def GetLink(self, ref, namespace=None, title=None): | 121 def GetLink(self, ref, namespace=None, title=None): |
110 """Resolve $ref |ref| in namespace |namespace| if not None, returning None | 122 """Resolve $ref |ref| in namespace |namespace| if not None, returning None |
111 if it cannot be resolved. | 123 if it cannot be resolved. |
112 """ | 124 """ |
113 link = self._object_store.Get(_MakeKey(namespace, ref, title), | 125 link = self._object_store.Get(_MakeKey(namespace, ref, title), |
114 object_store.REFERENCE_RESOLVER).Get() | 126 object_store.REFERENCE_RESOLVER).Get() |
115 if link is not None: | 127 if link is not None: |
116 return link | 128 return link |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 index += 1 | 192 index += 1 |
181 rest = '%s%s' % (ref[index:], rest) | 193 rest = '%s%s' % (ref[index:], rest) |
182 ref = ref[:index] | 194 ref = ref[:index] |
183 while not ref[-1].isalnum(): | 195 while not ref[-1].isalnum(): |
184 rest = '%s%s' % (ref[-1], rest) | 196 rest = '%s%s' % (ref[-1], rest) |
185 ref = ref[:-1] | 197 ref = ref[:-1] |
186 ref_dict = self.SafeGetLink(ref, namespace=namespace, title=title) | 198 ref_dict = self.SafeGetLink(ref, namespace=namespace, title=title) |
187 formatted_text.append('<a href="%(href)s">%(text)s</a>%(rest)s' % | 199 formatted_text.append('<a href="%(href)s">%(text)s</a>%(rest)s' % |
188 { 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest }) | 200 { 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest }) |
189 return ''.join(formatted_text) | 201 return ''.join(formatted_text) |
OLD | NEW |