Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 72 lines (60 sloc) 2.092 kB
1a3c231 @jgm Changed plugin names, made them all work.
authored
1 module ImgTex (plugin) where
3b3b480 @jgm Added plugins/ImgTexPlugins.hs (thanks to Kohei OZAKI).
authored
2 {-
3
4 This plugin provides a clear math LaTeX output.
5 (* latex and dvipng executable must be in the path.)
6
7 like this:
8
9 ~~~ {.dvipng}
10 \nabla \times \bm{V}
11 =
12 \frac{1}{h_1 h_2 h_3}
13 \begin{vmatrix}
14 h_1 e_1 & h_2 e_2 & h_3 e_3 \\
15 \frac{\partial}{\partial q_{1}} &
16 \frac{\partial}{\partial q_{2}} &
17 \frac{\partial}{\partial q_{3}} \\
18 h_1 V_1 & h_2 V_2 & h_3 V_3
19 \end{vmatrix}
20 ~~~
21
22 License: GPL
23 written by Kohei OZAKI <i@smly.org>
1a3c231 @jgm Changed plugin names, made them all work.
authored
24 modified by John MacFarlane to use withTempDir
3b3b480 @jgm Added plugins/ImgTexPlugins.hs (thanks to Kohei OZAKI).
authored
25
26 -}
27
3da0cc8 @jgm Fixed plugins to use new Network.Gitit prefix.
authored
28 import Network.Gitit.Interface
3b3b480 @jgm Added plugins/ImgTexPlugins.hs (thanks to Kohei OZAKI).
authored
29 import System.Process (system)
30 import System.Directory
31 import Data.ByteString.Lazy.UTF8 (fromString)
32 import Data.Digest.Pure.SHA
33 import System.FilePath
34 import Control.Monad.Trans (liftIO)
35
36 plugin :: Plugin
0b4d172 Modified plugins for new API.
John MacFarlane authored
37 plugin = mkPageTransformM transformBlock
3b3b480 @jgm Added plugins/ImgTexPlugins.hs (thanks to Kohei OZAKI).
authored
38
112e8b0 @jgm Plugins: hlint and import cleanup (gwern).
authored
39 templateHeader, templateFooter :: String
40 templateHeader = concat
41 [ "\\documentclass[12pt]{article}\n"
42 , "\\usepackage{amsmath,amssymb,bm}\n"
43 , "\\begin{document}\n"
44 , "\\thispagestyle{empty}\n"
45 , "\\[\n"]
3b3b480 @jgm Added plugins/ImgTexPlugins.hs (thanks to Kohei OZAKI).
authored
46
47 templateFooter =
112e8b0 @jgm Plugins: hlint and import cleanup (gwern).
authored
48 "\n"
3b3b480 @jgm Added plugins/ImgTexPlugins.hs (thanks to Kohei OZAKI).
authored
49 ++ "\\]\n"
50 ++ "\\end{document}\n"
51
1a3c231 @jgm Changed plugin names, made them all work.
authored
52 transformBlock :: Block -> PluginM Block
112e8b0 @jgm Plugins: hlint and import cleanup (gwern).
authored
53 transformBlock (CodeBlock (_, classes, namevals) contents)
3b3b480 @jgm Added plugins/ImgTexPlugins.hs (thanks to Kohei OZAKI).
authored
54 | "dvipng" `elem` classes = do
1a3c231 @jgm Changed plugin names, made them all work.
authored
55 cfg <- askConfig
3b3b480 @jgm Added plugins/ImgTexPlugins.hs (thanks to Kohei OZAKI).
authored
56 let (name, outfile) = case lookup "name" namevals of
57 Just fn -> ([Str fn], fn ++ ".png")
58 Nothing -> ([], uniqueName contents ++ ".png")
1a3c231 @jgm Changed plugin names, made them all work.
authored
59 curr <- liftIO getCurrentDirectory
60 liftIO $ withTempDir "gitit-imgtex" $ \tmpdir -> do
61 setCurrentDirectory tmpdir
62 writeFile (outfile ++ ".tex") (templateHeader ++ contents ++ templateFooter)
63 system $ "latex " ++ outfile ++ ".tex > /dev/null"
3b3b480 @jgm Added plugins/ImgTexPlugins.hs (thanks to Kohei OZAKI).
authored
64 setCurrentDirectory curr
1a3c231 @jgm Changed plugin names, made them all work.
authored
65 system $ "dvipng -T tight -bd 1000 -freetype0 -Q 5 --gamma 1.3 " ++
66 (tmpdir </> outfile <.> "dvi") ++ " -o " ++ (staticDir cfg </> "img" </> outfile)
db846da @jgm Updated plugins for new static root (/ not /_static/).
authored
67 return $ Para [Image name ("/img" </> outfile, "")]
299e19a Simplified plugin API.
John MacFarlane authored
68 transformBlock x = return x
3b3b480 @jgm Added plugins/ImgTexPlugins.hs (thanks to Kohei OZAKI).
authored
69
70 uniqueName :: String -> String
71 uniqueName = showDigest . sha1 . fromString
Something went wrong with that request. Please try again.