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