git.iko.soy/typeable/pandoc
on master help sign in
T.P.App.Opt: custom FromJSON instance for LineEnding.
6e537aeJohn MacFarlane
src/Text/Pandoc/App/Opt.hs+17−8
@@ -1,3 +1,4 @@
1+{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE NoImplicitPrelude #-}
2{-# LANGUAGE NoImplicitPrelude #-}
2{-# LANGUAGE CPP #-}
3{-# LANGUAGE CPP #-}
3{-# LANGUAGE DeriveGeneric #-}
4{-# LANGUAGE DeriveGeneric #-}
@@ -31,16 +32,29 @@ import Text.Pandoc.Options (TopLevelDivision (TopLevelDefault),
31 ObfuscationMethod (NoObfuscation),
32 ObfuscationMethod (NoObfuscation),
32 CiteMethod (Citeproc))
33 CiteMethod (Citeproc))
33import Text.Pandoc.Shared (camelCaseToHyphenated)
34import Text.Pandoc.Shared (camelCaseToHyphenated)
35+import qualified Data.Text as T
36+import Data.Aeson (defaultOptions, Options(..), FromJSON(..), ToJSON(..),
37+ Value(..))
34#ifdef DERIVE_JSON_VIA_TH
38#ifdef DERIVE_JSON_VIA_TH
35−import Data.Aeson.TH (deriveJSON, defaultOptions, Options(..))
39+import Data.Aeson.TH (deriveJSON)
36#else
40#else
37−import Data.Aeson (FromJSON (..), ToJSON (..), Options(..)
38− defaultOptions, genericToEncoding)
41+import Data.Aeson (genericToEncoding)
39#endif
42#endif
40
43
41-- | The type of line-endings to be used when writing plain-text.
44-- | The type of line-endings to be used when writing plain-text.
42data LineEnding = LF | CRLF | Native deriving (Show, Generic)
45data LineEnding = LF | CRLF | Native deriving (Show, Generic)
43
46
47+instance ToJSON LineEnding
48+
49+instance FromJSON LineEnding where
50+ parseJSON (String t) =
51+ case T.toLower t of
52+ "lf" -> return LF
53+ "crlf" -> return CRLF
54+ "native" -> return Native
55+ _ -> fail "Expecting LF, CRLF, or Native"
56+ parseJSON _ = fail "Expecting string"
57+
44-- | Data structure for command line options.
58-- | Data structure for command line options.
45data Opt = Opt
59data Opt = Opt
46 { optTabStop :: Int -- ^ Number of spaces per tab
60 { optTabStop :: Int -- ^ Number of spaces per tab
@@ -191,15 +205,10 @@ defaultOpts = Opt
191#ifdef DERIVE_JSON_VIA_TH
205#ifdef DERIVE_JSON_VIA_TH
192-- see https://github.com/jgm/pandoc/pull/4083
206-- see https://github.com/jgm/pandoc/pull/4083
193-- using generic deriving caused long compilation times
207-- using generic deriving caused long compilation times
194−$(deriveJSON defaultOptions ''LineEnding)
195$(deriveJSON
208$(deriveJSON
196 defaultOptions{ fieldLabelModifier =
209 defaultOptions{ fieldLabelModifier =
197 camelCaseToHyphenated . drop 3 } ''Opt)
210 camelCaseToHyphenated . drop 3 } ''Opt)
198#else
211#else
199−instance ToJSON LineEnding where
200− toEncoding = genericToEncoding defaultOptions
201−instance FromJSON LineEnding
202−
203instance ToJSON Opt where
212instance ToJSON Opt where
204 toEncoding = genericToEncoding
213 toEncoding = genericToEncoding
205 defaultOptions{ fieldLabelModifier =
214 defaultOptions{ fieldLabelModifier =