git.iko.soy/typeable/pandoc
on master help sign in
Text.Pandoc.App: ToJSON and FromJSON instances for Opts.

This can be used e.g. to pass options via web interface, such as trypandoc.

commitaa1e39858dd0ad25fd5e0cf0e2e19182bd4f157b authorJohn MacFarlane <jgm@berkeley.edu> dateSun 21 May 2017, 11:42 (9 years ago) parent8c1b81b
3 files changed+66−5
src/Text/Pandoc/App.hs+22−5 src/Text/Pandoc/Logging.hs+10 src/Text/Pandoc/Options.hs+34
src/Text/Pandoc/App.hs+22−5
@@ -1,6 +1,7 @@
11{-# LANGUAGE CPP #-}
22{-# LANGUAGE ScopedTypeVariables #-}
33{-# LANGUAGE TupleSections #-}
4+{-# LANGUAGE DeriveGeneric #-}
45{-
56Copyright (C) 2006-2017 John MacFarlane <jgm@berkeley.edu>
67
@@ -42,12 +43,14 @@ import qualified Control.Exception as E
4243import Control.Monad.Except (throwError)
4344import Control.Monad
4445import Control.Monad.Trans
45−import Data.Aeson (eitherDecode', encode)
46+import Data.Aeson (eitherDecode', encode, ToJSON(..), FromJSON(..),
47+ genericToEncoding, defaultOptions)
4648import qualified Data.ByteString as BS
4749import qualified Data.ByteString.Lazy as B
4850import Data.Char (toLower, toUpper)
4951import qualified Data.Set as Set
5052import Data.Foldable (foldrM)
53+import GHC.Generics
5154import Data.List (intercalate, isPrefixOf, isSuffixOf, sort)
5255import qualified Data.Map as M
5356import Data.Maybe (fromMaybe, isJust, isNothing)
@@ -65,7 +68,8 @@ import System.Directory (Permissions (..), doesFileExist, findExecutable,
6568import System.Environment (getArgs, getEnvironment, getProgName)
6669import System.Exit (ExitCode (..), exitSuccess)
6770import System.FilePath
68−import System.IO (stdout, nativeNewline, Newline(..))
71+import System.IO (stdout, nativeNewline)
72+import qualified System.IO as IO (Newline(..))
6973import System.IO.Error (isDoesNotExistError)
7074import Text.Pandoc
7175import Text.Pandoc.Builder (setMeta)
@@ -86,6 +90,12 @@ import System.Posix.IO (stdOutput)
8690import System.Posix.Terminal (queryTerminal)
8791#endif
8892
93+data Newline = LF | CRLF deriving (Show, Generic)
94+
95+instance ToJSON Newline where
96+ toEncoding = genericToEncoding defaultOptions
97+instance FromJSON Newline
98+
8999parseOptions :: [OptDescr (Opt -> IO Opt)] -> Opt -> IO Opt
90100parseOptions options' defaults = do
91101 rawArgs <- map UTF8.decodeArg <$> getArgs
@@ -411,7 +421,10 @@ convertWithOpts opts = do
411421 return $ ("csl", jatsEncoded) : optMetadata opts
412422 else return $ optMetadata opts
413423
414− let eol = fromMaybe nativeNewline $ optEol opts
424+ let eol = case optEol opts of
425+ Just CRLF -> IO.CRLF
426+ Just LF -> IO.LF
427+ Nothing -> nativeNewline
415428
416429 runIO' $ do
417430 setResourcePath (optResourcePath opts)
@@ -572,7 +585,11 @@ data Opt = Opt
572585 , optIncludeInHeader :: [FilePath] -- ^ Files to include in header
573586 , optResourcePath :: [FilePath] -- ^ Path to search for images etc
574587 , optEol :: Maybe Newline -- ^ Enforce line-endings
575− }
588+ } deriving (Generic, Show)
589+
590+instance ToJSON Opt where
591+ toEncoding = genericToEncoding defaultOptions
592+instance FromJSON Opt
576593
577594-- | Defaults for command-line options.
578595defaultOpts :: Opt
@@ -790,7 +807,7 @@ writeFnBinary :: MonadIO m => FilePath -> B.ByteString -> m ()
790807writeFnBinary "-" = liftIO . B.putStr
791808writeFnBinary f = liftIO . B.writeFile (UTF8.encodePath f)
792809
793−writerFn :: MonadIO m => Newline -> FilePath -> String -> m ()
810+writerFn :: MonadIO m => IO.Newline -> FilePath -> String -> m ()
794811writerFn eol "-" = liftIO . UTF8.putStrWith eol
795812writerFn eol f = liftIO . UTF8.writeFileWith eol f
796813
src/Text/Pandoc/Logging.hs+10
@@ -39,6 +39,7 @@ module Text.Pandoc.Logging (
3939 , messageVerbosity
4040 ) where
4141
42+import Control.Monad (mzero)
4243import Data.Aeson
4344import Data.Aeson.Encode.Pretty (Config (..), defConfig, encodePretty',
4445 keyOrder)
@@ -56,6 +57,15 @@ data Verbosity = ERROR | WARNING | INFO | DEBUG
5657
5758instance ToJSON Verbosity where
5859 toJSON x = toJSON (show x)
60+instance FromJSON Verbosity where
61+ parseJSON (String t) =
62+ case t of
63+ "ERROR" -> return ERROR
64+ "WARNING" -> return WARNING
65+ "INFO" -> return INFO
66+ "DEBUG" -> return DEBUG
67+ _ -> mzero
68+ parseJSON _ = mzero
5969
6070data LogMessage =
6171 SkippedContent String SourcePos
src/Text/Pandoc/Options.hs+34
@@ -45,6 +45,8 @@ module Text.Pandoc.Options ( module Text.Pandoc.Extensions
4545 , def
4646 , isEnabled
4747 ) where
48+import Data.Aeson (ToJSON(..), FromJSON(..),
49+ genericToEncoding, defaultOptions)
4850import Data.Data (Data)
4951import Data.Default
5052import qualified Data.Set as Set
@@ -104,17 +106,29 @@ data HTMLMathMethod = PlainMath
104106 | KaTeX String String -- url of stylesheet and katex.js
105107 deriving (Show, Read, Eq, Data, Typeable, Generic)
106108
109+instance ToJSON HTMLMathMethod where
110+ toEncoding = genericToEncoding defaultOptions
111+instance FromJSON HTMLMathMethod
112+
107113data CiteMethod = Citeproc -- use citeproc to render them
108114 | Natbib -- output natbib cite commands
109115 | Biblatex -- output biblatex cite commands
110116 deriving (Show, Read, Eq, Data, Typeable, Generic)
111117
118+instance ToJSON CiteMethod where
119+ toEncoding = genericToEncoding defaultOptions
120+instance FromJSON CiteMethod
121+
112122-- | Methods for obfuscating email addresses in HTML.
113123data ObfuscationMethod = NoObfuscation
114124 | ReferenceObfuscation
115125 | JavascriptObfuscation
116126 deriving (Show, Read, Eq, Data, Typeable, Generic)
117127
128+instance ToJSON ObfuscationMethod where
129+ toEncoding = genericToEncoding defaultOptions
130+instance FromJSON ObfuscationMethod
131+
118132-- | Varieties of HTML slide shows.
119133data HTMLSlideVariant = S5Slides
120134 | SlidySlides
@@ -124,18 +138,30 @@ data HTMLSlideVariant = S5Slides
124138 | NoSlides
125139 deriving (Show, Read, Eq, Data, Typeable, Generic)
126140
141+instance ToJSON HTMLSlideVariant where
142+ toEncoding = genericToEncoding defaultOptions
143+instance FromJSON HTMLSlideVariant
144+
127145-- | Options for accepting or rejecting MS Word track-changes.
128146data TrackChanges = AcceptChanges
129147 | RejectChanges
130148 | AllChanges
131149 deriving (Show, Read, Eq, Data, Typeable, Generic)
132150
151+instance ToJSON TrackChanges where
152+ toEncoding = genericToEncoding defaultOptions
153+instance FromJSON TrackChanges
154+
133155-- | Options for wrapping text in the output.
134156data WrapOption = WrapAuto -- ^ Automatically wrap to width
135157 | WrapNone -- ^ No non-semantic newlines
136158 | WrapPreserve -- ^ Preserve wrapping of input source
137159 deriving (Show, Read, Eq, Data, Typeable, Generic)
138160
161+instance ToJSON WrapOption where
162+ toEncoding = genericToEncoding defaultOptions
163+instance FromJSON WrapOption
164+
139165-- | Options defining the type of top-level headers.
140166data TopLevelDivision = TopLevelPart -- ^ Top-level headers become parts
141167 | TopLevelChapter -- ^ Top-level headers become chapters
@@ -144,12 +170,20 @@ data TopLevelDivision = TopLevelPart -- ^ Top-level headers become parts
144170 -- heuristics
145171 deriving (Show, Read, Eq, Data, Typeable, Generic)
146172
173+instance ToJSON TopLevelDivision where
174+ toEncoding = genericToEncoding defaultOptions
175+instance FromJSON TopLevelDivision
176+
147177-- | Locations for footnotes and references in markdown output
148178data ReferenceLocation = EndOfBlock -- ^ End of block
149179 | EndOfSection -- ^ prior to next section header (or end of document)
150180 | EndOfDocument -- ^ at end of document
151181 deriving (Show, Read, Eq, Data, Typeable, Generic)
152182
183+instance ToJSON ReferenceLocation where
184+ toEncoding = genericToEncoding defaultOptions
185+instance FromJSON ReferenceLocation
186+
153187-- | Options for writers
154188data WriterOptions = WriterOptions
155189 { writerTemplate :: Maybe String -- ^ Template to use