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 platform_util import ExtractPlatformFromURL |
8 from third_party.json_schema_compiler.model import UnixName | 9 from third_party.json_schema_compiler.model import UnixName |
9 | 10 |
10 | 11 |
11 class DocumentRenderer(object): | 12 class DocumentRenderer(object): |
12 '''Performs document-level rendering such as the title, references, | 13 '''Performs document-level rendering such as the title, references, |
13 and table of contents: pulling that data out of the document, then | 14 and table of contents: pulling that data out of the document, then |
14 replacing the $(title), $(ref:...) and $(table_of_contents) tokens with them. | 15 replacing the $(title), $(ref:...) and $(table_of_contents) tokens with them. |
15 | 16 |
16 This can be thought of as a parallel to TemplateRenderer; while | 17 This can be thought of as a parallel to TemplateRenderer; while |
17 TemplateRenderer is responsible for interpreting templates and rendering files | 18 TemplateRenderer is responsible for interpreting templates and rendering files |
18 within the template engine, DocumentRenderer is responsible for interpreting | 19 within the template engine, DocumentRenderer is responsible for interpreting |
19 higher-level document concepts like the title and TOC, then performing string | 20 higher-level document concepts like the title and TOC, then performing string |
20 replacement for them. The syntax for this replacement is $(...) where ... is | 21 replacement for them. The syntax for this replacement is $(...) where ... is |
21 the concept. Currently title and table_of_contents are supported. | 22 the concept. Currently title and table_of_contents are supported. |
22 ''' | 23 ''' |
23 | 24 |
24 def __init__(self, table_of_contents_renderer, ref_resolver): | 25 def __init__(self, table_of_contents_renderer, platform_bundle): |
25 self._table_of_contents_renderer = table_of_contents_renderer | 26 self._table_of_contents_renderer = table_of_contents_renderer |
26 self._ref_resolver = ref_resolver | 27 self._platform_bundle = platform_bundle |
27 | 28 |
28 def _RenderLinks(self, document, path): | 29 def _RenderLinks(self, document, path): |
29 ''' Replaces all $(ref:...) references in |document| with html links. | 30 ''' Replaces all $(ref:...) references in |document| with html links. |
30 | 31 |
31 References have two forms: | 32 References have two forms: |
32 | 33 |
33 $(ref:api.node) - Replaces the reference with a link to node on the | 34 $(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 API page. The title is set to the name of the node. |
35 | 36 |
36 $(ref:api.node Title) - Same as the previous form, but title is set | 37 $(ref:api.node Title) - Same as the previous form, but title is set |
(...skipping 23 matching lines...) Expand all Loading... |
60 new_document.append(document[cursor_index:end_ref_index + 1]) | 61 new_document.append(document[cursor_index:end_ref_index + 1]) |
61 else: | 62 else: |
62 ref = document[start_ref_index:end_ref_index] | 63 ref = document[start_ref_index:end_ref_index] |
63 ref_parts = ref[len(START_REF):].split(None, 1) | 64 ref_parts = ref[len(START_REF):].split(None, 1) |
64 | 65 |
65 # Guess the api name from the html name, replacing '_' with '.' (e.g. | 66 # Guess the api name from the html name, replacing '_' with '.' (e.g. |
66 # if the page is app_window.html, guess the api name is app.window) | 67 # if the page is app_window.html, guess the api name is app.window) |
67 api_name = os.path.splitext(os.path.basename(path))[0].replace('_', '.') | 68 api_name = os.path.splitext(os.path.basename(path))[0].replace('_', '.') |
68 title = ref_parts[0] if len(ref_parts) == 1 else ref_parts[1] | 69 title = ref_parts[0] if len(ref_parts) == 1 else ref_parts[1] |
69 | 70 |
70 ref_dict = self._ref_resolver.SafeGetLink(ref_parts[0], | 71 platform = ExtractPlatformFromURL(path) |
71 namespace=api_name, | 72 if platform is None: |
72 title=title) | 73 logging.error('Cannot resolve reference without a platform.') |
| 74 continue |
| 75 ref_dict = self._platform_bundle.GetReferenceResolver( |
| 76 platform).SafeGetLink(ref_parts[0], |
| 77 namespace=api_name, |
| 78 title=title, |
| 79 path=path) |
73 | 80 |
74 new_document.append(document[cursor_index:start_ref_index]) | 81 new_document.append(document[cursor_index:start_ref_index]) |
75 new_document.append('<a href=%s>%s</a>' % (ref_dict['href'], | 82 new_document.append('<a href=%s>%s</a>' % (ref_dict['href'], |
76 ref_dict['text'])) | 83 ref_dict['text'])) |
77 | 84 |
78 cursor_index = end_ref_index + 1 | 85 cursor_index = end_ref_index + 1 |
79 start_ref_index = document.find(START_REF, cursor_index) | 86 start_ref_index = document.find(START_REF, cursor_index) |
80 | 87 |
81 new_document.append(document[cursor_index:]) | 88 new_document.append(document[cursor_index:]) |
82 | 89 |
83 return ''.join(new_document) | 90 return ''.join(new_document) |
84 | 91 |
85 def Render(self, document, path, render_title=False): | 92 def Render(self, document, path, render_title=False): |
86 # Render links first so that parsing and later replacements aren't | 93 # Render links first so that parsing and later replacements aren't |
87 # affected by $(ref...) substitutions | 94 # affected by $(ref...) substitutions |
88 document = self._RenderLinks(document, path) | 95 document = self._RenderLinks(document, path) |
89 | 96 |
90 parsed_document = ParseDocument(document, expect_title=render_title) | 97 parsed_document = ParseDocument(document, expect_title=render_title) |
91 toc_text, toc_warnings = self._table_of_contents_renderer.Render( | 98 toc_text, toc_warnings = self._table_of_contents_renderer.Render( |
92 parsed_document.sections) | 99 parsed_document.sections) |
93 | 100 |
94 # Only 1 title and 1 table of contents substitution allowed; in the common | 101 # Only 1 title and 1 table of contents substitution allowed; in the common |
95 # case, save necessarily running over the entire file. | 102 # case, save necessarily running over the entire file. |
96 if parsed_document.title: | 103 if parsed_document.title: |
97 document = document.replace('$(title)', parsed_document.title, 1) | 104 document = document.replace('$(title)', parsed_document.title, 1) |
98 return (document.replace('$(table_of_contents)', toc_text, 1), | 105 return (document.replace('$(table_of_contents)', toc_text, 1), |
99 parsed_document.warnings + toc_warnings) | 106 parsed_document.warnings + toc_warnings) |
OLD | NEW |