From 9ab60a4d1588cfed153ca9efea9c9546bc0e041b Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Thu, 27 Jun 2013 20:56:03 -0700 Subject: Shared: Added `defField`. `defField` is like `setField`, but does nothing if the field already has a value. --- src/Text/Pandoc/Shared.hs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/Text') diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs index 31730a9e7..d6663f193 100644 --- a/src/Text/Pandoc/Shared.hs +++ b/src/Text/Pandoc/Shared.hs @@ -65,6 +65,7 @@ module Text.Pandoc.Shared ( makeMeta, metaToJSON, setField, + defField, -- * TagSoup HTML handling renderTags', -- * File handling @@ -562,6 +563,19 @@ setField field val (Object hashmap) = _ -> toJSON [oldval, newval] setField _ _ x = x +defField :: ToJSON a + => String + -> a + -> Value + -> Value +-- | Set a field of a JSON object if it currently has no value. +-- If it has a value, do nothing. +-- This is a utility function to be used in preparing template contexts. +defField field val (Object hashmap) = + Object $ H.insertWith f (T.pack field) (toJSON val) hashmap + where f _newval oldval = oldval +defField _ _ x = x + -- -- TagSoup HTML handling -- -- cgit v1.2.3