Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ TAGS
.diagrams-cache
/cabal.sandbox.config
/.cabal-sandbox/
.stack*
76 changes: 44 additions & 32 deletions diagrams-pandoc.cabal
Original file line number Diff line number Diff line change
@@ -1,36 +1,48 @@
-- Initial diagrams-pandoc.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/
name: diagrams-pandoc
version: 0.1
cabal-version: >=1.10
build-type: Simple
license: BSD3
license-file: LICENSE
maintainer: [email protected]
bug-reports: http://github.com/diagrams/diagrams-pandoc/issues
synopsis: A pandoc filter to express diagrams inline using the haskell EDSL _diagrams_
category: Text
author: Daniel Bergey
extra-source-files:
README.md

name: diagrams-pandoc
version: 0.1
synopsis: A pandoc filter to express diagrams inline using the haskell EDSL _diagrams_
-- description:
license: BSD3
license-file: LICENSE
author: Daniel Bergey
maintainer: [email protected]
category: Text
build-type: Simple
Bug-reports: http://github.com/diagrams/diagrams-pandoc/issues
Extra-source-files: README.md
cabal-version: >=1.10
Source-repository head
type: git
location: http://github.com/diagrams/diagrams-pandoc.git
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.16,
pandoc >=1.12.4.5 && <1.16,
diagrams-lib >=1.3 && <1.4,
linear >=1.10 && <1.20,
diagrams-builder >=0.7 && <0.8,
diagrams-cairo >=1.3 && <1.4,
directory >=1.2 && <1.3,
filepath >=1.3 && <1.5,
split -any
exposed-modules:
Text.Pandoc.Diagrams
exposed: True
buildable: True
default-language: Haskell2010
hs-source-dirs: src
ghc-options: -Wall

executable diagrams-pandoc
main-is: Main.hs
-- other-modules:
-- other-extensions:
build-depends: base >= 4.6 && < 4.9,
pandoc-types >= 1.12.4.5 && < 1.13,
diagrams-lib >= 1.3 && < 1.4,
linear >= 1.10 && < 1.20,
diagrams-builder >= 0.7 && < 0.8,
diagrams-cairo >= 1.3 && < 1.4,
directory >= 1.2 && < 1.3,
filepath >= 1.3 && < 1.5,
optparse-applicative >= 0.11 && < 0.12
-- hs-source-dirs:
default-language: Haskell2010
build-depends:
base >=4.6 && <4.9,
pandoc-types >=1.12.4.5 && <1.13,
optparse-applicative >=0.11 && <0.12,
filepath >=1.3 && <1.5,
diagrams-pandoc ==0.1
main-is: src/Main.hs
buildable: True
default-language: Haskell2010
27 changes: 27 additions & 0 deletions src/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Options.Applicative (Parser, ParserInfo,
execParser, fullDesc, header,
help, helper, info, long,
metavar, progDesc, short,
strOption, value, (<>))
import Text.Pandoc.Diagrams
import Text.Pandoc.JSON

main :: IO ()
main = do
--opts <- execParser withHelp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove the option parsing? If so, why keep the optparse-applicative dependency at all?

toJSONFilter $ insertDiagrams (Opts "images" "example") False

optsParser :: Parser Opts
optsParser = Opts
<$> strOption (long "out" <> short 'o' <> metavar "DIR"
<> help "Directory for image files" <> value "images")
<*> strOption (long "expression" <> long "expr" <> short 'e' <>
metavar "NAME" <>
help "name of Diagram value in Haskell snippet" <>
value "example")

withHelp :: ParserInfo Opts
withHelp = info
(helper <*> optsParser)
(fullDesc <> progDesc "interpret inline Haskell code to images in Pandoc output\nhttps://github.com/bergey/diagrams-pandoc"
<> header "diagrams-pandoc - a Pandoc filter for inline Diagrams")
96 changes: 48 additions & 48 deletions Main.hs → src/Text/Pandoc/Diagrams.hs
Original file line number Diff line number Diff line change
@@ -1,40 +1,60 @@
import Control.Applicative
import Control.Monad (when)
import Data.List (delete)

module Text.Pandoc.Diagrams where

import Control.Monad (when)
import Data.List (delete)
import Diagrams.Backend.Cairo
import Diagrams.Backend.Cairo.Internal
import qualified Diagrams.Builder as DB
import Diagrams.Prelude (centerXY, pad, (&), (.~))
import Diagrams.Size (dims)
import Linear (V2(..), zero)
import Options.Applicative
import System.Directory (createDirectory,
doesDirectoryExist)
import System.FilePath ((<.>), (</>))
import qualified Diagrams.Builder as DB
import Diagrams.Prelude (centerXY, pad, (&), (.~))
import Diagrams.Size (dims)
import Linear (V2 (..), zero)
import System.Directory (createDirectory,
doesDirectoryExist)
import System.FilePath ((<.>), (</>), pathSeparator)
import System.IO
import Text.Pandoc.JSON
import Text.Pandoc.Builder hiding (code, codeBlock)
import Text.Pandoc.Generic

