summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2017-08-12 20:25:08 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2017-08-12 20:27:42 -0700
commitbe9957bddc1e1edac2375b6b945ab4d07f6198d2 (patch)
tree40f2075b87bd95f708542f420822946ffc741ac7
parent0ab8670a0eb52b5da2bef64b2cc1917e4d5ddb54 (diff)
Escape MetaString values (as added with --metadata flag).
Previously they would be transmitted to the template without any escaping. Note that `--M title='*foo*'` yields a different result from --- title: *foo* --- In the latter case, we have emphasis; in the former case, just a string with literal asterisks (which will be escaped in formats, like Markdown, that require it). Closes #3792.
-rw-r--r--src/Text/Pandoc/Writers/Shared.hs14
-rw-r--r--test/command/3792.md13
2 files changed, 21 insertions, 6 deletions
diff --git a/src/Text/Pandoc/Writers/Shared.hs b/src/Text/Pandoc/Writers/Shared.hs
index 8369bc09c..c6a5fdaf8 100644
--- a/src/Text/Pandoc/Writers/Shared.hs
+++ b/src/Text/Pandoc/Writers/Shared.hs
@@ -42,7 +42,7 @@ module Text.Pandoc.Writers.Shared (
, gridTable
)
where
-import Control.Monad (liftM, zipWithM)
+import Control.Monad (zipWithM)
import Data.Aeson (FromJSON (..), Result (..), ToJSON (..), Value (Object),
encode, fromJSON)
import qualified Data.HashMap.Strict as H
@@ -51,6 +51,7 @@ import qualified Data.Map as M
import Data.Maybe (isJust)
import qualified Data.Text as T
import qualified Data.Traversable as Traversable
+import qualified Text.Pandoc.Builder as Builder
import Text.Pandoc.Definition
import Text.Pandoc.Options
import Text.Pandoc.Pretty
@@ -103,14 +104,15 @@ metaValueToJSON :: (Monad m, ToJSON a)
-> ([Inline] -> m a)
-> MetaValue
-> m Value
-metaValueToJSON blockWriter inlineWriter (MetaMap metamap) = liftM toJSON $
+metaValueToJSON blockWriter inlineWriter (MetaMap metamap) = toJSON <$>
Traversable.mapM (metaValueToJSON blockWriter inlineWriter) metamap
-metaValueToJSON blockWriter inlineWriter (MetaList xs) = liftM toJSON $
+metaValueToJSON blockWriter inlineWriter (MetaList xs) = toJSON <$>
Traversable.mapM (metaValueToJSON blockWriter inlineWriter) xs
metaValueToJSON _ _ (MetaBool b) = return $ toJSON b
-metaValueToJSON _ _ (MetaString s) = return $ toJSON s
-metaValueToJSON blockWriter _ (MetaBlocks bs) = liftM toJSON $ blockWriter bs
-metaValueToJSON _ inlineWriter (MetaInlines bs) = liftM toJSON $ inlineWriter bs
+metaValueToJSON _ inlineWriter (MetaString s) = toJSON <$>
+ inlineWriter (Builder.toList (Builder.text s))
+metaValueToJSON blockWriter _ (MetaBlocks bs) = toJSON <$> blockWriter bs
+metaValueToJSON _ inlineWriter (MetaInlines is) = toJSON <$> inlineWriter is
-- | Retrieve a field value from a JSON object.
getField :: FromJSON a
diff --git a/test/command/3792.md b/test/command/3792.md
new file mode 100644
index 000000000..7c76afee7
--- /dev/null
+++ b/test/command/3792.md
@@ -0,0 +1,13 @@
+Make sure metadata values are treated as strings,
+and properly escaped.
+
+```
+% pandoc -t markdown -s -M title="<this> *that*"
+ok
+^D
+---
+title: '\<this\> \*that\*'
+---
+
+ok
+```