##// END OF EJS Templates
remove strip_math_space...
MinRK -
Show More
@@ -56,7 +56,6 b' default_filters = {'
56 'html2text' : filters.html2text,
56 'html2text' : filters.html2text,
57 'add_anchor': filters.add_anchor,
57 'add_anchor': filters.add_anchor,
58 'ansi2latex': filters.ansi2latex,
58 'ansi2latex': filters.ansi2latex,
59 'strip_math_space': filters.strip_math_space,
60 'wrap_text': filters.wrap_text,
59 'wrap_text': filters.wrap_text,
61 'escape_latex': filters.escape_latex,
60 'escape_latex': filters.escape_latex,
62 'citation2latex': filters.citation2latex,
61 'citation2latex': filters.citation2latex,
@@ -44,8 +44,7 b' LATEX_SUBS = {'
44 # Functions
44 # Functions
45 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
46
46
47 __all__ = ['escape_latex',
47 __all__ = ['escape_latex']
48 'strip_math_space']
49
48
50 def escape_latex(text):
49 def escape_latex(text):
51 """
50 """
@@ -61,63 +60,4 b' def escape_latex(text):'
61 text = pattern.sub(replacement, text)
60 text = pattern.sub(replacement, text)
62
61
63 return text
62 return text
64
65
66 def strip_math_space(text):
67 """
68 Remove the space between latex math commands and enclosing $ symbols.
69 This filter is important because latex isn't as flexible as the notebook
70 front end when it comes to flagging math using ampersand symbols.
71
72 Parameters
73 ----------
74 text : str
75 Text to filter.
76 """
77
78 # First, scan through the markdown looking for $. If
79 # a $ symbol is found, without a preceding \, assume
80 # it is the start of a math block. UNLESS that $ is
81 # not followed by another within two math_lines.
82 math_regions = []
83 math_lines = 0
84 within_math = False
85 math_start_index = 0
86 ptext = ''
87 last_character = ""
88 skip = False
89 for index, char in enumerate(text):
90
91 #Make sure the character isn't preceeded by a backslash
92 if (char == "$" and last_character != "\\"):
93
94 # Close the math region if this is an ending $
95 if within_math:
96 within_math = False
97 skip = True
98 ptext = ptext+'$'+text[math_start_index+1:index].strip()+'$'
99 math_regions.append([math_start_index, index+1])
100 else:
101
102 # Start a new math region
103 within_math = True
104 math_start_index = index
105 math_lines = 0
106
107 # If we are in a math region, count the number of lines parsed.
108 # Cancel the math region if we find two line breaks!
109 elif char == "\n":
110 if within_math:
111 math_lines += 1
112 if math_lines > 1:
113 within_math = False
114 ptext = ptext+text[math_start_index:index]
115
63
116 # Remember the last character so we can easily watch
117 # for backslashes
118 last_character = char
119 if not within_math and not skip:
120 ptext = ptext+char
121 if skip:
122 skip = False
123 return ptext
@@ -15,7 +15,7 b' Module with tests for Latex'
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 from ...tests.base import TestsBase
17 from ...tests.base import TestsBase
18 from ..latex import escape_latex, strip_math_space
18 from ..latex import escape_latex
19
19
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
@@ -43,24 +43,3 b' class TestLatex(TestsBase):'
43 self.assertEqual(escape_latex(test), result)
43 self.assertEqual(escape_latex(test), result)
44
44
45
45
46 def test_strip_math_space(self):
47 """strip_math_space test"""
48 tests = [
49 ('$e$','$e$'),
50 ('$ e $','$e$'),
51 ('xxx$e^i$yyy','xxx$e^i$yyy'),
52 ('xxx$ e^i $yyy','xxx$e^i$yyy'),
53 ('xxx$e^i $yyy','xxx$e^i$yyy'),
54 ('xxx$ e^i$yyy','xxx$e^i$yyy'),
55 ('\$ e $ e $','\$ e $e$'),
56 ('','')]
57
58 for test in tests:
59 self._try_strip_math_space(test[0], test[1])
60
61
62 def _try_strip_math_space(self, test, result):
63 """
64 Try to remove spaces between dollar symbols and contents correctly
65 """
66 self.assertEqual(strip_math_space(test), result)
@@ -14,27 +14,22 b' they are converted.'
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 from __future__ import print_function, absolute_import
16 from __future__ import print_function, absolute_import
17 import os
18
17
19 # Third-party import, needed for Pygments latex definitions.
18 from .base import Preprocessor
20 from pygments.formatters import LatexFormatter
21
22 # ipy imports
23 from .base import (Preprocessor)
24 from IPython.nbconvert import filters
25
19
26 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
27 # Classes
21 # Classes
28 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
29
23
30 class LatexPreprocessor(Preprocessor):
24 class LatexPreprocessor(Preprocessor):
31 """
25 """Preprocessor for latex destined documents.
32 Converter for latex destined documents.
26
27 Mainly populates the `latex` key in the resources dict,
28 adding definitions for pygments highlight styles.
33 """
29 """
34
30
35 def preprocess(self, nb, resources):
31 def preprocess(self, nb, resources):
36 """
32 """Preprocessing to apply on each notebook.
37 Preprocessing to apply on each notebook.
38
33
39 Parameters
34 Parameters
40 ----------
35 ----------
@@ -44,31 +39,9 b' class LatexPreprocessor(Preprocessor):'
44 Additional resources used in the conversion process. Allows
39 Additional resources used in the conversion process. Allows
45 preprocessors to pass variables into the Jinja engine.
40 preprocessors to pass variables into the Jinja engine.
46 """
41 """
47 # Generate Pygments definitions for Latex
42 # Generate Pygments definitions for Latex
48 resources["latex"] = {}
43 from pygments.formatters import LatexFormatter
49 resources["latex"]["pygments_definitions"] = LatexFormatter().get_style_defs()
50 return super(LatexPreprocessor, self).preprocess(nb, resources)
51
52
53 def preprocess_cell(self, cell, resources, index):
54 """
55 Apply a transformation on each cell,
56
57 Parameters
58 ----------
59 cell : NotebookNode cell
60 Notebook cell being processed
61 resources : dictionary
62 Additional resources used in the conversion process. Allows
63 preprocessors to pass variables into the Jinja engine.
64 index : int
65 Modified index of the cell being processed (see base.py)
66 """
67
44
68 #If the cell is a markdown cell, preprocess the ampersands used to
45 resources.setdefault("latex", {})
69 #remove the space between them and their contents. Latex will complain
46 resources["latex"].setdefault("pygments_definitions", LatexFormatter().get_style_defs())
70 #if spaces exist between the ampersands and the math content.
47 return nb, resources
71 #See filters.latex.rm_math_space for more information.
72 if hasattr(cell, "source") and cell.cell_type == "markdown":
73 cell.source = filters.strip_math_space(cell.source)
74 return cell, resources
@@ -47,5 +47,5 b' class TestLatex(PreprocessorTestsBase):'
47 # Make sure the code cell wasn't modified.
47 # Make sure the code cell wasn't modified.
48 self.assertEqual(nb.worksheets[0].cells[0].input, '$ e $')
48 self.assertEqual(nb.worksheets[0].cells[0].input, '$ e $')
49
49
50 # Verify that the markdown cell was processed.
50 # Verify that the markdown cell wasn't processed.
51 self.assertEqual(nb.worksheets[0].cells[1].source, '$e$')
51 self.assertEqual(nb.worksheets[0].cells[1].source, '$ e $')
@@ -55,13 +55,13 b' In [{{ cell.prompt_number }}]:'
55
55
56 {% block markdowncell scoped %}
56 {% block markdowncell scoped %}
57 <div class="text_cell_render border-box-sizing rendered_html">
57 <div class="text_cell_render border-box-sizing rendered_html">
58 {{ cell.source | strip_math_space | markdown2html | strip_files_prefix }}
58 {{ cell.source | markdown2html | strip_files_prefix }}
59 </div>
59 </div>
60 {%- endblock markdowncell %}
60 {%- endblock markdowncell %}
61
61
62 {% block headingcell scoped %}
62 {% block headingcell scoped %}
63 <div class="text_cell_render border-box-sizing rendered_html">
63 <div class="text_cell_render border-box-sizing rendered_html">
64 {{ ("#" * cell.level + cell.source) | replace('\n', ' ') | strip_math_space | markdown2html | strip_files_prefix | add_anchor }}
64 {{ ("#" * cell.level + cell.source) | replace('\n', ' ') | markdown2html | strip_files_prefix | add_anchor }}
65 </div>
65 </div>
66 {% endblock headingcell %}
66 {% endblock headingcell %}
67
67
General Comments 0
You need to be logged in to leave comments. Login now