Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: chrome/common/extensions/docs/server2/sidenav_data_source.py

Issue 15087006: Docserver: there is only one. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: epic rebase Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 7
8 import compiled_file_system as compiled_fs 8 import compiled_file_system as compiled_fs
9 from third_party.json_schema_compiler.model import UnixName 9 from third_party.json_schema_compiler.model import UnixName
10 10
11 class SidenavDataSource(object): 11 class SidenavDataSource(object):
12 """This class reads in and caches a JSON file representing the side navigation 12 """This class reads in and caches a JSON file representing the side navigation
13 menu. 13 menu.
14 """ 14 """
15 class Factory(object): 15 class Factory(object):
16 def __init__(self, compiled_fs_factory, json_path, base_path): 16 def __init__(self, compiled_fs_factory, json_path):
17 self._cache = compiled_fs_factory.Create(self._CreateSidenavDict, 17 self._cache = compiled_fs_factory.Create(self._CreateSidenavDict,
18 SidenavDataSource) 18 SidenavDataSource)
19 self._json_path = json_path 19 self._json_path = json_path
20 self._base_path = base_path
21 20
22 def Create(self, path): 21 def Create(self, path):
23 """Create a SidenavDataSource, binding it to |path|. |path| is the url 22 """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 23 of the page that is being rendered. It is used to determine which item
25 in the sidenav should be highlighted. 24 in the sidenav should be highlighted.
26 """ 25 """
27 return SidenavDataSource(self._cache, 26 return SidenavDataSource(self._cache, self._json_path, path)
28 self._json_path,
29 path,
30 self._base_path)
31 27
32 def _AddLevels(self, items, level): 28 def _AddLevels(self, items, level):
33 """Levels represent how deeply this item is nested in the sidenav. We 29 """Levels represent how deeply this item is nested in the sidenav. We
34 start at 2 because the top <ul> is the only level 1 element. 30 start at 2 because the top <ul> is the only level 1 element.
35 """ 31 """
36 for item in items: 32 for item in items:
37 item['level'] = level 33 item['level'] = level
38 if 'items' in item: 34 if 'items' in item:
39 self._AddLevels(item['items'], level + 1) 35 self._AddLevels(item['items'], level + 1)
40 36
41 def _CreateSidenavDict(self, json_path, json_str): 37 def _CreateSidenavDict(self, json_path, json_str):
42 items = json.loads(json_str) 38 items = json.loads(json_str)
43 self._AddLevels(items, 2); 39 self._AddLevels(items, 2);
44 return items 40 return items
45 41
46 def __init__(self, cache, json_path, path, base_path): 42 def __init__(self, cache, json_path, path):
47 self._cache = cache 43 self._cache = cache
48 self._json_path = json_path 44 self._json_path = json_path
49 self._href = '/' + path 45 self._href = '/' + path
50 self._file_dir = base_path
51 46
52 def _AddSelected(self, items): 47 def _AddSelected(self, items):
53 for item in items: 48 for item in items:
54 if item.get('href', '') == self._href: 49 if item.get('href', '') == self._href:
55 item['selected'] = True 50 item['selected'] = True
56 return True 51 return True
57 if 'items' in item: 52 if 'items' in item:
58 if self._AddSelected(item['items']): 53 if self._AddSelected(item['items']):
59 item['child_selected'] = True 54 item['child_selected'] = True
60 return True 55 return True
61 return False 56 return False
62 57
63 def _QualifyHrefs(self, items): 58 def _QualifyHrefs(self, items):
64 for item in items: 59 for item in items:
65 if 'items' in item: 60 if 'items' in item:
66 self._QualifyHrefs(item['items']) 61 self._QualifyHrefs(item['items'])
67 62
68 href = item.get('href') 63 href = item.get('href')
69 if href is not None and not href.startswith(('http://', 'https://')): 64 if href is not None and not href.startswith(('http://', 'https://')):
70 if not href.startswith('/'): 65 if not href.startswith('/'):
71 logging.warn('Paths in sidenav must be qualified. %s is not.' % href) 66 logging.warn('Paths in sidenav must be qualified. %s is not.' % href)
72 href = '/' + href 67 href = '/' + href
73 item['href'] = '%s%s' % (self._file_dir, href) 68 item['href'] = href
74 69
75 def get(self, key): 70 def get(self, key):
76 sidenav = copy.deepcopy(self._cache.GetFromFile( 71 sidenav = copy.deepcopy(self._cache.GetFromFile(
77 '%s/%s_sidenav.json' % (self._json_path, key))) 72 '%s/%s_sidenav.json' % (self._json_path, key)))
78 self._AddSelected(sidenav) 73 self._AddSelected(sidenav)
79 self._QualifyHrefs(sidenav) 74 self._QualifyHrefs(sidenav)
80 return sidenav 75 return sidenav
OLDNEW
« no previous file with comments | « chrome/common/extensions/docs/server2/servlet.py ('k') | chrome/common/extensions/docs/server2/sidenav_data_source_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698