on master help sign in
Merge pull request #154 from snoyberg/152-literal-multiline

Use literal style for multiline strings (fixes #152)

commit26be7fdd9bcb8193c720e50f744d0f4bdd16815b authorMichael Snoyman <michael@snoyman.com> dateTue 18 Sep 2018, 07:18 (8 years ago) parentc16c516e0f5c85
2 files changed+14−4
ChangeLog.md+1 src/Data/Yaml.hs+13−4
ChangeLog.md+1
@@ -3,6 +3,7 @@
33## 0.10.2.0
44
55* Add `EncodeOptions` and `FormatOptions` to control the style of the encoded YAML. [#153](https://github.com/snoyberg/yaml/pull/153)
6+* Default to using literal style for multiline strings [#152](https://github.com/snoyberg/yaml/issues/152)
67
78## 0.10.1.1
89
src/Data/Yaml.hs+13−4
@@ -106,6 +106,7 @@ import qualified Data.Conduit.List as CL
106106import qualified Data.HashMap.Strict as M
107107import qualified Data.HashSet as HashSet
108108import Data.Text.Encoding (encodeUtf8)
109+import qualified Data.Text as T
109110import qualified Data.Text.Encoding as TE
110111import qualified Data.Text.Lazy as TL
111112import Data.Text.Lazy.Builder (toLazyText)
@@ -130,7 +131,13 @@ data EncodeOptions = EncodeOptions
130131-- __WARNING__: You must ensure that special strings (like @"yes"@\/@"no"@\/@"null"@\/@"1234"@) are not encoded with the 'Plain' style, because
131132-- then they will be decoded as boolean, null or numeric values. You can use 'isSpecialString' to detect them.
132133--
133−-- By default, strings are encoded with the `Plain` style, except special strings, which are encoded with `SingleQuoted`.
134+-- By default, strings are encoded as follows:
135+--
136+-- * Any string containing a newline character uses the 'Literal' style
137+--
138+-- * Otherwise, any special string (see 'isSpecialString') uses 'SingleQuoted'
139+--
140+-- * Otherwise, use 'Plain'
134141--
135142-- @since 0.10.2.0
136143setStringStyle :: (Text -> ( Tag, Style )) -> EncodeOptions -> EncodeOptions
@@ -156,9 +163,11 @@ defaultEncodeOptions = EncodeOptions
156163 { encodeOptionsStringStyle = \s ->
157164 -- Empty strings need special handling to ensure they get quoted. This avoids:
158165 -- https://github.com/snoyberg/yaml/issues/24
159− if isSpecialString s
160− then ( NoTag, SingleQuoted )
161− else ( StrTag, PlainNoTag )
166+ case () of
167+ ()
168+ | "\n" `T.isInfixOf` s -> ( NoTag, Literal )
169+ | isSpecialString s -> ( NoTag, SingleQuoted )
170+ | otherwise -> ( StrTag, PlainNoTag )
162171 , encodeOptionsFormat = defaultFormatOptions
163172 }
164173