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

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

Issue 12521030: Extension docs: Include sidenav in 404 pages (Closed) Base URL: https://src.chromium.org/svn/trunk/src/
Patch Set: see last comment Created 7 years, 6 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
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 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):
23 """Create a SidenavDataSource, binding it to |path|. |path| is the url 24 """Create a SidenavDataSource.
24 of the page that is being rendered. It is used to determine which item
25 in the sidenav should be highlighted.
26 """ 25 """
27 return SidenavDataSource(self._cache, self._json_path, path) 26 return SidenavDataSource(self._cache,
27 self._json_path,
28 self._base_path)
28 29
29 def _AddLevels(self, items, level): 30 def _AddLevels(self, items, level):
30 """Levels represent how deeply this item is nested in the sidenav. We 31 """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. 32 start at 2 because the top <ul> is the only level 1 element.
32 """ 33 """
33 for item in items: 34 for item in items:
34 item['level'] = level 35 item['level'] = level
35 if 'items' in item: 36 if 'items' in item:
36 self._AddLevels(item['items'], level + 1) 37 self._AddLevels(item['items'], level + 1)
37 38
38 def _CreateSidenavDict(self, json_path, json_str): 39 def _CreateSidenavDict(self, json_path, json_str):
39 items = json.loads(json_str) 40 items = json.loads(json_str)
40 self._AddLevels(items, 2); 41 self._AddLevels(items, 2);
41 return items 42 return items
42 43
43 def __init__(self, cache, json_path, path): 44 def __init__(self, cache, json_path, base_path):
44 self._cache = cache 45 self._cache = cache
45 self._json_path = json_path 46 self._json_path = json_path
46 self._file_name = path.split('/')[-1] 47 self._base_path = base_path
48
49 ''' Bind the sidenav data source to |path|, which is the url of the page
50 that is being rendered. It is used to determine which item in the sidenav
51 should be highlighted.
52 '''
53 def SetPath(self, path):
54 if '/' in path:
55 doc_class, self._file_name = path.split('/', 1)
56 self._file_dir = '%s/%s' % (self._base_path, doc_class)
57 else:
58 self._file_dir = ''
59 self._file_name = path
47 60
48 def _AddSelected(self, items): 61 def _AddSelected(self, items):
49 for item in items: 62 for item in items:
50 if item.get('fileName', '') == self._file_name: 63 if item.get('href', '') == self._file_name:
51 item['selected'] = True 64 item['selected'] = True
52 return True 65 return True
53 if 'items' in item: 66 if 'items' in item:
54 if self._AddSelected(item['items']): 67 if self._AddSelected(item['items']):
55 item['child_selected'] = True 68 item['child_selected'] = True
56 return True 69 return True
57 return False 70 return False
58 71
72 def _ConvertToAbsolutePath(self, items):
73 for item in items:
74 file_name = item.get('href')
75 if file_name is not None and not file_name.startswith(('/',
76 'http://',
77 'https://')):
78 item['href'] = '%s/%s' % (self._file_dir, 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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698