From 641849b70a851c45ce3d3fb438eeee7fd813d070 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Thu, 8 Oct 2020 10:54:16 -0700 Subject: 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. --- src/Text/Pandoc/Writers/Markdown.hs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'src/Text/Pandoc') 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 -- cgit v1.2.3