packages feed

pandoc-pyplot 2.1.0.1 → 2.1.1.0

raw patch · 7 files changed

+156/−33 lines, 7 filesdep +deepseqdep +open-browserdep +template-haskelldep ~filepathdep ~hashabledep ~pandoc

Dependencies added: deepseq, open-browser, template-haskell

Dependency ranges changed: filepath, hashable, pandoc, pandoc-types, text, typed-process

Files

CHANGELOG.md view
@@ -2,6 +2,12 @@ 
 pandoc-pyplot uses [Semantic Versioning](http://semver.org/spec/v2.0.0.html)
 
+Release 2.1.1.0
+---------------
+
+* Added a command-line option to open the HTML manual in the default web browser.
+* Added documentation regarding compatibility with pandoc-crossref. This was always supported but not explicitly documented.
+
 Release 2.1.0.1
 ---------------
 
README.md view
@@ -2,7 +2,7 @@ 
 [![Hackage version](https://img.shields.io/hackage/v/pandoc-pyplot.svg)](http://hackage.haskell.org/package/pandoc-pyplot) [![Stackage version (LTS)](http://stackage.org/package/pandoc-pyplot/badge/lts)](http://stackage.org/nightly/package/pandoc-pyplot) [![Stackage version (nightly)](http://stackage.org/package/pandoc-pyplot/badge/nightly)](http://stackage.org/nightly/package/pandoc-pyplot) [![Build status](https://ci.appveyor.com/api/projects/status/qbmq9cyks5jup48e?svg=true)](https://ci.appveyor.com/project/LaurentRDC/pandoc-pyplot) ![GitHub](https://img.shields.io/github/license/LaurentRDC/pandoc-pyplot.svg)
 
-`pandoc-pyplot` turns Python code present in your documents to embedded Matplotlib figures.
+`pandoc-pyplot` turns Python code present in your documents into embedded Matplotlib figures.
 
 ## Usage
 
@@ -96,6 +96,26 @@ 
 This `include` parameter is perfect for longer documents with many plots. Simply define the style you want in a separate script! You can also import packages this way, or define functions you often use.
 
+### Compatibility with pandoc-crossref
+
+[`pandoc-crossref`](https://github.com/lierdakil/pandoc-crossref) is a pandoc filter that makes it effortless to cross-reference objects on documents. 
+
+You can use `pandoc-crossref` in conjunction with `pandoc-pyplot` for the ultimate figure-making pipeline. You can combine both in a figure like so:
+
+```markdown
+    ```{#fig:myexample .pyplot caption="This is a caption"}
+    # Insert figure script here
+    ```
+
+    As you can see in @fig:myexample, ...
+```
+
+If the above source is located in file `myfile.md`, you can render the figure and references by applying `pandoc-pyplot` **first**, and then `pandoc-crossref`. For example:
+
+```bash
+pandoc --filter pandoc-pyplot --filter pandoc-crossref -i myfile.md -o myfile.html
+```
+
 ### Configurable
 
 *New in version 2.1.0.0*
@@ -209,4 +229,4 @@ 
 ## Warning
 
-Do not run this filter on unknown documents. There is nothing in `pandoc-pyplot` that can stop a Python script from performing **evil actions**.+Do not run this filter on unknown documents. There is nothing in `pandoc-pyplot` that can stop a Python script from performing **evil actions**.
executable/Main.hs view
@@ -1,40 +1,85 @@-{-# LANGUAGE LambdaCase #-}
+{-# LANGUAGE LambdaCase      #-}
+{-# LANGUAGE TemplateHaskell #-}
 
 module Main where
 
 import           Data.Default.Class        (def)
 import           Data.List                 (intersperse)
+import           Data.Monoid               ((<>))
+import qualified Data.Text                 as T
 
 import           System.Environment        (getArgs)
 import           System.Directory          (doesFileExist)
+import           System.IO.Temp            (writeSystemTempFile)
 
 import           Text.Pandoc.Filter.Pyplot (plotTransformWithConfig, configuration, SaveFormat(..))
 import           Text.Pandoc.JSON          (toJSONFilter)
 
+import           Web.Browser               (openBrowser)
+
 import qualified Data.Version              as V
 import           Paths_pandoc_pyplot       (version)
 
+import           ManPage                   (embedManualHtml)
+
 supportedSaveFormats :: [SaveFormat]
 supportedSaveFormats = enumFromTo minBound maxBound
 
--- The formatting is borrowed from Python's argparse library
+manualHtml :: T.Text
+manualHtml = T.pack $(embedManualHtml)
+
+data Flag = Help
+          | Version
+          | Formats
+          | Manual
+          | InvalidFlag
+    deriving (Eq)
+
+parseFlag :: [String] -> Maybe Flag
+parseFlag s
+    | null s                               = Nothing 
+    | head s `elem` ["-h", "--help"]       = Just Help
+    | head s `elem` ["-v", "--version"]    = Just Version
+    | head s `elem` ["-f", "--formats"]    = Just Formats
+    | head s `elem` ["-m", "--manual"]     = Just Manual
+    | otherwise                            = Just InvalidFlag
+
+flagAction :: Flag -> IO ()
+flagAction f
+    | f == Help    = showHelp
+    | f == Version = showVersion
+    | f == Formats = showFormats
+    | f == Manual  = showManual
+    | otherwise    = showError    -- Includes InvalidFlag
+    where   
+        showHelp    = putStrLn help
+        showVersion = putStrLn (V.showVersion version)
+        showFormats = putStrLn . mconcat . intersperse ", " . fmap show $ supportedSaveFormats
+        showManual  = writeSystemTempFile "pandoc-pyplot-manual.html" (T.unpack manualHtml) 
+                        >>= \fp -> openBrowser ("file:///" <> fp) >> return ()
+        showError   = putStrLn "Invalid flag. Please read `pandoc-pyplot --help` for information on valid flags."
+
 help :: String
 help =
     "\n\
     \\n\
-    \   usage: pandoc-pyplot [-h, --help] [-v, --version] [-f, --formats] \n\
+    \   usage: pandoc-pyplot [-h, --help] [-v, --version] [-f, --formats] [-m, --manual]\n\
     \\n\
-    \   This pandoc filter generates plots from Python code blocks using Matplotlib. \n\
+    \   This pandoc filter generates plots from Python code blocks using Matplotlib.\n\
     \   This allows to keep documentation and figures in perfect synchronicity.\n\
     \\n\
     \   Optional arguments:\n\
-    \       -h, --help     Show this help message and exit\n\
-    \       -v, --version  Show version number and exit \n\
-    \       -f, --formats  Show supported output figure formats and exit \n\
+    \       -h, --help       Show this help message and exit.\n\
+    \       -v, --version    Show version number and exit.\n\
+    \       -f, --formats    Show supported output figure formats and exit.\n\
+    \       -m, --manual     Open the manual page in the default web browser and exit.\n\
     \\n\
     \   To use with pandoc: \n\
     \       pandoc -s --filter pandoc-pyplot input.md --output output.html\n\
     \\n\
+    \   If you use pandoc-pyplot in combination with other filters, you probably want\n\
+    \   to run pandoc-pyplot first. See the manual (`pandoc-pyplot --manual`) for details.\n\
+    \\n\
     \   More information can be found in the repository README, located at \n\
     \       https://github.com/LaurentRDC/pandoc-pyplot\n"
 
@@ -45,13 +90,6 @@                 then configuration ".pandoc-pyplot.yml"
                 else def
 
-    getArgs >>= \case
-        (arg:_)
-            | arg `elem` ["-h", "--help"] -> showHelp
-            | arg `elem` ["-v", "--version"] -> showVersion
-            | arg `elem` ["-f", "--formats"] -> showFormats
-        _ -> toJSONFilter (plotTransformWithConfig config)
-  where
-    showHelp    = putStrLn help
-    showVersion = putStrLn (V.showVersion version)
-    showFormats = putStrLn . mconcat . intersperse ", " . fmap show $ supportedSaveFormats
+    getArgs >>= \args -> case parseFlag args of
+        Just f  -> flagAction f
+        Nothing -> toJSONFilter (plotTransformWithConfig config)
+ executable/ManPage.hs view
@@ -0,0 +1,51 @@+{-# LANGUAGE TemplateHaskellQuotes #-}
+{-|
+This module was inspired by pandoc-crossref
+|-}
+
+module ManPage ( embedManualHtml ) where
+
+import           Control.DeepSeq            (($!!))
+
+import           Data.String 
+import qualified Data.Text                  as T
+
+import           Language.Haskell.TH.Syntax
+
+import qualified Text.Pandoc                as P
+import           Text.Pandoc.Highlighting   (pygments)
+
+import System.FilePath                      (FilePath)
+import System.IO
+
+docFile :: FilePath
+docFile = "README.md"
+
+readDocFile :: IO String
+readDocFile = withFile docFile ReadMode $ \h -> do
+    hSetEncoding h utf8
+    cont <- hGetContents h
+    return $!! cont
+
+readerOpts :: P.ReaderOptions
+readerOpts = P.def { P.readerExtensions = P.githubMarkdownExtensions
+                   , P.readerStandalone = True
+                   }
+
+embedManual :: (P.Pandoc -> P.PandocPure T.Text) -> Q Exp
+embedManual fmt = do
+    qAddDependentFile docFile
+    d <- runIO readDocFile
+    let pd  = either (error . show) id $ P.runPure $ P.readMarkdown readerOpts (T.pack d)
+        txt = either (error . show) id $ P.runPure $ fmt pd
+    strToExp $ T.unpack txt
+    where
+        strToExp :: String -> Q Exp
+        strToExp s = return $ VarE 'fromString `AppE` LitE (StringL s)
+
+embedManualHtml :: Q Exp
+embedManualHtml = do
+    t <- runIO $ fmap (either (error . show) id) $ P.runIO $ P.getDefaultTemplate "html5"
+    embedManual $ P.writeHtml5String P.def { P.writerTemplate = Just t
+                                           , P.writerHighlightStyle = Just pygments
+                                           }
pandoc-pyplot.cabal view
@@ -1,8 +1,8 @@ name:           pandoc-pyplot
-version:        2.1.0.1
+version:        2.1.1.0
 cabal-version:  >= 1.12
-synopsis:       A Pandoc filter for including figures generated from Matplotlib
-description:    A pandoc filter for including figures generated from Matplotlib. Keep the document and Python code in the same location. Output from Matplotlib is captured and included as a figure.
+synopsis:       A Pandoc filter to include figures generated from Python code blocks
+description:    A Pandoc filter to include figures generated from Python code blocks. Keep the document and Python code in the same location. Output from Matplotlib is captured and included as a figure.
 category:       Documentation
 homepage:       https://github.com/LaurentRDC/pandoc-pyplot#readme
 bug-reports:    https://github.com/LaurentRDC/pandoc-pyplot/issues
@@ -36,23 +36,24 @@         src
     ghc-options: -Wall -Wcompat
     build-depends:
-          base     >=4 && <5
+          base               >=4 && <5
         , containers
         , directory
         , data-default-class >= 0.1.2
-        , filepath
-        , hashable > 1 && < 2
-        , pandoc   > 2 && < 3
-        , pandoc-types >1.12 && <2
+        , filepath           >= 1.4   && < 2
+        , hashable           >= 1     && < 2
+        , pandoc             >= 2     && < 3
+        , pandoc-types       >=1.12   && < 2
         , temporary
-        , text
-        , typed-process
+        , text               >= 1     && < 2
+        , typed-process      >= 0.2.1 && < 1
         , yaml >= 0.8.16
     default-language: Haskell2010
 
 executable pandoc-pyplot
     main-is: Main.hs
     other-modules:
+        ManPage
         Paths_pandoc_pyplot
     hs-source-dirs:
         executable
@@ -61,8 +62,15 @@           base >=4 && <5
         , directory
         , data-default-class >= 0.1.2
+        , deepseq
+        , filepath
+        , open-browser       >= 0.2.1.0
+        , pandoc
         , pandoc-pyplot
-        , pandoc-types >1.12 && <2
+        , pandoc-types       >1.12 && <2
+        , template-haskell   > 2.7 && < 3
+        , temporary
+        , text
     default-language: Haskell2010
 
 test-suite tests
@@ -71,7 +79,7 @@     main-is:         Main.hs
     build-depends:   base                 >= 4 && < 5
                    , directory
-                   , data-default-class >= 0.1.2
+                   , data-default-class   >= 0.1.2
                    , filepath
                    , hspec
                    , hspec-expectations
src/Text/Pandoc/Filter/Pyplot/FigureSpec.hs view
@@ -84,7 +84,7 @@ 
 -- | Determine the path a figure should have.
 figurePath :: FigureSpec -> FilePath
-figurePath spec = (directory spec </> stem spec)
+figurePath spec = directory spec </> stem spec
   where
     stem = flip addExtension ext . show . hash
     ext  = extension . saveFormat $ spec
stack.yaml view
@@ -17,7 +17,7 @@ #
 # resolver: ./custom-snapshot.yaml
 # resolver: https://example.com/snapshots/2018-01-01.yaml
-resolver: lts-13.14
+resolver: lts-13.16
 
 # User packages to be built.
 # Various formats can be used as shown in the example below.