From d0f40ba727b502c6caba47e53c9df69198305bb9 Mon Sep 17 00:00:00 2001 From: Kameron Lutes Date: Fri, 30 May 2025 05:54:43 +0900 Subject: [PATCH] Add cmdline option to quote and keep multiline strings Specifying the -quote options forcibly disables multiline strings. In some cases it is nice to have normally quoted singleline strings as well as multiline strings. Add a new cmdline option '-ml' that results in multiline strings in addition to normally quoted singleline strings. --- bin/hjson | 2 ++ lib/hjson-stringify.js | 2 +- test/assets/extra/force_ml_string_result.hjson | 9 +++++++++ test/assets/extra/force_ml_string_result.json | 4 ++++ test/assets/extra/force_ml_string_test.hjson | 9 +++++++++ test/assets/extra/force_ml_string_testmeta.hjson | 6 ++++++ test/assets/testlist.txt | 1 + 7 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/assets/extra/force_ml_string_result.hjson create mode 100644 test/assets/extra/force_ml_string_result.json create mode 100644 test/assets/extra/force_ml_string_test.hjson create mode 100644 test/assets/extra/force_ml_string_testmeta.hjson 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