##// END OF EJS Templates
`strip_files_prefix` now also strips markdown style links, `latex_base` updated to include filter
Peter Davis -
Show More
@@ -61,7 +61,6 b' default_filters = {'
61 'citation2latex': filters.citation2latex,
61 'citation2latex': filters.citation2latex,
62 'path2url': filters.path2url,
62 'path2url': filters.path2url,
63 'add_prompts': filters.add_prompts,
63 'add_prompts': filters.add_prompts,
64 'strip_url_static_file_prefix': filters.strip_url_static_file_prefix,
65 }
64 }
66
65
67 #-----------------------------------------------------------------------------
66 #-----------------------------------------------------------------------------
@@ -19,8 +19,6 b' import re'
19 # Globals and constants
19 # Globals and constants
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 MARKDOWN_IMAGE_RE = re.compile(r'!\[(?P<caption>.*?)\]\(/?files/(?P<location>.*?)\)')
23
24 LATEX_RE_SUBS = (
22 LATEX_RE_SUBS = (
25 (re.compile(r'\.\.\.+'), r'\\ldots'),
23 (re.compile(r'\.\.\.+'), r'\\ldots'),
26 )
24 )
@@ -46,8 +44,7 b' LATEX_SUBS = {'
46 # Functions
44 # Functions
47 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
48
46
49 __all__ = ['escape_latex',
47 __all__ = ['escape_latex']
50 'strip_url_static_file_prefix']
51
48
52 def escape_latex(text):
49 def escape_latex(text):
53 """
50 """
@@ -63,10 +60,4 b' def escape_latex(text):'
63 text = pattern.sub(replacement, text)
60 text = pattern.sub(replacement, text)
64
61
65 return text
62 return text
66
67
68 def strip_url_static_file_prefix(text):
69 text = MARKDOWN_IMAGE_RE.sub(r'![\1](\2)', text)
70 return text
71
72
63
@@ -123,18 +123,21 b' def strip_dollars(text):'
123
123
124
124
125 files_url_pattern = re.compile(r'(src|href)\=([\'"]?)files/')
125 files_url_pattern = re.compile(r'(src|href)\=([\'"]?)files/')
126 markdown_url_pattern = re.compile(r'!\[(?P<caption>.*?)\]\(/?files/(?P<location>.*?)\)')
126
127
127 def strip_files_prefix(text):
128 def strip_files_prefix(text):
128 """
129 """
129 Fix all fake URLs that start with `files/`,
130 Fix all fake URLs that start with `files/`, stripping out the `files/` prefix.
130 stripping out the `files/` prefix.
131 Applies to both urls (for html) and relative paths (for markdown paths).
131
132
132 Parameters
133 Parameters
133 ----------
134 ----------
134 text : str
135 text : str
135 Text in which to replace 'src="files/real...' with 'src="real...'
136 Text in which to replace 'src="files/real...' with 'src="real...'
136 """
137 """
137 return files_url_pattern.sub(r"\1=\2", text)
138 cleaned_text = files_url_pattern.sub(r"\1=\2", text)
139 cleaned_text = markdown_url_pattern.sub(r'![\1](\2)', cleaned_text)
140 return cleaned_text
138
141
139
142
140 def comment_lines(text, prefix='# '):
143 def comment_lines(text, prefix='# '):
@@ -41,20 +41,3 b' class TestLatex(TestsBase):'
41 def _try_escape_latex(self, test, result):
41 def _try_escape_latex(self, test, result):
42 """Try to remove latex from string"""
42 """Try to remove latex from string"""
43 self.assertEqual(escape_latex(test), result)
43 self.assertEqual(escape_latex(test), result)
44
45 def test_strip_url_static_file_prefix(self):
46 tests = [
47 ('hello!', 'hello!'),
48 ('hello![caption]', 'hello![caption]'),
49 ('hello![caption](/url/location.gif)', 'hello![caption](/url/location.gif)'),
50 ('hello![caption](url/location.gif)', 'hello![caption](url/location.gif)'),
51 ('hello![caption](url/location.gif)', 'hello![caption](url/location.gif)'),
52 ('hello![caption](files/url/location.gif)', 'hello![caption](url/location.gif)'),
53 ('hello![caption](/files/url/location.gif)', 'hello![caption](url/location.gif)'),
54 ]
55
56 for test in tests:
57 self._test_strip_url_static_file_prefix(test[0], test[1])
58
59 def _test_strip_url_static_file_prefix(self, test, result):
60 self.assertEqual(strip_url_static_file_prefix(test), result)
@@ -87,7 +87,13 b' class TestStrings(TestsBase):'
87 ('/files', '/files'),
87 ('/files', '/files'),
88 ('test="/files"', 'test="/files"'),
88 ('test="/files"', 'test="/files"'),
89 ('My files are in `files/`', 'My files are in `files/`'),
89 ('My files are in `files/`', 'My files are in `files/`'),
90 ('<a href="files/test.html">files/test.html</a>', '<a href="test.html">files/test.html</a>')]
90 ('<a href="files/test.html">files/test.html</a>', '<a href="test.html">files/test.html</a>'),
91 ('hello![caption]', 'hello![caption]'),
92 ('hello![caption](/url/location.gif)', 'hello![caption](/url/location.gif)'),
93 ('hello![caption](url/location.gif)', 'hello![caption](url/location.gif)'),
94 ('hello![caption](url/location.gif)', 'hello![caption](url/location.gif)'),
95 ('hello![caption](files/url/location.gif)', 'hello![caption](url/location.gif)'),
96 ('hello![caption](/files/url/location.gif)', 'hello![caption](url/location.gif)'),]
91 for test in tests:
97 for test in tests:
92 self._try_files_prefix(test[0], test[1])
98 self._try_files_prefix(test[0], test[1])
93
99
@@ -202,7 +202,7 b' This template does not define a docclass, the inheriting class must define this.'
202 ((* elif cell.level == 6 -*))
202 ((* elif cell.level == 6 -*))
203 ((* block h6 -*))\\*\textit((* endblock h6 -*))
203 ((* block h6 -*))\\*\textit((* endblock h6 -*))
204 ((*- endif -*))
204 ((*- endif -*))
205 {((( cell.source | replace('\n', ' ') | citation2latex | strip_url_static_file_prefix | markdown2latex )))}
205 {((( cell.source | replace('\n', ' ') | citation2latex | strip_files_prefix | markdown2latex )))}
206
206
207 ((* endblock headingcell *))
207 ((* endblock headingcell *))
208
208
@@ -215,7 +215,7 b' This template does not define a docclass, the inheriting class must define this.'
215
215
216 % Render markdown
216 % Render markdown
217 ((* block markdowncell scoped *))
217 ((* block markdowncell scoped *))
218 ((( cell.source | citation2latex | strip_url_static_file_prefix | markdown2latex )))
218 ((( cell.source | citation2latex | strip_files_prefix | markdown2latex )))
219 ((* endblock markdowncell *))
219 ((* endblock markdowncell *))
220
220
221 % Spit out the contents of raw cells unmodified
221 % Spit out the contents of raw cells unmodified
General Comments 0
You need to be logged in to leave comments. Login now