-
-
Save rwst/1437841 to your computer and use it in GitHub Desktop.
shell for script to turn pandoc math to svg
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
-- math2svg.hs | |
-- compile with: | |
-- ghc --make math2svg.hs | |
-- run using: | |
-- pandoc -t json -s | ./math2svg | pandoc -f json | |
mport Text.Pandoc | |
import Text.JSON.Generic | |
import System.Cmd | |
import System.Posix.Directory | |
import System.IO | |
import System.IO.Temp | |
import System.Process | |
main = getContents >>= transform . decodeJSON >>= putStrLn . encodeJSON | |
transform :: Pandoc -> IO Pandoc | |
transform = bottomUpM mathToSvg | |
mathToSvg :: Inline -> IO Inline | |
mathToSvg (Math mathType x) = do | |
let delim = case mathType of | |
InlineMath -> "$" | |
DisplayMath -> "$$" | |
withSystemTempDirectory "pandoc." $ \tmpDir -> | |
do | |
changeWorkingDirectory tmpDir | |
phandle <- runProcess "tex" | |
[("\\nopagenumbers " ++ delim ++ filter (/='\n') x ++ delim ++ " \\bye")] | |
Nothing Nothing Nothing (Just stderr) Nothing | |
waitForProcess phandle | |
shandle <- runProcess "dvisvgm" | |
["-e", "-n", "texput.dvi"] | |
Nothing Nothing Nothing Nothing Nothing | |
waitForProcess shandle | |
svg <- readFile "texput.svg" | |
return (RawInline "html" svg) | |
mathToSvg x = return x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment