Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1384)

Side by Side Diff: chrome/common/extensions/docs/server2/document_renderer.py

Issue 441053004: Docserver: Modify DocumentRenderer to generate absolute links (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 platform_util import ExtractPlatformFromURL
9 from third_party.json_schema_compiler.model import UnixName 9 from third_party.json_schema_compiler.model import UnixName
10 10
11 11
12 class DocumentRenderer(object): 12 class DocumentRenderer(object):
13 '''Performs document-level rendering such as the title, references, 13 '''Performs document-level rendering such as the title, references,
14 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
15 replacing the $(title), $(ref:...) and $(table_of_contents) tokens with them. 15 replacing the $(title), $(ref:...) and $(table_of_contents) tokens with them.
16 16
17 This can be thought of as a parallel to TemplateRenderer; while 17 This can be thought of as a parallel to TemplateRenderer; while
18 TemplateRenderer is responsible for interpreting templates and rendering files 18 TemplateRenderer is responsible for interpreting templates and rendering files
19 within the template engine, DocumentRenderer is responsible for interpreting 19 within the template engine, DocumentRenderer is responsible for interpreting
20 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
21 replacement for them. The syntax for this replacement is $(...) where ... is 21 replacement for them. The syntax for this replacement is $(...) where ... is
22 the concept. Currently title and table_of_contents are supported. 22 the concept. Currently title and table_of_contents are supported.
23 ''' 23 '''
24 24
25 def __init__(self, table_of_contents_renderer, platform_bundle): 25 def __init__(self, table_of_contents_renderer, platform_bundle):
26 self._table_of_contents_renderer = table_of_contents_renderer 26 self._table_of_contents_renderer = table_of_contents_renderer
27 self._platform_bundle = platform_bundle 27 self._platform_bundle = platform_bundle
28 28
29 def _RenderLinks(self, document, path): 29 def _RenderLinks(self, document, host, path):
30 ''' Replaces all $(ref:...) references in |document| with html links. 30 ''' Replaces all $(ref:...) references in |document| with html links.
31 31
32 References have two forms: 32 References have two forms:
33 33
34 $(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
35 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.
36 36
37 $(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
38 to "Title". 38 to "Title".
39 ''' 39 '''
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 if platform is None: 72 if platform is None:
73 logging.error('Cannot resolve reference without a platform.') 73 logging.error('Cannot resolve reference without a platform.')
74 continue 74 continue
75 ref_dict = self._platform_bundle.GetReferenceResolver( 75 ref_dict = self._platform_bundle.GetReferenceResolver(
76 platform).SafeGetLink(ref_parts[0], 76 platform).SafeGetLink(ref_parts[0],
77 namespace=api_name, 77 namespace=api_name,
78 title=title, 78 title=title,
79 path=path) 79 path=path)
80 80
81 new_document.append(document[cursor_index:start_ref_index]) 81 new_document.append(document[cursor_index:start_ref_index])
82 new_document.append('<a href=%s>%s</a>' % (ref_dict['href'], 82 new_document.append('<a href=%s/%s/%s>%s</a>' % (host,
not at google - send to devlin 2014/08/06 23:00:48 you shouldn't need a host, just /some/link will de
ahernandez 2014/08/07 00:56:12 This complains about links with '/' at the front:h
not at google - send to devlin 2014/08/07 15:21:18 yeah, the leading / validity check is to catch bug
ahernandez 2014/08/07 17:21:31 Changing it to '<a href=/%s/%s>%s</a>' is what I'm
not at google - send to devlin 2014/08/07 17:37:08 such links are valid, at least on the web. I shoul
83 ref_dict['text'])) 83 platform,
84 ref_dict['href'],
85 ref_dict['text']))
84 86
85 cursor_index = end_ref_index + 1 87 cursor_index = end_ref_index + 1
86 start_ref_index = document.find(START_REF, cursor_index) 88 start_ref_index = document.find(START_REF, cursor_index)
87 89
88 new_document.append(document[cursor_index:]) 90 new_document.append(document[cursor_index:])
89 91
90 return ''.join(new_document) 92 return ''.join(new_document)
91 93
92 def Render(self, document, path, render_title=False): 94 def Render(self, document, host, path, render_title=False):
95 ''' |document|: document to be rendered.
96 |host|: server the document is requested from.
97 |path|: request path to the document.
98 |render_title|: boolean representing whether or not to render a title.
99 '''
93 # Render links first so that parsing and later replacements aren't 100 # Render links first so that parsing and later replacements aren't
94 # affected by $(ref...) substitutions 101 # affected by $(ref...) substitutions
95 document = self._RenderLinks(document, path) 102 document = self._RenderLinks(document, host, path)
96 103
97 parsed_document = ParseDocument(document, expect_title=render_title) 104 parsed_document = ParseDocument(document, expect_title=render_title)
98 toc_text, toc_warnings = self._table_of_contents_renderer.Render( 105 toc_text, toc_warnings = self._table_of_contents_renderer.Render(
99 parsed_document.sections) 106 parsed_document.sections)
100 107
101 # Only 1 title and 1 table of contents substitution allowed; in the common 108 # Only 1 title and 1 table of contents substitution allowed; in the common
102 # case, save necessarily running over the entire file. 109 # case, save necessarily running over the entire file.
103 if parsed_document.title: 110 if parsed_document.title:
104 document = document.replace('$(title)', parsed_document.title, 1) 111 document = document.replace('$(title)', parsed_document.title, 1)
105 return (document.replace('$(table_of_contents)', toc_text, 1), 112 return (document.replace('$(table_of_contents)', toc_text, 1),
106 parsed_document.warnings + toc_warnings) 113 parsed_document.warnings + toc_warnings)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698