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