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.
This commit is contained in:
John MacFarlane 2021-04-25 10:31:33 -07:00
parent 7f4850c9de
commit 547bc2cdf8
9 changed files with 16 additions and 17 deletions

View file

@ -26,7 +26,7 @@ import Data.Default
import Data.List (intersperse, sortOn, transpose) import Data.List (intersperse, sortOn, transpose)
import Data.List.NonEmpty (nonEmpty, NonEmpty(..)) import Data.List.NonEmpty (nonEmpty, NonEmpty(..))
import qualified Data.Map as M import qualified Data.Map as M
import Data.Maybe (fromMaybe, mapMaybe) import Data.Maybe (fromMaybe, mapMaybe, isNothing)
import qualified Data.Set as Set import qualified Data.Set as Set
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as T import qualified Data.Text as T
@ -142,18 +142,17 @@ valToYaml (SimpleVal x)
| otherwise = | otherwise =
if hasNewlines x if hasNewlines x
then hang 0 ("|" <> cr) 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 <> "\"" then "\"" <> fmap escapeInDoubleQuotes x <> "\""
else x else x
where where
needsDoubleQuotes t (positive, isFirst) needsDoubleQuotes isFirst t
= if T.any isBadAnywhere t || = if T.any isBadAnywhere t ||
(isFirst && T.any isYamlPunct (T.take 1 t)) (isFirst && T.any isYamlPunct (T.take 1 t))
then (True, False) then Nothing
else (positive, False) else Just False
isBadAnywhere '#' = True isBadAnywhere '#' = True
isBadAnywhere ':' = True isBadAnywhere ':' = True
isBadAnywhere '`' = False
isBadAnywhere _ = False isBadAnywhere _ = False
hasNewlines NewLine = True hasNewlines NewLine = True
hasNewlines BlankLines{} = True hasNewlines BlankLines{} = True

View file

@ -9,7 +9,7 @@ title: My Article
[^1]: Dept. of This and That [^1]: Dept. of This and That
^D ^D
--- ---
author: "John Doe[^1]" author: John Doe[^1]
date: 2014 date: 2014
title: My Article title: My Article
--- ---

View file

@ -63,7 +63,7 @@ references:
publisher-place: Cambridge publisher-place: Cambridge
title: "Le *De Anima* dans la tradition grècque: Quelques aspects de title: "Le *De Anima* dans la tradition grècque: Quelques aspects de
l'interpretation du traité, de Theophraste à Themistius" l'interpretation du traité, de Theophraste à Themistius"
title-short: *De Anima* dans la tradition grècque title-short: "*De Anima* dans la tradition grècque"
type: paper-conference type: paper-conference
- author: - author:
- family: Salam - family: Salam

View file

@ -72,8 +72,8 @@ references:
language: la language: la
number-of-volumes: 2 number-of-volumes: 2
publisher-place: Leipzig publisher-place: Leipzig
title: "Regesta Pontificum Romanorum ab condita ecclesia ad annum post title: Regesta Pontificum Romanorum ab condita ecclesia ad annum post
Christum natum [mcxcviii]{.smallcaps}" Christum natum [mcxcviii]{.smallcaps}
title-short: Regesta Pontificum Romanorum title-short: Regesta Pontificum Romanorum
type: book type: book
--- ---

View file

@ -86,7 +86,7 @@ references:
publisher-place: Cambridge publisher-place: Cambridge
title: "Le *De Anima* dans la tradition grècque: Quelques aspects de title: "Le *De Anima* dans la tradition grècque: Quelques aspects de
l'interpretation du traité, de Theophraste à Themistius" l'interpretation du traité, de Theophraste à Themistius"
title-short: *De Anima* dans la tradition grècque title-short: "*De Anima* dans la tradition grècque"
type: paper-conference type: paper-conference
--- ---

View file

@ -36,7 +36,7 @@ references:
language: en-US language: en-US
publisher: Princeton University Press publisher: Princeton University Press
publisher-place: Princeton publisher-place: Princeton
title: "Aristotle's \"De Motu Animalium\"" title: Aristotle's "De Motu Animalium"
type: book type: book
--- ---

View file

@ -63,8 +63,8 @@ references:
issued: 1969 issued: 1969
language: de-DE language: de-DE
page: 189-216 page: 189-216
title: "Intention" und "Intentionalität" in der Scholastik, bei title: "\"Intention\" und \"Intentionalität\" in der Scholastik, bei
Brentano und Husserl Brentano und Husserl"
title-short: Intention und Intentionalität title-short: Intention und Intentionalität
type: article-journal type: article-journal
volume: 29 volume: 29

View file

@ -63,9 +63,9 @@ references:
id: item1 id: item1
issued: 2013 issued: 2013
language: en-US language: en-US
title: "A title, in English, with a Proper Name and an ACRONYM and a title: A title, in English, with a Proper Name and an ACRONYM and a
[camelCase]{.nocase} word and some units, 400 [nm]{.nocase}, [camelCase]{.nocase} word and some units, 400 [nm]{.nocase},
3 [cm]{.nocase}, and a quote, *Alea [iacta est]{.nocase}*" 3 [cm]{.nocase}, and a quote, *Alea [iacta est]{.nocase}*
type: article-journal type: article-journal
--- ---

View file

@ -9,7 +9,7 @@
nocite: "[@*]" nocite: "[@*]"
references: references:
- id: item1 - id: item1
title: "The title [of this book]{.nodecor}" title: The title [of this book]{.nodecor}
type: book type: book
--- ---