diff --git a/lib.nix b/lib.nix index 14f25b5..9395260 100644 --- a/lib.nix +++ b/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: diff --git a/lib.test.nix b/lib.test.nix index 34d1822..48c3b66 100644 --- a/lib.test.nix +++ b/lib.test.nix @@ -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