Skip to content

Commit 46e6d92

Browse files
authored
Merge pull request #61 from mgajda/michal/asymptote-support
Support Asymptote diagrams
2 parents 7e98e10 + 4920ed6 commit 46e6d92

File tree

14 files changed

+213
-17
lines changed

14 files changed

+213
-17
lines changed

.github/workflows/ci.yml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,23 @@ jobs:
9898
run: |
9999
Rscript -e "install.packages('ggplot2', repos='http://cran.rstudio.com/')"
100100
101-
- name: Install Octave, Gnuplot, Graphviz, and PlantUML [Linux]
101+
- name: Install Octave, Gnuplot, Graphviz, PlantUML, and Asymptote [Linux]
102102
if: runner.os == 'Linux'
103103
run: |
104104
sudo apt-get update
105105
sudo apt-get --quiet --yes install octave
106106
sudo apt-get --quiet --yes install gnuplot
107107
sudo apt-get --quiet --yes install graphviz
108-
108+
sudo apt-get --quiet --yes install asymptote
109109
sudo apt-get --quiet --yes install plantuml
110+
echo $(dot -version</dev/null)
111+
echo $(asy --version)
112+
echo $(asy --environment)
110113
echo $(plantuml -version)
111114
# The ubuntu package version is too old
112115
sudo curl -L "http://sourceforge.net/projects/plantuml/files/plantuml.jar/download" -o "/usr/share/plantuml/plantuml.jar"
113116
echo $(plantuml -version)
117+
echo $PATH
114118
115119
- name: Install Graphviz, GNUplot, and PlantUML [Windows]
116120
if: runner.os == 'Windows'
@@ -120,13 +124,22 @@ jobs:
120124
dot -c
121125
122126
choco install --yes --no-progress gnuplot
127+
#refreshenv
128+
#gnuplot --version
129+
130+
choco install --yes --no-progress asymptote
131+
#refreshenv
132+
#asy -version
133+
#asy -environment
134+
123135
choco install --yes --no-progress plantuml
124136
plantuml -h
125137
126138
- name: Install Octave, Gnuplot, Graphviz, and PlantUML [Mac]
127139
if: runner.os == 'macOS'
128140
run: |
129141
brew update
142+
brew install asymptote
130143
brew install octave
131144
brew install gnuplot
132145
brew install graphviz
@@ -202,6 +215,12 @@ jobs:
202215
exit 1
203216
fi
204217
pandoc-plot clean tests/issue53.md
218+
219+
pandoc --filter pandoc-plot -i tests/issue55.md -t native
220+
if [ $(ls "plots" | wc -l) != 2 ]; then
221+
exit 1
222+
fi
223+
pandoc-plot clean tests/issue55.md
205224
206225
- name: Build documentation
207226
run: source tools/mkmanual.sh

cabal.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ test-show-details: streaming
44

55
jobs: $ncpus
66

7-
allow-newer: haddock-library:base,
7+
allow-newer: haddock-library:base,

pandoc-plot.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ library
8282
Text.Pandoc.Filter.Plot.Renderers.PlantUML
8383
Text.Pandoc.Filter.Plot.Renderers.SageMath
8484
Text.Pandoc.Filter.Plot.Renderers.D2
85+
Text.Pandoc.Filter.Plot.Renderers.Asymptote
8586
Text.Pandoc.Filter.Plot.Monad
8687
Text.Pandoc.Filter.Plot.Monad.Logging
8788
Text.Pandoc.Filter.Plot.Monad.Types

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ defaultConfiguration =
6565
plantumlPreamble = mempty,
6666
sagemathPreamble = mempty,
6767
d2Preamble = mempty,
68+
asyPreamble = mempty,
6869
-- Executables
6970
matplotlibExe = python,
7071
matlabExe = "matlab",
@@ -80,6 +81,7 @@ defaultConfiguration =
8081
plantumlExe = "java",
8182
sagemathExe = "sage",
8283
d2Exe = "d2",
84+
asyExe = "asy",
8385
-- Command line arguments
8486
matplotlibCmdArgs = mempty,
8587
matlabCmdArgs = mempty,
@@ -95,6 +97,7 @@ defaultConfiguration =
9597
plantumlCmdArgs = "-jar plantuml.jar",
9698
sagemathCmdArgs = mempty,
9799
d2CmdArgs = mempty,
100+
asyCmdArgs = mempty,
98101
-- Extras
99102
matplotlibTightBBox = False,
100103
matplotlibTransparent = False
@@ -155,7 +158,8 @@ data ConfigPrecursor = ConfigPrecursor
155158
_plotsjlPrec :: !PlotsjlPrecursor,
156159
_plantumlPrec :: !PlantUMLPrecursor,
157160
_sagemathPrec :: !SageMathPrecursor,
158-
_d2Prec :: !D2Precursor
161+
_d2Prec :: !D2Precursor,
162+
_asyPrec :: !AsyPrecursor
159163
}
160164

