aboutsummaryrefslogtreecommitdiff
path: root/src/Text
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2021-04-25 10:31:33 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2021-04-25 10:31:33 -0700
commit547bc2cdf83b8be926de55521674c0e8fab12db5 (patch)
tree85a937460cbdbae248d3621f2b22c0fed54a6b34 /src/Text
parent7f4850c9de086a2f6df072bf15357cd05335cffd (diff)
downloadpandoc-547bc2cdf83b8be926de55521674c0e8fab12db5.tar.gz
Add quotes properly in markdown YAML metadata fields.
This fixes a bug, which caused the writer to look at the LAST rather than the FIRST character in determining whether quotes were needed. So we got spurious quotes in some cases and didn't get necessary quotes in others. Closes #7245. Updated a number of test cases accordingly.
Diffstat (limited to 'src/Text')
-rw-r--r--src/Text/Pandoc/Writers/Markdown.hs11
1 files changed, 5 insertions, 6 deletions
diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs
index daf45ed53..3295d9e6c 100644
--- a/src/Text/Pandoc/Writers/Markdown.hs
+++ b/src/Text/Pandoc/Writers/Markdown.hs
@@ -26,7 +26,7 @@ import Data.Default
import Data.List (intersperse, sortOn, transpose)
import Data.List.NonEmpty (nonEmpty, NonEmpty(..))
import qualified Data.Map as M
-import Data.Maybe (fromMaybe, mapMaybe)
+import Data.Maybe (fromMaybe, mapMaybe, isNothing)
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as T
@@ -142,18 +142,17 @@ valToYaml (SimpleVal x)
| otherwise =
if hasNewlines x
then hang 0 ("|" <> cr) x
- else if fst $ foldr needsDoubleQuotes (False, True) x
+ else if isNothing $ foldM needsDoubleQuotes True x
then "\"" <> fmap escapeInDoubleQuotes x <> "\""
else x
where
- needsDoubleQuotes t (positive, isFirst)
+ needsDoubleQuotes isFirst t
= if T.any isBadAnywhere t ||
(isFirst && T.any isYamlPunct (T.take 1 t))
- then (True, False)
- else (positive, False)
+ then Nothing
+ else Just False
isBadAnywhere '#' = True
isBadAnywhere ':' = True
- isBadAnywhere '`' = False
isBadAnywhere _ = False
hasNewlines NewLine = True
hasNewlines BlankLines{} = True