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