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