Show More
@@ -1,76 +1,75 | |||||
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]+"\n" | |
67 | print 'catching up with --',text[math_start_index:index],'--' |
|
|||
68 |
|
67 | |||
69 | # Remember the last character so we can easily watch |
|
68 | # Remember the last character so we can easily watch | |
70 | # for backslashes |
|
69 | # for backslashes | |
71 | last_character = char |
|
70 | last_character = char | |
72 | if not within_math and not skip: |
|
71 | if not within_math and not skip: | |
73 | ptext = ptext+char |
|
72 | ptext = ptext+char | |
74 | if skip: |
|
73 | if skip: | |
75 | skip = False |
|
74 | skip = False | |
76 | return ptext |
|
75 | return ptext |
General Comments 0
You need to be logged in to leave comments.
Login now