OLD | NEW |
---|---|
(Empty) | |
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 | |
3 # found in the LICENSE file. | |
4 | |
5 import re | |
6 from third_party.handlebar import Handlebar | |
7 | |
8 class IntroDataSource(object): | |
9 """This class fetches and loads JSON APIs with the fetcher passed in with | |
10 |cache_builder|, so the APIs can be plugged into templates. | |
11 """ | |
12 def __init__(self, cache_builder, base_path): | |
13 self._cache = cache_builder.build(self._MakeIntroDict) | |
14 self._base_path = base_path | |
15 | |
16 def _MakeIntroDict(self, intro): | |
17 headings = re.findall('<h([23]) id\="(.+)">(.+)</h[23]>', intro) | |
18 toc = [] | |
19 for heading in headings: | |
20 level, link, title = heading | |
21 if level == '2': | |
22 toc.append({ 'link': link, 'title': title, 'subheadings': [] }) | |
23 else: | |
24 toc[-1]['subheadings'].append({ 'link': link, 'title': title }) | |
25 return { 'intro': Handlebar(intro), 'toc': toc } | |
26 | |
27 def __getitem__(self, key): | |
28 return self.get(key) | |
29 | |
30 def get(self, key): | |
31 index = key.rfind('.html') | |
32 if index > 0: | |
33 key = key[:index] | |
34 safe_key = key.replace('.', '_') | |
35 real_path = safe_key + '.html' | |
not at google - send to devlin
2012/07/10 01:00:20
Pull this into a util since the template data sour
cduvall
2012/07/10 01:13:14
Done.
| |
36 try: | |
37 return self._cache.get(self._base_path + '/' + real_path) | |
38 except: | |
39 pass | |
40 return None | |
OLD | NEW |