Repository: Automattic/juice
Pull request #327 (2018-12-18 21:54:09)

Add codeBlocks support to optionsFile.
- In order to support automated build processes, optionsFile now supports the addition of `codeBlocks` as an option, allowing parsing of non-standard templating languages such as FreeMarker.


Patch:
diff --git a/README.md b/README.md
index 635fe8f..7d6945e 100644
--- a/README.md
+++ b/README.md
@@ -180,6 +180,7 @@ These are additional options not included in the standard `juice` options listed
 
 - `--css [filepath]` will load and inject CSS into `extraCss`.
 - `--options-file [filepath]` will load and inject options from a JSON file. Options from the CLI will be given priority over options in the file when there is a conflict.
+- `codeBlocks` is optionally supported in the options file if you include it. This will allow you to support different template languages in a build process.
 
 ### Running Juice in the Browser
 
diff --git a/index.js b/index.js
index c966e14..a91aa59 100644
--- a/index.js
+++ b/index.js
@@ -44,6 +44,12 @@ function juiceFile(filePath, options, callback) {
       return callback(err);
     }
     options = utils.getDefaultOptions(options); // so we can mutate options without guilt
+    // Optional support for codeBlocks within optionsFile
+    if (options.codeBlocks) {
+      Object.keys(options.codeBlocks).forEach(function(key) {
+        juice.codeBlocks[key] = options.codeBlocks[key];
+      });
+    }
     if (!options.webResources.relativeTo) {
       var rel = path.dirname(path.relative(process.cwd(),filePath));
       options.webResources.relativeTo = rel;


Test patch:
diff --git a/test/cases/cli/code-block-cli.css b/test/cases/cli/code-block-cli.css
new file mode 100644
index 0000000..e69de29
diff --git a/test/cases/cli/code-block-cli.html b/test/cases/cli/code-block-cli.html
new file mode 100644
index 0000000..c0b4f84
--- /dev/null
+++ b/test/cases/cli/code-block-cli.html
@@ -0,0 +1,3 @@
+<#if test??>
+    <p class="text"></p>
+</#if>
\ No newline at end of file
diff --git a/test/cases/cli/code-block-cli.json b/test/cases/cli/code-block-cli.json
new file mode 100644
index 0000000..d3b8b9f
--- /dev/null
+++ b/test/cases/cli/code-block-cli.json
@@ -0,0 +1,12 @@
+{
+    "codeBlocks": {
+        "freeMarkerStart": {
+            "start": "<#",
+            "end": ">"
+        },
+        "freeMarkerEnd": {
+            "start": "</#",
+            "end": ">"
+        }
+    }
+}
\ No newline at end of file
diff --git a/test/cases/cli/code-block-cli.out b/test/cases/cli/code-block-cli.out
new file mode 100644
index 0000000..c0b4f84
--- /dev/null
+++ b/test/cases/cli/code-block-cli.out
@@ -0,0 +1,3 @@
+<#if test??>
+    <p class="text"></p>
+</#if>
\ No newline at end of file
diff --git a/test/cli.js b/test/cli.js
index 6c1e44c..d885c8d 100644
--- a/test/cli.js
+++ b/test/cli.js
@@ -93,3 +93,22 @@ it('cli options included', function(done) {
     done();
   });
 });
+
+it('cli supports codeBlock', function(done) {
+  var htmlPath = 'test/cases/cli/code-block-cli.html';
+  var optionsFilePath = 'test/cases/cli/code-block-cli.json';
+  var expectedPath = 'test/cases/cli/code-block-cli.out';
+  var outputPath = 'tmp/code-block-cli.out';
+
+  var juiceProcess = spawn('bin/juice', [htmlPath, '--options-file', optionsFilePath, outputPath]);
+
+  juiceProcess.on('error', done);
+
+  juiceProcess.on('exit', function(code) {
+    assert(code === 0, 'Expected exit code to be 0');
+    var output = fs.readFileSync(outputPath, 'utf8').replace(/\r/g, '');
+    var expectedOutput = fs.readFileSync(expectedPath, 'utf8');
+    assert.equal(output, expectedOutput);
+    done();
+  });
+});
\ No newline at end of file