aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <[email protected]>2022-08-31 10:30:49 -0700
committerJohn MacFarlane <[email protected]>2022-08-31 10:30:49 -0700
commit751383f33370928c7576f17d1a85d48cc9e8e30e (patch)
tree4cc486e2344b771c4d268500c926d6a3b707f608
parent1c4e9d6b05153ce894c9eccc8e39b0950956d9a8 (diff)
T.P.Citeproc.CslJson: allow an object with `items` property...
...in addition to an array of references. This is what is returned by e.g. `https://api.zotero.org/groups/904125/items?v=3&q=صحافة&format=csljson` See comment in #7151.
-rw-r--r--src/Text/Pandoc/Citeproc/CslJson.hs16
1 files changed, 10 insertions, 6 deletions
diff --git a/src/Text/Pandoc/Citeproc/CslJson.hs b/src/Text/Pandoc/Citeproc/CslJson.hs
index 43c1a87ec..2a7fc6c18 100644
--- a/src/Text/Pandoc/Citeproc/CslJson.hs
+++ b/src/Text/Pandoc/Citeproc/CslJson.hs
@@ -6,7 +6,8 @@ where
import Citeproc.CslJson
import Citeproc.Types
import Control.Monad.Identity (runIdentity)
-import Data.Aeson (eitherDecodeStrict')
+import Data.Aeson (eitherDecodeStrict', FromJSON(parseJSON), (.:), Value(..))
+import Data.Aeson.Types (parseEither)
import Data.ByteString (ByteString)
import Text.Pandoc.Builder as B
import Data.Text (Text)
@@ -31,8 +32,11 @@ fromCslJson (CslDiv t x) = B.spanWith ("",["csl-" <> t],[]) (fromCslJson x)
fromCslJson (CslLink u x) = B.link u "" (fromCslJson x)
cslJsonToReferences :: ByteString -> Either String [Reference Inlines]
-cslJsonToReferences raw =
- case eitherDecodeStrict' raw of
- Left e -> Left e
- Right cslrefs -> Right $
- map (runIdentity . traverse (return . fromCslJson)) cslrefs
+cslJsonToReferences raw = do
+ items <-
+ case eitherDecodeStrict' raw of
+ Left e -> Left e
+ Right (Object o) -> parseEither (.: "items") o
+ Right val@(Array _) -> parseEither parseJSON val
+ Right _ -> Left "expecting Object or Array"
+ pure $ map (runIdentity . traverse (return . fromCslJson)) items