Show More
@@ -1766,12 +1766,12 b' def renderer_from_filename(filename, exc' | |||||
1766 | return None |
|
1766 | return None | |
1767 |
|
1767 | |||
1768 |
|
1768 | |||
1769 | def render(source, renderer='rst', mentions=False, relative_url=None, |
|
1769 | def render(source, renderer='rst', mentions=False, relative_urls=None, | |
1770 | repo_name=None): |
|
1770 | repo_name=None): | |
1771 |
|
1771 | |||
1772 | def maybe_convert_relative_links(html_source): |
|
1772 | def maybe_convert_relative_links(html_source): | |
1773 | if relative_url: |
|
1773 | if relative_urls: | |
1774 | return relative_links(html_source, relative_url) |
|
1774 | return relative_links(html_source, relative_urls) | |
1775 | return html_source |
|
1775 | return html_source | |
1776 |
|
1776 | |||
1777 | if renderer == 'rst': |
|
1777 | if renderer == 'rst': |
@@ -73,7 +73,7 b' class RhodeCodeWriter(writers.html4css1.' | |||||
73 | self.translator_class = CustomHTMLTranslator |
|
73 | self.translator_class = CustomHTMLTranslator | |
74 |
|
74 | |||
75 |
|
75 | |||
76 | def relative_links(html_source, server_path): |
|
76 | def relative_links(html_source, server_paths): | |
77 | if not html_source: |
|
77 | if not html_source: | |
78 | return html_source |
|
78 | return html_source | |
79 |
|
79 | |||
@@ -92,12 +92,16 b' def relative_links(html_source, server_p' | |||||
92 | for el in doc.cssselect('img, video'): |
|
92 | for el in doc.cssselect('img, video'): | |
93 | src = el.attrib.get('src') |
|
93 | src = el.attrib.get('src') | |
94 | if src: |
|
94 | if src: | |
95 | el.attrib['src'] = relative_path(src, server_path) |
|
95 | el.attrib['src'] = relative_path(src, server_paths['raw']) | |
96 |
|
96 | |||
97 | for el in doc.cssselect('a:not(.gfm)'): |
|
97 | for el in doc.cssselect('a:not(.gfm)'): | |
98 | src = el.attrib.get('href') |
|
98 | src = el.attrib.get('href') | |
99 | if src: |
|
99 | if src: | |
100 |
el.attrib['href'] |
|
100 | raw_mode = el.attrib['href'].endswith('?raw=1') | |
|
101 | if raw_mode: | |||
|
102 | el.attrib['href'] = relative_path(src, server_paths['raw']) | |||
|
103 | else: | |||
|
104 | el.attrib['href'] = relative_path(src, server_paths['standard']) | |||
101 |
|
105 | |||
102 | return lxml.html.tostring(doc) |
|
106 | return lxml.html.tostring(doc) | |
103 |
|
107 |
@@ -74,7 +74,13 b'' | |||||
74 | %else: |
|
74 | %else: | |
75 | % if c.file.size < c.visual.cut_off_limit_file: |
|
75 | % if c.file.size < c.visual.cut_off_limit_file: | |
76 | %if c.renderer and not c.annotate: |
|
76 | %if c.renderer and not c.annotate: | |
77 | ${h.render(c.file.content, renderer=c.renderer, relative_url=h.route_path('repo_file_raw',repo_name=c.repo_name,commit_id=c.commit.raw_id,f_path=c.f_path))} |
|
77 | <% | |
|
78 | relative_urls = { | |||
|
79 | 'raw': h.route_path('repo_file_raw',repo_name=c.repo_name,commit_id=c.commit.raw_id,f_path=c.f_path), | |||
|
80 | 'standard': h.route_path('repo_files',repo_name=c.repo_name,commit_id=c.commit.raw_id,f_path=c.f_path), | |||
|
81 | } | |||
|
82 | %> | |||
|
83 | ${h.render(c.file.content, renderer=c.renderer, relative_urls=relative_urls)} | |||
78 | %else: |
|
84 | %else: | |
79 | <table class="cb codehilite"> |
|
85 | <table class="cb codehilite"> | |
80 | %if c.annotate: |
|
86 | %if c.annotate: |
@@ -245,11 +245,14 b' def test_relative_path(src_path, server_' | |||||
245 | ('<div></div>', '<div></div>'), |
|
245 | ('<div></div>', '<div></div>'), | |
246 | ('<img src="/file.png"></img>', '<img src="/path/raw/file.png">'), |
|
246 | ('<img src="/file.png"></img>', '<img src="/path/raw/file.png">'), | |
247 | ('<img src="data:abcd"/>', '<img src="data:abcd">'), |
|
247 | ('<img src="data:abcd"/>', '<img src="data:abcd">'), | |
248 | ('<a href="/file.png"></a>', '<a href="/path/raw/file.png"></a>'), |
|
248 | ('<a href="/file.png?raw=1"></a>', '<a href="/path/raw/file.png?raw=1"></a>'), | |
|
249 | ('<a href="/file.png"></a>', '<a href="/path/file.png"></a>'), | |||
249 | ('<a href="#anchor"></a>', '<a href="#anchor"></a>'), |
|
250 | ('<a href="#anchor"></a>', '<a href="#anchor"></a>'), | |
250 | ('<a href="./README.md"></a>', '<a href="/path/raw/README.md"></a>'), |
|
251 | ('<a href="./README.md?raw=1"></a>', '<a href="/path/raw/README.md?raw=1"></a>'), | |
251 |
('<a href=" |
|
252 | ('<a href="./README.md"></a>', '<a href="/path/README.md"></a>'), | |
|
253 | ('<a href="../README.md"></a>', '<a href="/README.md"></a>'), | |||
252 |
|
254 | |||
253 | ]) |
|
255 | ]) | |
254 | def test_relative_links(src_html, expected_html): |
|
256 | def test_relative_links(src_html, expected_html): | |
255 | assert relative_links(src_html, '/path/raw/file.md') == expected_html |
|
257 | server_paths = {'raw': '/path/raw/file.md', 'standard': '/path/file.md'} | |
|
258 | assert relative_links(src_html, server_paths=server_paths) == expected_html |
General Comments 0
You need to be logged in to leave comments.
Login now