-
Notifications
You must be signed in to change notification settings - Fork 9
Create a library and add various options #2
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
Changes from all commits
3ba90ab
38b7215
3fe5c62
556d0a2
f6653ca
9dc481e
f3fd95e
a935882
2688551
45dbed0
38c571c
75e1f02
03cab63
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,3 +15,4 @@ TAGS | |
| .diagrams-cache | ||
| /cabal.sandbox.config | ||
| /.cabal-sandbox/ | ||
| .stack* | ||
| 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 |
| 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 | ||
| 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") | ||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this |
||
| -- 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm in favour of |
||
| (Just Above) -> Table [] [AlignLeft] [] [] [[[codeBlock]], [[imgBlock]]] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably be |
||
| Nothing -> Nothing | ||
|
|
||
| -- Copied from https://github.com/diagrams/diagrams-doc/blob/master/doc/Xml2Html.hs | ||
| -- With the CPP removed, thereby requiring Cairo | ||
|
|
@@ -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") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| flags: {} | ||
| packages: | ||
| - '.' | ||
| - ../diagrams-builder | ||
| extra-deps: [] | ||
| resolver: lts-3.2 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| rm -rf images | ||
| rm -rf *.html |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| Here is a square: | ||
|
|
||
| ``` {.diagrams} | ||
| example = square 1 | ||
| ``` | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| Here is a square: | ||
|
|
||
| ``` {.diagrams echo=Above} | ||
| example = square 1 | ||
| ``` | ||
|
|
| 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 |
There was a problem hiding this comment.
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-applicativedependency at all?