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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
163 return ref_data | 163 return ref_data |
164 logging.error('$ref %s could not be resolved in namespace %s.' % | 164 logging.error('$ref %s could not be resolved in namespace %s.' % |
165 (ref, namespace)) | 165 (ref, namespace)) |
166 type_name = ref.rsplit('.', 1)[-1] | 166 type_name = ref.rsplit('.', 1)[-1] |
167 return { | 167 return { |
168 'href': '#type-%s' % type_name, | 168 'href': '#type-%s' % type_name, |
169 'text': title or ref, | 169 'text': title or ref, |
170 'name': ref | 170 'name': ref |
171 } | 171 } |
172 | 172 |
173 def ResolveAllLinks(self, text, namespace=None): | 173 def ResolveAllLinks(self, text, relative_to='', namespace=None): |
174 """This method will resolve all $ref links in |text| using namespace | 174 """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 | 175 |namespace| if not None. Any links that cannot be resolved will be replaced |
176 using the default link format that |SafeGetLink| uses. | 176 using the default link format that |SafeGetLink| uses. |
177 The links will be generated relative to |relative_to|. | |
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 relative_to is not '': | |
not at google - send to devlin
2013/11/27 21:46:09
"is not" is for object identity. I don't know whet
benwells
2013/12/02 06:32:32
Nice catch. Turns out multiplying a sequence by a
| |
187 link_prefix = '../' * (relative_to.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 |