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 import logging | 5 import logging |
6 import os | 6 import os |
7 from document_parser import ParseDocument | 7 from document_parser import ParseDocument |
8 from third_party.json_schema_compiler.model import UnixName | 8 from third_party.json_schema_compiler.model import UnixName |
9 | 9 |
10 | 10 |
11 class DocumentRenderer(object): | 11 class DocumentRenderer(object): |
12 '''Performs document-level rendering such as the title, references, | 12 '''Performs document-level rendering such as the title, references, |
13 and table of contents: pulling that data out of the document, then | 13 and table of contents: pulling that data out of the document, then |
14 replacing the $(title), $(ref:...) and $(table_of_contents) tokens with them. | 14 replacing the $(title), $(ref:...) and $(table_of_contents) tokens with them. |
15 | 15 |
16 This can be thought of as a parallel to TemplateRenderer; while | 16 This can be thought of as a parallel to TemplateRenderer; while |
17 TemplateRenderer is responsible for interpreting templates and rendering files | 17 TemplateRenderer is responsible for interpreting templates and rendering files |
18 within the template engine, DocumentRenderer is responsible for interpreting | 18 within the template engine, DocumentRenderer is responsible for interpreting |
19 higher-level document concepts like the title and TOC, then performing string | 19 higher-level document concepts like the title and TOC, then performing string |
20 replacement for them. The syntax for this replacement is $(...) where ... is | 20 replacement for them. The syntax for this replacement is $(...) where ... is |
21 the concept. Currently title and table_of_contents are supported. | 21 the concept. Currently title and table_of_contents are supported. |
22 ''' | 22 ''' |
23 | 23 |
24 def __init__(self, table_of_contents_renderer, ref_resolver): | 24 def __init__(self, table_of_contents_renderer, ref_resolver): |
25 self._table_of_contents_renderer = table_of_contents_renderer | 25 self._table_of_contents_renderer = table_of_contents_renderer |
26 self._ref_resolver = ref_resolver | 26 self._ref_resolver = ref_resolver |
27 | 27 |
28 def _RenderLinks(self, document, path): | 28 def _RenderLinks(self, document, path): |
29 ''' Replaces all $(ref:...) references in |document| with html links | 29 ''' Replaces all $(ref:...) references in |document| with html links. |
| 30 |
| 31 References have two forms: |
| 32 |
| 33 $(ref:api.node) - Replaces the reference with a link to node on the |
| 34 API page. The title is set to the name of the node. |
| 35 |
| 36 $(ref:api.node Title) - Same as the previous form, but title is set |
| 37 to "Title". |
30 ''' | 38 ''' |
31 START_REF = '$(ref:' | 39 START_REF = '$(ref:' |
32 END_REF = ')' | 40 END_REF = ')' |
33 MAX_REF_LENGTH = 256 | 41 MAX_REF_LENGTH = 256 |
34 | 42 |
35 new_document = [] | 43 new_document = [] |
36 | 44 |
37 # Keeps track of position within |document| | 45 # Keeps track of position within |document| |
38 cursor_index = 0 | 46 cursor_index = 0 |
39 start_ref_index = document.find(START_REF) | 47 start_ref_index = document.find(START_REF) |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 parsed_document = ParseDocument(document, expect_title=render_title) | 90 parsed_document = ParseDocument(document, expect_title=render_title) |
83 toc_text, toc_warnings = self._table_of_contents_renderer.Render( | 91 toc_text, toc_warnings = self._table_of_contents_renderer.Render( |
84 parsed_document.sections) | 92 parsed_document.sections) |
85 | 93 |
86 # Only 1 title and 1 table of contents substitution allowed; in the common | 94 # Only 1 title and 1 table of contents substitution allowed; in the common |
87 # case, save necessarily running over the entire file. | 95 # case, save necessarily running over the entire file. |
88 if parsed_document.title: | 96 if parsed_document.title: |
89 document = document.replace('$(title)', parsed_document.title, 1) | 97 document = document.replace('$(title)', parsed_document.title, 1) |
90 return (document.replace('$(table_of_contents)', toc_text, 1), | 98 return (document.replace('$(table_of_contents)', toc_text, 1), |
91 parsed_document.warnings + toc_warnings) | 99 parsed_document.warnings + toc_warnings) |
OLD | NEW |