OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 extensions_paths import PRIVATE_TEMPLATES | 5 from extensions_paths import PRIVATE_TEMPLATES |
6 from file_system import FileNotFoundError | 6 from file_system import FileNotFoundError |
7 | 7 |
8 | 8 |
9 class TableOfContentsRenderer(object): | 9 class TableOfContentsRenderer(object): |
10 '''Renders a table of contents pulled from a list of DocumentSections | 10 '''Renders a table of contents pulled from a list of DocumentSections |
11 returned from document_parser.ParseDocument. | 11 returned from document_parser.ParseDocument. |
12 | 12 |
13 This performs key functionality of DocumentRenderer, pulled into its own | 13 This performs key functionality of DocumentRenderer, pulled into its own |
14 class for testability. | 14 class for testability. |
15 ''' | 15 ''' |
16 | 16 |
17 def __init__(self, host_file_system, compiled_fs_factory): | 17 def __init__(self, |
| 18 host_file_system, |
| 19 compiled_fs_factory, |
| 20 template_renderer): |
18 self._templates = compiled_fs_factory.ForTemplates(host_file_system) | 21 self._templates = compiled_fs_factory.ForTemplates(host_file_system) |
| 22 self._template_renderer = template_renderer |
19 | 23 |
20 def Render(self, sections): | 24 def Render(self, sections): |
21 '''Renders a list of DocumentSections |sections| and returns a tuple | 25 '''Renders a list of DocumentSections |sections| and returns a tuple |
22 (text, warnings). | 26 (text, warnings). |
23 ''' | 27 ''' |
24 path = '%s/table_of_contents.html' % PRIVATE_TEMPLATES | 28 path = '%s/table_of_contents.html' % PRIVATE_TEMPLATES |
25 try: | 29 try: |
26 table_of_contents_template = self._templates.GetFromFile(path).Get() | 30 table_of_contents_template = self._templates.GetFromFile(path).Get() |
27 except FileNotFoundError: | 31 except FileNotFoundError: |
28 return '', ['%s not found' % path] | 32 return '', ['%s not found' % path] |
29 | 33 |
30 def make_toc_items(entries): | 34 def make_toc_items(entries): |
31 return [{ | 35 return [{ |
32 'attributes': [(key, val) for key, val in entry.attributes.iteritems() | 36 'attributes': [(key, val) for key, val in entry.attributes.iteritems() |
33 if key != 'id'], | 37 if key != 'id'], |
34 'link': entry.attributes.get('id', ''), | 38 'link': entry.attributes.get('id', ''), |
35 'subheadings': make_toc_items(entry.entries), | 39 'subheadings': make_toc_items(entry.entries), |
36 'title': entry.name, | 40 'title': entry.name, |
37 } for entry in entries] | 41 } for entry in entries] |
38 | 42 |
39 toc_items = [] | 43 toc_items = [] |
40 for section in sections: | 44 for section in sections: |
41 items_for_section = make_toc_items(section.structure) | 45 items_for_section = make_toc_items(section.structure) |
42 if toc_items and items_for_section: | 46 if toc_items and items_for_section: |
43 items_for_section[0]['separator'] = True | 47 items_for_section[0]['separator'] = True |
44 toc_items.extend(items_for_section) | 48 toc_items.extend(items_for_section) |
45 | 49 |
46 table_of_contents = table_of_contents_template.Render({ | 50 return self._template_renderer.Render( |
47 'items': toc_items | 51 self._templates.GetFromFile( |
48 }) | 52 '%s/table_of_contents.html' % PRIVATE_TEMPLATES).Get(), |
49 return table_of_contents.text, table_of_contents.errors | 53 None, # no request |
| 54 data_sources=('partials'), |
| 55 additional_context={'items': toc_items}) |
OLD | NEW |