-- TODO choose output format based on pandoc target
backendExt :: String
backendExt = "png"

main :: IO ()
main = do
opts <- execParser withHelp
toJSONFilter $ insertDiagrams opts

insertDiagrams :: Opts -> Block -> IO [Block]
insertDiagrams opts (CodeBlock (ident, classes, attrs) code)
| "diagram-haskell" `elem` classes = (++ [bl']) <$> img
| "diagram" `elem` classes = img
where
img = do
data Opts = Opts {
_outDir :: FilePath,
_expression :: String
}

data Echo = Above | Below
deriving (Read, Show)

-- | Transform a blog post by looking for code blocks with class
-- @diagrams@, and replacing them with images generated by evaluating the
-- identifier @diagrams@ and rendering the resulting diagram. In
-- addition, blocks with class @diagrams-def@ are collected (and deleted
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this diagrams-def feature actually implemented?

-- from the output) and provided as additional definitions that will
-- be in scope during evaluation of all @diagrams@ blocks.
renderBlockDiagrams :: FilePath -> Bool -> Pandoc -> IO Pandoc
renderBlockDiagrams outDir absolutePath p = bottomUpM (insertDiagrams (Opts outDir "example") absolutePath) p

insertDiagrams :: Opts -> Bool -> Block -> IO Block
insertDiagrams opts absolutePath (CodeBlock (ident, classes, attrs) code) | "diagrams" `elem` classes = do
d <- compileDiagram opts code
return $ case d of
Left _err -> []
Right imgName -> [Plain [Image [] (imgName,"")]] -- no alt text, no title
bl' = CodeBlock (ident, "haskell":delete "diagram-haskell" classes, attrs) code
insertDiagrams _ block = return [block]
let imgBlock = case d of
Left _err -> error "diagram not compiling"
Right imgName -> Plain [Image [] ((if absolutePath then pathSeparator : imgName else imgName),"")]
let codeBlock = CodeBlock (ident, "haskell":delete "diagrams" classes, attrs) code
let block' = case readEcho attrs of
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a hesitant about depending on key-value attributes, because they're not well supported by formats besides Markdown. OTOH, diagrams-pandoc doesn't currently handle RST anyway, even though it would be easy. Maybe if / when we try to use diagrams-pandoc for diagrams-doc, we'll switch to markdown anyway. @cchalmers thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm in favour of diagrams-doc using markdown. I actually already converted everything to markdown when I last worked on this and it wasn't too bad.

(Just Above) -> Table [] [AlignLeft] [] [] [[[codeBlock]], [[imgBlock]]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a table? We don't currently use a table in the documentation, and I generally try to avoid tables for layout in HTML.

(Just Below) -> Table [] [AlignLeft] [] [] [[[imgBlock]], [[codeBlock]]]
Nothing -> imgBlock
return block'
insertDiagrams _ _ block = return block

readEcho :: [(String, String)] -> Maybe Echo
readEcho attrs = case lookup "echo" attrs of
Just e -> Just (read e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably be readMaybe e. Or maybe a custom parser to allow other capitalization.

Nothing -> Nothing

-- Copied from https://github.com/diagrams/diagrams-doc/blob/master/doc/Xml2Html.hs
-- With the CPP removed, thereby requiring Cairo
Expand Down Expand Up @@ -103,23 +123,3 @@ compileDiagram opts src = do
ensureDir dir = do
b <- doesDirectoryExist dir
when (not b) $ createDirectory dir

data Opts = Opts {
_outDir :: FilePath,
_expression :: String
}

optsParser :: Parser Opts
optsParser = Opts
<$> strOption (long "out" <> short 'o' <> metavar "DIR"
<> help "Directory for image files" <> value "images")
<*> strOption (long "expression" <> long "expr" <> short 'e' <>
metavar "NAME" <>
help "name of Diagram value in Haskell snippet" <>
value "example")

withHelp :: ParserInfo Opts
withHelp = info
(helper <*> optsParser)
(fullDesc <> progDesc "interpret inline Haskell code to images in Pandoc output\nhttps://github.com/bergey/diagrams-pandoc"
<> header "diagrams-pandoc - a Pandoc filter for inline Diagrams")
6 changes: 6 additions & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
flags: {}
packages:
- '.'
- ../diagrams-builder
extra-deps: []
resolver: lts-3.2
2 changes: 2 additions & 0 deletions test/cleanTests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rm -rf images
rm -rf *.html
6 changes: 6 additions & 0 deletions test/test1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Here is a square:

``` {.diagrams}
example = square 1
```

6 changes: 6 additions & 0 deletions test/test2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Here is a square:

``` {.diagrams echo=Above}
example = square 1
```

5 changes: 5 additions & 0 deletions test/tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#test diagram
pandoc -t html test1.md --filter diagrams-pandoc -o test1.html -s

#test diagram with code
pandoc -t html test2.md --filter diagrams-pandoc -o test2.html -s