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