Chromium Code Reviews| Index: chrome/common/extensions/docs/server2/template_data_source.py |
| diff --git a/chrome/common/extensions/docs/server2/template_data_source.py b/chrome/common/extensions/docs/server2/template_data_source.py |
| index a19cf167cb96829daa555017a1070a022f07a12e..b5396d43d21881d53b569e5d94e9664861a5d355 100644 |
| --- a/chrome/common/extensions/docs/server2/template_data_source.py |
| +++ b/chrome/common/extensions/docs/server2/template_data_source.py |
| @@ -2,6 +2,8 @@ |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| +import json |
| +import re |
| from path_utils import FormatKey |
| from third_party.handlebar import Handlebar |
| @@ -16,6 +18,7 @@ class TemplateDataSource(object): |
| api_data_source, |
| intro_data_source, |
| cache_builder, |
| + samples_path, |
| base_paths): |
| self._branch_info = self._MakeBranchDict(branch) |
| self._static_resources = ((('/' + branch) if branch != 'local' else '') + |
| @@ -23,7 +26,10 @@ class TemplateDataSource(object): |
| self._api_data_source = api_data_source |
| self._intro_data_source = intro_data_source |
| self._cache = cache_builder.build(self._LoadTemplate) |
| + self._dir_cache = cache_builder.build(self._MakeSamplesList) |
| + self._file_cache = cache_builder.build(lambda x: x) |
|
not at google - send to devlin
2012/07/12 00:22:48
Same comment as earlier. file_cache not necessary
cduvall
2012/07/12 01:51:23
Done.
|
| self._base_paths = base_paths |
| + self._samples_path = samples_path |
| def _MakeBranchDict(self, branch): |
| return { |
| @@ -37,6 +43,56 @@ class TemplateDataSource(object): |
| 'current': branch |
| } |
| + def _InsertApiItems(self, api_items, js_file): |
| + funcs = re.findall('(chrome\.[a-zA-Z0-9\.]+)', |
| + self._file_cache.get(js_file)) |
| + return api_items + list(set(funcs) - set(api_items)) |
|
not at google - send to devlin
2012/07/12 00:22:48
This is pretty indirect, just pass through set and
cduvall
2012/07/12 01:51:23
Done.
|
| + |
| + def _MakeApiLink(self, prefix, item): |
| + api, name = item.replace('chrome.', '').rsplit('.', 1) |
| + return api + '.html#' + prefix + '-' + name |
| + |
| + def _GetDescriptionFromManifest(self, path): |
| + manifest = self._file_cache.get(path + '/manifest.json') |
| + return json.loads(manifest).get('description') |
| + |
| + def _MakeSamplesList(self, files): |
| + samples_list = [] |
| + for filename in files: |
| + if filename.rsplit('/')[-1] == 'manifest.json': |
|
not at google - send to devlin
2012/07/12 00:22:48
if filename.rsplit('/')[-1] != 'manifest.json':
cduvall
2012/07/12 01:51:23
Done.
|
| + # This is a little hacky, but it makes a sample page. |
| + sample_path = filename.rsplit('/', 1)[-2] |
| + sample_files = filter(lambda x: x.startswith(sample_path), files) |
| + api_items = [] |
| + for file_ in sample_files: |
| + if file_.endswith('.js'): |
| + api_items = self._InsertApiItems(api_items, file_) |
| + |
| + api_calls = [] |
| + for item in api_items: |
| + if len(item.split('.')) < 3: |
| + continue |
| + if item.endswith('.addListener'): |
| + item = item.replace('.addListener', '') |
| + api_calls.append({ |
| + 'name': item.rsplit('.')[-1], |
| + 'link': self._MakeApiLink('event', item) |
| + }) |
| + else: |
| + api_calls.append({ |
| + 'name': item.rsplit('.')[-1], |
| + 'link': self._MakeApiLink('method', item) |
| + }) |
| + |
| + samples_list.append({ |
| + 'name': filename.rsplit('/')[-2], |
| + 'description': self._GetDescriptionFromManifest(sample_path), |
| + 'path': sample_path.split('/', 1)[1], |
| + 'files': [f.replace(sample_path + '/', '') for f in sample_files], |
| + 'api_calls': api_calls |
| + }) |
| + return samples_list |
|
not at google - send to devlin
2012/07/12 00:22:48
wow badass.
Definitely pull this stuff out of her
|
| + |
| def _LoadTemplate(self, template): |
| return Handlebar(template) |
| @@ -54,6 +110,7 @@ class TemplateDataSource(object): |
| 'branchInfo': self._branch_info, |
| 'intros': self._intro_data_source, |
| 'partials': self, |
| + 'samples': self._dir_cache.get(self._samples_path, True), |
|
not at google - send to devlin
2012/07/12 00:22:48
Can we make this lazily populate somehow? Most tem
cduvall
2012/07/12 01:51:23
Done.
|
| 'static': self._static_resources |
| }).text |