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 import posixpath | |
8 | 9 |
9 import compiled_file_system as compiled_fs | 10 import compiled_file_system as compiled_fs |
10 from third_party.json_schema_compiler.model import UnixName | 11 from third_party.json_schema_compiler.model import UnixName |
11 | 12 |
12 class SidenavDataSource(object): | 13 class SidenavDataSource(object): |
13 """This class reads in and caches a JSON file representing the side navigation | 14 """This class reads in and caches a JSON file representing the side navigation |
14 menu. | 15 menu. |
15 """ | 16 """ |
16 class Factory(object): | 17 class Factory(object): |
17 def __init__(self, compiled_fs_factory, json_path): | 18 def __init__(self, compiled_fs_factory, json_path, base_path): |
18 self._cache = compiled_fs_factory.Create(self._CreateSidenavDict, | 19 self._cache = compiled_fs_factory.Create(self._CreateSidenavDict, |
19 SidenavDataSource) | 20 SidenavDataSource) |
20 self._json_path = json_path | 21 self._json_path = json_path |
22 self._base_path = base_path | |
21 | 23 |
22 def Create(self, path): | 24 def Create(self, path): |
23 """Create a SidenavDataSource, binding it to |path|. |path| is the url | 25 """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 | 26 of the page that is being rendered. It is used to determine which item |
25 in the sidenav should be highlighted. | 27 in the sidenav should be highlighted. |
26 """ | 28 """ |
27 return SidenavDataSource(self._cache, self._json_path, path) | 29 return SidenavDataSource(self._cache, |
30 self._json_path, | |
31 path, | |
32 self._base_path) | |
28 | 33 |
29 def _AddLevels(self, items, level): | 34 def _AddLevels(self, items, level): |
30 """Levels represent how deeply this item is nested in the sidenav. We | 35 """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. | 36 start at 2 because the top <ul> is the only level 1 element. |
32 """ | 37 """ |
33 for item in items: | 38 for item in items: |
34 item['level'] = level | 39 item['level'] = level |
35 if 'items' in item: | 40 if 'items' in item: |
36 self._AddLevels(item['items'], level + 1) | 41 self._AddLevels(item['items'], level + 1) |
37 | 42 |
38 def _CreateSidenavDict(self, json_path, json_str): | 43 def _CreateSidenavDict(self, json_path, json_str): |
39 items = json.loads(json_str) | 44 items = json.loads(json_str) |
40 self._AddLevels(items, 2); | 45 self._AddLevels(items, 2); |
41 return items | 46 return items |
42 | 47 |
43 def __init__(self, cache, json_path, path): | 48 def __init__(self, cache, json_path, path, base_path): |
44 self._cache = cache | 49 self._cache = cache |
45 self._json_path = json_path | 50 self._json_path = json_path |
46 self._file_name = path.split('/')[-1] | 51 if '/' in path: |
52 self._doc_class, self._file_name = path.split('/', 1) | |
not at google - send to devlin
2013/05/28 23:45:27
can we nevertheless call this file_dir and file_na
| |
53 else: | |
54 self._doc_class = '' | |
55 self._file_name = path | |
56 self._base_path = base_path | |
47 | 57 |
48 def _AddSelected(self, items): | 58 def _AddSelected(self, items): |
49 for item in items: | 59 for item in items: |
50 if item.get('fileName', '') == self._file_name: | 60 if item.get('fileName', '') == self._file_name: |
51 item['selected'] = True | 61 item['selected'] = True |
52 return True | 62 return True |
53 if 'items' in item: | 63 if 'items' in item: |
54 if self._AddSelected(item['items']): | 64 if self._AddSelected(item['items']): |
55 item['child_selected'] = True | 65 item['child_selected'] = True |
56 return True | 66 return True |
57 return False | 67 return False |
58 | 68 |
69 def _ConvertToAbsolutePath(self, items): | |
70 for item in items: | |
71 file_name = item.get('fileName') | |
72 if file_name is not None and not file_name.startswith(('/', | |
73 'http://', | |
74 'https://')): | |
75 item['fileName'] = posixpath.join('/', | |
76 self._base_path, | |
77 self._doc_class, | |
78 file_name) | |
79 if 'items' in item: | |
80 self._ConvertToAbsolutePath(item['items']) | |
81 | |
59 def get(self, key): | 82 def get(self, key): |
60 sidenav = copy.deepcopy(self._cache.GetFromFile( | 83 sidenav = copy.deepcopy(self._cache.GetFromFile( |
61 '%s/%s_sidenav.json' % (self._json_path, key))) | 84 '%s/%s_sidenav.json' % (self._json_path, key))) |
62 self._AddSelected(sidenav) | 85 self._AddSelected(sidenav) |
86 self._ConvertToAbsolutePath(sidenav) | |
63 return sidenav | 87 return sidenav |
OLD | NEW |