pandoc-pyplot 1.0.1.0 → 1.0.2.0
raw patch · 4 files changed
+49/−20 lines, 4 filesdep ~base
Dependency ranges changed: base
Files
- README.md +37/−5
- package.yaml +1/−1
- pandoc-pyplot.cabal +2/−2
- src/Text/Pandoc/Filter/Pyplot.hs +9/−12
README.md view
@@ -4,7 +4,7 @@ ## A Pandoc filter for generating figures with Matplotlib from code directly in documents -Inspired by [sphinx](https://sphinxdoc.org)'s `plot_directive`, `pandoc-pyplot` helps turn Python code present in your documents to embedded Matplotlib figures. +Inspired by [sphinx](https://sphinxdoc.org)'s `plot_directive`, `pandoc-pyplot` helps turn Python code present in your documents to embedded Matplotlib figures. ## Usage @@ -46,9 +46,9 @@ In case of an output format that supports links (e.g. HTML), the embedded image generated by `pandoc-pyplot` will be a link to the source code which was used to generate the file. Therefore, other people can see what Python code was used to create your figures. -### Alternate text +### Captions -You can also specify some alternate text for your image. This is done using the optional `plot_alt` parameter: +You can also specify a caption for your image. This is done using the optional `plot_alt` parameter: ```markdown ```{plot_target=my_figure.jpg, plot_alt="This is a simple figure"} @@ -60,11 +60,43 @@ ``` ``` -## Requirements +## Installation -This filter only works with the Matplotlib plotting library. Therefore, you a Python interpreter and at least [Matplotlib](https://matplotlib.org/) installed. The python interpreter is expected to be discoverable using the name `"python"` (as opposed to `"python3"`, for example) +### Binaries +Windows binaries are available on [GitHub](https://github.com/LaurentRDC/pandoc-pyplot/releases). Place the executable in a location that is in your PATH to be able to call it. + +### From Hackage/Stackage + +`pandoc-pyplot` is available on Hackage. Using the [`cabal-install`](https://www.haskell.org/cabal/) tool: + +```bash +cabal update +cabal install pandoc-pyplot +``` + +Similarly, `pandoc-pyplot` is available on Stackage: + +```bash +stack update +stack install pandoc-pyplot +``` + +### From source + +Building from source can be done using [`stack`](https://docs.haskellstack.org/en/stable/README/) or [`cabal`](https://www.haskell.org/cabal/): + +```bash +git clone github.com/LaurentRDC/pandoc-pyplot.git +cd pandoc-pylot +stack install # Alternatively, `cabal install` +``` + ## Running the filter + +### Requirements + +This filter only works with the Matplotlib plotting library. Therefore, you a Python interpreter and at least [Matplotlib](https://matplotlib.org/) installed. The python interpreter is expected to be discoverable using the name `"python"` (as opposed to `"python3"`, for example) The filter program must be in your `PATH`. In case it is, you can use the filter with Pandoc as follows:
package.yaml view
@@ -1,5 +1,5 @@ name: pandoc-pyplot-version: '1.0.1.0'+version: '1.0.2.0' github: "LaurentRDC/pandoc-pyplot" license: MIT author: "Laurent P. René de Cotret"
pandoc-pyplot.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: 02a0b4cc9681a38e27465380086e167be8805a63959e4299baaca728b3e17c09+-- hash: 563259592becbb4f2496b0f914f546163be046510966e163f71175faa94504ad name: pandoc-pyplot-version: 1.0.1.0+version: 1.0.2.0 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. category: Documentation
src/Text/Pandoc/Filter/Pyplot.hs view
@@ -42,7 +42,6 @@ data FigureSpec = FigureSpec { target :: FilePath -- ^ filepath where generated figure will be saved , alt :: String -- ^ Alternate text for the figure (optional) - , caption :: String -- ^ Figure caption (optional) , script :: PythonScript -- ^ Source code for the figure , blockAttrs :: Attr -- ^ Attributes not related to @pandoc-pyplot@ will be propagated } @@ -61,10 +60,9 @@ presentableScript spec = mconcat [ "# Source code for ", target spec, "\n", script spec ] -- Keys that pandoc-pyplot will look for in code blocks -targetKey, altTextKey, captionKey :: String +targetKey, altTextKey :: String targetKey = "plot_target" altTextKey = "plot_alt" -captionKey = "plot_caption" -- | Determine inclusion specifications from Block attributes. -- Note that the target key is required, but all other parameters are optional @@ -73,12 +71,11 @@ createInclusion <$> M.lookup targetKey attrs' where attrs' = M.fromList attrs - inclusionKeys = [ targetKey, altTextKey, captionKey ] + inclusionKeys = [ targetKey, altTextKey ] filteredAttrs = filter (\(k,_) -> k `notElem` inclusionKeys) attrs createInclusion fname = FigureSpec { target = fname , alt = M.findWithDefault "Figure generated by pandoc-pyplot" altTextKey attrs' - , caption = M.findWithDefault mempty captionKey attrs' , script = content -- Propagate attributes that are not related to pandoc-pyplot , blockAttrs = (id', cls, filteredAttrs) @@ -125,14 +122,14 @@ -- Propagate attributes that are not related to pandoc-pyplot let relevantAttrs = blockAttrs spec - image = Image relevantAttrs [Str $ alt spec] (figurePath, "") - srcTarget = (sourcePath, "Click on this figure to see the source code") + srcTarget = Link nullAttr [Str "Source code"] (sourcePath, "") + caption' = [Str $ alt spec, Space, Str "(", srcTarget, Str ")"] + -- To render images as figures with captions, the target title + -- must be "fig:" + -- Janky? yes + image = Image relevantAttrs caption' (figurePath, "fig:") - -- TODO: use FigureSpec caption - -- We make the figure be a link to the source code - return $ Right $ Para [ - Link nullAttr [image] srcTarget - ] + return $ Right $ Para $ [image] -- | Translate filter error to an error message showError :: PandocPyplotError -> String