##// END OF EJS Templates
Added spaces between comments.
Jonathan Frederic -
Show More
@@ -1,92 +1,97 b''
1 1 """Latex transformer.
2 2
3 3 Module that allows latex output notebooks to be conditioned before
4 4 they are converted.
5 5 """
6 6 #-----------------------------------------------------------------------------
7 7 # Copyright (c) 2013, the IPython Development Team.
8 8 #
9 9 # Distributed under the terms of the Modified BSD License.
10 10 #
11 11 # The full license is in the file COPYING.txt, distributed with this software.
12 12 #-----------------------------------------------------------------------------
13 13
14 14 #-----------------------------------------------------------------------------
15 15 # Imports
16 16 #-----------------------------------------------------------------------------
17 17 from __future__ import print_function, absolute_import
18 18
19 19 # Our own imports
20 20 # Needed to override transformer
21 21 from .transformers import (ActivatableTransformer) #TODO
22 22
23 23 #-----------------------------------------------------------------------------
24 24 # Classes
25 25 #-----------------------------------------------------------------------------
26 26 class LatexTransformer(ActivatableTransformer):
27 27 """
28 28 Converter for latex destined documents.
29 29 """
30 30
31 31 def cell_transform(self, cell, other, index):
32 32 """
33 33 Apply a transformation on each cell,
34 34
35 35 receive the current cell, the resource dict and the index of current cell as parameter.
36 36
37 37 Returns modified cell and resource dict.
38 38 """
39
39 40 if hasattr(cell, "source") and cell.cell_type == "markdown":
40 41 cell.source = rm_math_space(cell.source)
41 42 return cell, other
42 43
43 44 #-----------------------------------------------------------------------------
44 45 # Functions
45 46 #-----------------------------------------------------------------------------
46 47 def rm_math_space(text):
47 48 """
48 49 Remove the space between latex math commands and enclosing $ symbols.
49 50 """
50 51
51 52 # First, scan through the markdown looking for $. If
52 53 # a $ symbol is found, without a preceding \, assume
53 54 # it is the start of a math block. UNLESS that $ is
54 55 # not followed by another within two math_lines.
55 56 math_regions = []
56 57 math_lines = 0
57 58 within_math = False
58 59 math_start_index = 0
59 60 ptext = ''
60 61 last_character = ""
61 62 skip = False
62 63 for index, char in enumerate(text):
64
63 65 #Make sure the character isn't preceeded by a backslash
64 66 if (char == "$" and last_character != "\\"):
67
65 68 # Close the math region if this is an ending $
66 69 if within_math:
67 70 within_math = False
68 71 skip = True
69 72 ptext = ptext+'$'+text[math_start_index+1:index].strip()+'$'
70 73 math_regions.append([math_start_index, index+1])
71 74 else:
75
72 76 # Start a new math region
73 77 within_math = True
74 78 math_start_index = index
75 79 math_lines = 0
80
76 81 # If we are in a math region, count the number of lines parsed.
77 82 # Cancel the math region if we find two line breaks!
78 83 elif char == "\n":
79 84 if within_math:
80 85 math_lines += 1
81 86 if math_lines > 1:
82 87 within_math = False
83 88 ptext = ptext+text[math_start_index:index]
84 89
85 90 # Remember the last character so we can easily watch
86 91 # for backslashes
87 92 last_character = char
88 93 if not within_math and not skip:
89 94 ptext = ptext+char
90 95 if skip:
91 96 skip = False
92 97 return ptext
General Comments 0
You need to be logged in to leave comments. Login now