@@ -1,4 +1,5 @@
11{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE ScopedTypeVariables #-}
23{- |
34 Module : Text.Pandoc.Readers.Metadata
45 Copyright : Copyright (C) 2006-2020 John MacFarlane
@@ -10,7 +11,10 @@
1011
1112Parse YAML/JSON metadata to 'Pandoc' 'Meta'.
1213-}
13−module Text.Pandoc.Readers.Metadata ( yamlBsToMeta, yamlMap ) where
14+module Text.Pandoc.Readers.Metadata (
15+ yamlBsToMeta,
16+ yamlBsToRefs,
17+ yamlMap ) where
1418
1519import Control.Monad
1620import Control.Monad.Except (throwError)
@@ -47,6 +51,45 @@ yamlBsToMeta pMetaValue bstr = do
4751 (T.pack err') pos
4852 return . return $ mempty
4953
54+-- Returns filtered list of references.
55+yamlBsToRefs :: PandocMonad m
56+ => ParserT Text ParserState m (F MetaValue)
57+ -> (Text -> Bool) -- ^ Filter for id
58+ -> BL.ByteString
59+ -> ParserT Text ParserState m (F [M.Map Text MetaValue])
60+yamlBsToRefs pMetaValue idpred bstr = do
61+ pos <- getPosition
62+ case YAML.decodeNode' YAML.failsafeSchemaResolver False False bstr of
63+ Right (YAML.Doc (YAML.Mapping _ _ o):_)
64+ -> case YAML.parseEither (o YAML..: "references") of
65+ Right ns -> do
66+ let g n = case YAML.parseEither (n YAML..: "id") of
67+ Right t -> idpred t ||
68+ case YAML.parseEither (n YAML..:
69+ "other-ids") of
70+ Right (oids :: [Text]) ->
71+ any idpred oids
72+ _ -> False
73+ _ -> False
74+ sequence <$> mapM (yamlMap pMetaValue) (filter g ns)
75+ Left _ -> do
76+ logMessage $ CouldNotParseYamlMetadata
77+ ("expecting 'references' field") pos
78+ return . return $ mempty
79+
80+ Right [] -> return . return $ mempty
81+ Right [YAML.Doc (YAML.Scalar _ YAML.SNull)]
82+ -> return . return $ mempty
83+ Right _ -> do logMessage $ CouldNotParseYamlMetadata "not an object"
84+ pos
85+ return . return $ mempty
86+ Left (_pos, err')
87+ -> do logMessage $ CouldNotParseYamlMetadata
88+ (T.pack err') pos
89+ return . return $ mempty
90+
91+
92+
5093nodeToKey :: PandocMonad m
5194 => YAML.Node YE.Pos
5295 -> m Text