aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc
diff options
context:
space:
mode:
authorJohn MacFarlane <fiddlosopher@gmail.com>2013-06-27 20:56:03 -0700
committerJohn MacFarlane <fiddlosopher@gmail.com>2013-06-27 21:13:18 -0700
commit9ab60a4d1588cfed153ca9efea9c9546bc0e041b (patch)
tree7499155ddfad428a02685c3ccbc5e6f6b757d140 /src/Text/Pandoc
parentdd96213c055d72b65e0c70552d9b4e3a824ba3ee (diff)
downloadpandoc-9ab60a4d1588cfed153ca9efea9c9546bc0e041b.tar.gz
Shared: Added `defField`.
`defField` is like `setField`, but does nothing if the field already has a value.
Diffstat (limited to 'src/Text/Pandoc')
-rw-r--r--src/Text/Pandoc/Shared.hs14
1 files changed, 14 insertions, 0 deletions
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
--