@@ -65,6 +65,7 @@ module Text.Pandoc.Shared (
6565 makeMeta,
6666 metaToJSON,
6767 setField,
68+ defField,
6869 -- * TagSoup HTML handling
6970 renderTags',
7071 -- * File handling
@@ -562,6 +563,19 @@ setField field val (Object hashmap) =
562563 _ -> toJSON [oldval, newval]
563564setField _ _ x = x
564565
566+defField :: ToJSON a
567+ => String
568+ -> a
569+ -> Value
570+ -> Value
571+-- | Set a field of a JSON object if it currently has no value.
572+-- If it has a value, do nothing.
573+-- This is a utility function to be used in preparing template contexts.
574+defField field val (Object hashmap) =
575+ Object $ H.insertWith f (T.pack field) (toJSON val) hashmap
576+ where f _newval oldval = oldval
577+defField _ _ x = x
578+
565579--
566580-- TagSoup HTML handling
567581--