Skip to content

Commit bf1fe20

Browse files
committed
Force the scripting engine to use the figure-specific executable when available
1 parent f24a05d commit bf1fe20

File tree

16 files changed

+56
-84
lines changed

16 files changed

+56
-84
lines changed

src/Text/Pandoc/Filter/Plot/Monad/Types.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module Text.Pandoc.Filter.Plot.Monad.Types
2626
inclusionKeys,
2727
Executable (..),
2828
exeFromPath,
29+
pathToExe,
2930
-- Utilities
3031
isWindows,
3132
)
@@ -37,7 +38,7 @@ import Data.String (IsString (..))
3738
import Data.Text (Text, pack, unpack)
3839
import Data.Yaml (FromJSON(..), ToJSON (toJSON), withText)
3940
import GHC.Generics (Generic)
40-
import System.FilePath (splitFileName)
41+
import System.FilePath (splitFileName, (</>))
4142
import System.Info (os)
4243
import Text.Pandoc.Definition (Attr)
4344

@@ -102,6 +103,9 @@ exeFromPath fp =
102103
let (dir, name) = splitFileName fp
103104
in Executable dir (pack name)
104105

106+
pathToExe :: Executable -> FilePath
107+
pathToExe (Executable dir name) = dir </> unpack name
108+
105109
-- | Source context for plotting scripts
106110
type Script = Text
107111

@@ -170,6 +174,8 @@ inclusionKeys = enumFromTo (minBound :: InclusionKey) maxBound
170174
data FigureSpec = FigureSpec
171175
{ -- | Renderer to use for this figure.
172176
renderer_ :: !Renderer,
177+
-- | Executable to use in rendering this figure.
178+
fsExecutable :: Executable,
173179
-- | Figure caption.
174180
caption :: !Text,
175181
-- | Append link to source code in caption.
@@ -263,13 +269,14 @@ data OutputSpec = OutputSpec
263269
oScriptPath :: FilePath,
264270
-- | Figure output path
265271
oFigurePath :: FilePath,
272+
-- | Executable to use during rendering
273+
oExecutable :: Executable,
266274
-- | Current working directory
267275
oCWD :: FilePath
268276
}
269277

