git.iko.soy/typeable/pandoc
on master help sign in
Add cabal flag `derive_json_via_th`
dc150dfAlbert Krewinkel
pandoc.cabal+7
@@ -337,6 +337,11 @@ flag embed_data_files
337 Description: Embed data files in binary for relocatable executable.
337 Description: Embed data files in binary for relocatable executable.
338 Default: False
338 Default: False
339
339
340+flag derive_json_via_th
341+ Description: Use Template Haskell instead of GHC Generics to derive ToJSON
342+ and FromJSON instances.
343+ Default: True
344+
340flag trypandoc
345flag trypandoc
341 Description: Build trypandoc cgi executable.
346 Description: Build trypandoc cgi executable.
342 Default: False
347 Default: False
@@ -413,6 +418,8 @@ library
413 cpp-options: -DEMBED_DATA_FILES
418 cpp-options: -DEMBED_DATA_FILES
414 build-depends: file-embed >= 0.0 && < 0.1
419 build-depends: file-embed >= 0.0 && < 0.1
415 other-modules: Text.Pandoc.Data
420 other-modules: Text.Pandoc.Data
421+ if flag(derive_json_via_th)
422+ cpp-options: -DDERIVE_JSON_VIA_TH
416 if os(windows)
423 if os(windows)
417 cpp-options: -D_WINDOWS
424 cpp-options: -D_WINDOWS
418 ghc-options: -Wall -fno-warn-unused-do-bind
425 ghc-options: -Wall -fno-warn-unused-do-bind
src/Text/Pandoc/App/CommandLineOptions.hs+20−2
@@ -2,8 +2,10 @@
2{-# LANGUAGE CPP #-}
2{-# LANGUAGE CPP #-}
3{-# LANGUAGE DeriveGeneric #-}
3{-# LANGUAGE DeriveGeneric #-}
4{-# LANGUAGE ScopedTypeVariables #-}
4{-# LANGUAGE ScopedTypeVariables #-}
5−{-# LANGUAGE TemplateHaskell #-}
6{-# LANGUAGE TupleSections #-}
5{-# LANGUAGE TupleSections #-}
6+#ifdef DERIVE_JSON_VIA_TH
7+{-# LANGUAGE TemplateHaskell #-}
8+#endif
7{-
9{-
8Copyright (C) 2006-2018 John MacFarlane <jgm@berkeley.edu>
10Copyright (C) 2006-2018 John MacFarlane <jgm@berkeley.edu>
9
11
@@ -46,7 +48,6 @@ import Control.Monad
46import Control.Monad.Trans
48import Control.Monad.Trans
47import Data.Aeson.Encode.Pretty (encodePretty', Config(..), keyOrder,
49import Data.Aeson.Encode.Pretty (encodePretty', Config(..), keyOrder,
48 defConfig, Indent(..), NumberFormat(..))
50 defConfig, Indent(..), NumberFormat(..))
49−import Data.Aeson.TH (deriveJSON, defaultOptions)
50import Data.Char (toLower, toUpper)
51import Data.Char (toLower, toUpper)
51import Data.List (intercalate, sort)
52import Data.List (intercalate, sort)
52import Data.Maybe (fromMaybe)
53import Data.Maybe (fromMaybe)
@@ -65,6 +66,13 @@ import Text.Pandoc.Writers.Math (defaultMathJaxURL, defaultKaTeXURL)
65import Text.Pandoc.Shared (ordNub, safeRead)
66import Text.Pandoc.Shared (ordNub, safeRead)
66import Text.Printf
67import Text.Printf
67
68
69+#ifdef DERIVE_JSON_VIA_TH
70+import Data.Aeson.TH (deriveJSON, defaultOptions)
71+#else
72+import Data.Aeson (FromJSON (..), ToJSON (..),
73+ defaultOptions, genericToEncoding)
74+#endif
75+
68#ifdef EMBED_DATA_FILES
76#ifdef EMBED_DATA_FILES
69import Text.Pandoc.Data (dataFiles)
77import Text.Pandoc.Data (dataFiles)
70import System.Directory (getAppUserDataDirectory)
78import System.Directory (getAppUserDataDirectory)
@@ -1086,5 +1094,15 @@ deprecatedOption o msg =
1086
1094
1087-- see https://github.com/jgm/pandoc/pull/4083
1095-- see https://github.com/jgm/pandoc/pull/4083
1088-- using generic deriving caused long compilation times
1096-- using generic deriving caused long compilation times
1097+#ifdef DERIVE_JSON_VIA_TH
1089$(deriveJSON defaultOptions ''LineEnding)
1098$(deriveJSON defaultOptions ''LineEnding)
1090$(deriveJSON defaultOptions ''Opt)
1099$(deriveJSON defaultOptions ''Opt)
1100+#else
1101+instance ToJSON LineEnding where
1102+ toEncoding = genericToEncoding defaultOptions
1103+instance FromJSON LineEnding
1104+
1105+instance ToJSON Opt where
1106+ toEncoding = genericToEncoding defaultOptions
1107+instance FromJSON Opt
1108+#endif
src/Text/Pandoc/Extensions.hs+22−4
@@ -1,9 +1,11 @@
1−{-# LANGUAGE NoImplicitPrelude #-}
2{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2+{-# LANGUAGE NoImplicitPrelude #-}
3{-# LANGUAGE DeriveDataTypeable #-}
3{-# LANGUAGE DeriveDataTypeable #-}
4{-# LANGUAGE DeriveGeneric #-}
4{-# LANGUAGE DeriveGeneric #-}
5+#ifndef AVOID_TEMPLATE_HASKELL
5{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6{-# LANGUAGE TemplateHaskell #-}
7{-# LANGUAGE TemplateHaskell #-}
8+#endif
7{-
9{-
8Copyright (C) 2012-2018 John MacFarlane <jgm@berkeley.edu>
10Copyright (C) 2012-2018 John MacFarlane <jgm@berkeley.edu>
9
11
@@ -50,8 +52,6 @@ module Text.Pandoc.Extensions ( Extension(..)
50 , multimarkdownExtensions )
52 , multimarkdownExtensions )
51where
53where
52import Prelude
54import Prelude
53−import Data.Aeson (FromJSON (..), ToJSON (..), defaultOptions)
54−import Data.Aeson.TH (deriveJSON)
55import Data.Bits (clearBit, setBit, testBit, (.|.))
55import Data.Bits (clearBit, setBit, testBit, (.|.))
56import Data.Data (Data)
56import Data.Data (Data)
57import Data.Typeable (Typeable)
57import Data.Typeable (Typeable)
@@ -59,8 +59,15 @@ import GHC.Generics (Generic)
59import Text.Pandoc.Shared (safeRead)
59import Text.Pandoc.Shared (safeRead)
60import Text.Parsec
60import Text.Parsec
61
61
62+#ifdef DERIVE_JSON_VIA_TH
63+import Data.Aeson.TH (deriveJSON, defaultOptions)
64+#else
65+import Data.Aeson (FromJSON (..), ToJSON (..),
66+ defaultOptions, genericToEncoding)
67+#endif
68+
62newtype Extensions = Extensions Integer
69newtype Extensions = Extensions Integer
63− deriving (Show, Read, Eq, Ord, Data, Typeable, Generic, ToJSON, FromJSON)
70+ deriving (Show, Read, Eq, Ord, Data, Typeable, Generic)
64
71
65instance Semigroup Extensions where
72instance Semigroup Extensions where
66 (Extensions a) <> (Extensions b) = Extensions (a .|. b)
73 (Extensions a) <> (Extensions b) = Extensions (a .|. b)
@@ -387,4 +394,15 @@ parseFormatSpec = parse formatSpec ""
387 '-' -> disableExtension ext
394 '-' -> disableExtension ext
388 _ -> enableExtension ext
395 _ -> enableExtension ext
389
396
397+#ifdef DERIVE_JSON_VIA_TH
390$(deriveJSON defaultOptions ''Extension)
398$(deriveJSON defaultOptions ''Extension)
399+$(deriveJSON defaultOptions ''Extensions)
400+#else
401+instance ToJSON Extension where
402+ toEncoding = genericToEncoding defaultOptions
403+instance FromJSON Extension
404+
405+instance ToJSON Extensions where
406+ toEncoding = genericToEncoding defaultOptions
407+instance FromJSON Extensions
408+#endif
src/Text/Pandoc/Filter.hs+18−3
@@ -1,5 +1,9 @@
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE DeriveGeneric #-}
1{-# LANGUAGE NoImplicitPrelude #-}
3{-# LANGUAGE NoImplicitPrelude #-}
4+#ifndef AVOID_TEMPLATE_HASKELL
2{-# LANGUAGE TemplateHaskell #-}
5{-# LANGUAGE TemplateHaskell #-}
6+#endif
3{-
7{-
4Copyright (C) 2006-2018 John MacFarlane <jgm@berkeley.edu>
8Copyright (C) 2006-2018 John MacFarlane <jgm@berkeley.edu>
5
9
@@ -34,9 +38,14 @@ module Text.Pandoc.Filter
34 ) where
38 ) where
35
39
36import Prelude
40import Prelude
41+#ifdef AVOID_TEMPLATE_HASKELL
37−import Data.Aeson (defaultOptions)
42+import Data.Aeson (FromJSON (..), ToJSON (..),
43+ defaultOptions, genericToEncoding)
44+#else
38−import Data.Aeson.TH (deriveJSON)
45+import Data.Aeson.TH (deriveJSON, defaultOptions)
46+#endif
39import Data.Foldable (foldrM)
47import Data.Foldable (foldrM)
48+import GHC.Generics (Generic)
40import Text.Pandoc.Class (PandocIO)
49import Text.Pandoc.Class (PandocIO)
41import Text.Pandoc.Definition (Pandoc)
50import Text.Pandoc.Definition (Pandoc)
42import Text.Pandoc.Options (ReaderOptions)
51import Text.Pandoc.Options (ReaderOptions)
@@ -47,7 +56,7 @@ import qualified Text.Pandoc.Filter.Path as Path
47-- | Type of filter and path to filter file.
56-- | Type of filter and path to filter file.
48data Filter = LuaFilter FilePath
57data Filter = LuaFilter FilePath
49 | JSONFilter FilePath
58 | JSONFilter FilePath
50− deriving (Show)
59+ deriving (Show, Generic)
51
60
52-- | Modify the given document using a filter.
61-- | Modify the given document using a filter.
53applyFilters :: ReaderOptions
62applyFilters :: ReaderOptions
@@ -67,4 +76,10 @@ expandFilterPath :: Filter -> PandocIO Filter
67expandFilterPath (LuaFilter fp) = LuaFilter <$> Path.expandFilterPath fp
76expandFilterPath (LuaFilter fp) = LuaFilter <$> Path.expandFilterPath fp
68expandFilterPath (JSONFilter fp) = JSONFilter <$> Path.expandFilterPath fp
77expandFilterPath (JSONFilter fp) = JSONFilter <$> Path.expandFilterPath fp
69
78
79+#ifdef AVOID_TEMPLATE_HASKELL
80+instance ToJSON Filter where
81+ toEncoding = genericToEncoding defaultOptions
82+instance FromJSON Filter
83+#else
70$(deriveJSON defaultOptions ''Filter)
84$(deriveJSON defaultOptions ''Filter)
85+#endif
src/Text/Pandoc/Options.hs+50−3
@@ -1,7 +1,11 @@
1−{-# LANGUAGE NoImplicitPrelude #-}
1+{-# LANGUAGE CPP #-}
2{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE DeriveDataTypeable #-}
3{-# LANGUAGE DeriveGeneric #-}
3{-# LANGUAGE DeriveGeneric #-}
4+{-# LANGUAGE NoImplicitPrelude #-}
5+#ifndef AVOID_TEMPLATE_HASKELL
4{-# LANGUAGE TemplateHaskell #-}
6{-# LANGUAGE TemplateHaskell #-}
7+#endif
8+
5{-
9{-
6Copyright (C) 2012-2018 John MacFarlane <jgm@berkeley.edu>
10Copyright (C) 2012-2018 John MacFarlane <jgm@berkeley.edu>
7
11
@@ -48,8 +52,6 @@ module Text.Pandoc.Options ( module Text.Pandoc.Extensions
48 , isEnabled
52 , isEnabled
49 ) where
53 ) where
50import Prelude
54import Prelude
51−import Data.Aeson (defaultOptions)
52−import Data.Aeson.TH (deriveJSON)
53import Data.Data (Data)
55import Data.Data (Data)
54import Data.Default
56import Data.Default
55import qualified Data.Set as Set
57import qualified Data.Set as Set
@@ -59,6 +61,13 @@ import Skylighting (SyntaxMap, defaultSyntaxMap)
59import Text.Pandoc.Extensions
61import Text.Pandoc.Extensions
60import Text.Pandoc.Highlighting (Style, pygments)
62import Text.Pandoc.Highlighting (Style, pygments)
61
63
64+#ifdef DERIVE_JSON_VIA_TH
65+import Data.Aeson.TH (deriveJSON, defaultOptions)
66+#else
67+import Data.Aeson (FromJSON (..), ToJSON (..),
68+ defaultOptions, genericToEncoding)
69+#endif
70+
62class HasSyntaxExtensions a where
71class HasSyntaxExtensions a where
63 getExtensions :: a -> Extensions
72 getExtensions :: a -> Extensions
64
73
@@ -239,6 +248,7 @@ instance HasSyntaxExtensions WriterOptions where
239isEnabled :: HasSyntaxExtensions a => Extension -> a -> Bool
248isEnabled :: HasSyntaxExtensions a => Extension -> a -> Bool
240isEnabled ext opts = ext `extensionEnabled` getExtensions opts
249isEnabled ext opts = ext `extensionEnabled` getExtensions opts
241
250
251+#ifdef DERIVE_JSON_VIA_TH
242$(deriveJSON defaultOptions ''ReaderOptions)
252$(deriveJSON defaultOptions ''ReaderOptions)
243$(deriveJSON defaultOptions ''HTMLMathMethod)
253$(deriveJSON defaultOptions ''HTMLMathMethod)
244$(deriveJSON defaultOptions ''CiteMethod)
254$(deriveJSON defaultOptions ''CiteMethod)
@@ -248,3 +258,40 @@ $(deriveJSON defaultOptions ''TrackChanges)
248$(deriveJSON defaultOptions ''WrapOption)
258$(deriveJSON defaultOptions ''WrapOption)
249$(deriveJSON defaultOptions ''TopLevelDivision)
259$(deriveJSON defaultOptions ''TopLevelDivision)
250$(deriveJSON defaultOptions ''ReferenceLocation)
260$(deriveJSON defaultOptions ''ReferenceLocation)
261+#else
262+instance ToJSON CiteMethod where
263+ toEncoding = genericToEncoding defaultOptions
264+instance FromJSON CiteMethod
265+
266+instance ToJSON ReaderOptions where
267+ toEncoding = genericToEncoding defaultOptions
268+instance FromJSON ReaderOptions
269+
270+instance ToJSON ObfuscationMethod where
271+ toEncoding = genericToEncoding defaultOptions
272+instance FromJSON ObfuscationMethod
273+
274+instance ToJSON WrapOption where
275+ toEncoding = genericToEncoding defaultOptions
276+instance FromJSON WrapOption
277+
278+instance ToJSON HTMLMathMethod where
279+ toEncoding = genericToEncoding defaultOptions
280+instance FromJSON HTMLMathMethod
281+
282+instance ToJSON HTMLSlideVariant where
283+ toEncoding = genericToEncoding defaultOptions
284+instance FromJSON HTMLSlideVariant
285+
286+instance ToJSON TopLevelDivision where
287+ toEncoding = genericToEncoding defaultOptions
288+instance FromJSON TopLevelDivision
289+
290+instance ToJSON ReferenceLocation where
291+ toEncoding = genericToEncoding defaultOptions
292+instance FromJSON ReferenceLocation
293+
294+instance ToJSON TrackChanges where
295+ toEncoding = genericToEncoding defaultOptions
296+instance FromJSON TrackChanges
297+#endif