| 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 # requestPath should be of the form (apps|extensions)/...../page.html. |
| 183 # link_prefix should that the target will point to |
| 184 # (apps|extensions)/target.html. Note multiplying a string by a negative |
| 185 # number gives the empty string. |
| 186 link_prefix = '../' * (relative_to.count('/') - 1) |
| 180 split_text = text.split('$ref:') | 187 split_text = text.split('$ref:') |
| 181 # |split_text| is an array of text chunks that all start with the | 188 # |split_text| is an array of text chunks that all start with the |
| 182 # argument to '$ref:'. | 189 # argument to '$ref:'. |
| 183 formatted_text = [split_text[0]] | 190 formatted_text = [split_text[0]] |
| 184 for ref_and_rest in split_text[1:]: | 191 for ref_and_rest in split_text[1:]: |
| 185 title = None | 192 title = None |
| 186 if ref_and_rest.startswith('[') and ']' in ref_and_rest: | 193 if ref_and_rest.startswith('[') and ']' in ref_and_rest: |
| 187 # Text was '$ref:[foo.bar maybe title] other stuff'. | 194 # Text was '$ref:[foo.bar maybe title] other stuff'. |
| 188 ref_with_title, rest = ref_and_rest[1:].split(']', 1) | 195 ref_with_title, rest = ref_and_rest[1:].split(']', 1) |
| 189 ref_with_title = ref_with_title.split(None, 1) | 196 ref_with_title = ref_with_title.split(None, 1) |
| 190 if len(ref_with_title) == 1: | 197 if len(ref_with_title) == 1: |
| 191 # Text was '$ref:[foo.bar] other stuff'. | 198 # Text was '$ref:[foo.bar] other stuff'. |
| 192 ref = ref_with_title[0] | 199 ref = ref_with_title[0] |
| 193 else: | 200 else: |
| 194 # Text was '$ref:[foo.bar title] other stuff'. | 201 # Text was '$ref:[foo.bar title] other stuff'. |
| 195 ref, title = ref_with_title | 202 ref, title = ref_with_title |
| 196 else: | 203 else: |
| 197 # Text was '$ref:foo.bar other stuff'. | 204 # Text was '$ref:foo.bar other stuff'. |
| 198 match = self._bare_ref.match(ref_and_rest) | 205 match = self._bare_ref.match(ref_and_rest) |
| 199 if match is None: | 206 if match is None: |
| 200 ref = '' | 207 ref = '' |
| 201 rest = ref_and_rest | 208 rest = ref_and_rest |
| 202 else: | 209 else: |
| 203 ref = match.group() | 210 ref = match.group() |
| 204 rest = ref_and_rest[match.end():] | 211 rest = ref_and_rest[match.end():] |
| 205 | 212 |
| 206 ref_dict = self.SafeGetLink(ref, namespace=namespace, title=title) | 213 ref_dict = self.SafeGetLink(ref, namespace=namespace, title=title) |
| 207 formatted_text.append('<a href="%(href)s">%(text)s</a>%(rest)s' % | 214 formatted_text.append('<a href="%s%s">%s</a>%s' % |
| 208 { 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest }) | 215 (link_prefix, ref_dict['href'], ref_dict['text'], rest)) |
| 209 return ''.join(formatted_text) | 216 return ''.join(formatted_text) |
| OLD | NEW |