git.iko.soy/typeable/pandoc
on master help sign in
Shared: Added `defField`.

`defField` is like `setField`, but does nothing if the field already has a value.

commit9ab60a4d1588cfed153ca9efea9c9546bc0e041b authorJohn MacFarlane <fiddlosopher@gmail.com> dateThu 27 Jun 2013, 20:56 (13 years ago) parentdd96213
1 file changed+14
src/Text/Pandoc/Shared.hs+14
src/Text/Pandoc/Shared.hs+14
@@ -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--