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 def _DetermineLinkPrefix(request): | |
| 48 # request.Path should be of the form (apps|extensions)/...../page.html. This | |
| 49 # function works out the relative path prefix required so that the target | |
| 50 # will point to (apps|extensions)/target.html. This is as many '../' as there | |
| 51 # are components between (apps|extensiosn) and page.html, in the example | |
| 52 # above | |
| 53 prefix = '' | |
| 54 components = request.path.split('/')[2:] | |
| 55 for piece in components: | |
| 56 prefix = prefix + '../' | |
| 57 return prefix | |
|
not at google - send to devlin
2013/11/21 05:26:50
could be written as
'../' * (path.count('/') - 1)
benwells
2013/11/22 05:44:01
holy cow, I can multiple strings with numbers !?!?
| |
| 47 | 58 |
| 48 class ReferenceResolver(object): | 59 class ReferenceResolver(object): |
| 49 """Resolves references to $ref's by searching through the APIs to find the | 60 """Resolves references to $ref's by searching through the APIs to find the |
| 50 correct node. | 61 correct node. |
| 51 | 62 |
| 52 $ref's have two forms: | 63 $ref's have two forms: |
| 53 | 64 |
| 54 $ref:api.node - Replaces the $ref with a link to node on the API page. The | 65 $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. | 66 title is set to the name of the node. |
| 56 | 67 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 163 return ref_data | 174 return ref_data |
| 164 logging.error('$ref %s could not be resolved in namespace %s.' % | 175 logging.error('$ref %s could not be resolved in namespace %s.' % |
| 165 (ref, namespace)) | 176 (ref, namespace)) |
| 166 type_name = ref.rsplit('.', 1)[-1] | 177 type_name = ref.rsplit('.', 1)[-1] |
| 167 return { | 178 return { |
| 168 'href': '#type-%s' % type_name, | 179 'href': '#type-%s' % type_name, |
| 169 'text': title or ref, | 180 'text': title or ref, |
| 170 'name': ref | 181 'name': ref |
| 171 } | 182 } |
| 172 | 183 |
| 173 def ResolveAllLinks(self, text, namespace=None): | 184 def ResolveAllLinks(self, text, request, namespace=None): |
|
not at google - send to devlin
2013/11/21 05:26:50
Rather than introduce a whole Request as a (concep
benwells
2013/11/22 05:44:01
Done.
| |
| 174 """This method will resolve all $ref links in |text| using namespace | 185 """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 | 186 |namespace| if not None. Any links that cannot be resolved will be replaced |
| 176 using the default link format that |SafeGetLink| uses. | 187 using the default link format that |SafeGetLink| uses. |
| 188 The links are relative, with the prefix for the path determined by | |
| 189 inspecting request.path. | |
| 177 """ | 190 """ |
| 178 if text is None or '$ref:' not in text: | 191 if text is None or '$ref:' not in text: |
| 179 return text | 192 return text |
| 193 link_prefix = '' | |
| 194 if request is not None: | |
| 195 link_prefix = _DetermineLinkPrefix(request) | |
| 180 split_text = text.split('$ref:') | 196 split_text = text.split('$ref:') |
| 181 # |split_text| is an array of text chunks that all start with the | 197 # |split_text| is an array of text chunks that all start with the |
| 182 # argument to '$ref:'. | 198 # argument to '$ref:'. |
| 183 formatted_text = [split_text[0]] | 199 formatted_text = [split_text[0]] |
| 184 for ref_and_rest in split_text[1:]: | 200 for ref_and_rest in split_text[1:]: |
| 185 title = None | 201 title = None |
| 186 if ref_and_rest.startswith('[') and ']' in ref_and_rest: | 202 if ref_and_rest.startswith('[') and ']' in ref_and_rest: |
| 187 # Text was '$ref:[foo.bar maybe title] other stuff'. | 203 # Text was '$ref:[foo.bar maybe title] other stuff'. |
| 188 ref_with_title, rest = ref_and_rest[1:].split(']', 1) | 204 ref_with_title, rest = ref_and_rest[1:].split(']', 1) |
| 189 ref_with_title = ref_with_title.split(None, 1) | 205 ref_with_title = ref_with_title.split(None, 1) |
| 190 if len(ref_with_title) == 1: | 206 if len(ref_with_title) == 1: |
| 191 # Text was '$ref:[foo.bar] other stuff'. | 207 # Text was '$ref:[foo.bar] other stuff'. |
| 192 ref = ref_with_title[0] | 208 ref = ref_with_title[0] |
| 193 else: | 209 else: |
| 194 # Text was '$ref:[foo.bar title] other stuff'. | 210 # Text was '$ref:[foo.bar title] other stuff'. |
| 195 ref, title = ref_with_title | 211 ref, title = ref_with_title |
| 196 else: | 212 else: |
| 197 # Text was '$ref:foo.bar other stuff'. | 213 # Text was '$ref:foo.bar other stuff'. |
| 198 match = self._bare_ref.match(ref_and_rest) | 214 match = self._bare_ref.match(ref_and_rest) |
| 199 if match is None: | 215 if match is None: |
| 200 ref = '' | 216 ref = '' |
| 201 rest = ref_and_rest | 217 rest = ref_and_rest |
| 202 else: | 218 else: |
| 203 ref = match.group() | 219 ref = match.group() |
| 204 rest = ref_and_rest[match.end():] | 220 rest = ref_and_rest[match.end():] |
| 205 | 221 |
| 206 ref_dict = self.SafeGetLink(ref, namespace=namespace, title=title) | 222 ref_dict = self.SafeGetLink(ref, namespace=namespace, title=title) |
| 207 formatted_text.append('<a href="%(href)s">%(text)s</a>%(rest)s' % | 223 formatted_text.append('<a href="%(prefix)s%(href)s">%(text)s</a>%(rest)s' % |
| 208 { 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest }) | 224 { 'prefix': link_prefix, |
| 225 'href': ref_dict['href'], | |
| 226 'text': ref_dict['text'], | |
| 227 'rest': rest }) | |
|
not at google - send to devlin
2013/11/21 05:26:50
we should kill this confusing %(prefix)s stuff and
benwells
2013/11/22 05:44:01
Done.
| |
| 209 return ''.join(formatted_text) | 228 return ''.join(formatted_text) |
| OLD | NEW |