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

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: 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, 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._href = '/' + path
51 self._file_dir = base_path
47 52
48 def _AddSelected(self, items): 53 def _AddSelected(self, items):
49 for item in items: 54 for item in items:
50 if item.get('fileName', '') == self._file_name: 55 if item.get('href', '') == self._href:
51 item['selected'] = True 56 item['selected'] = True
52 return True 57 return True
53 if 'items' in item: 58 if 'items' in item:
54 if self._AddSelected(item['items']): 59 if self._AddSelected(item['items']):
55 item['child_selected'] = True 60 item['child_selected'] = True
56 return True 61 return True
57 return False 62 return False
58 63
64 def _QualifyHrefs(self, items):
65 for item in items:
66 if 'items' in item:
67 self._QualifyHrefs(item['items'])
68
69 href = item.get('href')
70 if href is not None and not href.startswith(('http://', 'https://')):
71 if not href.startswith('/'):
72 logging.warn('Paths in sidenav must be qualified. %s is not.' % href)
73 href = '/' + href
74 item['href'] = '%s%s' % (self._file_dir, href)
75
59 def get(self, key): 76 def get(self, key):
60 sidenav = copy.deepcopy(self._cache.GetFromFile( 77 sidenav = copy.deepcopy(self._cache.GetFromFile(
61 '%s/%s_sidenav.json' % (self._json_path, key))) 78 '%s/%s_sidenav.json' % (self._json_path, key)))
62 self._AddSelected(sidenav) 79 self._AddSelected(sidenav)
80 self._QualifyHrefs(sidenav)
63 return sidenav 81 return sidenav
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698