Commit 9b966196 authored by benwells@chromium.org's avatar benwells@chromium.org

Correct $ref links in extensions documentation server relative location.

The links are now prefixed as needed with enough '../'s as appropriate.
This fixes $ref links from manifest articles.

BUG=315390

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@238337 0039d316-1c4b-4281-b951-d872f2087c98
parent b763b84f
application: chrome-apps-doc
version: 2-42-10
version: 2-43-0
runtime: python27
api_version: 1
threadsafe: false
......
......@@ -2,4 +2,4 @@ cron:
- description: Repopulates all cached data.
url: /_cron
schedule: every 5 minutes
target: 2-42-10
target: 2-43-0
......@@ -36,7 +36,9 @@ class IntroDataSource(DataSource):
# Guess the name of the API from the path to the intro.
api_name = os.path.splitext(intro_path.split('/')[-1])[0]
return Handlebar(
self._ref_resolver.ResolveAllLinks(intro, namespace=api_name),
self._ref_resolver.ResolveAllLinks(intro,
relative_to=self._request.path,
namespace=api_name),
name=intro_path)
def get(self, key):
......
......@@ -5,6 +5,7 @@
from intro_data_source import IntroDataSource
from server_instance import ServerInstance
from servlet import Request
from test_data.canned_data import CANNED_TEST_FILE_SYSTEM_DATA
from test_file_system import TestFileSystem
import unittest
......@@ -15,7 +16,8 @@ class IntroDataSourceTest(unittest.TestCase):
TestFileSystem(CANNED_TEST_FILE_SYSTEM_DATA))
def testIntro(self):
intro_data_source = IntroDataSource(self._server_instance, None)
intro_data_source = IntroDataSource(
self._server_instance, Request.ForTest(''))
intro_data = intro_data_source.get('test_intro')
article_data = intro_data_source.get('test_article')
......
......@@ -170,13 +170,20 @@ class ReferenceResolver(object):
'name': ref
}
def ResolveAllLinks(self, text, namespace=None):
def ResolveAllLinks(self, text, relative_to='', namespace=None):
"""This method will resolve all $ref links in |text| using namespace
|namespace| if not None. Any links that cannot be resolved will be replaced
using the default link format that |SafeGetLink| uses.
The links will be generated relative to |relative_to|.
"""
if text is None or '$ref:' not in text:
return text
# requestPath should be of the form (apps|extensions)/...../page.html.
# link_prefix should that the target will point to
# (apps|extensions)/target.html. Note multiplying a string by a negative
# number gives the empty string.
link_prefix = '../' * (relative_to.count('/') - 1)
split_text = text.split('$ref:')
# |split_text| is an array of text chunks that all start with the
# argument to '$ref:'.
......@@ -204,6 +211,6 @@ class ReferenceResolver(object):
rest = ref_and_rest[match.end():]
ref_dict = self.SafeGetLink(ref, namespace=namespace, title=title)
formatted_text.append('<a href="%(href)s">%(text)s</a>%(rest)s' %
{ 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest })
formatted_text.append('<a href="%s%s">%s</a>%s' %
(link_prefix, ref_dict['href'], ref_dict['text'], rest))
return ''.join(formatted_text)
......@@ -170,5 +170,47 @@ class APIDataSourceTest(unittest.TestCase):
resolver.ResolveAllLinks('$ref:bar.bon.',
namespace='baz'))
# If a request is provided it should construct an appropriate relative link.
self.assertEqual(
'Hi <a href="../../bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>, '
'<a href="../../bar.bon.html#property-bar_bon_p3">Bon Bon</a>, '
'<a href="../../bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>',
resolver.ResolveAllLinks(
'Hi $ref:bar_bon_p3, $ref:[bar_bon_p3 Bon Bon], $ref:bar_bon_p3',
relative_to='big/long/path/bar.html',
namespace='bar.bon'))
self.assertEqual(
'Hi <a href="bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>, '
'<a href="bar.bon.html#property-bar_bon_p3">Bon Bon</a>, '
'<a href="bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>',
resolver.ResolveAllLinks(
'Hi $ref:bar_bon_p3, $ref:[bar_bon_p3 Bon Bon], $ref:bar_bon_p3',
relative_to='',
namespace='bar.bon'))
self.assertEqual(
'Hi <a href="bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>, '
'<a href="bar.bon.html#property-bar_bon_p3">Bon Bon</a>, '
'<a href="bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>',
resolver.ResolveAllLinks(
'Hi $ref:bar_bon_p3, $ref:[bar_bon_p3 Bon Bon], $ref:bar_bon_p3',
relative_to='bar.html',
namespace='bar.bon'))
self.assertEqual(
'Hi <a href="bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>, '
'<a href="bar.bon.html#property-bar_bon_p3">Bon Bon</a>, '
'<a href="bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>',
resolver.ResolveAllLinks(
'Hi $ref:bar_bon_p3, $ref:[bar_bon_p3 Bon Bon], $ref:bar_bon_p3',
relative_to='foo/bar.html',
namespace='bar.bon'))
self.assertEqual(
'Hi <a href="../bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>, '
'<a href="../bar.bon.html#property-bar_bon_p3">Bon Bon</a>, '
'<a href="../bar.bon.html#property-bar_bon_p3">bar_bon_p3</a>',
resolver.ResolveAllLinks(
'Hi $ref:bar_bon_p3, $ref:[bar_bon_p3 Bon Bon], $ref:bar_bon_p3',
relative_to='foo/baz/bar.html',
namespace='bar.bon'))
if __name__ == '__main__':
unittest.main()
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