git.iko.soy/typeable/pandoc
on commonmark-github help sign in
66 lines 2.7 kB history raw
1{-# LANGUAGE OverloadedStrings #-}
2module Main where
3import Network.Wai.Handler.CGI
4import Network.Wai
5import Control.Applicative ((<$>))
6import Data.Maybe (fromMaybe)
7import Network.HTTP.Types.Status (status200)
8import Network.HTTP.Types.Header (hContentType)
9import Network.HTTP.Types.URI (queryToQueryText)
10import Text.Pandoc
11import Text.Pandoc.Writers.Math (defaultMathJaxURL)
12import Text.Pandoc.Highlighting (pygments)
13import Text.Pandoc.Readers (getReader, Reader(..))
14import Text.Pandoc.Writers (getWriter, Writer(..))
15import Text.Pandoc.Shared (tabFilter)
16import Data.Aeson
17import qualified Data.Text as T
18import Data.Text (Text)
20main :: IO ()
21main = run app
23app :: Application
24app req respond = do
25 let query = queryToQueryText $ queryString req
26 let getParam x = maybe (error $ T.unpack x ++ " paramater not set")
27 return $ lookup x query
28 text <- getParam "text" >>= checkLength . fromMaybe T.empty
29 fromFormat <- fromMaybe "" <$> getParam "from"
30 toFormat <- fromMaybe "" <$> getParam "to"
31 let reader = case getReader (T.unpack fromFormat) of
32 Right (TextReader r, es) -> r readerOpts{
33 readerExtensions = es }
34 _ -> error $ "could not find reader for "
35 ++ T.unpack fromFormat
36 let writer = case getWriter (T.unpack toFormat) of
37 Right (TextWriter w, es) -> w writerOpts{
38 writerExtensions = es }
39 _ -> error $ "could not find writer for " ++
40 T.unpack toFormat
41 let result = case runPure $ reader (tabFilter 4 text) >>= writer of
42 Right s -> s
43 Left err -> error (show err)
44 let output = encode $ object [ T.pack "html" .= result
45 , T.pack "name" .=
46 if fromFormat == "markdown_strict"
47 then T.pack "pandoc (strict)"
48 else T.pack "pandoc"
49 , T.pack "version" .= pandocVersion]
50 respond $ responseLBS status200 [(hContentType,"text/json; charset=UTF-8")] output
52checkLength :: Text -> IO Text
53checkLength t =
54 if T.length t > 10000
55 then error "exceeds length limit of 10,000 characters"
56 else return t
58writerOpts :: WriterOptions
59writerOpts = def { writerReferenceLinks = True,
60 writerEmailObfuscation = NoObfuscation,
61 writerHTMLMathMethod = MathJax (defaultMathJaxURL ++
62 "MathJax.js?config=TeX-AMS_CHTML-full"),
63 writerHighlightStyle = Just pygments }
65readerOpts :: ReaderOptions
66readerOpts = def