on master help sign in
Merge pull request #154 from snoyberg/152-literal-multiline
26be7fdMichael Snoyman
ChangeLog.md+1
@@ -3,6 +3,7 @@
3## 0.10.2.0
3## 0.10.2.0
4
4
5* Add `EncodeOptions` and `FormatOptions` to control the style of the encoded YAML. [#153](https://github.com/snoyberg/yaml/pull/153)
5* 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)
6
7
7## 0.10.1.1
8## 0.10.1.1
8
9
src/Data/Yaml.hs+13−4
@@ -106,6 +106,7 @@ import qualified Data.Conduit.List as CL
106import qualified Data.HashMap.Strict as M
106import qualified Data.HashMap.Strict as M
107import qualified Data.HashSet as HashSet
107import qualified Data.HashSet as HashSet
108import Data.Text.Encoding (encodeUtf8)
108import Data.Text.Encoding (encodeUtf8)
109+import qualified Data.Text as T
109import qualified Data.Text.Encoding as TE
110import qualified Data.Text.Encoding as TE
110import qualified Data.Text.Lazy as TL
111import qualified Data.Text.Lazy as TL
111import Data.Text.Lazy.Builder (toLazyText)
112import Data.Text.Lazy.Builder (toLazyText)
@@ -130,7 +131,13 @@ data EncodeOptions = EncodeOptions
130-- __WARNING__: You must ensure that special strings (like @"yes"@\/@"no"@\/@"null"@\/@"1234"@) are not encoded with the 'Plain' style, because
131-- __WARNING__: You must ensure that special strings (like @"yes"@\/@"no"@\/@"null"@\/@"1234"@) are not encoded with the 'Plain' style, because
131-- then they will be decoded as boolean, null or numeric values. You can use 'isSpecialString' to detect them.
132-- then they will be decoded as boolean, null or numeric values. You can use 'isSpecialString' to detect them.
132--
133--
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'
134--
141--
135-- @since 0.10.2.0
142-- @since 0.10.2.0
136setStringStyle :: (Text -> ( Tag, Style )) -> EncodeOptions -> EncodeOptions
143setStringStyle :: (Text -> ( Tag, Style )) -> EncodeOptions -> EncodeOptions
@@ -156,9 +163,11 @@ defaultEncodeOptions = EncodeOptions
156 { encodeOptionsStringStyle = \s ->
163 { encodeOptionsStringStyle = \s ->
157 -- Empty strings need special handling to ensure they get quoted. This avoids:
164 -- Empty strings need special handling to ensure they get quoted. This avoids:
158 -- https://github.com/snoyberg/yaml/issues/24
165 -- https://github.com/snoyberg/yaml/issues/24
166+ case () of
167+ ()
168+ | "\n" `T.isInfixOf` s -> ( NoTag, Literal )
159− if isSpecialString s
169+ | isSpecialString s -> ( NoTag, SingleQuoted )
160− then ( NoTag, SingleQuoted )
161− else ( StrTag, PlainNoTag )
170+ | otherwise -> ( StrTag, PlainNoTag )
162 , encodeOptionsFormat = defaultFormatOptions
171 , encodeOptionsFormat = defaultFormatOptions
163 }
172 }
164
173