##// END OF EJS Templates
fix write binary and non math $...
Matthias BUSSONNIER -
Show More
@@ -1,73 +1,76 b''
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 ptext = ptext+text[math_start_index:index]+"\n"
67 print 'catching up with --',text[math_start_index:index],'--'
68
66 69 # Remember the last character so we can easily watch
67 70 # for backslashes
68 71 last_character = char
69 72 if not within_math and not skip:
70 73 ptext = ptext+char
71 74 if skip:
72 75 skip = False
73 76 return ptext
@@ -1,155 +1,155 b''
1 1 #!/usr/bin/env python
2 2 """
3 3 ================================================================================
4 4
5 5 |,---. | | , .| |
6 6 ||---', .|--- |---.,---.,---. |\ ||---.,---.,---.,---.. ,,---.,---.|---
7 7 || | || | || || | | \ || || | || | \ / |---'| |
8 8 `` `---|`---'` '`---'` ' ` `'`---'`---'`---'` ' `' `---'` `---'
9 9 `---'
10 10 ================================================================================
11 11
12 12 Highly experimental for now
13 13
14 14 """
15 15 #-----------------------------------------------------------------------------
16 16 # Imports
17 17 #-----------------------------------------------------------------------------
18 18 from __future__ import print_function
19 19 import sys
20 20 import io
21 21 import os
22 22
23 23 from converters.template import ConverterTemplate
24 24 # From IPython
25 25
26 26 # All the stuff needed for the configurable things
27 27 from IPython.config.application import Application
28 28 from IPython.config.loader import ConfigFileNotFound
29 29 from IPython.utils.traitlets import Unicode, Bool
30 30
31 31 from converters.transformers import (ExtractFigureTransformer)
32 32
33 33 from converters.config import GlobalConfigurable
34 34
35 35
36 36 class NbconvertApp(Application):
37 37 """A basic application to convert ipynb files
38 38
39 39 """
40 40
41 41 stdout = Bool(True, config=True,
42 42 help="""
43 43 Wether to print the converted ipynb file to stdout
44 44 use full do diff files without actually writing a new file
45 45 """)
46 46
47 47 write = Bool(False, config=True,
48 48 help="""Shoudl the converted notebook file be written to disk
49 49 along with potential extracted resources.
50 50 """
51 51 )
52 52
53 53 fileext = Unicode('txt', config=True,
54 54 help="""Extension of the file that should be written to disk"""
55 55 )
56 56
57 57 aliases = {
58 58 'stdout':'NbconvertApp.stdout',
59 59 'write':'NbconvertApp.write',
60 60 }
61 61
62 62 flags = {}
63 63 flags['no-stdout'] = (
64 64 {'NbconvertApp' : {'stdout' : False}},
65 65 """Do not print converted file to stdout, equivalent to --stdout=False
66 66 """
67 67 )
68 68
69 69 def __init__(self, **kwargs):
70 70 super(NbconvertApp, self).__init__(**kwargs)
71 71 self.classes.insert(0, ConverterTemplate)
72 72 # register class here to have help with help all
73 73 self.classes.insert(0, ExtractFigureTransformer)
74 74 self.classes.insert(0, GlobalConfigurable)
75 75
76 76 def load_config_file(self, profile_name):
77 77 """load a config file from the config file dir
78 78
79 79 profile_name : {string} name of the profile file to load without file extension.
80 80 """
81 81 try:
82 82 Application.load_config_file(
83 83 self,
84 84 profile_name+'.nbcv',
85 85 path=[os.path.join(os.getcwdu(), 'profile')]
86 86 )
87 87 return True
88 88 except ConfigFileNotFound:
89 89 self.log.warn("Config file for profile '%s' not found, giving up ", profile_name)
90 90 return False
91 91
92 92
93 93 def initialize(self, argv=None):
94 94 """parse command line and load config"""
95 95 self.parse_command_line(argv)
96 96 cl_config = self.config
97 97 profile_file = argv[1]
98 98 if not self.load_config_file(profile_file):
99 99 exit(1)
100 100 self.update_config(cl_config)
101 101
102 102
103 103
104 104 def run(self):
105 105 """Convert a notebook in one step"""
106 106 ipynb_file = (self.extra_args or [None])[2]
107 107
108 108 # If you are writting a custom transformer, append it to the dictionary
109 109 # below.
110 110 userpreprocessors = {}
111 111
112 112 # Create the converter
113 113 C = ConverterTemplate(config=self.config, preprocessors=userpreprocessors)
114 114
115 115 output, resources = C.from_filename(ipynb_file)
116 116 if self.stdout :
117 117 print(output.encode('utf-8'))
118 118
119 119 out_root = ipynb_file[:-6].replace('.', '_').replace(' ', '_')
120 120
121 121 keys = resources.get('figures', {}).keys()
122 122 if self.write :
123 123 with io.open(os.path.join(out_root+'.'+self.fileext), 'w') as f:
124 124 f.write(output)
125 125 if keys :
126 126 if self.write:
127 127 files_dir = out_root+'_files'
128 128 if not os.path.exists(out_root+'_files'):
129 129 os.mkdir(files_dir)
130 130 for key in keys:
131 with io.open(os.path.join(files_dir, key), 'wb') as f:
131 with io.open(os.path.join(files_dir, key), 'w') as f:
132 132 f.write(resources['figures'][key])
133 133 elif self.stdout:
134 134 print('''====================== Keys in Resources ==================================''')
135 135 print(resources['figures'].keys())
136 136 print("""
137 137 ===========================================================================
138 138 you are responsible from writing those data do a file in the right place if
139 139 they need to be.
140 140 ===========================================================================
141 141 """)
142 142
143 143 def main():
144 144 """Convert a notebook to html in one step"""
145 145 app = NbconvertApp.instance()
146 146 app.description = __doc__
147 147 app.initialize(argv=sys.argv)
148 148 app.start()
149 149 app.run()
150 150 #-----------------------------------------------------------------------------
151 151 # Script main
152 152 #-----------------------------------------------------------------------------
153 153
154 154 if __name__ == '__main__':
155 155 main()
General Comments 0
You need to be logged in to leave comments. Login now