Docserver: Modify DocumentRenderer to generate absolute links

BUG=400775
NOTRY=True

Review URL: https://codereview.chromium.org/441053004

Cr-Commit-Position: refs/heads/master@{#288290}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@288290 0039d316-1c4b-4281-b951-d872f2087c98
parent 98a92447
application: chrome-apps-doc application: chrome-apps-doc
version: 3-39-2 version: 3-39-3
runtime: python27 runtime: python27
api_version: 1 api_version: 1
threadsafe: false threadsafe: false
......
...@@ -2,4 +2,4 @@ cron: ...@@ -2,4 +2,4 @@ cron:
- description: Repopulates all cached data. - description: Repopulates all cached data.
url: /_cron url: /_cron
schedule: every 5 minutes schedule: every 5 minutes
target: 3-39-2 target: 3-39-3
...@@ -79,8 +79,10 @@ class DocumentRenderer(object): ...@@ -79,8 +79,10 @@ class DocumentRenderer(object):
path=path) path=path)
new_document.append(document[cursor_index:start_ref_index]) new_document.append(document[cursor_index:start_ref_index])
new_document.append('<a href=%s>%s</a>' % (ref_dict['href'], new_document.append('<a href=%s/%s>%s</a>' % (
ref_dict['text'])) self._platform_bundle._base_path + platform,
ref_dict['href'],
ref_dict['text']))
cursor_index = end_ref_index + 1 cursor_index = end_ref_index + 1
start_ref_index = document.find(START_REF, cursor_index) start_ref_index = document.find(START_REF, cursor_index)
...@@ -90,6 +92,10 @@ class DocumentRenderer(object): ...@@ -90,6 +92,10 @@ class DocumentRenderer(object):
return ''.join(new_document) return ''.join(new_document)
def Render(self, document, path, render_title=False): def Render(self, document, path, render_title=False):
''' |document|: document to be rendered.
|path|: request path to the document.
|render_title|: boolean representing whether or not to render a title.
'''
# Render links first so that parsing and later replacements aren't # Render links first so that parsing and later replacements aren't
# affected by $(ref...) substitutions # affected by $(ref...) substitutions
document = self._RenderLinks(document, path) document = self._RenderLinks(document, path)
......
...@@ -15,28 +15,32 @@ class DocumentRendererUnittest(unittest.TestCase): ...@@ -15,28 +15,32 @@ class DocumentRendererUnittest(unittest.TestCase):
def setUp(self): def setUp(self):
self._renderer = ServerInstance.ForTest( self._renderer = ServerInstance.ForTest(
TestFileSystem(CANNED_TEST_FILE_SYSTEM_DATA)).document_renderer TestFileSystem(CANNED_TEST_FILE_SYSTEM_DATA)).document_renderer
self._path = 'apps/some/path/to/document.html'
def _Render(self, document, render_title=False):
return self._renderer.Render(document,
self._path,
render_title=render_title)
def testNothingToSubstitute(self): def testNothingToSubstitute(self):
document = 'hello world' document = 'hello world'
path = 'apps/some/path/to/document.html'
text, warnings = self._renderer.Render(document, path) text, warnings = self._Render(document)
self.assertEqual(document, text) self.assertEqual(document, text)
self.assertEqual([], warnings) self.assertEqual([], warnings)
text, warnings = self._renderer.Render(document, path, render_title=True) text, warnings = self._Render(document, render_title=True)
self.assertEqual(document, text) self.assertEqual(document, text)
self.assertEqual(['Expected a title'], warnings) self.assertEqual(['Expected a title'], warnings)
def testTitles(self): def testTitles(self):
document = '<h1>title</h1> then $(title) then another $(title)' document = '<h1>title</h1> then $(title) then another $(title)'
path = 'apps/some/path/to/document.html'
text, warnings = self._renderer.Render(document, path) text, warnings = self._Render(document)
self.assertEqual(document, text) self.assertEqual(document, text)
self.assertEqual(['Found unexpected title "title"'], warnings) self.assertEqual(['Found unexpected title "title"'], warnings)
text, warnings = self._renderer.Render(document, path, render_title=True) text, warnings = self._Render(document, render_title=True)
self.assertEqual('<h1>title</h1> then title then another $(title)', text) self.assertEqual('<h1>title</h1> then title then another $(title)', text)
self.assertEqual([], warnings) self.assertEqual([], warnings)
...@@ -45,73 +49,74 @@ class DocumentRendererUnittest(unittest.TestCase): ...@@ -45,73 +49,74 @@ class DocumentRendererUnittest(unittest.TestCase):
'and another $(table_of_contents)') 'and another $(table_of_contents)')
expected_document = ('here is a toc <table-of-contents> and another ' expected_document = ('here is a toc <table-of-contents> and another '
'$(table_of_contents)') '$(table_of_contents)')
path = 'apps/some/path/to/document.html'
text, warnings = self._renderer.Render(document, path) text, warnings = self._Render(document)
self.assertEqual(expected_document, text) self.assertEqual(expected_document, text)
self.assertEqual([], warnings) self.assertEqual([], warnings)
text, warnings = self._renderer.Render(document, path, render_title=True) text, warnings = self._Render(document, render_title=True)
self.assertEqual(expected_document, text) self.assertEqual(expected_document, text)
self.assertEqual(['Expected a title'], warnings) self.assertEqual(['Expected a title'], warnings)
def testRefs(self): def testRefs(self):
# The references in this and subsequent tests won't actually be resolved # The references in this and subsequent tests won't actually be resolved
document = 'A ref $(ref:baz.baz_e1) here, $(ref:foo.foo_t3 ref title) there' document = 'A ref $(ref:baz.baz_e1) here, $(ref:foo.foo_t3 ref title) there'
expected_document = ('A ref <a href=#type-baz_e1>baz.baz_e1</a> ' expected_document = ''.join([
'here, <a href=#type-foo_t3>ref title</a> ' 'A ref <a href=/apps/#type-baz_e1>baz.baz_e1</a> here, ',
'there') '<a href=/apps/#type-foo_t3>ref title</a> there'
path = 'apps/some/path/to/document.html' ])
text, warnings = self._renderer.Render(document, path) text, warnings = self._Render(document)
self.assertEqual(expected_document, text) self.assertEqual(expected_document, text)
self.assertEqual([], warnings) self.assertEqual([], warnings)
text, warnings = self._renderer.Render(document, path, render_title=True) text, warnings = self._Render(document, render_title=True)
self.assertEqual(expected_document, text) self.assertEqual(expected_document, text)
self.assertEqual(['Expected a title'], warnings) self.assertEqual(['Expected a title'], warnings)
def testTitleAndToc(self): def testTitleAndToc(self):
document = '<h1>title</h1> $(title) and $(table_of_contents)' document = '<h1>title</h1> $(title) and $(table_of_contents)'
path = 'apps/some/path/to/document.html'
text, warnings = self._renderer.Render(document, path) text, warnings = self._Render(document)
self.assertEqual('<h1>title</h1> $(title) and <table-of-contents>', text) self.assertEqual('<h1>title</h1> $(title) and <table-of-contents>', text)
self.assertEqual(['Found unexpected title "title"'], warnings) self.assertEqual(['Found unexpected title "title"'], warnings)
text, warnings = self._renderer.Render(document, path, render_title=True) text, warnings = self._Render(document, render_title=True)
self.assertEqual('<h1>title</h1> title and <table-of-contents>', text) self.assertEqual('<h1>title</h1> title and <table-of-contents>', text)
self.assertEqual([], warnings) self.assertEqual([], warnings)
def testRefInTitle(self): def testRefInTitle(self):
document = '<h1>$(ref:baz.baz_e1 title)</h1> A $(title) was here' document = '<h1>$(ref:baz.baz_e1 title)</h1> A $(title) was here'
expected_document_no_title = ('<h1><a href=#type-baz_e1>' href = '/apps/#type-baz_e1'
'title</a></h1> A $(title) was here') expected_document_no_title = ''.join([
'<h1><a href=%s>title</a></h1> A $(title) was here' % href
])
expected_document = ('<h1><a href=#type-baz_e1>title</a></h1>' expected_document = ''.join([
' A title was here') '<h1><a href=%s>title</a></h1> A title was here' % href
path = 'apps/some/path/to/document.html' ])
text, warnings = self._renderer.Render(document, path) text, warnings = self._Render(document)
self.assertEqual(expected_document_no_title, text) self.assertEqual(expected_document_no_title, text)
self.assertEqual([('Found unexpected title "title"')], warnings) self.assertEqual([('Found unexpected title "title"')], warnings)
text, warnings = self._renderer.Render(document, path, render_title=True) text, warnings = self._Render(document, render_title=True)
self.assertEqual(expected_document, text) self.assertEqual(expected_document, text)
self.assertEqual([], warnings) self.assertEqual([], warnings)
def testRefSplitAcrossLines(self): def testRefSplitAcrossLines(self):
document = 'Hello, $(ref:baz.baz_e1 world). A $(ref:foo.foo_t3\n link)' document = 'Hello, $(ref:baz.baz_e1 world). A $(ref:foo.foo_t3\n link)'
expected_document = ('Hello, <a href=#type-baz_e1>world</a>. A <a href=' expected_document = ''.join([
'#type-foo_t3>link</a>') 'Hello, <a href=/apps/#type-baz_e1>world</a>. ',
'A <a href=/apps/#type-foo_t3>link</a>'
])
path = 'apps/some/path/to/document.html'
text, warnings = self._renderer.Render(document, path) text, warnings = self._Render(document)
self.assertEqual(expected_document, text) self.assertEqual(expected_document, text)
self.assertEqual([], warnings) self.assertEqual([], warnings)
text, warnings = self._renderer.Render(document, path, render_title=True) text, warnings = self._Render(document, render_title=True)
self.assertEqual(expected_document, text) self.assertEqual(expected_document, text)
self.assertEqual(['Expected a title'], warnings) self.assertEqual(['Expected a title'], warnings)
...@@ -126,17 +131,22 @@ class DocumentRendererUnittest(unittest.TestCase): ...@@ -126,17 +131,22 @@ class DocumentRendererUnittest(unittest.TestCase):
'reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla ' 'reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla '
'pariatur. Excepteur sint occaecat cupidatat non proident, sunt in ' 'pariatur. Excepteur sint occaecat cupidatat non proident, sunt in '
'culpa qui officia deserunt mollit anim id est laborum.') 'culpa qui officia deserunt mollit anim id est laborum.')
document = ('An invalid $(ref:foo.foo_t3 a title ' + _LOREM_IPSUM + document = ''.join([
'$(ref:baz.baz_e1) here') 'An invalid $(ref:foo.foo_t3 a title ',
expected_document = ('An invalid $(ref:foo.foo_t3 a title ' + _LOREM_IPSUM + _LOREM_IPSUM,
'<a href=#type-baz_e1>baz.baz_e1</a> here') '$(ref:baz.baz_e1) here'
path = 'apps/some/path/to/document_api.html' ])
expected_document = ''.join([
text, warnings = self._renderer.Render(document, path) 'An invalid $(ref:foo.foo_t3 a title ',
_LOREM_IPSUM,
'<a href=/apps/#type-baz_e1>baz.baz_e1</a> here'
])
text, warnings = self._Render(document)
self.assertEqual(expected_document, text) self.assertEqual(expected_document, text)
self.assertEqual([], warnings) self.assertEqual([], warnings)
text, warnings = self._renderer.Render(document, path, render_title=True) text, warnings = self._Render(document, render_title=True)
self.assertEqual(expected_document, text) self.assertEqual(expected_document, text)
self.assertEqual(['Expected a title'], warnings) self.assertEqual(['Expected a title'], warnings)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment