##// END OF EJS Templates
fix latex_transformer and add test
Matthias BUSSONNIER -
Show More
@@ -1,75 +1,75 b''
1 """
1 """
2 Module that allows latex output notebooks to be conditioned before
2 Module that allows latex output notebooks to be conditioned before
3 they are converted.
3 they are converted.
4 """
4 """
5 from __future__ import absolute_import
5 from __future__ import absolute_import
6
6
7 # Configurable traitlets
7 # Configurable traitlets
8
8
9 # Needed to override transformer
9 # Needed to override transformer
10 from .transformers import (ActivatableTransformer)
10 from .transformers import (ActivatableTransformer)
11
11
12 class LatexTransformer(ActivatableTransformer):
12 class LatexTransformer(ActivatableTransformer):
13 """
13 """
14 Converter for latex destined documents.
14 Converter for latex destined documents.
15 """
15 """
16
16
17 def cell_transform(self, cell, other, index):
17 def cell_transform(self, cell, other, index):
18 """
18 """
19 Apply a transformation on each cell,
19 Apply a transformation on each cell,
20
20
21 receive the current cell, the resource dict and the index of current cell as parameter.
21 receive the current cell, the resource dict and the index of current cell as parameter.
22
22
23 Returns modified cell and resource dict.
23 Returns modified cell and resource dict.
24 """
24 """
25 if hasattr(cell, "source") and cell.cell_type == "markdown":
25 if hasattr(cell, "source") and cell.cell_type == "markdown":
26 cell.source = remove_math_space(cell.source)
26 cell.source = remove_math_space(cell.source)
27 return cell, other
27 return cell, other
28
28
29 def remove_math_space(text):
29 def remove_math_space(text):
30 """
30 """
31 Remove the space between latex math commands and enclosing $ symbols.
31 Remove the space between latex math commands and enclosing $ symbols.
32 """
32 """
33
33
34 # First, scan through the markdown looking for $. If
34 # First, scan through the markdown looking for $. If
35 # a $ symbol is found, without a preceding \, assume
35 # a $ symbol is found, without a preceding \, assume
36 # it is the start of a math block. UNLESS that $ is
36 # it is the start of a math block. UNLESS that $ is
37 # not followed by another within two math_lines.
37 # not followed by another within two math_lines.
38 math_regions = []
38 math_regions = []
39 math_lines = 0
39 math_lines = 0
40 within_math = False
40 within_math = False
41 math_start_index = 0
41 math_start_index = 0
42 ptext = ''
42 ptext = ''
43 last_character = ""
43 last_character = ""
44 skip = False
44 skip = False
45 for index, char in enumerate(text):
45 for index, char in enumerate(text):
46 #Make sure the character isn't preceeded by a backslash
46 #Make sure the character isn't preceeded by a backslash
47 if (char == "$" and last_character != "\\"):
47 if (char == "$" and last_character != "\\"):
48 # Close the math region if this is an ending $
48 # Close the math region if this is an ending $
49 if within_math:
49 if within_math:
50 within_math = False
50 within_math = False
51 skip = True
51 skip = True
52 ptext = ptext+'$'+text[math_start_index+1:index].strip()+'$'
52 ptext = ptext+'$'+text[math_start_index+1:index].strip()+'$'
53 math_regions.append([math_start_index, index+1])
53 math_regions.append([math_start_index, index+1])
54 else:
54 else:
55 # Start a new math region
55 # Start a new math region
56 within_math = True
56 within_math = True
57 math_start_index = index
57 math_start_index = index
58 math_lines = 0
58 math_lines = 0
59 # If we are in a math region, count the number of lines parsed.
59 # If we are in a math region, count the number of lines parsed.
60 # Cancel the math region if we find two line breaks!
60 # Cancel the math region if we find two line breaks!
61 elif char == "\n":
61 elif char == "\n":
62 if within_math:
62 if within_math:
63 math_lines += 1
63 math_lines += 1
64 if math_lines > 1:
64 if math_lines > 1:
65 within_math = False
65 within_math = False
66 ptext = ptext+text[math_start_index:index]+"\n"
66 ptext = ptext+text[math_start_index:index]
67
67
68 # Remember the last character so we can easily watch
68 # Remember the last character so we can easily watch
69 # for backslashes
69 # for backslashes
70 last_character = char
70 last_character = char
71 if not within_math and not skip:
71 if not within_math and not skip:
72 ptext = ptext+char
72 ptext = ptext+char
73 if skip:
73 if skip:
74 skip = False
74 skip = False
75 return ptext
75 return ptext
@@ -1,25 +1,38 b''
1 import io
1 import io
2 import nose.tools as nt
2 import nose.tools as nt
3 from nose.tools import nottest
3 from nose.tools import nottest
4
4
5 from converters.latex_transformer import remove_math_space
5 from converters.latex_transformer import remove_math_space
6
6
7 @nottest
7 @nottest
8 def test_space(input, reference):
8 def test_space(input, reference):
9 nt.assert_equal(remove_math_space(input),reference)
9 nt.assert_equal(remove_math_space(input),reference)
10
10
11
11
12
12
13 def test_evens():
13 def test_evens():
14 unchanged = [
15 """
16 you should be able to type
17
18 $ a single dollar and go to the line
19
20 it shouldn't be transformed.
21 """
22 ]
14 references = [
23 references = [
15 ('$e$','$e$'),
24 ('$e$','$e$'),
16 ('$ e $','$e$'),
25 ('$ e $','$e$'),
17 ('xxx$e^i$yyy','xxx$e^i$yyy'),
26 ('xxx$e^i$yyy','xxx$e^i$yyy'),
18 ('xxx$ e^i $yyy','xxx$e^i$yyy'),
27 ('xxx$ e^i $yyy','xxx$e^i$yyy'),
19 ('xxx$e^i $yyy','xxx$e^i$yyy'),
28 ('xxx$e^i $yyy','xxx$e^i$yyy'),
20 ('xxx$ e^i$yyy','xxx$e^i$yyy'),
29 ('xxx$ e^i$yyy','xxx$e^i$yyy'),
21 ('\$ e $ e $','\$ e $e$'),
30 ('\$ e $ e $','\$ e $e$'),
31 ('',''),
22 ]
32 ]
23
33
24 for k,v in references :
34 for k,v in references :
25 yield test_space, k,v
35 yield test_space, k,v
36
37 for unch in unchanged :
38 yield test_space, unch, unch
General Comments 0
You need to be logged in to leave comments. Login now