lib: escape \ symbol
This commit is contained in:
parent
dc43d7ceda
commit
65d4a061a4
2 changed files with 6 additions and 2 deletions
4
lib.nix
4
lib.nix
|
@ -16,7 +16,7 @@
|
|||
*/
|
||||
let
|
||||
inherit (builtins) isString isFloat isInt isBool isList isAttrs isNull isPath;
|
||||
inherit (builtins) concatStringsSep filter mapAttrs attrValues concatLists match;
|
||||
inherit (builtins) concatStringsSep filter mapAttrs attrValues concatLists match replaceStrings;
|
||||
|
||||
isNotNull = v: !(isNull v);
|
||||
excludeNull = expr: filter isNotNull expr;
|
||||
|
@ -176,7 +176,7 @@ let
|
|||
toLuaBool = expr: if expr then "true" else "false";
|
||||
toLuaNumber = toString;
|
||||
toLuaString = expr:
|
||||
let str = validString expr; in
|
||||
let str = replaceStrings [ "\\" ] [ "\\\\" ] (validString expr); in
|
||||
let isLines = match ".*\n.*" str; in
|
||||
if isLines != null then "[[\n${str}]]" else "\"${str}\"";
|
||||
toLuaList = onValue: expr:
|
||||
|
|
|
@ -35,6 +35,10 @@ with nix2lua; pkgs.lib.runTests {
|
|||
expr = toLua "hello world";
|
||||
expected = ''"hello world"'';
|
||||
};
|
||||
"test returns an escaped lua string" = {
|
||||
expr = toLua ''\hello world'';
|
||||
expected = ''"\\hello world"'';
|
||||
};
|
||||
"test returns a multiline lua string" = {
|
||||
expr = toLua ''
|
||||
hello
|
||||
|
|
Loading…
Reference in a new issue