Chromium Code Reviews| Index: chrome/common/extensions/docs/server2/intro_data_source.py |
| diff --git a/chrome/common/extensions/docs/server2/intro_data_source.py b/chrome/common/extensions/docs/server2/intro_data_source.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b9bd8c91d38aa74f459213e48dd857ac64205ea0 |
| --- /dev/null |
| +++ b/chrome/common/extensions/docs/server2/intro_data_source.py |
| @@ -0,0 +1,40 @@ |
| +# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import re |
| +from third_party.handlebar import Handlebar |
| + |
| +class IntroDataSource(object): |
| + """This class fetches and loads JSON APIs with the fetcher passed in with |
| + |cache_builder|, so the APIs can be plugged into templates. |
| + """ |
| + def __init__(self, cache_builder, base_path): |
| + self._cache = cache_builder.build(self._MakeIntroDict) |
| + self._base_path = base_path |
| + |
| + def _MakeIntroDict(self, intro): |
| + headings = re.findall('<h([23]) id\="(.+)">(.+)</h[23]>', intro) |
| + toc = [] |
| + for heading in headings: |
| + level, link, title = heading |
| + if level == '2': |
| + toc.append({ 'link': link, 'title': title, 'subheadings': [] }) |
| + else: |
| + toc[-1]['subheadings'].append({ 'link': link, 'title': title }) |
| + return { 'intro': Handlebar(intro), 'toc': toc } |
| + |
| + def __getitem__(self, key): |
| + return self.get(key) |
| + |
| + def get(self, key): |
| + index = key.rfind('.html') |
| + if index > 0: |
| + key = key[:index] |
| + safe_key = key.replace('.', '_') |
| + 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.
|
| + try: |
| + return self._cache.get(self._base_path + '/' + real_path) |
| + except: |
| + pass |
| + return None |