Show More
@@ -1,128 +1,73 | |||
|
1 | 1 | """ |
|
2 | 2 |
Module that allows latex output notebooks to be conditioned before |
|
3 | 3 | they are converted. |
|
4 | 4 | """ |
|
5 | 5 | from __future__ import absolute_import |
|
6 | 6 | |
|
7 | 7 | # Configurable traitlets |
|
8 | from IPython.utils.traitlets import Unicode, Bool | |
|
9 | 8 | |
|
10 | 9 | # Needed to override transformer |
|
11 | 10 | from .transformers import (ActivatableTransformer) |
|
12 | 11 | |
|
13 | 12 | class LatexTransformer(ActivatableTransformer): |
|
14 | 13 | """ |
|
15 | 14 | Converter for latex destined documents. |
|
16 | 15 |
""" |
|
17 | 16 | |
|
18 | 17 | def cell_transform(self, cell, other, index): |
|
19 | 18 | """ |
|
20 | 19 | Apply a transformation on each cell, |
|
21 | 20 | |
|
22 | 21 | receive the current cell, the resource dict and the index of current cell as parameter. |
|
23 | 22 | |
|
24 | 23 | Returns modified cell and resource dict. |
|
25 | 24 | """ |
|
26 | 25 | if hasattr(cell, "source") and cell.cell_type == "markdown": |
|
27 |
cell.source = |
|
|
26 | cell.source = remove_math_space(cell.source) | |
|
28 | 27 | return cell, other |
|
29 | 28 | |
|
30 |
|
|
|
29 | def remove_math_space(text): | |
|
31 | 30 |
|
|
32 | 31 |
|
|
33 | 32 |
|
|
34 | 33 | |
|
35 | 34 |
|
|
36 | 35 |
|
|
37 | 36 |
|
|
38 | 37 |
|
|
39 | 38 |
|
|
40 | 39 |
|
|
41 | 40 |
|
|
42 | 41 |
|
|
43 | index = 0 | |
|
42 | ptext = '' | |
|
44 | 43 |
|
|
45 | for char in text: #Loop through each character in the text. | |
|
46 | ||
|
44 | skip = False | |
|
45 | for index, char in enumerate(text): | |
|
47 | 46 |
|
|
48 | 47 |
|
|
49 | ||
|
50 | 48 |
|
|
51 | 49 |
|
|
52 | 50 |
|
|
51 | skip = True | |
|
52 | ptext = ptext+'$'+text[math_start_index+1:index].strip()+'$' | |
|
53 | 53 |
|
|
54 | 54 |
|
|
55 | ||
|
56 | 55 |
|
|
57 | 56 |
|
|
58 | 57 |
|
|
59 | 58 |
|
|
60 | ||
|
61 | 59 |
|
|
62 | 60 |
|
|
63 | 61 |
|
|
64 | 62 |
|
|
65 | 63 |
|
|
66 | 64 |
|
|
67 | 65 |
|
|
68 | ||
|
69 | 66 |
|
|
70 | 67 |
|
|
71 | 68 |
|
|
72 | ||
|
73 |
|
|
|
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: | |
|
124 | mathblock = math_blocks[(index -1)/2] | |
|
125 | mathblock = mathblock[1:len(mathblock)-2] | |
|
126 | output += "$" + mathblock.strip() + "$" | |
|
127 | return output | |
|
128 | ||
|
69 | if not within_math and not skip: | |
|
70 | ptext = ptext+char | |
|
71 | if skip: | |
|
72 | skip = False | |
|
73 | return ptext |
@@ -1,27 +1,25 | |||
|
1 | 1 | import io |
|
2 | 2 | import nose.tools as nt |
|
3 | 3 | from nose.tools import nottest |
|
4 | 4 | |
|
5 |
from converters import |
|
|
6 | lt = latex_transformer.LatexTransformer() | |
|
7 | lt.enabled = True | |
|
5 | from converters.latex_transformer import remove_math_space | |
|
8 | 6 | |
|
9 | 7 | @nottest |
|
10 | 8 | def test_space(input, reference): |
|
11 |
nt.assert_equal( |
|
|
9 | nt.assert_equal(remove_math_space(input),reference) | |
|
12 | 10 | |
|
13 | 11 | |
|
14 | 12 | |
|
15 | 13 | def test_evens(): |
|
16 | 14 | references = [ |
|
17 | 15 | ('$e$','$e$'), |
|
18 | 16 | ('$ e $','$e$'), |
|
19 | 17 | ('xxx$e^i$yyy','xxx$e^i$yyy'), |
|
20 | 18 | ('xxx$ e^i $yyy','xxx$e^i$yyy'), |
|
21 | 19 | ('xxx$e^i $yyy','xxx$e^i$yyy'), |
|
22 | 20 | ('xxx$ e^i$yyy','xxx$e^i$yyy'), |
|
23 | 21 | ('\$ e $ e $','\$ e $e$'), |
|
24 | 22 | ] |
|
25 | 23 | |
|
26 | 24 | for k,v in references : |
|
27 | 25 | yield test_space, k,v |
General Comments 0
You need to be logged in to leave comments.
Login now