MaybeJustJames / yaml

Work with YAML in Elm.
BSD 3-Clause "New" or "Revised" License
9 stars 5 forks source link

Multi-line string #31

Open ypyl opened 1 year ago

ypyl commented 1 year ago

it seems the parser converts multiline string to simple string, e.g.

description: |
  Some information

will be saved internally as

description: | Some description

My assumption is that it should be like

description: |\n  Some description
RDBModel commented 1 year ago

I was able to add such support by modifying multilineStep and postProcessString in Util.elm file:

multilineStep : Int -> List String -> P.Parser (P.Step (List String) String)
multilineStep indent lines =
    let
        multilineString : List String -> String
        multilineString lines_ =
            String.join "\n" (List.reverse lines_)

        conclusion line indent_ =
            let
                intendedLine =
                    if List.isEmpty lines then
                        line
                    else
                        String.repeat (indent + 1) " " ++ line
            in
            if indent_ > indent then
                P.Loop (intendedLine :: lines)

            else
                P.Done (multilineString (intendedLine :: lines))
    in
    P.oneOf
        [ P.succeed conclusion
            |= characters (not << isNewLine)
            |. P.chompIf isNewLine
            |. spaces
            |= P.getCol
        , P.succeed (P.Done <| multilineString lines)
        ]
  postProcessString : String -> String
postProcessString str =
    -- let
    --     regexFromString : String -> Regex
    --     regexFromString =
    --         Regex.fromString >> Maybe.withDefault Regex.never
    -- in
    str
        -- |> Regex.replace (regexFromString "\\s\\s+")
        --     (\match ->
        --         if String.contains "\n\n" match.match then
        --             "\n"

        --         else
        --             " "
        --     )
RDBModel commented 1 year ago

I will be happy to prepare PR if my change makes sense. As I am basically removing postProcessString and I am not sure that it is correct.

MaybeJustJames commented 1 year ago

If it passes the tests then I'm happy to accept it

RDBModel commented 1 year ago

I need some time to understand how to write/run and check unit tests in Elm. As for Elm, unfortunately I am following the next principle at least for now: image

lovebug356 commented 8 months ago

An improvement has been proposed in https://github.com/MaybeJustJames/yaml/pull/36 to parse literal multi-line strings, with tests ;-).