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 path_utils import FormatKey |
| 7 from third_party.handlebar import Handlebar |
| 8 |
| 9 class IntroDataSource(object): |
| 10 """This class fetches and loads JSON APIs with the fetcher passed in with |
| 11 |cache_builder|, so the APIs can be plugged into templates. |
| 12 """ |
| 13 def __init__(self, cache_builder, base_path): |
| 14 self._cache = cache_builder.build(self._MakeIntroDict) |
| 15 self._base_path = base_path |
| 16 |
| 17 def _MakeIntroDict(self, intro): |
| 18 headings = re.findall('<h([23]) id\="(.+)">(.+)</h[23]>', intro) |
| 19 toc = [] |
| 20 for heading in headings: |
| 21 level, link, title = heading |
| 22 if level == '2': |
| 23 toc.append({ 'link': link, 'title': title, 'subheadings': [] }) |
| 24 else: |
| 25 toc[-1]['subheadings'].append({ 'link': link, 'title': title }) |
| 26 return { 'intro': Handlebar(intro), 'toc': toc } |
| 27 |
| 28 def __getitem__(self, key): |
| 29 return self.get(key) |
| 30 |
| 31 def get(self, key): |
| 32 real_path = FormatKey(key) |
| 33 try: |
| 34 return self._cache.get(self._base_path + '/' + real_path) |
| 35 except: |
| 36 pass |
| 37 return None |
OLD | NEW |