diff --git a/swh/web/api/apidoc.py b/swh/web/api/apidoc.py --- a/swh/web/api/apidoc.py +++ b/swh/web/api/apidoc.py @@ -78,8 +78,12 @@ par = par.replace('', '**') par = par.replace('', '``') par = par.replace('', '``') - # remove parsed document markups - par = re.sub('<[^<]+?>', '', par) + # keep links to web pages + if '', + r'`\1 <\2>`_', par) + # remove parsed document markups but keep rst links + par = re.sub(r'<[^<]+?>(?!`_)', '', par) # api urls cleanup to generate valid links afterwards subs_made = 1 while subs_made: @@ -88,8 +92,8 @@ while subs_made: (par, subs_made) = re.subn(r'(:http:.*)(\[.*\])', r'\1', par) par = par.replace('//', '/') - # transform references to api endpoints into valid rst links - par = re.sub(':http:get:`([^,]*)`', r'`<\1>`_', par) + # transform references to api endpoints doc into valid rst links + par = re.sub(':http:get:`([^,`]*)`', r'`\1 <\1doc/>`_', par) # transform references to some elements into bold text par = re.sub(':http:header:`(.*)`', r'**\1**', par) par = re.sub(':func:`(.*)`', r'**\1**', par) diff --git a/swh/web/tests/api/test_apidoc.py b/swh/web/tests/api/test_apidoc.py --- a/swh/web/tests/api/test_apidoc.py +++ b/swh/web/tests/api/test_apidoc.py @@ -322,8 +322,9 @@ { 'name': 'directory_url', 'type': 'string', - 'doc': ('link to ``_ to get information about ' - 'the directory associated to the revision') + 'doc': ('link to `/api/1/directory/ `_ ' + 'to get information about the directory associated to ' + 'the revision') }, { 'name': 'id', @@ -346,8 +347,8 @@ 'doc': ('the parents of the revision, i.e. the previous revisions ' 'that head directly to it, each entry of that array ' 'contains an unique parent revision identifier but also a ' - 'link to ``_ to get more information ' - 'about it') + 'link to `/api/1/revision/ `_ ' + 'to get more information about it') }, { 'name': 'type', @@ -450,3 +451,49 @@ assert input_html_doc in html assert output_html_doc in html + + +@api_route(r'/endpoint/links/in/doc/', 'api-1-endpoint-links-in-doc') +@api_doc('/endpoint/links/in/doc/') +def apidoc_test_endpoint_with_links_in_doc(request): + """ + .. http:get:: /api/1/post/endpoint/ + + Endpoint documentation with links to + :http:get:`/api/1/content/[(hash_type):](hash)/`, + :http:get:`/api/1/directory/(sha1_git)/[(path)/]` + and `archive `_. + """ + pass + + +def test_apidoc_with_links(client): + url = reverse('api-1-endpoint-links-in-doc') + rv = client.get(url, HTTP_ACCEPT='text/html') + assert rv.status_code == 200, rv.content + assert_template_used(rv, 'api/apidoc.html') + + html = prettify_html(rv.content) + + first_link = textwrap.indent(( + '\n' + ' /api/1/content/\n' + '' + ), ' '*9) + + second_link = textwrap.indent(( + '\n' + ' /api/1/directory/\n' + '' + ), ' '*9) + + third_link = textwrap.indent(( + '\n' + ' archive\n' + '' + ), ' '*9) + + assert first_link in html + assert second_link in html + assert third_link in html