270278
data Renderer = Renderer
271279
{ rendererToolkit :: Toolkit,
272-
rendererExe :: Executable,
273280
rendererCapture :: FigureSpec -> FilePath -> Script,
274281
rendererCommand :: OutputSpec -> Text,
275282
rendererSupportedSaveFormats :: [SaveFormat],

src/Text/Pandoc/Filter/Plot/Parse.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,11 @@ parseFigureSpec block@(CodeBlock (id', classes, attrs) _) = do
108108

109109
-- Decide between reading from file or using document content
110110
content <- parseContent block
111+
112+
defaultExe <- fromJust <$> (executable rendererToolkit)
111113

112114
let caption = Map.findWithDefault mempty (tshow CaptionK) attrs'
115+
fsExecutable = maybe defaultExe (exeFromPath . unpack) $ Map.lookup (tshow ExecutableK) attrs'
113116
withSource = maybe defWithSource readBool (Map.lookup (tshow WithSourceK) attrs')
114117
script = mconcat $ intersperse "\n" [header, includeScript, content]
115118
directory = makeValid $ unpack $ Map.findWithDefault (pack $ defaultDirectory conf) (tshow DirectoryK) attrs'

src/Text/Pandoc/Filter/Plot/Renderers/Bokeh.hs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,12 @@ bokeh = do
2929
then return Nothing
3030
else do
3131
cmdargs <- asksConfig bokehCmdArgs
32-
mexe <- executable Bokeh
3332
return $
34-
mexe >>= \exe@(Executable _ exename) ->
3533
return
3634
Renderer
3735
{ rendererToolkit = Bokeh,
38-
rendererExe = exe,
3936
rendererCapture = appendCapture bokehCaptureFragment,
40-
rendererCommand = bokehCommand cmdargs exename,
37+
rendererCommand = bokehCommand cmdargs,
4138
rendererSupportedSaveFormats = bokehSupportedSaveFormats,
4239
rendererChecks = [bokehCheckIfShow],
4340
rendererLanguage = "python",
@@ -48,8 +45,8 @@ bokeh = do
4845
bokehSupportedSaveFormats :: [SaveFormat]
4946
bokehSupportedSaveFormats = [PNG, SVG, HTML]
5047

51-
bokehCommand :: Text -> Text -> OutputSpec -> Text
52-
bokehCommand cmdargs exe OutputSpec {..} = [st|#{exe} #{cmdargs} "#{oScriptPath}"|]
48+
bokehCommand :: Text -> OutputSpec -> Text
49+
bokehCommand cmdargs OutputSpec {..} = [st|#{pathToExe oExecutable} #{cmdargs} "#{oScriptPath}"|]
5350

5451
bokehAvailable :: PlotM Bool
5552
bokehAvailable = do

src/Text/Pandoc/Filter/Plot/Renderers/GGPlot2.hs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,12 @@ ggplot2 = do
2828
then return Nothing
2929
else do
3030
cmdargs <- asksConfig ggplot2CmdArgs
31-
mexe <- executable GGPlot2
3231
return $
33-
mexe >>= \exe@(Executable _ exename) ->
3432
return
3533
Renderer
3634
{ rendererToolkit = GGPlot2,
37-
rendererExe = exe,
3835
rendererCapture = ggplot2Capture,
39-
rendererCommand = ggplot2Command cmdargs exename,
36+
rendererCommand = ggplot2Command cmdargs,
4037
rendererSupportedSaveFormats = ggplot2SupportedSaveFormats,
4138
rendererChecks = mempty,
4239
rendererLanguage = "r",
@@ -47,8 +44,8 @@ ggplot2 = do
4744
ggplot2SupportedSaveFormats :: [SaveFormat]
4845
ggplot2SupportedSaveFormats = [PNG, PDF, SVG, JPG, EPS, TIF]
4946

50-
ggplot2Command :: Text -> Text -> OutputSpec -> Text
51-
ggplot2Command cmdargs exe OutputSpec {..} = [st|#{exe} #{cmdargs} "#{oScriptPath}"|]
47+
ggplot2Command :: Text -> OutputSpec -> Text
48+
ggplot2Command cmdargs OutputSpec {..} = [st|#{pathToExe oExecutable} #{cmdargs} "#{oScriptPath}"|]
5249

5350
ggplot2Available :: PlotM Bool
5451
ggplot2Available = do

src/Text/Pandoc/Filter/Plot/Renderers/GNUPlot.hs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,12 @@ gnuplot = do
2727
then return Nothing
2828
else do
2929
cmdargs <- asksConfig gnuplotCmdArgs
30-
mexe <- executable GNUPlot
3130
return $
32-
mexe >>= \exe@(Executable _ exename) ->
3331
return
3432
Renderer
3533
{ rendererToolkit = GNUPlot,
36-
rendererExe = exe,
3734
rendererCapture = gnuplotCapture,
38-
rendererCommand = gnuplotCommand cmdargs exename,
35+
rendererCommand = gnuplotCommand cmdargs,
3936
rendererSupportedSaveFormats = gnuplotSupportedSaveFormats,
4037
rendererChecks = mempty,
4138
rendererLanguage = "gnuplot",
@@ -46,8 +43,8 @@ gnuplot = do
4643
gnuplotSupportedSaveFormats :: [SaveFormat]
4744
gnuplotSupportedSaveFormats = [LaTeX, PNG, SVG, EPS, GIF, JPG, PDF]
4845

49-
gnuplotCommand :: Text -> Text -> OutputSpec -> Text
50-
gnuplotCommand cmdargs exe OutputSpec {..} = [st|#{exe} #{cmdargs} -c "#{oScriptPath}"|]
46+
gnuplotCommand :: Text -> OutputSpec -> Text
47+
gnuplotCommand cmdargs OutputSpec {..} = [st|#{pathToExe oExecutable} #{cmdargs} -c "#{oScriptPath}"|]
5148

5249
gnuplotAvailable :: PlotM Bool
5350
gnuplotAvailable = do

src/Text/Pandoc/Filter/Plot/Renderers/Graphviz.hs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,12 @@ graphviz = do
2828
then return Nothing
2929
else do
3030
cmdargs <- asksConfig graphvizCmdArgs
31-
mexe <- executable Graphviz
3231
return $
33-
mexe >>= \exe@(Executable _ exename) ->
3432
return
3533
Renderer
3634
{ rendererToolkit = Graphviz,
37-
rendererExe = exe,
3835
rendererCapture = graphvizCapture,
39-
rendererCommand = graphvizCommand cmdargs exename,
36+
rendererCommand = graphvizCommand cmdargs,
4037
rendererSupportedSaveFormats = graphvizSupportedSaveFormats,
4138
rendererChecks = mempty,
4239
rendererLanguage = "dot",
@@ -47,11 +44,11 @@ graphviz = do
4744
graphvizSupportedSaveFormats :: [SaveFormat]
4845
graphvizSupportedSaveFormats = [PNG, PDF, SVG, JPG, EPS, WEBP, GIF]
4946

50-
graphvizCommand :: Text -> Text -> OutputSpec -> Text
51-
graphvizCommand cmdargs exe OutputSpec {..} =
47+
graphvizCommand :: Text -> OutputSpec -> Text
48+
graphvizCommand cmdargs OutputSpec {..} =
5249
let fmt = fmap toLower . show . saveFormat $ oFigureSpec
5350
dpi' = dpi oFigureSpec
54-
in [st|#{exe} #{cmdargs} -T#{fmt} -Gdpi=#{dpi'} -o "#{oFigurePath}" "#{oScriptPath}"|]
51+
in [st|#{pathToExe oExecutable} #{cmdargs} -T#{fmt} -Gdpi=#{dpi'} -o "#{oFigurePath}" "#{oScriptPath}"|]
5552

5653
graphvizAvailable :: PlotM Bool
5754
graphvizAvailable = do

src/Text/Pandoc/Filter/Plot/Renderers/Mathematica.hs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,12 @@ mathematica = do
2727
then return Nothing
2828
else do
2929
cmdargs <- asksConfig mathematicaCmdArgs
30-
mexe <- executable Mathematica
3130
return $
32-
mexe >>= \exe@(Executable _ exename) ->
3331
return
3432
Renderer
3533
{ rendererToolkit = Mathematica,
36-
rendererExe = exe,
3734
rendererCapture = mathematicaCapture,
38-
rendererCommand = mathematicaCommand cmdargs exename,
35+
rendererCommand = mathematicaCommand cmdargs,
3936
rendererSupportedSaveFormats = mathematicaSupportedSaveFormats,
4037
rendererChecks = mempty,
4138
rendererLanguage = "mathematica",
@@ -46,8 +43,8 @@ mathematica = do
4643
mathematicaSupportedSaveFormats :: [SaveFormat]
4744
mathematicaSupportedSaveFormats = [PNG, PDF, SVG, JPG, EPS, GIF, TIF]
4845

49-
mathematicaCommand :: Text -> Text -> OutputSpec -> Text
50-
mathematicaCommand cmdargs exe OutputSpec {..} = [st|#{exe} #{cmdargs} -script "#{oScriptPath}"|]
46+
mathematicaCommand :: Text -> OutputSpec -> Text
47+
mathematicaCommand cmdargs OutputSpec {..} = [st|#{pathToExe oExecutable} #{cmdargs} -script "#{oScriptPath}"|]
5148

5249
mathematicaAvailable :: PlotM Bool
5350
mathematicaAvailable = do

src/Text/Pandoc/Filter/Plot/Renderers/Matlab.hs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,12 @@ matlab = do
2828
then return Nothing
2929
else do
3030
cmdargs <- asksConfig matlabCmdArgs
31-
mexe <- executable Matlab
3231
return $
33-
mexe >>= \exe@(Executable _ exename) ->
3432
return
3533
Renderer
3634
{ rendererToolkit = Matlab,
37-
rendererExe = exe,
3835
rendererCapture = matlabCapture,
39-
rendererCommand = matlabCommand cmdargs exename,
36+
rendererCommand = matlabCommand cmdargs,
4037
rendererSupportedSaveFormats = matlabSupportedSaveFormats,
4138
rendererChecks = mempty,
4239
rendererLanguage = "matlab",
@@ -47,13 +44,13 @@ matlab = do
4744
matlabSupportedSaveFormats :: [SaveFormat]
4845
matlabSupportedSaveFormats = [PNG, PDF, SVG, JPG, EPS, GIF, TIF]
4946

50-
matlabCommand :: Text -> Text -> OutputSpec -> Text
51-
matlabCommand cmdargs exe OutputSpec {..} =
47+
matlabCommand :: Text -> OutputSpec -> Text
48+
matlabCommand cmdargs OutputSpec {..} =
5249
-- The MATLAB 'run' function will switch to the directory where the script
5350
-- is located before executing the script. Therefore, we first save the current
5451
-- working directory in the variable 'pandoc_plot_cwd' so that we can use it
5552
-- when exporting the figure
56-
[st|#{exe} #{cmdargs} -sd '#{oCWD}' -noFigureWindows -batch "pandoc_plot_cwd=pwd; run('#{oScriptPath}')"|]
53+
[st|#{pathToExe oExecutable} #{cmdargs} -sd '#{oCWD}' -noFigureWindows -batch "pandoc_plot_cwd=pwd; run('#{oScriptPath}')"|]
5754

5855
-- On Windows at least, "matlab -help" actually returns -1, even though the
5956
-- help text is shown successfully!

src/Text/Pandoc/Filter/Plot/Renderers/Matplotlib.hs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,12 @@ matplotlib = do
3434
then return Nothing
3535
else do
3636
cmdargs <- asksConfig matplotlibCmdArgs
37-
mexe <- executable Matplotlib
3837
return $
39-
mexe >>= \exe@(Executable _ exename) ->
4038
return
4139
Renderer
4240
{ rendererToolkit = Matplotlib,
43-
rendererExe = exe,
4441
rendererCapture = matplotlibCapture,
45-
rendererCommand = matplotlibCommand cmdargs exename,
42+
rendererCommand = matplotlibCommand cmdargs,
4643
rendererSupportedSaveFormats = matplotlibSupportedSaveFormats,
4744
rendererChecks = [matplotlibCheckIfShow],
4845
rendererLanguage = "python",
@@ -53,8 +50,8 @@ matplotlib = do
5350
matplotlibSupportedSaveFormats :: [SaveFormat]
5451
matplotlibSupportedSaveFormats = [PNG, PDF, SVG, JPG, EPS, GIF, TIF]
5552

56-
matplotlibCommand :: Text -> Text -> OutputSpec -> Text
57-
matplotlibCommand cmdargs exe OutputSpec {..} = [st|#{exe} #{cmdargs} "#{oScriptPath}"|]
53+
matplotlibCommand :: Text -> OutputSpec -> Text
54+
matplotlibCommand cmdargs OutputSpec {..} = [st|#{pathToExe oExecutable} #{cmdargs} "#{oScriptPath}"|]
5855

5956
matplotlibCapture :: FigureSpec -> FilePath -> Script
6057
matplotlibCapture = appendCapture matplotlibCaptureFragment

src/Text/Pandoc/Filter/Plot/Renderers/Octave.hs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,12 @@ octave = do
2727
then return Nothing
2828
else do
2929
cmdargs <- asksConfig octaveCmdArgs
30-
mexe <- executable Octave
3130
return $
32-
mexe >>= \exe@(Executable _ exename) ->
3331
return
3432
Renderer
3533
{ rendererToolkit = Octave,
36-
rendererExe = exe,
3734
rendererCapture = octaveCapture,
38-
rendererCommand = octaveCommand cmdargs exename,
35+
rendererCommand = octaveCommand cmdargs,
3936
rendererSupportedSaveFormats = octaveSupportedSaveFormats,
4037
rendererChecks = mempty,
4138
rendererLanguage = "matlab",
@@ -46,8 +43,8 @@ octave = do
4643
octaveSupportedSaveFormats :: [SaveFormat]
4744
octaveSupportedSaveFormats = [PNG, PDF, SVG, JPG, EPS, GIF, TIF]
4845

49-
octaveCommand :: Text -> Text -> OutputSpec -> Text
50-
octaveCommand cmdargs exe OutputSpec {..} = [st|#{exe} #{cmdargs} --no-gui --no-window-system "#{oScriptPath}"|]
46+
octaveCommand :: Text -> OutputSpec -> Text
47+
octaveCommand cmdargs OutputSpec {..} = [st|#{pathToExe oExecutable} #{cmdargs} --no-gui --no-window-system "#{oScriptPath}"|]
5148

5249
octaveAvailable :: PlotM Bool
5350
octaveAvailable = do

0 commit comments

Comments
 (0)