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 from path_utils import FormatKey |
5 from third_party.handlebar import Handlebar | 6 from third_party.handlebar import Handlebar |
6 | 7 |
7 EXTENSIONS_URL = '/chrome/extensions' | 8 EXTENSIONS_URL = '/chrome/extensions' |
8 | 9 |
9 class TemplateDataSource(object): | 10 class TemplateDataSource(object): |
10 """This class fetches and compiles templates using the fetcher passed in with | 11 """This class fetches and compiles templates using the fetcher passed in with |
11 |cache_builder|. | 12 |cache_builder|. |
12 """ | 13 """ |
13 def __init__(self, branch, api_data_source, cache_builder, base_paths): | 14 def __init__(self, |
| 15 branch, |
| 16 api_data_source, |
| 17 intro_data_source, |
| 18 cache_builder, |
| 19 base_paths): |
14 self._branch_info = self._MakeBranchDict(branch) | 20 self._branch_info = self._MakeBranchDict(branch) |
15 self._static_resources = ((('/' + branch) if branch != 'local' else '') + | 21 self._static_resources = ((('/' + branch) if branch != 'local' else '') + |
16 '/static') | 22 '/static') |
17 self._api_data_source = api_data_source | 23 self._api_data_source = api_data_source |
| 24 self._intro_data_source = intro_data_source |
18 self._cache = cache_builder.build(self._LoadTemplate) | 25 self._cache = cache_builder.build(self._LoadTemplate) |
19 self._base_paths = base_paths | 26 self._base_paths = base_paths |
20 | 27 |
21 def _MakeBranchDict(self, branch): | 28 def _MakeBranchDict(self, branch): |
22 return { | 29 return { |
23 'showWarning': branch != 'stable', | 30 'showWarning': branch != 'stable', |
24 'branches': [ | 31 'branches': [ |
25 { 'name': 'Stable', 'path': EXTENSIONS_URL + '/stable' }, | 32 { 'name': 'Stable', 'path': EXTENSIONS_URL + '/stable' }, |
26 { 'name': 'Dev', 'path': EXTENSIONS_URL + '/dev' }, | 33 { 'name': 'Dev', 'path': EXTENSIONS_URL + '/dev' }, |
27 { 'name': 'Beta', 'path': EXTENSIONS_URL + '/beta' }, | 34 { 'name': 'Beta', 'path': EXTENSIONS_URL + '/beta' }, |
(...skipping 10 matching lines...) Expand all Loading... |
38 the partial templates needed from |self._cache|. Partials are retrieved | 45 the partial templates needed from |self._cache|. Partials are retrieved |
39 from the TemplateDataSource with the |get| method. | 46 from the TemplateDataSource with the |get| method. |
40 """ | 47 """ |
41 template = self.get(template_name) | 48 template = self.get(template_name) |
42 if not template: | 49 if not template: |
43 return '' | 50 return '' |
44 # TODO error handling | 51 # TODO error handling |
45 return template.render({ | 52 return template.render({ |
46 'apis': self._api_data_source, | 53 'apis': self._api_data_source, |
47 'branchInfo': self._branch_info, | 54 'branchInfo': self._branch_info, |
| 55 'intros': self._intro_data_source, |
48 'partials': self, | 56 'partials': self, |
49 'static': self._static_resources | 57 'static': self._static_resources |
50 }).text | 58 }).text |
51 | 59 |
52 def __getitem__(self, key): | 60 def __getitem__(self, key): |
53 return self.get(key) | 61 return self.get(key) |
54 | 62 |
55 def get(self, key): | 63 def get(self, key): |
56 index = key.rfind('.html') | 64 real_path = FormatKey(key) |
57 if index > 0: | |
58 key = key[:index] | |
59 safe_key = key.replace('.', '_') | |
60 real_path = safe_key + '.html' | |
61 for base_path in self._base_paths: | 65 for base_path in self._base_paths: |
62 try: | 66 try: |
63 return self._cache.get(base_path + '/' + real_path) | 67 return self._cache.get(base_path + '/' + real_path) |
64 except: | 68 except: |
65 pass | 69 pass |
66 return None | 70 return None |
OLD | NEW |