Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/Yaml/Encode.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Yaml.Encode exposing
, string, int, float, bool, null, value
, list, record, dict
, document
, safeEncodeString, safeEncodeKey, safeEncodeMultilineString
)

{-| Turn Elm values into [YAML](https://yaml.org). You use `Yaml.Encode` in a very similar
Expand Down Expand Up @@ -44,6 +45,43 @@ have a look at

import Dict exposing (Dict)
import Yaml.Parser.Ast exposing (Value(..))
import String.Extra exposing (quote)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer not to add dependencies



safeEncodeString : String -> Encoder
safeEncodeString s =
if String.contains "\n" s then
safeEncodeMultilineString s
else
encodeSpecialChars s
|> encodeQuotes
|> quote
|> string

encodeQuotes : String -> String
encodeQuotes s =
if String.contains "\"" s then
String.replace "\"" "\\\"" s
else
s

encodeSpecialChars : String -> String
encodeSpecialChars s =
if String.startsWith "*" s || String.startsWith "!" s || String.startsWith "&" s then
"\\" ++ s
else
s

safeEncodeMultilineString : String -> Encoder
safeEncodeMultilineString s =
string (String.join "\n" (List.map encodeQuotes (String.split "\n" s)))

safeEncodeKey : String -> Encoder
safeEncodeKey key =
if String.contains ":" key || String.contains " " key then
string (quote key)
else
string key


{-| Keep track of Encoder state while encoding
Expand Down
Loading