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 import copy | 5 import copy |
6 import logging | 6 import logging |
7 import os | 7 import os |
8 | 8 |
| 9 from docs_server_utils import GetLinkToRefType |
9 import third_party.json_schema_compiler.model as model | 10 import third_party.json_schema_compiler.model as model |
10 | 11 |
11 def _RemoveNoDocs(item): | 12 def _RemoveNoDocs(item): |
12 if type(item) == dict: | 13 if type(item) == dict: |
13 if item.get('nodoc', False): | 14 if item.get('nodoc', False): |
14 return True | 15 return True |
15 for key, value in item.items(): | 16 for key, value in item.items(): |
16 if _RemoveNoDocs(value): | 17 if _RemoveNoDocs(value): |
17 del item[key] | 18 del item[key] |
18 elif type(item) == list: | 19 elif type(item) == list: |
19 for i in item: | 20 for i in item: |
20 if _RemoveNoDocs(i): | 21 if _RemoveNoDocs(i): |
21 item.remove(i) | 22 item.remove(i) |
22 return False | 23 return False |
23 | 24 |
24 def _GetLinkToRefType(namespace_name, ref_type): | |
25 if ref_type.startswith(namespace_name + '.'): | |
26 type_name = ref_type[len(namespace_name + '.'):] | |
27 return { 'href': '#type-' + type_name, 'text': type_name } | |
28 elif '.' not in ref_type: | |
29 return { 'href': '#type-' + ref_type, 'text': ref_type } | |
30 api, type_name = ref_type.rsplit('.', 1) | |
31 return { 'href': api + '.html#type-' + type_name, 'text': ref_type } | |
32 | |
33 def _FormatValue(value): | 25 def _FormatValue(value): |
34 """Inserts commas every three digits for integer values. It is magic. | 26 """Inserts commas every three digits for integer values. It is magic. |
35 """ | 27 """ |
36 s = str(value) | 28 s = str(value) |
37 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) | 29 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) |
38 | 30 |
39 class HandlebarDictGenerator(object): | 31 class HandlebarDictGenerator(object): |
40 """Uses a Model from the JSON Schema Compiler and generates a dict that | 32 """Uses a Model from the JSON Schema Compiler and generates a dict that |
41 a Handlebar template can use for a data source. | 33 a Handlebar template can use for a data source. |
42 """ | 34 """ |
(...skipping 20 matching lines...) Expand all Loading... |
63 parts = ref.split(' ', 1) | 55 parts = ref.split(' ', 1) |
64 if len(parts) == 1: | 56 if len(parts) == 1: |
65 ref = parts[0] | 57 ref = parts[0] |
66 rest = '' | 58 rest = '' |
67 else: | 59 else: |
68 ref, rest = parts | 60 ref, rest = parts |
69 rest = ' ' + rest | 61 rest = ' ' + rest |
70 if not ref[-1].isalnum(): | 62 if not ref[-1].isalnum(): |
71 rest = ref[-1] + rest | 63 rest = ref[-1] + rest |
72 ref = ref[:-1] | 64 ref = ref[:-1] |
73 ref_dict = _GetLinkToRefType(self._namespace.name, ref) | 65 ref_dict = GetLinkToRefType(self._namespace.name, ref) |
74 formatted_description.append('<a href="%(href)s">%(text)s</a>%(rest)s' % | 66 formatted_description.append('<a href="%(href)s">%(text)s</a>%(rest)s' % |
75 { 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest }) | 67 { 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest }) |
76 return ''.join(formatted_description) | 68 return ''.join(formatted_description) |
77 | 69 |
78 def Generate(self): | 70 def Generate(self): |
79 try: | 71 try: |
80 return { | 72 return { |
81 'name': self._namespace.name, | 73 'name': self._namespace.name, |
82 'types': map(self._GenerateType, self._namespace.types.values()), | 74 'types': map(self._GenerateType, self._namespace.types.values()), |
83 'functions': self._GenerateFunctions(self._namespace.functions), | 75 'functions': self._GenerateFunctions(self._namespace.functions), |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 if property_.type_ == model.PropertyType.CHOICES: | 162 if property_.type_ == model.PropertyType.CHOICES: |
171 dst_dict['choices'] = map(self._GenerateProperty, | 163 dst_dict['choices'] = map(self._GenerateProperty, |
172 property_.choices.values()) | 164 property_.choices.values()) |
173 # We keep track of which is last for knowing when to add "or" between | 165 # We keep track of which is last for knowing when to add "or" between |
174 # choices in templates. | 166 # choices in templates. |
175 if len(dst_dict['choices']) > 0: | 167 if len(dst_dict['choices']) > 0: |
176 dst_dict['choices'][-1]['last'] = True | 168 dst_dict['choices'][-1]['last'] = True |
177 elif property_.type_ == model.PropertyType.ADDITIONAL_PROPERTIES: | 169 elif property_.type_ == model.PropertyType.ADDITIONAL_PROPERTIES: |
178 dst_dict['additional_properties'] = True | 170 dst_dict['additional_properties'] = True |
179 elif property_.type_ == model.PropertyType.REF: | 171 elif property_.type_ == model.PropertyType.REF: |
180 dst_dict['link'] = _GetLinkToRefType(self._namespace.name, | 172 dst_dict['link'] = GetLinkToRefType(self._namespace.name, |
181 property_.ref_type) | 173 property_.ref_type) |
182 elif property_.type_ == model.PropertyType.ARRAY: | 174 elif property_.type_ == model.PropertyType.ARRAY: |
183 dst_dict['array'] = self._GenerateProperty(property_.item_type) | 175 dst_dict['array'] = self._GenerateProperty(property_.item_type) |
184 elif property_.type_ == model.PropertyType.ENUM: | 176 elif property_.type_ == model.PropertyType.ENUM: |
185 dst_dict['enum_values'] = [] | 177 dst_dict['enum_values'] = [] |
186 for enum_value in property_.enum_values: | 178 for enum_value in property_.enum_values: |
187 dst_dict['enum_values'].append({'name': enum_value}) | 179 dst_dict['enum_values'].append({'name': enum_value}) |
188 if len(dst_dict['enum_values']) > 0: | 180 if len(dst_dict['enum_values']) > 0: |
189 dst_dict['enum_values'][-1]['last'] = True | 181 dst_dict['enum_values'][-1]['last'] = True |
190 elif property_.instance_of: | 182 elif property_.instance_of: |
191 dst_dict['simple_type'] = property_.instance_of.lower() | 183 dst_dict['simple_type'] = property_.instance_of.lower() |
192 else: | 184 else: |
193 dst_dict['simple_type'] = property_.type_.name.lower() | 185 dst_dict['simple_type'] = property_.type_.name.lower() |
OLD | NEW |