add support of multiline string

This commit is contained in:
Dmitriy Pleshevskiy 2024-04-20 23:43:21 +03:00
parent 8c4f3ca834
commit 3c08fa87d1
Signed by: pleshevskiy
GPG Key ID: 17041163DA10A9A2
2 changed files with 16 additions and 2 deletions

View File

@ -16,7 +16,7 @@
*/
let
inherit (builtins) isString isFloat isInt isBool isList isAttrs isNull isPath;
inherit (builtins) concatStringsSep filter mapAttrs attrValues concatLists;
inherit (builtins) concatStringsSep filter mapAttrs attrValues concatLists match;
isNotNull = v: !(isNull v);
excludeNull = expr: filter isNotNull expr;
@ -175,7 +175,10 @@ let
toLuaBool = expr: if expr then "true" else "false";
toLuaNumber = toString;
toLuaString = expr: "\"${validString expr}\"";
toLuaString = expr:
let str = validString expr; in
let isLines = match ".*\n.*" str; in
if isLines != null then "[[\n${str}]]" else "\"${str}\"";
toLuaList = onValue: expr:
let
wrapObj = expr: "{ ${concatStringsSep ", " expr} }";

View File

@ -35,6 +35,17 @@ with nix2lua; pkgs.lib.runTests {
expr = toLua "hello world";
expected = ''"hello world"'';
};
"test returns a multiline lua string" = {
expr = toLua ''
hello
world
'';
expected = ''
[[
hello
world
]]'';
};
"test returns a path as a lua string" = {
expr = toLua /hello/world;
expected = ''"/hello/world"'';