-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpandoc_filter_external.hs
28 lines (25 loc) · 1.01 KB
/
pandoc_filter_external.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import Data.Monoid( (<>) )
import qualified Data.ByteString.Char8 as B
import Data.IORef( IORef, newIORef, readIORef, writeIORef )
import Text.Pandoc.JSON( Block( CodeBlock, RawBlock, Plain )
, Inline( Image )
, nullAttr
, toJSONFilter )
import Text.AsciiDiagram( parseAsciiDiagram, saveAsciiDiagramAsSvg )
import qualified Data.Text as T
import qualified Data.Text.Encoding as E
import Debug.Trace
main :: IO ()
main = do
counter <- newIORef 0
toJSONFilter (insertDiagrams counter)
insertDiagrams :: IORef Int -> Block -> IO [Block]
insertDiagrams counter (CodeBlock (_ident, classes, _attrs) code)
| "ditaa" `elem` classes || "asciidiagram" `elem` classes = do
c <- readIORef counter
writeIORef counter (c + 1)
let path = ("diagrams/_" <> show c <> ".svg")
saveAsciiDiagramAsSvg path . parseAsciiDiagram $ T.pack code
let img = Image nullAttr [] (path, "")
return [Plain [img]]
insertDiagrams _ block = return [block]