Chromium Code Reviews| 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 copy import deepcopy | 5 from copy import deepcopy |
| 6 import logging | 6 import logging |
| 7 import re | 7 import re |
| 8 import string | 8 import string |
| 9 | 9 |
| 10 from file_system import FileNotFoundError | 10 from file_system import FileNotFoundError |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 37 key = '%s/%s' % (namespace, ref) | 37 key = '%s/%s' % (namespace, ref) |
| 38 # AppEngine doesn't like keys > 500, but there will be some other stuff | 38 # AppEngine doesn't like keys > 500, but there will be some other stuff |
| 39 # that goes into this key, so truncate it earlier. This shoudn't be | 39 # that goes into this key, so truncate it earlier. This shoudn't be |
| 40 # happening anyway unless there's a bug, such as http://crbug.com/314102. | 40 # happening anyway unless there's a bug, such as http://crbug.com/314102. |
| 41 max_size = 256 | 41 max_size = 256 |
| 42 if len(key) > max_size: | 42 if len(key) > max_size: |
| 43 logging.error('Key was >%s characters: %s' % (max_size, key)) | 43 logging.error('Key was >%s characters: %s' % (max_size, key)) |
| 44 key = key[:max_size] | 44 key = key[:max_size] |
| 45 return key | 45 return key |
| 46 | 46 |
| 47 | |
|
not at google - send to devlin
2013/11/22 20:16:10
python style is actually to have 2 blank lines bet
benwells
2013/11/27 07:28:31
Done.
| |
| 48 class ReferenceResolver(object): | 47 class ReferenceResolver(object): |
| 49 """Resolves references to $ref's by searching through the APIs to find the | 48 """Resolves references to $ref's by searching through the APIs to find the |
| 50 correct node. | 49 correct node. |
| 51 | 50 |
| 52 $ref's have two forms: | 51 $ref's have two forms: |
| 53 | 52 |
| 54 $ref:api.node - Replaces the $ref with a link to node on the API page. The | 53 $ref:api.node - Replaces the $ref with a link to node on the API page. The |
| 55 title is set to the name of the node. | 54 title is set to the name of the node. |
| 56 | 55 |
| 57 $ref:[api.node The Title] - Same as the previous form but title is set to | 56 $ref:[api.node The Title] - Same as the previous form but title is set to |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 163 return ref_data | 162 return ref_data |
| 164 logging.error('$ref %s could not be resolved in namespace %s.' % | 163 logging.error('$ref %s could not be resolved in namespace %s.' % |
| 165 (ref, namespace)) | 164 (ref, namespace)) |
| 166 type_name = ref.rsplit('.', 1)[-1] | 165 type_name = ref.rsplit('.', 1)[-1] |
| 167 return { | 166 return { |
| 168 'href': '#type-%s' % type_name, | 167 'href': '#type-%s' % type_name, |
| 169 'text': title or ref, | 168 'text': title or ref, |
| 170 'name': ref | 169 'name': ref |
| 171 } | 170 } |
| 172 | 171 |
| 173 def ResolveAllLinks(self, text, namespace=None): | 172 def ResolveAllLinks(self, text, requestPath='', namespace=None): |
|
not at google - send to devlin
2013/11/22 20:16:10
requestPath -> request_path
benwells
2013/11/27 07:28:31
Done.
| |
| 174 """This method will resolve all $ref links in |text| using namespace | 173 """This method will resolve all $ref links in |text| using namespace |
| 175 |namespace| if not None. Any links that cannot be resolved will be replaced | 174 |namespace| if not None. Any links that cannot be resolved will be replaced |
| 176 using the default link format that |SafeGetLink| uses. | 175 using the default link format that |SafeGetLink| uses. |
| 176 The links are relative, with the prefix for the path determined by | |
| 177 inspecting request.path. | |
|
not at google - send to devlin
2013/11/22 20:16:10
Perhaps just "Links will be generated relative to
benwells
2013/11/27 07:28:31
Done.
| |
| 177 """ | 178 """ |
| 178 if text is None or '$ref:' not in text: | 179 if text is None or '$ref:' not in text: |
| 179 return text | 180 return text |
| 181 | |
| 182 link_prefix = '' | |
| 183 # requestPath should be of the form (apps|extensions)/...../page.html. | |
| 184 # link_prefix should that the target will point to | |
| 185 # (apps|extensions)/target.html. | |
| 186 if requestPath is not '': | |
| 187 link_prefix = '../' * (requestPath.count('/') - 1) | |
| 180 split_text = text.split('$ref:') | 188 split_text = text.split('$ref:') |
| 181 # |split_text| is an array of text chunks that all start with the | 189 # |split_text| is an array of text chunks that all start with the |
| 182 # argument to '$ref:'. | 190 # argument to '$ref:'. |
| 183 formatted_text = [split_text[0]] | 191 formatted_text = [split_text[0]] |
| 184 for ref_and_rest in split_text[1:]: | 192 for ref_and_rest in split_text[1:]: |
| 185 title = None | 193 title = None |
| 186 if ref_and_rest.startswith('[') and ']' in ref_and_rest: | 194 if ref_and_rest.startswith('[') and ']' in ref_and_rest: |
| 187 # Text was '$ref:[foo.bar maybe title] other stuff'. | 195 # Text was '$ref:[foo.bar maybe title] other stuff'. |
| 188 ref_with_title, rest = ref_and_rest[1:].split(']', 1) | 196 ref_with_title, rest = ref_and_rest[1:].split(']', 1) |
| 189 ref_with_title = ref_with_title.split(None, 1) | 197 ref_with_title = ref_with_title.split(None, 1) |
| 190 if len(ref_with_title) == 1: | 198 if len(ref_with_title) == 1: |
| 191 # Text was '$ref:[foo.bar] other stuff'. | 199 # Text was '$ref:[foo.bar] other stuff'. |
| 192 ref = ref_with_title[0] | 200 ref = ref_with_title[0] |
| 193 else: | 201 else: |
| 194 # Text was '$ref:[foo.bar title] other stuff'. | 202 # Text was '$ref:[foo.bar title] other stuff'. |
| 195 ref, title = ref_with_title | 203 ref, title = ref_with_title |
| 196 else: | 204 else: |
| 197 # Text was '$ref:foo.bar other stuff'. | 205 # Text was '$ref:foo.bar other stuff'. |
| 198 match = self._bare_ref.match(ref_and_rest) | 206 match = self._bare_ref.match(ref_and_rest) |
| 199 if match is None: | 207 if match is None: |
| 200 ref = '' | 208 ref = '' |
| 201 rest = ref_and_rest | 209 rest = ref_and_rest |
| 202 else: | 210 else: |
| 203 ref = match.group() | 211 ref = match.group() |
| 204 rest = ref_and_rest[match.end():] | 212 rest = ref_and_rest[match.end():] |
| 205 | 213 |
| 206 ref_dict = self.SafeGetLink(ref, namespace=namespace, title=title) | 214 ref_dict = self.SafeGetLink(ref, namespace=namespace, title=title) |
| 207 formatted_text.append('<a href="%(href)s">%(text)s</a>%(rest)s' % | 215 formatted_text.append('<a href="%s%s">%s</a>%s' % |
| 208 { 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest }) | 216 (link_prefix, ref_dict['href'], ref_dict['text'], rest)) |
| 209 return ''.join(formatted_text) | 217 return ''.join(formatted_text) |
| OLD | NEW |