-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cabal file, update pandoc-types for Text, lint
- Loading branch information
Showing
5 changed files
with
104 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,6 @@ | |
!.gitignore | ||
!*.hs | ||
!*.md | ||
!*.cabal | ||
!LICENSE | ||
!example.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,58 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module GraphvizBlock (graphvizBlock) where | ||
|
||
import Data.ByteString.Lazy.UTF8 (fromString) | ||
import Data.Digest.Pure.SHA (sha256, showDigest) | ||
import System.Exit (ExitCode (ExitSuccess)) | ||
import System.Process (readProcessWithExitCode) | ||
import Text.Pandoc | ||
import Data.ByteString.Lazy.UTF8 ( fromString ) | ||
import Data.Digest.Pure.SHA ( sha256 | ||
, showDigest | ||
) | ||
import qualified Data.Text as T | ||
import System.Exit ( ExitCode(ExitSuccess) ) | ||
import System.Process ( readProcessWithExitCode ) | ||
import Text.Pandoc.Definition | ||
|
||
graphvizBlock :: Maybe Format -> Block -> IO Block | ||
graphvizBlock (Just format) (CodeBlock (id, classes, keyvals) content) | ||
| elem "graphviz" classes = | ||
case format of | ||
Format "html" -> | ||
do | ||
(ec, out, err) <- readProcessWithExitCode "dot" ["-Tsvg", "-K" ++ layout] content | ||
return $ case ec of | ||
ExitSuccess -> RawBlock (Format "html") | ||
("<div id=\"" ++ id ++ "\" class=\"graphviz\">" ++ out ++ "</div>") | ||
_ -> CodeBlock (id, classes, keyvals) err | ||
Format "latex" -> | ||
do | ||
(ec, out, err) <- readProcessWithExitCode | ||
"dot2tex" ["--figonly", "--progoptions=-K" ++ layout] content | ||
return $ if length err == 0 | ||
then RawBlock (Format "latex") out | ||
else CodeBlock (id, classes, keyvals) err | ||
_ -> | ||
do | ||
(ec, out, err) <- readProcessWithExitCode | ||
"dot" ["-Tpng", "-K" ++ layout, "-o" ++ filename] content | ||
return $ case ec of | ||
ExitSuccess -> Para [Image (id, [], []) [Str content] (filename, id)] | ||
_ -> CodeBlock (id, classes, keyvals) err | ||
where | ||
uniq = ((showDigest . sha256 . fromString) (layout ++ "/" ++ content)) | ||
filename = "graphviz-" ++ uniq ++ ".png" | ||
where | ||
layout = | ||
case lookup "layout" keyvals of | ||
Just x -> x | ||
_ -> "dot" | ||
graphvizBlock (Just format) (CodeBlock (ident, classes, keyvals) content) | ||
| "graphviz" `elem` classes = case format of | ||
Format "html" -> do | ||
(ec, out, err) <- | ||
readProcessWithExitCode "dot" ["-Tsvg", "-K" <> layout] | ||
$ T.unpack content | ||
return $ case ec of | ||
ExitSuccess -> RawBlock | ||
(Format "html") | ||
( "<div ident=\"" | ||
<> ident | ||
<> "\" class=\"graphviz\">" | ||
<> T.pack out | ||
<> "</div>" | ||
) | ||
_ -> CodeBlock (ident, classes, keyvals) $ T.pack err | ||
Format "latex" -> do | ||
(_, out, err) <- | ||
readProcessWithExitCode | ||
"dot2tex" | ||
["--figonly", "--progoptions=-K" ++ layout] | ||
$ T.unpack content | ||
return $ if null err | ||
then RawBlock (Format "latex") $ T.pack out | ||
else CodeBlock (ident, classes, keyvals) $ T.pack err | ||
_ -> do | ||
(ec, _, err) <- | ||
readProcessWithExitCode | ||
"dot" | ||
["-Tpng", "-K" ++ layout, "-o" ++ filename] | ||
$ T.unpack content | ||
return $ case ec of | ||
ExitSuccess -> Para | ||
[Image (ident, [], []) [Str content] (T.pack filename, ident)] | ||
_ -> CodeBlock (ident, classes, keyvals) $ T.pack err | ||
where | ||
uniq = (showDigest . sha256 . fromString) | ||
(layout <> "/" <> T.unpack content) | ||
filename = "graphviz-" <> uniq <> ".png" | ||
where | ||
layout = T.unpack $ case lookup "layout" keyvals of | ||
Just x -> x | ||
_ -> "dot" | ||
graphvizBlock _ x = return x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import Text.Pandoc.JSON | ||
import GraphvizBlock | ||
|
||
main :: IO () | ||
main = toJSONFilter graphvizBlock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
name: pandoc-filter-graphviz | ||
version: 0.1.0.0 | ||
synopsis: This filter provides Graphviz code block support. | ||
homepage: https://github.com/Hakuyume/pandoc-filter-graphviz | ||
license: BSD3 | ||
license-file: LICENSE | ||
author: Toru Ogawa | ||
copyright: BSD3 | ||
category: Text | ||
build-type: Simple | ||
extra-source-files: README.md | ||
cabal-version: >=2.0 | ||
|
||
executable pandoc-filter-graphviz | ||
main-is: Main.hs | ||
other-modules: | ||
GraphvizBlock | ||
|
||
build-depends: | ||
base == 4.* | ||
, utf8-string ^>= 1.0.1.1 | ||
, SHA ^>= 1.6.4.4 | ||
, text ^>= 1.2.4.0 | ||
, process ^>= 1.6.8.0 | ||
, pandoc-types ^>= 1.20 | ||
|
||
default-language: Haskell2010 | ||
|
||
ghc-options: -Wall | ||
-threaded | ||
-rtsopts | ||
-with-rtsopts=-N | ||
-Wincomplete-uni-patterns | ||
-Wincomplete-record-updates | ||
-Wcompat | ||
-Widentities | ||
-Wredundant-constraints | ||
-fhide-source-paths | ||
-Wmissing-export-lists | ||
-Wpartial-fields |