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 json | 6 import json |
7 import logging | 7 import logging |
8 | 8 |
9 import compiled_file_system as compiled_fs | 9 import compiled_file_system as compiled_fs |
10 from third_party.json_schema_compiler.model import UnixName | 10 from third_party.json_schema_compiler.model import UnixName |
11 | 11 |
12 class SidenavDataSource(object): | 12 class SidenavDataSource(object): |
13 """This class reads in and caches a JSON file representing the side navigation | 13 """This class reads in and caches a JSON file representing the side navigation |
14 menu. | 14 menu. |
15 """ | 15 """ |
16 class Factory(object): | 16 class Factory(object): |
17 def __init__(self, compiled_fs_factory, json_path): | 17 def __init__(self, compiled_fs_factory, json_path, base_path): |
18 self._cache = compiled_fs_factory.Create(self._CreateSidenavDict, | 18 self._cache = compiled_fs_factory.Create(self._CreateSidenavDict, |
19 SidenavDataSource) | 19 SidenavDataSource) |
20 self._json_path = json_path | 20 self._json_path = json_path |
21 self._base_path = base_path | |
21 | 22 |
22 def Create(self, path): | 23 def Create(self, path): |
23 """Create a SidenavDataSource, binding it to |path|. |path| is the url | 24 """Create a SidenavDataSource, binding it to |path|. |path| is the url |
24 of the page that is being rendered. It is used to determine which item | 25 of the page that is being rendered. It is used to determine which item |
25 in the sidenav should be highlighted. | 26 in the sidenav should be highlighted. |
26 """ | 27 """ |
27 return SidenavDataSource(self._cache, self._json_path, path) | 28 return SidenavDataSource(self._cache, |
29 self._json_path, | |
30 path, | |
31 self._base_path) | |
28 | 32 |
29 def _AddLevels(self, items, level): | 33 def _AddLevels(self, items, level): |
30 """Levels represent how deeply this item is nested in the sidenav. We | 34 """Levels represent how deeply this item is nested in the sidenav. We |
31 start at 2 because the top <ul> is the only level 1 element. | 35 start at 2 because the top <ul> is the only level 1 element. |
32 """ | 36 """ |
33 for item in items: | 37 for item in items: |
34 item['level'] = level | 38 item['level'] = level |
35 if 'items' in item: | 39 if 'items' in item: |
36 self._AddLevels(item['items'], level + 1) | 40 self._AddLevels(item['items'], level + 1) |
37 | 41 |
38 def _CreateSidenavDict(self, json_path, json_str): | 42 def _CreateSidenavDict(self, json_path, json_str): |
39 items = json.loads(json_str) | 43 items = json.loads(json_str) |
40 self._AddLevels(items, 2); | 44 self._AddLevels(items, 2); |
41 return items | 45 return items |
42 | 46 |
43 def __init__(self, cache, json_path, path): | 47 def __init__(self, cache, json_path, path, base_path): |
44 self._cache = cache | 48 self._cache = cache |
45 self._json_path = json_path | 49 self._json_path = json_path |
46 self._file_name = path.split('/')[-1] | 50 self._file_name = path.split('/')[-1] |
51 self._doc_class = path.split('/')[0] | |
52 self._base_path = base_path | |
not at google - send to devlin
2013/05/28 14:53:37
perhaps make this all:
self._dir_name, self._file
方觉(Fang Jue)
2013/05/28 23:37:50
Suppose we want to add manifest/*.html (with the m
not at google - send to devlin
2013/05/28 23:45:27
I see. It should be manifest/version.html, that ma
| |
47 | 53 |
48 def _AddSelected(self, items): | 54 def _AddSelected(self, items): |
49 for item in items: | 55 for item in items: |
50 if item.get('fileName', '') == self._file_name: | 56 if item.get('fileName', '') == self._file_name: |
51 item['selected'] = True | 57 item['selected'] = True |
52 return True | 58 return True |
53 if 'items' in item: | 59 if 'items' in item: |
54 if self._AddSelected(item['items']): | 60 if self._AddSelected(item['items']): |
55 item['child_selected'] = True | 61 item['child_selected'] = True |
56 return True | 62 return True |
57 return False | 63 return False |
58 | 64 |
65 def _ConvertToAbsolutePath(self, items): | |
66 for item in items: | |
67 file_name = item.get('fileName') | |
68 if file_name is not None and not file_name.startswith(('/', | |
69 'http://', | |
70 'https://')): | |
71 item['fileName'] = '%s/%s/%s' % (self._base_path, | |
not at google - send to devlin
2013/05/28 14:53:37
'fileName' is actually wrong, it should have been
方觉(Fang Jue)
2013/05/28 23:37:50
*sidenav.json, sidenav_data_source and its tests,
not at google - send to devlin
2013/05/28 23:45:27
href makes sense.
| |
72 self._doc_class, | |
not at google - send to devlin
2013/05/28 14:53:37
doc class could be empty if there is no branch, wh
| |
73 file_name) | |
74 if 'items' in item: | |
75 self._ConvertToAbsolutePath(item['items']) | |
76 | |
59 def get(self, key): | 77 def get(self, key): |
60 sidenav = copy.deepcopy(self._cache.GetFromFile( | 78 sidenav = copy.deepcopy(self._cache.GetFromFile( |
61 '%s/%s_sidenav.json' % (self._json_path, key))) | 79 '%s/%s_sidenav.json' % (self._json_path, key))) |
62 self._AddSelected(sidenav) | 80 self._AddSelected(sidenav) |
81 self._ConvertToAbsolutePath(sidenav) | |
63 return sidenav | 82 return sidenav |
OLD | NEW |