Skip to content

Commit

Permalink
Add cabal file, update pandoc-types for Text, lint
Browse files Browse the repository at this point in the history
  • Loading branch information
meck committed Feb 18, 2020
1 parent 5e0ec28 commit f13292c
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 46 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
!.gitignore
!*.hs
!*.md
!*.cabal
!LICENSE
!example.md
90 changes: 53 additions & 37 deletions GraphvizBlock.hs
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
1 change: 1 addition & 0 deletions Main.hs
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
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@ This filter provides Graphviz code block support.
- graphviz
- dot2tex (pdf/LaTeX support)

On Debian, you can install the required packages as follows:
```
$ sudo apt install pandoc graphviz dot2tex ghc libghc-utf8-string-dev libghc-sha-dev libghc-pandoc-dev
```
## Installation
Install using [Cabal](https://www.haskell.org/cabal/) 3.0 or later

## Setup
### Build and install filter

### Build filter
```
$ cd pandoc-filter-graphviz
$ ghc --make Main.hs -o pandoc-filter-graphviz
$ cabal install pandoc-filter-graphviz
```
Note: `-dynamic` option is required in some environments.

## Setup

### Add TikZ to LaTeX template
You can skip this step if you do not use pdf/LaTeX.
Expand Down Expand Up @@ -55,5 +53,5 @@ You can skip this step if you do not use pdf/LaTeX.

## Usage
```
$ pandoc -F ./pandoc-filter-graphviz example.md -o example.html
$ pandoc -F pandoc-filter-graphviz example.md -o example.html
```
40 changes: 40 additions & 0 deletions pandoc-filter-graphviz.cabal
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

0 comments on commit f13292c

Please sign in to comment.