diff --git a/bin/hjson b/bin/hjson index 25ed3ac..1f07cce 100644 --- a/bin/hjson +++ b/bin/hjson @@ -32,6 +32,7 @@ if (args["-help"] || args["?"] || args.h) { console.error(" -sl output the opening brace on the same line"); console.error(" -quote quote all strings"); console.error(" -quote=all quote keys as well"); + console.error(" -ml use multiline format for multiline strings"); console.error(" -js output in JavaScript/JSON compatible format"); console.error(" can be used with -rt and // comments"); console.error(" -rt round trip comments"); @@ -88,6 +89,7 @@ function convert(text) { quotes: args.quote, separator: args.js, keepWsc: args.rt, + multiline: args.ml ? "force" : "std", colors: !args.nocol && process.stdout.isTTY, dsf: dsf, }); diff --git a/lib/hjson-stringify.js b/lib/hjson-stringify.js index 6d800af..4cfb935 100644 --- a/lib/hjson-stringify.js +++ b/lib/hjson-stringify.js @@ -47,7 +47,7 @@ module.exports = function(data, opt) { bracesSameLine = opt.bracesSameLine; quoteKeys = opt.quotes === 'all' || opt.quotes === 'keys'; quoteStrings = opt.quotes === 'all' || opt.quotes === 'strings' || opt.separator === true; - if (quoteStrings || opt.multiline == 'off') multiline = 0; + if ((quoteStrings && opt.multiline !== 'force') || opt.multiline == 'off') multiline = 0; else multiline = opt.multiline == 'no-tabs' ? 2 : 1; separator = opt.separator === true ? token.com[0] : ''; dsfDef = opt.dsf; diff --git a/test/assets/extra/force_ml_string_result.hjson b/test/assets/extra/force_ml_string_result.hjson new file mode 100644 index 0000000..5bcc669 --- /dev/null +++ b/test/assets/extra/force_ml_string_result.hjson @@ -0,0 +1,9 @@ +{ + unquoted_string: "an unquoted normal string" + ml_string: + ''' + this is + a multiline + string + ''' +} \ No newline at end of file diff --git a/test/assets/extra/force_ml_string_result.json b/test/assets/extra/force_ml_string_result.json new file mode 100644 index 0000000..989d7b1 --- /dev/null +++ b/test/assets/extra/force_ml_string_result.json @@ -0,0 +1,4 @@ +{ + "unquoted_string": "an unquoted normal string", + "ml_string": "this is\na multiline\nstring" +} \ No newline at end of file diff --git a/test/assets/extra/force_ml_string_test.hjson b/test/assets/extra/force_ml_string_test.hjson new file mode 100644 index 0000000..ec93b50 --- /dev/null +++ b/test/assets/extra/force_ml_string_test.hjson @@ -0,0 +1,9 @@ +{ + unquoted_string: an unquoted normal string + ml_string: + ''' + this is + a multiline + string + ''' +} diff --git a/test/assets/extra/force_ml_string_testmeta.hjson b/test/assets/extra/force_ml_string_testmeta.hjson new file mode 100644 index 0000000..d01d2cc --- /dev/null +++ b/test/assets/extra/force_ml_string_testmeta.hjson @@ -0,0 +1,6 @@ +{ + options: { + multiline: force + quotes: strings + } +} diff --git a/test/assets/testlist.txt b/test/assets/testlist.txt index eae48cf..502f943 100644 --- a/test/assets/testlist.txt +++ b/test/assets/testlist.txt @@ -82,6 +82,7 @@ stringify/quotes_always_test.hjson stringify/quotes_keys_test.hjson stringify/quotes_strings_ml_test.json stringify/quotes_strings_test.hjson +extra/force_ml_string_test.hjson extra/notabs_test.json extra/root_test.hjson extra/separator_test.json \ No newline at end of file