diagrams-pandoc 0.2 → 0.3
raw patch · 4 files changed
+81/−21 lines, 4 filesdep ~pandoc-types
Dependency ranges changed: pandoc-types
Files
- CHANGELOG.md +5/−0
- README.md +22/−0
- diagrams-pandoc.cabal +12/−6
- src/Text/Pandoc/Diagrams.hs +42/−15
+ CHANGELOG.md view
@@ -0,0 +1,5 @@+## v0.3 (2016-02-23)++ - Choose output type based on Pandoc output format+ - Set diagram size from Attributes in Pandoc IR / markdown+ - Support latest `pandoc-types`
README.md view
@@ -23,6 +23,21 @@ The file demo.html should now have an img tag pointing at a PNG of a circle. +## Attributes++You can specify attributes to control how the diagram is generated.+The following, for example,+``` markdown+ ~~~ {.diagram width=800 height=400}+ example = circle 1+ ~~~+```+will override the default width and height of the generated diagram.+The following attributes are supported:++* `width`: The width of the generated diagram, in pixels. The default is 500.+* `height`: The height of the generated diagram, in pixels. The default is 200.+ ## Details `diagrams-pandoc` compiles code blocks containing diagrams expressions@@ -31,6 +46,9 @@ [pandoc filter](http://johnmacfarlane.net/pandoc/scripting.html) as shown above. +`diagrams-pandoc` evaluates the diagrams expression `example` by+default. This can be modified by passing a command line argument.+ `diagrams-pandoc` is aware of two code block classes. A block with the `diagram` class will be replaced by the resulting image---the code will not appear in the output. A block with the `diagram-haskell`@@ -38,6 +56,10 @@ block. The input block is replaced by image appears before the code block, and the `diagram-haskell` class is replaced by the `haskell` class, so that pandoc can perform syntax highlighting as usual.++`diagrams-pandoc` produces images in the `pdf` format when used with+the `latex` and `beamer` writers of `pandoc` and produced `png` output+otherwise. I have only tested with pandoc's markdown reader. In particular, the rst reader does not attach classes to code blocks, only to Div elements.
diagrams-pandoc.cabal view
@@ -1,7 +1,12 @@ name: diagrams-pandoc-version: 0.2-synopsis: A pandoc filter to express diagrams inline using the haskell EDSL _diagrams_--- description: +version: 0.3+synopsis: A Pandoc filter to express diagrams inline using the Haskell EDSL _Diagrams_+description: 'diagrams-pandoc' replaces appropriately marked code+ blocks in pandoc input with images. The code blocks are compiled+ using the Haskell EDSL Diagrams. Each block should define a+ Diagram named 'example', to be output. This name and other+ defaults can be overridden by command-line arguments to the+ diagrams-pandoc program. license: BSD3 license-file: LICENSE author: Daniel Bergey@@ -9,15 +14,16 @@ category: Text build-type: Simple Bug-reports: http://github.com/diagrams/diagrams-pandoc/issues-Extra-source-files: README.md+Extra-source-files: README.md, CHANGELOG.md cabal-version: >=1.10+Tested-with: GHC == 7.8.4, GHC == 7.10.2 Source-repository head type: git location: http://github.com/diagrams/diagrams-pandoc.git library build-depends: base >= 4.6 && < 4.9,- pandoc-types >= 1.12.4.5 && < 1.13,+ pandoc-types >= 1.16 && < 1.17, diagrams-lib >= 1.3 && < 1.4, linear >= 1.10 && < 1.21, diagrams-builder >= 0.7 && < 0.8,@@ -34,7 +40,7 @@ -- other-modules: -- other-extensions: build-depends: base >= 4.6 && < 4.9,- pandoc-types >= 1.12.4.5 && < 1.13,+ pandoc-types >= 1.16 && < 1.17, diagrams-lib >= 1.3 && < 1.4, linear >= 1.10 && < 1.21, diagrams-builder >= 0.7 && < 0.8,
src/Text/Pandoc/Diagrams.hs view
@@ -1,3 +1,5 @@+{-# LANGUAGE CPP #-}+ -- | Convert appropriately annotated Code blocks to an image, with or -- without display of the code. Interpret the Code blocks as Haskell -- code using the Diagrams libraries.@@ -13,18 +15,28 @@ import Diagrams.Prelude (centerXY, pad, (&), (.~)) import Diagrams.Size (dims) import Linear (V2 (..), zero)-import System.Directory (createDirectory,- doesDirectoryExist)+import System.Directory (createDirectoryIfMissing) import System.FilePath ((<.>), (</>)) import System.IO import Text.Pandoc.Definition --- TODO choose output format based on pandoc target-backendExt :: String-backendExt = "png"+#if __GLASGOW_HASKELL__ < 710+import Control.Applicative+#endif +backendExt :: String -> String+backendExt "beamer" = "pdf"+backendExt "latex" = "pdf"+backendExt _ = "png"++-- Return output type for a string+findOutputType :: String -> OutputType+findOutputType "beamer" = PDF+findOutputType "latex" = PDF+findOutputType _ = PNG+ data Opts = Opts {- _outFormat :: String, -- ^ Not currently used+ _outFormat :: String, _outDir :: FilePath, _expression :: String }@@ -41,10 +53,10 @@ | "diagram" `elem` classes = (:[]) <$> img where img = do- d <- compileDiagram opts code+ d <- compileDiagram opts attrs code return $ case d of Left _err -> Null -- TODO log an error here- Right imgName -> Plain [Image [] (imgName,"")] -- no alt text, no title+ Right imgName -> Plain [Image ("",[],[]) [] (imgName,"")] -- no alt text, no title bl' = CodeBlock (ident, "haskell":delete "diagram-haskell" classes, attrs) code echo = readEcho attrs insertDiagrams _ block = return [block]@@ -52,10 +64,10 @@ -- Copied from https://github.com/diagrams/diagrams-doc/blob/master/doc/Xml2Html.hs -- With the CPP removed, thereby requiring Cairo -- TODO clean this up, move it into -builder somehow--- | Compile the literate source code of a diagram to a .png file with+-- | Compile the literate source code of a diagram to a .png/.pdf file with -- a file name given by a hash of the source code contents-compileDiagram :: Opts -> String -> IO (Either String String)-compileDiagram opts src = do+compileDiagram :: Opts -> [(String,String)] -> String -> IO (Either String String)+compileDiagram opts attrs src = do ensureDir $ _outDir opts let@@ -66,7 +78,11 @@ zero - (CairoOptions "default.png" (dims $ V2 500 200) PNG False)+ ( CairoOptions "default.png"+ (dims $ V2 (widthAttribute attrs) (heightAttribute attrs))+ (findOutputType $ _outFormat opts)+ False+ ) & DB.snippets .~ [src] & DB.imports .~@@ -112,10 +128,21 @@ return $ Right (mkFile (DB.hashToHexStr hash)) where- mkFile base = _outDir opts </> base <.> backendExt+ mkFile base = _outDir opts </> base <.> (backendExt $ _outFormat opts) ensureDir dir = do- b <- doesDirectoryExist dir- when (not b) $ createDirectory dir+ createDirectoryIfMissing True dir++widthAttribute :: [(String,String)] -> Double+widthAttribute attrs =+ case lookup "width" attrs of+ Nothing -> 500+ Just v -> read v :: Double++heightAttribute :: [(String,String)] -> Double+heightAttribute attrs =+ case lookup "height" attrs of+ Nothing -> 200+ Just v -> read v :: Double readEcho :: [(String, String)] -> Echo readEcho attrs = case lookup "echo" attrs of