git.iko.soy/typeable/pandoc
on master help sign in
Text.Pandoc.App: ToJSON and FromJSON instances for Opts.
aa1e398John MacFarlane
src/Text/Pandoc/App.hs+22−5
@@ -1,6 +1,7 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE ScopedTypeVariables #-}
2{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE TupleSections #-}
3{-# LANGUAGE TupleSections #-}
4+{-# LANGUAGE DeriveGeneric #-}
4{-
5{-
5Copyright (C) 2006-2017 John MacFarlane <jgm@berkeley.edu>
6Copyright (C) 2006-2017 John MacFarlane <jgm@berkeley.edu>
6
7
@@ -42,12 +43,14 @@ import qualified Control.Exception as E
42import Control.Monad.Except (throwError)
43import Control.Monad.Except (throwError)
43import Control.Monad
44import Control.Monad
44import Control.Monad.Trans
45import Control.Monad.Trans
45−import Data.Aeson (eitherDecode', encode)
46+import Data.Aeson (eitherDecode', encode, ToJSON(..), FromJSON(..),
47+ genericToEncoding, defaultOptions)
46import qualified Data.ByteString as BS
48import qualified Data.ByteString as BS
47import qualified Data.ByteString.Lazy as B
49import qualified Data.ByteString.Lazy as B
48import Data.Char (toLower, toUpper)
50import Data.Char (toLower, toUpper)
49import qualified Data.Set as Set
51import qualified Data.Set as Set
50import Data.Foldable (foldrM)
52import Data.Foldable (foldrM)
53+import GHC.Generics
51import Data.List (intercalate, isPrefixOf, isSuffixOf, sort)
54import Data.List (intercalate, isPrefixOf, isSuffixOf, sort)
52import qualified Data.Map as M
55import qualified Data.Map as M
53import Data.Maybe (fromMaybe, isJust, isNothing)
56import Data.Maybe (fromMaybe, isJust, isNothing)
@@ -65,7 +68,8 @@ import System.Directory (Permissions (..), doesFileExist, findExecutable,
65import System.Environment (getArgs, getEnvironment, getProgName)
68import System.Environment (getArgs, getEnvironment, getProgName)
66import System.Exit (ExitCode (..), exitSuccess)
69import System.Exit (ExitCode (..), exitSuccess)
67import System.FilePath
70import System.FilePath
68−import System.IO (stdout, nativeNewline, Newline(..))
71+import System.IO (stdout, nativeNewline)
72+import qualified System.IO as IO (Newline(..))
69import System.IO.Error (isDoesNotExistError)
73import System.IO.Error (isDoesNotExistError)
70import Text.Pandoc
74import Text.Pandoc
71import Text.Pandoc.Builder (setMeta)
75import Text.Pandoc.Builder (setMeta)
@@ -86,6 +90,12 @@ import System.Posix.IO (stdOutput)
86import System.Posix.Terminal (queryTerminal)
90import System.Posix.Terminal (queryTerminal)
87#endif
91#endif
88
92
93+data Newline = LF | CRLF deriving (Show, Generic)
94+
95+instance ToJSON Newline where
96+ toEncoding = genericToEncoding defaultOptions
97+instance FromJSON Newline
98+
89parseOptions :: [OptDescr (Opt -> IO Opt)] -> Opt -> IO Opt
99parseOptions :: [OptDescr (Opt -> IO Opt)] -> Opt -> IO Opt
90parseOptions options' defaults = do
100parseOptions options' defaults = do
91 rawArgs <- map UTF8.decodeArg <$> getArgs
101 rawArgs <- map UTF8.decodeArg <$> getArgs
@@ -411,7 +421,10 @@ convertWithOpts opts = do
411 return $ ("csl", jatsEncoded) : optMetadata opts
421 return $ ("csl", jatsEncoded) : optMetadata opts
412 else return $ optMetadata opts
422 else return $ optMetadata opts
413
423
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
415
428
416 runIO' $ do
429 runIO' $ do
417 setResourcePath (optResourcePath opts)
430 setResourcePath (optResourcePath opts)
@@ -572,7 +585,11 @@ data Opt = Opt
572 , optIncludeInHeader :: [FilePath] -- ^ Files to include in header
585 , optIncludeInHeader :: [FilePath] -- ^ Files to include in header
573 , optResourcePath :: [FilePath] -- ^ Path to search for images etc
586 , optResourcePath :: [FilePath] -- ^ Path to search for images etc
574 , optEol :: Maybe Newline -- ^ Enforce line-endings
587 , 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
576
593
577-- | Defaults for command-line options.
594-- | Defaults for command-line options.
578defaultOpts :: Opt
595defaultOpts :: Opt
@@ -790,7 +807,7 @@ writeFnBinary :: MonadIO m => FilePath -> B.ByteString -> m ()
790writeFnBinary "-" = liftIO . B.putStr
807writeFnBinary "-" = liftIO . B.putStr
791writeFnBinary f = liftIO . B.writeFile (UTF8.encodePath f)
808writeFnBinary f = liftIO . B.writeFile (UTF8.encodePath f)
792
809
793−writerFn :: MonadIO m => Newline -> FilePath -> String -> m ()
810+writerFn :: MonadIO m => IO.Newline -> FilePath -> String -> m ()
794writerFn eol "-" = liftIO . UTF8.putStrWith eol
811writerFn eol "-" = liftIO . UTF8.putStrWith eol
795writerFn eol f = liftIO . UTF8.writeFileWith eol f
812writerFn eol f = liftIO . UTF8.writeFileWith eol f
796
813
src/Text/Pandoc/Logging.hs+10
@@ -39,6 +39,7 @@ module Text.Pandoc.Logging (
39 , messageVerbosity
39 , messageVerbosity
40 ) where
40 ) where
41
41
42+import Control.Monad (mzero)
42import Data.Aeson
43import Data.Aeson
43import Data.Aeson.Encode.Pretty (Config (..), defConfig, encodePretty',
44import Data.Aeson.Encode.Pretty (Config (..), defConfig, encodePretty',
44 keyOrder)
45 keyOrder)
@@ -56,6 +57,15 @@ data Verbosity = ERROR | WARNING | INFO | DEBUG
56
57
57instance ToJSON Verbosity where
58instance ToJSON Verbosity where
58 toJSON x = toJSON (show x)
59 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
59
69
60data LogMessage =
70data LogMessage =
61 SkippedContent String SourcePos
71 SkippedContent String SourcePos
src/Text/Pandoc/Options.hs+34
@@ -45,6 +45,8 @@ module Text.Pandoc.Options ( module Text.Pandoc.Extensions
45 , def
45 , def
46 , isEnabled
46 , isEnabled
47 ) where
47 ) where
48+import Data.Aeson (ToJSON(..), FromJSON(..),
49+ genericToEncoding, defaultOptions)
48import Data.Data (Data)
50import Data.Data (Data)
49import Data.Default
51import Data.Default
50import qualified Data.Set as Set
52import qualified Data.Set as Set
@@ -104,17 +106,29 @@ data HTMLMathMethod = PlainMath
104 | KaTeX String String -- url of stylesheet and katex.js
106 | KaTeX String String -- url of stylesheet and katex.js
105 deriving (Show, Read, Eq, Data, Typeable, Generic)
107 deriving (Show, Read, Eq, Data, Typeable, Generic)
106
108
109+instance ToJSON HTMLMathMethod where
110+ toEncoding = genericToEncoding defaultOptions
111+instance FromJSON HTMLMathMethod
112+
107data CiteMethod = Citeproc -- use citeproc to render them
113data CiteMethod = Citeproc -- use citeproc to render them
108 | Natbib -- output natbib cite commands
114 | Natbib -- output natbib cite commands
109 | Biblatex -- output biblatex cite commands
115 | Biblatex -- output biblatex cite commands
110 deriving (Show, Read, Eq, Data, Typeable, Generic)
116 deriving (Show, Read, Eq, Data, Typeable, Generic)
111
117
118+instance ToJSON CiteMethod where
119+ toEncoding = genericToEncoding defaultOptions
120+instance FromJSON CiteMethod
121+
112-- | Methods for obfuscating email addresses in HTML.
122-- | Methods for obfuscating email addresses in HTML.
113data ObfuscationMethod = NoObfuscation
123data ObfuscationMethod = NoObfuscation
114 | ReferenceObfuscation
124 | ReferenceObfuscation
115 | JavascriptObfuscation
125 | JavascriptObfuscation
116 deriving (Show, Read, Eq, Data, Typeable, Generic)
126 deriving (Show, Read, Eq, Data, Typeable, Generic)
117
127
128+instance ToJSON ObfuscationMethod where
129+ toEncoding = genericToEncoding defaultOptions
130+instance FromJSON ObfuscationMethod
131+
118-- | Varieties of HTML slide shows.
132-- | Varieties of HTML slide shows.
119data HTMLSlideVariant = S5Slides
133data HTMLSlideVariant = S5Slides
120 | SlidySlides
134 | SlidySlides
@@ -124,18 +138,30 @@ data HTMLSlideVariant = S5Slides
124 | NoSlides
138 | NoSlides
125 deriving (Show, Read, Eq, Data, Typeable, Generic)
139 deriving (Show, Read, Eq, Data, Typeable, Generic)
126
140
141+instance ToJSON HTMLSlideVariant where
142+ toEncoding = genericToEncoding defaultOptions
143+instance FromJSON HTMLSlideVariant
144+
127-- | Options for accepting or rejecting MS Word track-changes.
145-- | Options for accepting or rejecting MS Word track-changes.
128data TrackChanges = AcceptChanges
146data TrackChanges = AcceptChanges
129 | RejectChanges
147 | RejectChanges
130 | AllChanges
148 | AllChanges
131 deriving (Show, Read, Eq, Data, Typeable, Generic)
149 deriving (Show, Read, Eq, Data, Typeable, Generic)
132
150
151+instance ToJSON TrackChanges where
152+ toEncoding = genericToEncoding defaultOptions
153+instance FromJSON TrackChanges
154+
133-- | Options for wrapping text in the output.
155-- | Options for wrapping text in the output.
134data WrapOption = WrapAuto -- ^ Automatically wrap to width
156data WrapOption = WrapAuto -- ^ Automatically wrap to width
135 | WrapNone -- ^ No non-semantic newlines
157 | WrapNone -- ^ No non-semantic newlines
136 | WrapPreserve -- ^ Preserve wrapping of input source
158 | WrapPreserve -- ^ Preserve wrapping of input source
137 deriving (Show, Read, Eq, Data, Typeable, Generic)
159 deriving (Show, Read, Eq, Data, Typeable, Generic)
138
160
161+instance ToJSON WrapOption where
162+ toEncoding = genericToEncoding defaultOptions
163+instance FromJSON WrapOption
164+
139-- | Options defining the type of top-level headers.
165-- | Options defining the type of top-level headers.
140data TopLevelDivision = TopLevelPart -- ^ Top-level headers become parts
166data TopLevelDivision = TopLevelPart -- ^ Top-level headers become parts
141 | TopLevelChapter -- ^ Top-level headers become chapters
167 | TopLevelChapter -- ^ Top-level headers become chapters
@@ -144,12 +170,20 @@ data TopLevelDivision = TopLevelPart -- ^ Top-level headers become parts
144 -- heuristics
170 -- heuristics
145 deriving (Show, Read, Eq, Data, Typeable, Generic)
171 deriving (Show, Read, Eq, Data, Typeable, Generic)
146
172
173+instance ToJSON TopLevelDivision where
174+ toEncoding = genericToEncoding defaultOptions
175+instance FromJSON TopLevelDivision
176+
147-- | Locations for footnotes and references in markdown output
177-- | Locations for footnotes and references in markdown output
148data ReferenceLocation = EndOfBlock -- ^ End of block
178data ReferenceLocation = EndOfBlock -- ^ End of block
149 | EndOfSection -- ^ prior to next section header (or end of document)
179 | EndOfSection -- ^ prior to next section header (or end of document)
150 | EndOfDocument -- ^ at end of document
180 | EndOfDocument -- ^ at end of document
151 deriving (Show, Read, Eq, Data, Typeable, Generic)
181 deriving (Show, Read, Eq, Data, Typeable, Generic)
152
182
183+instance ToJSON ReferenceLocation where
184+ toEncoding = genericToEncoding defaultOptions
185+instance FromJSON ReferenceLocation
186+
153-- | Options for writers
187-- | Options for writers
154data WriterOptions = WriterOptions
188data WriterOptions = WriterOptions
155 { writerTemplate :: Maybe String -- ^ Template to use
189 { writerTemplate :: Maybe String -- ^ Template to use