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