Created
March 5, 2009 15:09
-
-
Save smly/74375 to your computer and use it in GitHub Desktop.
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
module ImgTexPlugin (plugin) where | |
{- | |
This plugin provides a clear math LaTeX output. | |
(latex and dvipng executable must be in the path.) | |
like this: | |
~~~ {.dvipng} | |
\nabla \times \bm{V} | |
= | |
\frac{1}{h_1 h_2 h_3} | |
\begin{vmatrix} | |
h_1 e_1 & h_2 e_2 & h_3 e_3 \\ | |
\frac{\partial}{\partial q_{1}} & | |
\frac{\partial}{\partial q_{2}} & | |
\frac{\partial}{\partial q_{3}} \\ | |
h_1 V_1 & h_2 V_2 & h_3 V_3 | |
\end{vmatrix} | |
~~~ | |
License: GPL | |
written by Kohei Ozaki <eowner at gmail.com> | |
-} | |
import Gitit.Interface | |
import Text.Pandoc.Shared | |
import System.Process (system) | |
import System.Directory | |
import Data.Char (ord) | |
import Data.ByteString.Lazy.UTF8 (fromString) | |
import Data.Digest.Pure.SHA | |
import Data.Generics (everywhereM, mkM) | |
import System.FilePath | |
import Control.Monad.Trans (liftIO) | |
plugin :: Plugin | |
plugin = PageTransform dvipngTransform | |
tmpdir = "/var/tmp" | |
teximgdir = "img" | |
templateHeader = | |
( "\\documentclass[12pt]{article}\n" | |
++ "\\usepackage{amsmath,amssymb,bm}\n" | |
++ "\\begin{document}\n" | |
++ "\\thispagestyle{empty}\n" | |
++ "\\[\n" | |
) | |
templateFooter = | |
( "\n" | |
++ "\\]\n" | |
++ "\\end{document}\n" | |
) | |
dvipngTransform :: AppState -> Pandoc -> Web Pandoc | |
dvipngTransform st = everywhereM (mkM (transformBlock st)) | |
transformBlock :: AppState -> Block -> Web Block | |
transformBlock st (CodeBlock (id, classes, namevals) contents) | |
| "dvipng" `elem` classes = do | |
let outfile = uniqueName contents ++ ".png" | |
liftIO $ do | |
cacheCheck <- doesFileExist (teximgdir </> outfile) | |
case cacheCheck of | |
False -> do | |
curr <- getCurrentDirectory | |
initTempDir outfile | |
writeFile (outfile++".tex") (templateHeader ++ contents ++ templateFooter) | |
system $ "latex " ++ (outfile++".tex") ++ " > /dev/null" | |
setCurrentDirectory curr | |
system $ "dvipng -T tight -bd 1000 -freetype0 -Q 5 --gamma 1.3 " | |
++ tmpdir </> "gitit-tmp-" ++ outfile </> outfile ++ ".dvi" | |
++ " -o " ++ (staticDir (config st) </> teximgdir </> outfile) | |
++ " > /dev/null" | |
finishTempDir outfile | |
True -> return () | |
return $ Para [Image [] ("/" ++ teximgdir </> outfile, "")] | |
transformBlock _ x = return x | |
mkTempDirName :: String -> String | |
mkTempDirName = \s -> tmpdir </> "gitit-tmp-" ++ s | |
initTempDir :: String -> IO () | |
initTempDir = (\f -> createDirectory f >> setCurrentDirectory f) . mkTempDirName | |
finishTempDir :: String -> IO () | |
finishTempDir = removeDirectoryRecursive . mkTempDirName | |
uniqueName :: String -> String | |
uniqueName = showDigest . sha1 . fromString |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment