-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtikz.hs
76 lines (62 loc) · 1.94 KB
/
tikz.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
{-# LANGUAGE OverloadedStrings #-}
module Tikz (
tikzPipeline,
doTikz
) where
import System.IO
import System.Exit
import System.Process
import System.Posix.Files
import System.FilePath.Posix
import Data.String
import Control.Monad
import Text.Pandoc
import Data.ByteString.Lazy.Char8 (pack)
import Data.Digest.Pure.MD5 (md5)
tikzClass = ["commute"]
texEngine = "pdflatex"
{-tex_engine = "lualatex"-}
--tex_engine = "xelatex"
tikzCmd = ["--output-directory=_cache" , "--jobname="]
inkscapeCmd = ["--export-plain-svg" ]
output = "images/"
preamble = readFile "preamble.tex"
postamble = readFile "postamble.tex"
ljoin = makeRelative "." . join
imageBlock :: FilePath -> Block
imageBlock fname = Plain [Image [] ("/" ++ fname, "")]
svgify :: FilePath -> IO Block
svgify file = do
(err, outs, _) <- readProcessWithExitCode "inkscape" [file, "--export-plain-svg", output] ""
case err of
ExitFailure _ -> do
putStrLn "Inkscape Failed"
return $ imageBlock "invalid tikz"
ExitSuccess ->
-- If you want to inline the svg
{-readFile "output.svg"-}
return $ imageBlock output
where
output = replaceExtension file "svg"
texcompile :: String -> IO String
texcompile tex = do
(err, outs, _) <- readProcessWithExitCode texEngine [ "-output-directory", output, "-jobname", jobname] tex
case err of
ExitFailure _ -> do
putStrLn "LaTeX Failed"
hPutStr stderr outs
return ""
ExitSuccess ->
return $ ljoin [output, replaceExtension jobname ".pdf"]
where
jobname = show . md5 $ pack tex
mash :: String -> IO String
mash contents = do
x <- preamble
y <- postamble
return $ concat [x, contents, y]
tikzPipeline :: String -> IO Block
tikzPipeline = mash >=> texcompile >=> svgify
doTikz :: Block -> IO Block
doTikz (CodeBlock (id, ["commute"], namevals) contents) = tikzPipeline contents
doTikz x = return x