##// END OF EJS Templates
Renamed ConvertSvgTransformer to svg2PdfTransformer
Jonathan Frederic -
Show More
@@ -1,12 +1,12 b''
1 # Class base Transformers
1 # Class base Transformers
2 from .activatable import ActivatableTransformer
2 from .activatable import ActivatableTransformer
3 from .base import ConfigurableTransformer
3 from .base import ConfigurableTransformer
4 from .convertfigures import ConvertFiguresTransformer
4 from .convertfigures import ConvertFiguresTransformer
5 from .convertsvg import ConvertSvgTransformer
5 from .svg2pdf import Svg2PdfTransformer
6 from .extractfigure import ExtractFigureTransformer
6 from .extractfigure import ExtractFigureTransformer
7 from .revealhelp import RevealHelpTransformer
7 from .revealhelp import RevealHelpTransformer
8 from .latex import LatexTransformer
8 from .latex import LatexTransformer
9 from .sphinx import SphinxTransformer
9 from .sphinx import SphinxTransformer
10
10
11 # decorated function Transformers
11 # decorated function Transformers
12 from .coalescestreams import coalesce_streams
12 from .coalescestreams import coalesce_streams
@@ -1,77 +1,77 b''
1 """Module containing a transformer that converts outputs in the notebook from
1 """Module containing a transformer that converts outputs in the notebook from
2 one format to another.
2 one format to another.
3 """
3 """
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (c) 2013, the IPython Development Team.
5 # Copyright (c) 2013, the IPython Development Team.
6 #
6 #
7 # Distributed under the terms of the Modified BSD License.
7 # Distributed under the terms of the Modified BSD License.
8 #
8 #
9 # The full license is in the file COPYING.txt, distributed with this software.
9 # The full license is in the file COPYING.txt, distributed with this software.
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Imports
13 # Imports
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 import os
16 import os
17 import sys
17 import sys
18 import subprocess
18 import subprocess
19
19
20 from IPython.utils.tempdir import TemporaryDirectory
20 from IPython.utils.tempdir import TemporaryDirectory
21 from IPython.utils.traitlets import Unicode
21 from IPython.utils.traitlets import Unicode
22
22
23 from .convertfigures import ConvertFiguresTransformer
23 from .convertfigures import ConvertFiguresTransformer
24
24
25
25
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27 # Constants
27 # Constants
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29
29
30 INKSCAPE_COMMAND = 'inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"'
30 INKSCAPE_COMMAND = 'inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"'
31 INKSCAPE_OSX_COMMAND = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"'
31 INKSCAPE_OSX_COMMAND = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"'
32
32
33
33
34 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
35 # Classes
35 # Classes
36 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
37
37
38 class ConvertSvgTransformer(ConvertFiguresTransformer):
38 class Svg2PdfTransformer(ConvertFiguresTransformer):
39 """
39 """
40 Converts all of the outputs in a notebook from one format to another.
40 Converts all of the outputs in a notebook from one format to another.
41 """
41 """
42
42
43 from_format = Unicode('svg', config=True, help='Format the converter accepts')
43 from_format = Unicode('svg', config=True, help='Format the converter accepts')
44 to_format = Unicode('pdf', config=False, help='Format the converter writes')
44 to_format = Unicode('pdf', config=False, help='Format the converter writes')
45
45
46
46
47 def convert_figure(self, data_format, data):
47 def convert_figure(self, data_format, data):
48 """
48 """
49 Convert a single Svg figure. Returns converted data.
49 Convert a single Svg figure. Returns converted data.
50 """
50 """
51
51
52 #Work in a temporary directory
52 #Work in a temporary directory
53 with TemporaryDirectory() as tmpdir:
53 with TemporaryDirectory() as tmpdir:
54
54
55 #Write fig to temp file
55 #Write fig to temp file
56 input_filename = os.path.join(tmpdir, 'figure.' + data_format)
56 input_filename = os.path.join(tmpdir, 'figure.' + data_format)
57 with open(input_filename, 'w') as f:
57 with open(input_filename, 'w') as f:
58 f.write(data)
58 f.write(data)
59
59
60 #Determine command (different on Mac OSX)
60 #Determine command (different on Mac OSX)
61 command = INKSCAPE_COMMAND
61 command = INKSCAPE_COMMAND
62 if sys.platform == 'darwin':
62 if sys.platform == 'darwin':
63 command = INKSCAPE_OSX_COMMAND
63 command = INKSCAPE_OSX_COMMAND
64
64
65 #Call conversion application
65 #Call conversion application
66 output_filename = os.path.join(tmpdir, 'figure.pdf')
66 output_filename = os.path.join(tmpdir, 'figure.pdf')
67 shell = command.format(from_filename=input_filename,
67 shell = command.format(from_filename=input_filename,
68 to_filename=output_filename)
68 to_filename=output_filename)
69 subprocess.call(shell, shell=True) #Shell=True okay since input is trusted.
69 subprocess.call(shell, shell=True) #Shell=True okay since input is trusted.
70
70
71 #Read output from drive
71 #Read output from drive
72 if os.path.isfile(output_filename):
72 if os.path.isfile(output_filename):
73 with open(output_filename, 'rb') as f:
73 with open(output_filename, 'rb') as f:
74 return f.read().encode("base64") #PDF is a nb supported binary
74 return f.read().encode("base64") #PDF is a nb supported binary
75 #data type, so base64 encode.
75 #data type, so base64 encode.
76 else:
76 else:
77 return TypeError("Inkscape svg to png conversion failed")
77 return TypeError("Inkscape svg to png conversion failed")
General Comments 0
You need to be logged in to leave comments. Login now