##// END OF EJS Templates
fix remove math space
Matthias BUSSONNIER -
Show More
@@ -1,11 +1,10 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 from IPython.utils.traitlets import Unicode, Bool
9
8
10 # Needed to override transformer
9 # Needed to override transformer
11 from .transformers import (ActivatableTransformer)
10 from .transformers import (ActivatableTransformer)
@@ -13,7 +12,7 b' from .transformers import (ActivatableTransformer)'
13 class LatexTransformer(ActivatableTransformer):
12 class LatexTransformer(ActivatableTransformer):
14 """
13 """
15 Converter for latex destined documents.
14 Converter for latex destined documents.
16 """
15 """
17
16
18 def cell_transform(self, cell, other, index):
17 def cell_transform(self, cell, other, index):
19 """
18 """
@@ -24,105 +23,51 b' class LatexTransformer(ActivatableTransformer):'
24 Returns modified cell and resource dict.
23 Returns modified cell and resource dict.
25 """
24 """
26 if hasattr(cell, "source") and cell.cell_type == "markdown":
25 if hasattr(cell, "source") and cell.cell_type == "markdown":
27 cell.source = self.remove_math_space(cell.source)
26 cell.source = remove_math_space(cell.source)
28 return cell, other
27 return cell, other
29
28
30 def remove_math_space(self, text):
29 def remove_math_space(text):
31 """
30 """
32 Remove the space between latex math commands and enclosing $ symbols.
31 Remove the space between latex math commands and enclosing $ symbols.
33 """
32 """
34
33
35 # First, scan through the markdown looking for $. If
34 # First, scan through the markdown looking for $. If
36 # a $ symbol is found, without a preceding \, assume
35 # a $ symbol is found, without a preceding \, assume
37 # it is the start of a math block. UNLESS that $ is
36 # it is the start of a math block. UNLESS that $ is
38 # not followed by another within two math_lines.
37 # not followed by another within two math_lines.
39 math_regions = []
38 math_regions = []
40 math_lines = 0
39 math_lines = 0
41 within_math = False
40 within_math = False
42 math_start_index = 0
41 math_start_index = 0
43 index = 0
42 ptext = ''
44 last_character = ""
43 last_character = ""
45 for char in text: #Loop through each character in the text.
44 skip = False
46
45 for index, char in enumerate(text):
47 #Make sure the character isn't preceeded by a backslash
46 #Make sure the character isn't preceeded by a backslash
48 if (char == "$" and last_character != "\\"):
47 if (char == "$" and last_character != "\\"):
49
48 # Close the math region if this is an ending $
50 # Close the math region if this is an ending $
49 if within_math:
51 if within_math:
50 within_math = False
52 within_math = False
51 skip = True
53 math_regions.append([math_start_index, index+1])
52 ptext = ptext+'$'+text[math_start_index+1:index].strip()+'$'
54 else:
53 math_regions.append([math_start_index, index+1])
55
56 # Start a new math region
57 within_math = True
58 math_start_index = index
59 math_lines = 0
60
61 # If we are in a math region, count the number of lines parsed.
62 # Cancel the math region if we find two line breaks!
63 elif char == "\n":
64 if within_math:
65 math_lines += 1
66 if math_lines > 1:
67 within_math = False
68
69 # Remember the last character so we can easily watch
70 # for backslashes
71 last_character = char
72
73 # Next index
74 index += 1
75
76 # Reset the index and last char
77 index = 0
78
79 # Now that we know what regions of the text are math and
80 # what regions aren't, we can separate them into "blocks"
81 text_blocks=[]
82 math_blocks=[]
83 was_math_block = False
84 current_block = ""
85 for char in text:
86
87 # Check if this is a math region.
88 ismath = False
89 for keypair in math_regions:
90 if (keypair[0] <= index and index <= keypair[1]):
91 ismath = True
92
93 # If the region type has changed since the last
94 # iteration, commit all read characters to that
95 # region type and reset the buffer.
96 if (ismath and not was_math_block):
97 was_math_block = True
98 text_blocks.append(current_block)
99 current_block=""
100 elif ((not ismath) and was_math_block):
101 was_math_block = False
102 math_blocks.append(current_block)
103 current_block=""
104
105 # Store the character
106 current_block += char
107
108 # Next index
109 index += 1
110
111 # Save whatever remains in the buffer that hasn't yet been saved.
112 if was_math_block:
113 math_blocks.append(current_block)
114 else:
115 text_blocks.append(current_block)
116
117 # Recombine the regions, while processing every math region, removing
118 # the spaces between the math and the $ symbols.
119 output = ""
120 for index in range(0,len(text_blocks) + len(math_blocks)):
121 if index % 2 == 0:
122 output += text_blocks[index/2]
123 else:
54 else:
124 mathblock = math_blocks[(index -1)/2]
55 # Start a new math region
125 mathblock = mathblock[1:len(mathblock)-2]
56 within_math = True
126 output += "$" + mathblock.strip() + "$"
57 math_start_index = index
127 return output
58 math_lines = 0
128
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!
61 elif char == "\n":
62 if within_math:
63 math_lines += 1
64 if math_lines > 1:
65 within_math = False
66 # Remember the last character so we can easily watch
67 # for backslashes
68 last_character = char
69 if not within_math and not skip:
70 ptext = ptext+char
71 if skip:
72 skip = False
73 return ptext
@@ -2,13 +2,11 b' 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 import latex_transformer
5 from converters.latex_transformer import remove_math_space
6 lt = latex_transformer.LatexTransformer()
7 lt.enabled = True
8
6
9 @nottest
7 @nottest
10 def test_space(input, reference):
8 def test_space(input, reference):
11 nt.assert_equal(lt.remove_math_space(input),reference)
9 nt.assert_equal(remove_math_space(input),reference)
12
10
13
11
14
12
General Comments 0
You need to be logged in to leave comments. Login now