aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Writers
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2020-10-08 10:54:16 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2020-10-08 10:54:53 -0700
commit641849b70a851c45ce3d3fb438eeee7fd813d070 (patch)
treefb1f7ad7e5e9f80abc1c361b22d1e8818193a061 /src/Text/Pandoc/Writers
parent36e010cb4a3dc2de2eca7286e62873ad5dfde1a8 (diff)
downloadpandoc-641849b70a851c45ce3d3fb438eeee7fd813d070.tar.gz
Be less aggressive about using quotes for YAML values.
We need quotes if `[` or `{` or `'` is at the beginning of the line, but not otherwise.
Diffstat (limited to 'src/Text/Pandoc/Writers')
-rw-r--r--src/Text/Pandoc/Writers/Markdown.hs15
1 files changed, 11 insertions, 4 deletions
diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs
index cddc57850..24e50f0f7 100644
--- a/src/Text/Pandoc/Writers/Markdown.hs
+++ b/src/Text/Pandoc/Writers/Markdown.hs
@@ -179,19 +179,26 @@ valToYaml (SimpleVal x)
| otherwise =
if hasNewlines x
then hang 0 ("|" <> cr) x
- else if any hasPunct x
+ else if fst $ foldr needsDoubleQuotes (False, True) x
then "\"" <> fmap escapeInDoubleQuotes x <> "\""
else x
where
+ needsDoubleQuotes t (positive, isFirst)
+ = if T.any isBadAnywhere t ||
+ (isFirst && T.any isYamlPunct (T.take 1 t))
+ then (True, False)
+ else (positive, False)
+ isBadAnywhere '#' = True
+ isBadAnywhere ':' = True
+ isBadAnywhere '`' = False
+ isBadAnywhere _ = False
hasNewlines NewLine = True
hasNewlines BlankLines{} = True
hasNewlines CarriageReturn = True
hasNewlines (Concat w z) = hasNewlines w || hasNewlines z
hasNewlines _ = False
- hasPunct = T.any isYamlPunct
isYamlPunct = (`elem` ['-','?',':',',','[',']','{','}',
- '#','&','*','!','|','>','\'','"',
- '%','@','`',',','[',']','{','}'])
+ '#','&','*','!','|','>','\'','"', '%','@','`'])
escapeInDoubleQuotes = T.replace "\"" "\\\"" . T.replace "\\" "\\\\"
valToYaml _ = empty