Show More
@@ -86,7 +86,7 b' class LatexExporter(Exporter):' | |||||
86 | def default_config(self): |
|
86 | def default_config(self): | |
87 | c = Config({ |
|
87 | c = Config({ | |
88 | 'NbConvertBase': { |
|
88 | 'NbConvertBase': { | |
89 | 'display_data_priority' : ['latex', 'png', 'jpg', 'svg', 'jpeg', 'text'] |
|
89 | 'display_data_priority' : ['latex', 'pdf', 'png', 'jpg', 'svg', 'jpeg', 'text'] | |
90 | }, |
|
90 | }, | |
91 | 'ExtractFigureTransformer': { |
|
91 | 'ExtractFigureTransformer': { | |
92 | 'enabled':True |
|
92 | 'enabled':True |
@@ -60,6 +60,13 b' it introduces a new line' | |||||
60 | \end{center} |
|
60 | \end{center} | |
61 | ((*- endblock -*)) |
|
61 | ((*- endblock -*)) | |
62 |
|
62 | |||
|
63 | ((*- block data_pdf -*)) | |||
|
64 | \begin{center} | |||
|
65 | \includegraphics[width=0.7\textwidth]{(((output.pdf_filename[:-4])))} | |||
|
66 | \par | |||
|
67 | \end{center} | |||
|
68 | ((*- endblock -*)) | |||
|
69 | ||||
63 | ((* block pyout *)) |
|
70 | ((* block pyout *)) | |
64 | ((* block data_priority scoped *))((( super() )))((* endblock *)) |
|
71 | ((* block data_priority scoped *))((( super() )))((* endblock *)) | |
65 | ((* endblock pyout *)) |
|
72 | ((* endblock pyout *)) |
@@ -358,6 +358,10 b' Note: For best display, use latex syntax highlighting. =))' | |||||
358 | ((( conditionally_center_output(insert_graphics(output.svg_filename)) ))) |
|
358 | ((( conditionally_center_output(insert_graphics(output.svg_filename)) ))) | |
359 | ((*- endblock -*)) |
|
359 | ((*- endblock -*)) | |
360 |
|
360 | |||
|
361 | ((*- block data_pdf -*)) | |||
|
362 | ((( conditionally_center_output(insert_graphics(output.pdf_filename[:-4])) ))) | |||
|
363 | ((*- endblock -*)) | |||
|
364 | ||||
361 | ((*- block data_latex *)) |
|
365 | ((*- block data_latex *)) | |
362 | ((* if resources.sphinx.centeroutput *))\begin{center}((* endif -*))((( output.latex | rm_math_space )))((*- if resources.sphinx.centeroutput *))\end{center} ((* endif -*)) |
|
366 | ((* if resources.sphinx.centeroutput *))\begin{center}((* endif -*))((( output.latex | rm_math_space )))((*- if resources.sphinx.centeroutput *))\end{center} ((* endif -*)) | |
363 | ((*- endblock -*)) |
|
367 | ((*- endblock -*)) |
@@ -49,31 +49,16 b' class ConvertFiguresTransformer(Transformer):' | |||||
49 | #Loop through all of the datatypes of the outputs in the cell. |
|
49 | #Loop through all of the datatypes of the outputs in the cell. | |
50 | for index, cell_out in enumerate(cell.get('outputs', [])): |
|
50 | for index, cell_out in enumerate(cell.get('outputs', [])): | |
51 | for data_type, data in cell_out.items(): |
|
51 | for data_type, data in cell_out.items(): | |
52 |
|
52 | # this must run *before* extract figures, | ||
53 | #Get the name of the file exported by the extract figure |
|
53 | # so figure_name and filename do not exist | |
54 | #transformer. Do not try to convert the figure if the extract |
|
54 | self._convert_figure(cell_out, resources, data_type, data) | |
55 | #fig transformer hasn't touched it |
|
|||
56 | filename = cell_out.get(data_type + '_filename', None) |
|
|||
57 | if filename: |
|
|||
58 | figure_name = filename[:filename.rfind('.')] |
|
|||
59 | self._convert_figure(cell_out, figure_name, resources, data_type, data) |
|
|||
60 | return cell, resources |
|
55 | return cell, resources | |
61 |
|
56 | |||
62 |
|
57 | |||
63 |
def _convert_figure(self, cell_out, resources, |
|
58 | def _convert_figure(self, cell_out, resources, data_type, data): | |
64 | """ |
|
59 | """ | |
65 | Convert a figure and output the results to the cell output |
|
60 | Convert a figure and output the results to the cell output | |
66 | """ |
|
61 | """ | |
67 |
|
62 | if not self.to_format in cell_out and data_type == self.from_format: | ||
68 | if not self.to_format in cell_out: |
|
63 | data = self.convert_figure(data_type, data) | |
69 | if data_type == self.from_format: |
|
64 | cell_out[self.to_format] = data | |
70 | filename = figure_name + '.' + self.to_format |
|
|||
71 | if filename not in resources['figures']: |
|
|||
72 |
|
||||
73 | #On the cell, make the figure available via |
|
|||
74 | # cell.outputs[i].pdf_filename ... etc (PDF in example) |
|
|||
75 | cell_out[self.to_format + '_filename'] = filename |
|
|||
76 |
|
||||
77 | #In the resources, make the figure available via |
|
|||
78 | # resources['figures']['filename'] = data |
|
|||
79 | resources['figures'][filename] = self.convert_figure(data_type, data) |
|
@@ -13,6 +13,7 b' one format to another.' | |||||
13 | # Imports |
|
13 | # Imports | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 |
|
15 | |||
|
16 | import base64 | |||
16 | import os |
|
17 | import os | |
17 | import sys |
|
18 | import sys | |
18 | import subprocess |
|
19 | import subprocess | |
@@ -37,16 +38,34 b" INKSCAPE_OSX_COMMAND = '/Applications/Inkscape.app/Contents/Resources/bin/inksca" | |||||
37 |
|
38 | |||
38 | class SVG2PDFTransformer(ConvertFiguresTransformer): |
|
39 | class SVG2PDFTransformer(ConvertFiguresTransformer): | |
39 | """ |
|
40 | """ | |
40 |
Converts all of the outputs in a notebook from |
|
41 | Converts all of the outputs in a notebook from SVG to PDF. | |
41 | """ |
|
42 | """ | |
42 |
|
43 | |||
43 | from_format = Unicode('svg', config=True, help='Format the converter accepts') |
|
44 | from_format = Unicode('svg', config=True, help='Format the converter accepts') | |
44 | to_format = Unicode('pdf', config=False, help='Format the converter writes') |
|
45 | to_format = Unicode('pdf', config=False, help='Format the converter writes') | |
|
46 | command = Unicode(config=True, | |||
|
47 | help="""The command to use for converting SVG to PDF | |||
|
48 | ||||
|
49 | This string is a template, which will be formatted with the keys | |||
|
50 | to_filename and from_filename. | |||
|
51 | ||||
|
52 | The conversion call must read the SVG from {from_flename}, | |||
|
53 | and write a PDF to {to_filename}. | |||
|
54 | """) | |||
|
55 | ||||
|
56 | def _command_default(self): | |||
|
57 | if sys.platform == "darwin": | |||
|
58 | return INKSCAPE_OSX_COMMAND | |||
|
59 | elif sys.platform == "win32": | |||
|
60 | # windows not yet supported | |||
|
61 | return "" | |||
|
62 | else: | |||
|
63 | return INKSCAPE_COMMAND | |||
45 |
|
64 | |||
46 |
|
65 | |||
47 | def convert_figure(self, data_format, data): |
|
66 | def convert_figure(self, data_format, data): | |
48 | """ |
|
67 | """ | |
49 |
Convert a single S |
|
68 | Convert a single SVG figure to PDF. Returns converted data. | |
50 | """ |
|
69 | """ | |
51 |
|
70 | |||
52 | #Work in a temporary directory |
|
71 | #Work in a temporary directory | |
@@ -54,24 +73,20 b' class SVG2PDFTransformer(ConvertFiguresTransformer):' | |||||
54 |
|
73 | |||
55 | #Write fig to temp file |
|
74 | #Write fig to temp file | |
56 | input_filename = os.path.join(tmpdir, 'figure.' + data_format) |
|
75 | input_filename = os.path.join(tmpdir, 'figure.' + data_format) | |
57 | with open(input_filename, 'w') as f: |
|
76 | with open(input_filename, 'wb') as f: | |
58 | f.write(data) |
|
77 | f.write(data) | |
59 |
|
78 | |||
60 | #Determine command (different on Mac OSX) |
|
|||
61 | command = INKSCAPE_COMMAND |
|
|||
62 | if sys.platform == 'darwin': |
|
|||
63 | command = INKSCAPE_OSX_COMMAND |
|
|||
64 |
|
||||
65 | #Call conversion application |
|
79 | #Call conversion application | |
66 | output_filename = os.path.join(tmpdir, 'figure.pdf') |
|
80 | output_filename = os.path.join(tmpdir, 'figure.pdf') | |
67 | shell = command.format(from_filename=input_filename, |
|
81 | shell = self.command.format(from_filename=input_filename, | |
68 | to_filename=output_filename) |
|
82 | to_filename=output_filename) | |
69 | subprocess.call(shell, shell=True) #Shell=True okay since input is trusted. |
|
83 | subprocess.call(shell, shell=True) #Shell=True okay since input is trusted. | |
70 |
|
84 | |||
71 | #Read output from drive |
|
85 | #Read output from drive | |
|
86 | # return value expects a filename | |||
72 | if os.path.isfile(output_filename): |
|
87 | if os.path.isfile(output_filename): | |
73 | with open(output_filename, 'rb') as f: |
|
88 | with open(output_filename, 'rb') as f: | |
74 |
|
|
89 | # PDF is a nb supported binary, data type, so base64 encode. | |
75 | #data type, so base64 encode. |
|
90 | return base64.encodestring(f.read()) | |
76 | else: |
|
91 | else: | |
77 | return TypeError("Inkscape svg to png conversion failed") |
|
92 | return TypeError("Inkscape svg to png conversion failed") |
General Comments 0
You need to be logged in to leave comments.
Login now