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