@@ -63,10 +63,6 @@ module Text.Pandoc.Shared (
6363 isTightList,
6464 addMetaField,
6565 makeMeta,
66− metaToJSON,
67− getField,
68− setField,
69− defField,
7066 -- * TagSoup HTML handling
7167 renderTags',
7268 -- * File handling
@@ -99,7 +95,7 @@ import Text.Pandoc.MIME (getMimeType)
9995import System.FilePath ( (</>), takeExtension, dropExtension )
10096import Data.Generics (Typeable, Data)
10197import qualified Control.Monad.State as S
102−import Control.Monad (msum, unless, liftM)
98+import Control.Monad (msum, unless)
10399import Text.Pandoc.Pretty (charWidth)
104100import System.Locale (defaultTimeLocale)
105101import Data.Time
@@ -111,10 +107,6 @@ import qualified Data.ByteString.Char8 as B8
111107import Network.HTTP (findHeader, rspBody,
112108 RequestMethod(..), HeaderName(..), mkRequest)
113109import Network.Browser (browse, setAllowRedirects, setOutHandler, request)
114−import qualified Data.Traversable as Traversable
115−import qualified Data.HashMap.Strict as H
116−import qualified Data.Text as T
117−import Data.Aeson (FromJSON(..), fromJSON, ToJSON (..), Value(Object), Result(..))
118110
119111#ifdef EMBED_DATA_FILES
120112import Text.Pandoc.Data (dataFiles)
@@ -524,78 +516,6 @@ makeMeta title authors date =
524516 $ addMetaField "date" (B.fromList date)
525517 $ nullMeta
526518
527−-- | Create JSON value for template from a 'Meta' and an association list
528−-- of variables, specified at the command line or in the writer.
529−-- Variables overwrite metadata fields with the same names.
530−-- If multiple variables are set with the same name, a list is
531−-- assigned.
532−metaToJSON :: Monad m
533− => ([Block] -> m String) -- ^ Writer for output format
534− -> ([Inline] -> m String) -- ^ Writer for output format
535− -> [(String, String)] -- ^ Variables
536− -> Meta -- ^ Metadata
537− -> m Value
538−metaToJSON blockWriter inlineWriter vars (Meta metamap) = do
539− let baseContext = foldl (\acc (x,y) -> setField x y acc) (Object H.empty) vars
540− renderedMap <- Traversable.mapM (metaValueToJSON blockWriter inlineWriter)
541− metamap
542− return $ M.foldWithKey (\key val obj -> defField key val obj)
543− baseContext renderedMap
544−
545−metaValueToJSON :: Monad m
546− => ([Block] -> m String)
547− -> ([Inline] -> m String)
548− -> MetaValue
549− -> m Value
550−metaValueToJSON blockWriter inlineWriter (MetaMap metamap) = liftM toJSON $
551− Traversable.mapM (metaValueToJSON blockWriter inlineWriter) metamap
552−metaValueToJSON blockWriter inlineWriter (MetaList xs) = liftM toJSON $
553− Traversable.mapM (metaValueToJSON blockWriter inlineWriter) xs
554−metaValueToJSON _ _ (MetaString s) = return $ toJSON s
555−metaValueToJSON blockWriter _ (MetaBlocks bs) = liftM toJSON $ blockWriter bs
556−metaValueToJSON _ inlineWriter (MetaInlines bs) = liftM toJSON $ inlineWriter bs
557−
558−-- | Retrieve a field value from a JSON object.
559−getField :: FromJSON a
560− => String
561− -> Value
562− -> Maybe a
563−getField field (Object hashmap) = do
564− result <- H.lookup (T.pack field) hashmap
565− case fromJSON result of
566− Success x -> return x
567− _ -> fail "Could not convert from JSON"
568−getField field _ = fail "Not a JSON object"
569−
570−setField :: ToJSON a
571− => String
572− -> a
573− -> Value
574− -> Value
575−-- | Set a field of a JSON object. If the field already has a value,
576−-- convert it into a list with the new value appended to the old value(s).
577−-- This is a utility function to be used in preparing template contexts.
578−setField field val (Object hashmap) =
579− Object $ H.insertWith combine (T.pack field) (toJSON val) hashmap
580− where combine newval oldval =
581− case fromJSON oldval of
582− Success xs -> toJSON $ xs ++ [newval]
583− _ -> toJSON [oldval, newval]
584−setField _ _ x = x
585−
586−defField :: ToJSON a
587− => String
588− -> a
589− -> Value
590− -> Value
591−-- | Set a field of a JSON object if it currently has no value.
592−-- If it has a value, do nothing.
593−-- This is a utility function to be used in preparing template contexts.
594−defField field val (Object hashmap) =
595− Object $ H.insertWith f (T.pack field) (toJSON val) hashmap
596− where f _newval oldval = oldval
597−defField _ _ x = x
598−
599519--
600520-- TagSoup HTML handling
601521--