Show More
@@ -1,76 +1,75 | |||
|
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 | 8 | |
|
9 | 9 | # Needed to override transformer |
|
10 | 10 | from .transformers import (ActivatableTransformer) |
|
11 | 11 | |
|
12 | 12 | class LatexTransformer(ActivatableTransformer): |
|
13 | 13 | """ |
|
14 | 14 | Converter for latex destined documents. |
|
15 | 15 | """ |
|
16 | 16 | |
|
17 | 17 | def cell_transform(self, cell, other, index): |
|
18 | 18 | """ |
|
19 | 19 | Apply a transformation on each cell, |
|
20 | 20 | |
|
21 | 21 | receive the current cell, the resource dict and the index of current cell as parameter. |
|
22 | 22 | |
|
23 | 23 | Returns modified cell and resource dict. |
|
24 | 24 | """ |
|
25 | 25 | if hasattr(cell, "source") and cell.cell_type == "markdown": |
|
26 | 26 | cell.source = remove_math_space(cell.source) |
|
27 | 27 | return cell, other |
|
28 | 28 | |
|
29 | 29 | def remove_math_space(text): |
|
30 | 30 | """ |
|
31 | 31 | Remove the space between latex math commands and enclosing $ symbols. |
|
32 | 32 | """ |
|
33 | 33 | |
|
34 | 34 | # First, scan through the markdown looking for $. If |
|
35 | 35 | # a $ symbol is found, without a preceding \, assume |
|
36 | 36 | # it is the start of a math block. UNLESS that $ is |
|
37 | 37 | # not followed by another within two math_lines. |
|
38 | 38 | math_regions = [] |
|
39 | 39 | math_lines = 0 |
|
40 | 40 | within_math = False |
|
41 | 41 | math_start_index = 0 |
|
42 | 42 | ptext = '' |
|
43 | 43 | last_character = "" |
|
44 | 44 | skip = False |
|
45 | 45 | for index, char in enumerate(text): |
|
46 | 46 | #Make sure the character isn't preceeded by a backslash |
|
47 | 47 | if (char == "$" and last_character != "\\"): |
|
48 | 48 | # Close the math region if this is an ending $ |
|
49 | 49 | if within_math: |
|
50 | 50 | within_math = False |
|
51 | 51 | skip = True |
|
52 | 52 | ptext = ptext+'$'+text[math_start_index+1:index].strip()+'$' |
|
53 | 53 | math_regions.append([math_start_index, index+1]) |
|
54 | 54 | else: |
|
55 | 55 | # Start a new math region |
|
56 | 56 | within_math = True |
|
57 | 57 | math_start_index = index |
|
58 | 58 | math_lines = 0 |
|
59 | 59 | # If we are in a math region, count the number of lines parsed. |
|
60 | 60 | # Cancel the math region if we find two line breaks! |
|
61 | 61 | elif char == "\n": |
|
62 | 62 | if within_math: |
|
63 | 63 | math_lines += 1 |
|
64 | 64 | if math_lines > 1: |
|
65 | 65 | within_math = False |
|
66 | 66 | ptext = ptext+text[math_start_index:index]+"\n" |
|
67 | print 'catching up with --',text[math_start_index:index],'--' | |
|
68 | 67 | |
|
69 | 68 | # Remember the last character so we can easily watch |
|
70 | 69 | # for backslashes |
|
71 | 70 | last_character = char |
|
72 | 71 | if not within_math and not skip: |
|
73 | 72 | ptext = ptext+char |
|
74 | 73 | if skip: |
|
75 | 74 | skip = False |
|
76 | 75 | return ptext |
General Comments 0
You need to be logged in to leave comments.
Login now