161165
defaultConfigPrecursor :: ConfigPrecursor
@@ -183,7 +187,8 @@ defaultConfigPrecursor =
183187
_plotsjlPrec = PlotsjlPrecursor Nothing (plotsjlExe defaultConfiguration) (plotsjlCmdArgs defaultConfiguration),
184188
_plantumlPrec = PlantUMLPrecursor Nothing (plantumlExe defaultConfiguration) (plantumlCmdArgs defaultConfiguration),
185189
_sagemathPrec = SageMathPrecursor Nothing (sagemathExe defaultConfiguration) (sagemathCmdArgs defaultConfiguration),
186-
_d2Prec = D2Precursor Nothing (d2Exe defaultConfiguration) (d2CmdArgs defaultConfiguration)
190+
_d2Prec = D2Precursor Nothing (d2Exe defaultConfiguration) (d2CmdArgs defaultConfiguration),
191+
_asyPrec = AsyPrecursor Nothing (asyExe defaultConfiguration) (asyCmdArgs defaultConfiguration)
187192
}
188193

189194
data LoggingPrecursor = LoggingPrecursor
@@ -226,6 +231,8 @@ data SageMathPrecursor = SageMathPrecursor {_sagemathPreamble :: !(Maybe FilePat
226231

227232
data D2Precursor = D2Precursor {_d2Preamble :: !(Maybe FilePath), _d2Exe :: !FilePath, _d2CmdArgs :: !Text}
228233

234+
data AsyPrecursor = AsyPrecursor {_asyPreamble :: !(Maybe FilePath), _asyExe :: !FilePath, _asyCmdArgs :: !Text}
235+
229236
instance FromJSON LoggingPrecursor where
230237
parseJSON (Object v) =
231238
LoggingPrecursor
@@ -298,6 +305,10 @@ instance FromJSON D2Precursor where
298305
parseJSON (Object v) = D2Precursor <$> v .:? asKey PreambleK <*> v .:? asKey ExecutableK .!= d2Exe defaultConfiguration <*> v .:? asKey CommandLineArgsK .!= d2CmdArgs defaultConfiguration
299306
parseJSON _ = fail $ mconcat ["Could not parse ", show SageMath, " configuration."]
300307

308+
instance FromJSON AsyPrecursor where
309+
parseJSON (Object v) = AsyPrecursor <$> v .:? asKey PreambleK <*> v .:? asKey ExecutableK .!= asyExe defaultConfiguration <*> v .:? asKey CommandLineArgsK .!= asyCmdArgs defaultConfiguration
310+
parseJSON _ = fail $ mconcat ["Could not parse ", show Asymptote, " configuration."]
311+
301312
toolkitAsKey :: Toolkit -> Key
302313
toolkitAsKey = fromString . unpack . cls
303314

@@ -328,6 +339,7 @@ instance FromJSON ConfigPrecursor where
328339
_plantumlPrec <- v .:? toolkitAsKey PlantUML .!= _plantumlPrec defaultConfigPrecursor
329340
_sagemathPrec <- v .:? toolkitAsKey SageMath .!= _sagemathPrec defaultConfigPrecursor
330341
_d2Prec <- v .:? toolkitAsKey D2 .!= _d2Prec defaultConfigPrecursor
342+
_asyPrec <- v .:? toolkitAsKey Asymptote .!= _asyPrec defaultConfigPrecursor
331343

332344
return $ ConfigPrecursor {..}
333345
parseJSON _ = fail "Could not parse configuration."
@@ -363,6 +375,7 @@ renderConfig ConfigPrecursor {..} = do
363375
plantumlExe = _plantumlExe _plantumlPrec
364376
sagemathExe = _sagemathExe _sagemathPrec
365377
d2Exe = _d2Exe _d2Prec
378+
asyExe = _asyExe _asyPrec
366379

367380
matplotlibCmdArgs = _matplotlibCmdArgs _matplotlibPrec
368381
matlabCmdArgs = _matlabCmdArgs _matlabPrec
@@ -378,6 +391,7 @@ renderConfig ConfigPrecursor {..} = do
378391
plantumlCmdArgs = _plantumlCmdArgs _plantumlPrec
379392
sagemathCmdArgs = _sagemathCmdArgs _sagemathPrec
380393
d2CmdArgs = _d2CmdArgs _d2Prec
394+
asyCmdArgs = _asyCmdArgs _asyPrec
381395

382396
matplotlibPreamble <- readPreamble (_matplotlibPreamble _matplotlibPrec)
383397
matlabPreamble <- readPreamble (_matlabPreamble _matlabPrec)
@@ -393,6 +407,7 @@ renderConfig ConfigPrecursor {..} = do
393407
plantumlPreamble <- readPreamble (_plantumlPreamble _plantumlPrec)
394408
sagemathPreamble <- readPreamble (_sagemathPreamble _sagemathPrec)
395409
d2Preamble <- readPreamble (_d2Preamble _d2Prec)
410+
asyPreamble <- readPreamble (_asyPreamble _asyPrec)
396411

397412
return Configuration {..}
398413
where

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ executable tk = exeSelector tk <&> exeFromPath
283283
exeSelector PlantUML = asksConfig plantumlExe
284284
exeSelector SageMath = asksConfig sagemathExe
285285
exeSelector D2 = asksConfig d2Exe
286+
exeSelector Asymptote = asksConfig asyExe
286287

287288
-- | The @Configuration@ type holds the default values to use
288289
-- when running pandoc-plot. These values can be overridden in code blocks.
@@ -353,6 +354,8 @@ data Configuration = Configuration
353354
sagemathPreamble :: !Script,
354355
-- | The default preamble script for the d2 toolkit.
355356
d2Preamble :: !Script,
357+
-- | The default preamble script for the Asymptote toolkit.
358+
asyPreamble :: !Script,
356359
-- | The executable to use to generate figures using the matplotlib toolkit.
357360
matplotlibExe :: !FilePath,
358361
-- | The executable to use to generate figures using the MATLAB toolkit.
@@ -381,6 +384,8 @@ data Configuration = Configuration
381384
sagemathExe :: !FilePath,
382385
-- | The executable to use to generate figures using d2.
383386
d2Exe :: !FilePath,
387+
-- | The executable to use to generate figures using Asymptote
388+
asyExe :: !FilePath,
384389
-- | Command-line arguments to pass to the Python interpreter for the Matplotlib toolkit
385390
matplotlibCmdArgs :: !Text,
386391
-- | Command-line arguments to pass to the interpreter for the MATLAB toolkit.
@@ -409,6 +414,8 @@ data Configuration = Configuration
409414
sagemathCmdArgs :: !Text,
410415
-- | Command-line arguments to pass to the interpreter for the d2 toolkit.
411416
d2CmdArgs :: !Text,
417+
-- | Command-line arguments to pass to the interpreter for the Asymptote toolkit.
418+
asyCmdArgs :: !Text,
412419
-- | Whether or not to make Matplotlib figures tight by default.
413420
matplotlibTightBBox :: !Bool,
414421
-- | Whether or not to make Matplotlib figures transparent by default.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ data Toolkit
6262
| PlantUML
6363
| SageMath
6464
| D2
65+
| Asymptote
6566
deriving (Bounded, Eq, Enum, Generic, Ord)
6667

6768
-- | This instance should only be used to display toolkit names
@@ -80,6 +81,7 @@ instance Show Toolkit where
8081
show PlantUML = "PlantUML"
8182
show SageMath = "SageMath"
8283
show D2 = "D2"
84+
show Asymptote = "Asymptote"
8385

8486
-- | Class name which will trigger the filter
8587
cls :: Toolkit -> Text
@@ -97,6 +99,7 @@ cls Plotsjl = "plotsjl"
9799
cls PlantUML = "plantuml"
98100
cls SageMath = "sageplot"
99101
cls D2 = "d2"
102+
cls Asymptote = "asy"
100103

101104
-- | Executable program, and sometimes the directory where it can be found.
102105
data Executable

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ import Text.Pandoc.Filter.Plot.Renderers.SageMath
9595
( sagemath,
9696
sagemathSupportedSaveFormats,
9797
)
98-
98+
import Text.Pandoc.Filter.Plot.Renderers.Asymptote
99+
( asymptote,
100+
asymptoteSupportedSaveFormats,
101+
)
99102
-- | Get the renderer associated with a toolkit.
100103
-- If the renderer has not been used before,
101104
-- initialize it and store where it is. It will be re-used.
@@ -114,6 +117,7 @@ renderer Plotsjl = plotsjl
114117
renderer PlantUML = plantuml
115118
renderer SageMath = sagemath
116119
renderer D2 = d2
120+
renderer Asymptote = asymptote
117121

118122
-- | Save formats supported by this renderer.
119123
supportedSaveFormats :: Toolkit -> [SaveFormat]
@@ -131,6 +135,7 @@ supportedSaveFormats Plotsjl = plotsjlSupportedSaveFormats
131135
supportedSaveFormats PlantUML = plantumlSupportedSaveFormats
132136
supportedSaveFormats SageMath = sagemathSupportedSaveFormats
133137
supportedSaveFormats D2 = d2SupportedSaveFormats
138+
supportedSaveFormats Asymptote = asymptoteSupportedSaveFormats
134139

135140
-- | The function that maps from configuration to the preamble.
136141
preambleSelector :: Toolkit -> (Configuration -> Script)
@@ -148,6 +153,7 @@ preambleSelector Plotsjl = plotsjlPreamble
148153
preambleSelector PlantUML = plantumlPreamble
149154
preambleSelector SageMath = sagemathPreamble
150155
preambleSelector D2 = d2Preamble
156+
preambleSelector Asymptote = asyPreamble
151157

152158
-- | Parse code block headers for extra attributes that are specific
153159
-- to this renderer. By default, no extra attributes are parsed.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE QuasiQuotes #-}
3+
{-# LANGUAGE RecordWildCards #-}
4+
{-# LANGUAGE NoImplicitPrelude #-}
5+
6+
-- |
7+
-- Module : $header$
8+
-- Copyright : (c) Laurent P René de Cotret, 2019 - present
9+
-- License : GNU GPL, version 2 or above
10+
-- Maintainer : [email protected]
11+
-- Stability : internal
12+
-- Portability : portable
13+
--
14+
-- Rendering Asymptote plots code blocks
15+
module Text.Pandoc.Filter.Plot.Renderers.Asymptote
16+
( asymptote,
17+
asymptoteSupportedSaveFormats,
18+
)
19+
where
20+
21+
import Text.Pandoc.Filter.Plot.Renderers.Prelude
22+
import Data.Char(toLower)
23+
24+
asymptote :: PlotM Renderer
25+
asymptote = do
26+
cmdargs <- asksConfig asyCmdArgs
27+
return
28+
$ Renderer
29+
{ rendererToolkit = Asymptote,
30+
rendererCapture = asymptoteCapture,
31+
rendererCommand = asymptoteCommand cmdargs,
32+
rendererAvailability = CommandSuccess $ \exe -> [st|#{pathToExe exe} -environment|],
33+
rendererSupportedSaveFormats = asymptoteSupportedSaveFormats,
34+
rendererChecks = mempty,
35+
rendererLanguage = "asy",
36+
rendererComment = mappend "// ",
37+
rendererScriptExtension = ".asy"
38+
}
39+
40+
asymptoteSupportedSaveFormats :: [SaveFormat]
41+
asymptoteSupportedSaveFormats = [PDF, EPS, PNG]
42+
43+
asymptoteCommand :: Text -> OutputSpec -> Text
44+
asymptoteCommand cmdArgs OutputSpec {..} =
45+
[st|#{pathToExe oExecutable} #{cmdArgs} -f #{toLower <$> show (saveFormat oFigureSpec)} -o "#{oFigurePath}" "#{oScriptPath}"|]
46+
47+
-- Asymptote export is entirely based on command-line arguments
48+
-- so there is no need to modify the script itself.
49+
asymptoteCapture :: FigureSpec -> FilePath -> Script
50+
asymptoteCapture FigureSpec {..} _ = script

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ ggplot2 = do
3838
}
3939

4040
ggplot2SupportedSaveFormats :: [SaveFormat]
41-
ggplot2SupportedSaveFormats = [PNG, PDF, SVG, JPG, EPS, TIF]
41+
ggplot2SupportedSaveFormats = [PNG, PDF, SVG, JPG, EPS]
4242

4343
ggplot2Command :: Text -> OutputSpec -> Text
4444
ggplot2Command cmdargs OutputSpec {..} = [st|#{pathToExe oExecutable} #{cmdargs} "#{oScriptPath}"|]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ matplotlib = do
4444
}
4545

4646
matplotlibSupportedSaveFormats :: [SaveFormat]
47-
matplotlibSupportedSaveFormats = [PNG, PDF, SVG, JPG, EPS, GIF, TIF]
47+
matplotlibSupportedSaveFormats = [PNG, PDF, SVG, JPG, EPS, TIF]
4848

4949
matplotlibCommand :: Text -> OutputSpec -> Text
5050
matplotlibCommand cmdargs OutputSpec {..} = [st|#{pathToExe oExecutable} #{cmdargs} "#{oScriptPath}"|]

0 commit comments

Comments
 (0)