1{-# LANGUAGE NoImplicitPrelude #-}
2{-# LANGUAGE OverloadedStrings #-}
5 Copyright : © 2014-2020 John MacFarlane <jgm@berkeley.edu>
6 License : GNU GPL, version 2 or above
8 Maintainer : John MacFarlane <jgm@berkeley.edu>
10 Portability : portable
12Provides a webservice which allows to try pandoc in the browser.
16import Network.Wai.Handler.CGI
18import Control.Applicative ((<$>))
19import Data.Maybe (fromMaybe)
20import Network.HTTP.Types.Status (status200)
21import Network.HTTP.Types.Header (hContentType)
22import Network.HTTP.Types.URI (queryToQueryText)
24import Text.Pandoc.Writers.Math (defaultMathJaxURL)
25import Text.Pandoc.Highlighting (pygments)
26import Text.Pandoc.Readers (getReader, Reader(..))
27import Text.Pandoc.Writers (getWriter, Writer(..))
28import Text.Pandoc.Shared (tabFilter)
30import qualified Data.Text as T
31import Data.Text (Text)
38 let query = queryToQueryText $ queryString req
39 let getParam x = maybe (error $ T.unpack x ++ " parameter not set")
40 return $ lookup x query
41 text <- getParam "text" >>= checkLength . fromMaybe T.empty
42 fromFormat <- fromMaybe "" <$> getParam "from"
43 toFormat <- fromMaybe "" <$> getParam "to"
44 standalone <- (==) "1" . fromMaybe "" <$> getParam "standalone"
45 compiledTemplate <- runIO . compileDefaultTemplate $ toFormat
46 let template = if standalone then either (const Nothing) Just compiledTemplate else Nothing
47 let reader = case runPure $ getReader fromFormat of
48 Right (TextReader r, es) -> r readerOpts{
49 readerExtensions = es }
50 _ -> error $ "could not find reader for "
51 ++ T.unpack fromFormat
52 let writer = case runPure $ getWriter toFormat of
53 Right (TextWriter w, es) -> w writerOpts{
54 writerExtensions = es, writerTemplate = template }
55 _ -> error $ "could not find writer for " ++
57 let result = case runPure $ reader (tabFilter 4 text) >>= writer of
59 Left err -> error (show err)
60 let output = encode $ object [ T.pack "html" .= result
62 if fromFormat == "markdown_strict"
63 then T.pack "pandoc (strict)"
65 , T.pack "version" .= pandocVersion]
66 respond $ responseLBS status200 [(hContentType,"text/json; charset=UTF-8")] output
68checkLength :: Text -> IO Text
71 then error "exceeds length limit of 10,000 characters"
74writerOpts :: WriterOptions
75writerOpts = def { writerReferenceLinks = True,
76 writerEmailObfuscation = NoObfuscation,
77 writerHTMLMathMethod = MathJax (defaultMathJaxURL <>
78 T.pack "tex-mml-chtml.js"),
79 writerHighlightStyle = Just pygments }
81readerOpts :: ReaderOptions
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list