File: texmath-cgi.hs

package info (click to toggle)
haskell-texmath 0.6.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 780 kB
  • ctags: 2
  • sloc: haskell: 1,726; sh: 32; makefile: 4
file content (27 lines) | stat: -rw-r--r-- 1,006 bytes parent folder | download
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
module Main where
import Network.CGI
import Text.XML.Light
import Text.TeXMath
import Text.TeXMath.Macros
import Data.Maybe (fromMaybe)
import Control.Monad
import Text.JSON
import Codec.Binary.UTF8.String (decodeString, encodeString)

cgiMain :: CGI CGIResult
cgiMain = do
  setHeader "Content-type" "text/xhtml; charset=UTF-8"
  latexFormula <- liftM (decodeString . fromMaybe "") $ getInput "latexFormula"
  rawMacros <- liftM (decodeString . fromMaybe "") $ getInput "macros"
  let (ms, _) = parseMacroDefinitions rawMacros
  output . encodeStrict $
    case texMathToMathML DisplayBlock (applyMacros (reverse ms) latexFormula) of
         Left e  -> toJSObject [("success", JSBool False)
                               , ("error", JSString $ toJSString e)]
         Right v -> toJSObject [("success", JSBool True)
                               , ("mathml", JSString $ toJSString $ encodeString $
                                     ppElement v)]

main :: IO ()
main = runCGI $ handleErrors cgiMain