Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New builder #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
126 changes: 5 additions & 121 deletions Main.hs
Original file line number Diff line number Diff line change
@@ -1,125 +1,9 @@
import Control.Applicative
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 System.IO
import Text.Pandoc.JSON

-- TODO choose output format based on pandoc target
backendExt :: String
backendExt = "png"
import Text.Pandoc.JSON
import Diagrams.Pandoc

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
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]

-- 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
-- a file name given by a hash of the source code contents
compileDiagram :: Opts -> String -> IO (Either String String)
compileDiagram opts src = do
ensureDir $ _outDir opts

let
bopts :: DB.BuildOpts Cairo V2 Double
bopts = DB.mkBuildOpts

Cairo

zero

(CairoOptions "default.png" (dims $ V2 500 200) PNG False)

& DB.snippets .~ [src]
& DB.imports .~
[ "Diagrams.TwoD.Types" -- WHY IS THIS NECESSARY =(
, "Diagrams.Core.Points"
-- GHC 7.2 bug? need V (Point R2) = R2 (see #65)
, "Diagrams.Backend.Cairo"
, "Diagrams.Backend.Cairo.Internal"
, "Graphics.SVGFonts"
, "Data.Typeable"
]
& DB.pragmas .~ ["DeriveDataTypeable"]
& DB.diaExpr .~ _expression opts
& DB.postProcess .~ (pad 1.1 . centerXY)
& DB.decideRegen .~
(DB.hashedRegenerate
(\hash opts' -> opts' { _cairoFileName = mkFile hash })
(_outDir opts)
)

res <- DB.buildDiagram bopts

case res of
DB.ParseErr err -> do
hPutStrLn stderr ("\nError while parsing\n" ++ src)
hPutStrLn stderr err
return $ Left "Error while parsing"

DB.InterpErr ierr -> do
hPutStrLn stderr ("\nError while interpreting\n" ++ src)
hPutStrLn stderr (DB.ppInterpError ierr)
return $ Left "Error while interpreting"

DB.Skipped hash -> do
hPutStr stderr "."
hFlush stderr
return $ Right (mkFile (DB.hashToHexStr hash))

DB.OK hash out -> do
hPutStr stderr "O"
hFlush stderr
fst out
return $ Right (mkFile (DB.hashToHexStr hash))

where
mkFile base = _outDir opts </> base <.> backendExt
ensureDir dir = do
b <- doesDirectoryExist dir
when (not b) $ createDirectory dir

data Opts = Opts {
_outDir :: FilePath,
_expression :: String
}
main = toJSONFilter defFilter

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")
defFilter :: Maybe Format -> Pandoc -> IO Pandoc
defFilter = pandocFilter (backendFilter id defaultFilters)

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")
75 changes: 59 additions & 16 deletions diagrams-pandoc.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
-- Initial diagrams-pandoc.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/

name: diagrams-pandoc
version: 0.1
synopsis: A pandoc filter to express diagrams inline using the haskell EDSL _diagrams_
Expand All @@ -15,18 +12,64 @@ build-type: Simple
-- extra-source-files:
cabal-version: >=1.10

library
exposed-modules: Diagrams.Pandoc

build-depends: base >=4.2 && < 4.8,
mtl >= 2.1 && < 2.3,
diagrams-lib >= 1.2 && < 1.3,
hint >= 0.4 && < 0.5,
directory,
filepath,
transformers >= 0.3 && < 0.5,
split >= 0.2 && < 0.3,
haskell-src-exts >= 1.16 && < 1.17,
cmdargs >= 0.6 && < 0.11,
lens >= 4.0 && < 4.7,
hashable >= 1.1 && < 1.3,
exceptions >= 0.3 && < 0.7,
temporary >= 1.2 && < 1.3,
diagrams-pgf,
diagrams-svg,
diagrams-rasterific,
linear,
diagrams-builder,
semigroups,
bytestring,
optparse-applicative,
pandoc-types,
pandoc

hs-source-dirs: src
default-language: Haskell2010

executable diagrams-pandoc
main-is: Main.hs
-- other-modules:
-- other-extensions:
build-depends: base >= 4.6 && < 4.8,
pandoc-types >= 1.12 && < 1.13,
diagrams-lib >= 1.0 && < 1.3,
linear >= 1.10 && < 1.16,
diagrams-builder >= 0.5 && < 0.7,
diagrams-cairo >= 1.0 && < 1.3,
directory >= 1.2 && < 1.3,
filepath >= 1.3 && < 1.4,
optparse-applicative >= 0.11 && < 0.12
-- hs-source-dirs:
default-language: Haskell2010
-- other-modules:
-- other-extensions:
build-depends: base >=4.2 && < 4.8,
mtl >= 2.1 && < 2.3,
diagrams-lib >= 1.2 && < 1.3,
hint >= 0.4 && < 0.5,
directory,
filepath,
transformers >= 0.3 && < 0.5,
split >= 0.2 && < 0.3,
haskell-src-exts >= 1.16 && < 1.17,
cmdargs >= 0.6 && < 0.11,
lens >= 4.0 && < 4.7,
hashable >= 1.1 && < 1.3,
exceptions >= 0.3 && < 0.7,
temporary >= 1.2 && < 1.3,
diagrams-pgf,
diagrams-svg,
diagrams-rasterific,
linear,
diagrams-builder,
pandoc-types,
bytestring,
optparse-applicative,
semigroups,
diagrams-pandoc
-- -- hs-source-dirs:
default-language: Haskell2010
Loading