Skip to content

Instantly share code, notes, and snippets.

@rwst
Forked from jgm/gist:1431411
Created December 6, 2011 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwst/1437841 to your computer and use it in GitHub Desktop.
Save rwst/1437841 to your computer and use it in GitHub Desktop.
shell for script to turn pandoc math to svg
-- 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