packages feed

pandoc-plot 1.2.0 → 1.2.1

raw patch · 6 files changed

+64/−10 lines, 6 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ Text.Pandoc.Filter.Plot: LaTeX :: SaveFormat
+ Text.Pandoc.Filter.Plot.Internal: LaTeX :: SaveFormat

Files

CHANGELOG.md view
@@ -2,6 +2,18 @@ 
 pandoc-plot uses [Semantic Versioning](http://semver.org/spec/v2.0.0.html)
 
+Release 1.2.1
+-------------
+
+* Added the ability to save plots as LaTeX directly from the GNUplot toolkit. To do this, simply set the output format to `latex`. The figure content will be embedded in the output document, which only makes sense for final conversion to LaTeX. For example:
+
+  ````markdown
+    ```{.gnuplot format=latex caption="This is a test."}
+    ...
+    ```
+  ````
+  This patch was contributed by Saku Laesvuori.
+
 Release 1.2.0
 -------------
 
pandoc-plot.cabal view
@@ -1,6 +1,6 @@ cabal-version:  2.2
 name:           pandoc-plot
-version:        1.2.0
+version:        1.2.1
 synopsis:       A Pandoc filter to include figures generated from code blocks using your plotting toolkit of choice.
 description:    A Pandoc filter to include figures generated from code blocks. 
                 Keep the document and code in the same location. Output is 
src/Text/Pandoc/Filter/Plot.hs view
@@ -100,10 +100,11 @@ 
 import Control.Concurrent (getNumCapabilities)
 import Data.Functor ((<&>))
+import Data.Map (singleton)
 import Data.Text (Text, pack, unpack)
 import Data.Version (Version)
 import Paths_pandoc_plot (version)
-import Text.Pandoc.Definition (Block, Pandoc (..))
+import Text.Pandoc.Definition (Block, Meta (..), MetaValue (..), Pandoc (..))
 import Text.Pandoc.Filter.Plot.Internal
   ( Configuration (..),
     FigureSpec,
@@ -152,7 +153,9 @@   -- TODO: make filter aware of target format
   runPlotM Nothing conf $ do
     debug $ mconcat ["Starting a new run, utilizing at most ", pack . show $ maxproc, " processes."]
-    mapConcurrentlyN maxproc make blocks <&> Pandoc meta
+    mapConcurrentlyN maxproc make blocks <&> Pandoc newMeta
+  where
+    newMeta = meta <> Meta (singleton "graphics" $ MetaBool True)
 
 -- | The version of the pandoc-plot package.
 --
src/Text/Pandoc/Filter/Plot/Embed.hs view
@@ -9,7 +9,7 @@ -- Stability   : internal
 -- Portability : portable
 --
--- Embedding HTML content
+-- Embedding HTML and LaTeX content
 module Text.Pandoc.Filter.Plot.Embed
   ( extractPlot,
     toFigure,
@@ -48,6 +48,7 @@ import Text.Pandoc.Filter.Plot.Parse (captionReader)
 import Text.Pandoc.Filter.Plot.Scripting (figurePath, sourceCodePath)
 import Text.Pandoc.Writers.HTML (writeHtml5String)
+import Text.Pandoc.Writers.LaTeX (writeLaTeX)
 import Text.Shakespeare.Text (st)
 
 -- | Convert a @FigureSpec@ to a Pandoc figure component.
@@ -70,10 +71,10 @@       caption' = if withSource' then captionText <> captionLinks else captionText
   builder attrs' target caption'
   where
-    builder =
-      if saveFormat spec == HTML
-        then interactiveBlock
-        else figure
+    builder = case saveFormat spec of
+      HTML -> interactiveBlock
+      LaTeX -> latexInput
+      _ -> figure
 
 figure ::
   Attr ->
@@ -109,6 +110,29 @@ -- </figure>
 --     |]
 
+latexInput :: Attr -> FilePath -> Inlines -> PlotM Block
+latexInput _ fp caption' = do
+  renderedCaption' <- writeLatex caption'
+  let renderedCaption =
+        if renderedCaption' /= ""
+          then [st|\caption{#{renderedCaption'}}|]
+          else ""
+  return $
+    RawBlock
+      "latex"
+      [st|
+    \begin{figure}
+        \centering
+        \input{#{pack $ normalizePath $ fp}}
+        #{renderedCaption}
+    \end{figure}
+        |]
+  where
+    normalizePath = map f
+      where
+        f '\\' = '/'
+        f x = x
+
 interactiveBlock ::
   Attr ->
   FilePath ->
@@ -135,6 +159,11 @@ -- | Convert Pandoc inlines to html
 writeHtml :: Inlines -> PlotM Text
 writeHtml is = liftIO $ handleError $ runPure $ writeHtml5String def document
+  where
+    document = Pandoc mempty [Para . toList $ is]
+
+writeLatex :: Inlines -> PlotM Text
+writeLatex is = liftIO $ handleError $ runPure $ writeLaTeX def document
   where
     document = Pandoc mempty [Para . toList $ is]
 
src/Text/Pandoc/Filter/Plot/Monad/Types.hs view
@@ -208,6 +208,8 @@     WEBP
   | -- | HTML for interactive plots.
     HTML
+  | -- | LaTeX text and pdf graphics
+    LaTeX
   deriving (Bounded, Enum, Eq, Show, Generic)
 
 instance IsString SaveFormat where
@@ -221,6 +223,7 @@     | s `elem` ["tif", "tiff", "TIF", "TIFF", ".tif", ".tiff"] = TIF
     | s `elem` ["webp", "WEBP", ".webp"] = WEBP
     | s `elem` ["html", "HTML", ".html"] = HTML
+    | s `elem` ["latex", "LaTeX", ".tex"] = LaTeX
     | otherwise =
       errorWithoutStackTrace $
         mconcat
@@ -238,6 +241,7 @@ 
 -- | Save format file extension
 extension :: SaveFormat -> String
+extension LaTeX = ".tex"
 extension fmt = mconcat [".", fmap toLower . show $ fmt]
 
 isWindows :: Bool
src/Text/Pandoc/Filter/Plot/Renderers/GNUPlot.hs view
@@ -44,7 +44,7 @@               }
 
 gnuplotSupportedSaveFormats :: [SaveFormat]
-gnuplotSupportedSaveFormats = [PNG, SVG, EPS, GIF, JPG, PDF]
+gnuplotSupportedSaveFormats = [LaTeX, PNG, SVG, EPS, GIF, JPG, PDF]
 
 gnuplotCommand :: Text -> Text -> OutputSpec -> Text
 gnuplotCommand cmdargs exe OutputSpec {..} = [st|#{exe} #{cmdargs} -c "#{oScriptPath}"|]
@@ -66,8 +66,13 @@ gnuplotCaptureFragment FigureSpec {..} fname =
   [st|
 set terminal #{terminalString saveFormat}
-set output '#{fname}'
+set output '#{normalizePath fname}'
 |]
+  where
+    normalizePath = map f
+      where
+        f '\\' = '/'
+        f x = x
 
 -- | Terminal name for supported save formats
 terminalString :: SaveFormat -> Text
@@ -77,4 +82,5 @@ terminalString GIF = "gif"
 terminalString JPG = "jpeg"
 terminalString PDF = "pdfcairo"
+terminalString LaTeX = "cairolatex"
 terminalString fmt = errorWithoutStackTrace $ "gnuplot: unsupported save format" <> show fmt