Add command line arguments to set indenting and wrapping options
authorNick Downing <nick@ndcode.org>
Mon, 19 Nov 2018 04:40:52 +0000 (15:40 +1100)
committerNick Downing <nick@ndcode.org>
Mon, 19 Nov 2018 04:40:52 +0000 (15:40 +1100)
cli.js
jstize.js

diff --git a/cli.js b/cli.js
index 038a3d5..a3ade7c 100755 (executable)
--- a/cli.js
+++ b/cli.js
@@ -1,10 +1,30 @@
 #!/usr/bin/env node
 
+let commander = require('commander')
 let fs = require('fs')
 let jstize = require('./jstize')
 
-fs.writeSync(
-  1,
-  jstize(fs.readFileSync(0, {encoding: 'utf-8'})),
-  {encoding: 'utf-8'}
+commander
+  .version('0.1.0', '-v, --version')
+  .option('-i, --indent <n>', 'indent [2]', '2')
+  .option('-j, --initial-indent <n>', 'initial indent [0]', '0')
+  .option('-n, --name <str>', 'message for --wrap [page.jst]', 'page.jst')
+  .option('-w, --wrap', 'wrap as jst_server template function', false)
+  .parse(process.argv)
+
+let indent = parseInt(commander.indent)
+let initial_indent = parseInt(commander.initialIndent)
+let text = jstize(
+  fs.readFileSync(0, {encoding: 'utf-8'}),
+  {
+    indent: indent,
+    initial_indent: initial_indent + commander.wrap ? indent : 0
+  }
 )
+if (commander.wrap)
+  text = `${' '.repeat(initial_indent)}return env => {
+${' '.repeat(initial_indent)}  let _out = []
+${text}${' '.repeat(initial_indent)}  _site.serve(env, 200, Buffer.from(_out.join('')), '${commander.name}')
+${' '.repeat(initial_indent)}}
+`
+fs.writeSync(1, text, {encoding: 'utf-8'})
index 257061e..923518f 100644 (file)
--- a/jstize.js
+++ b/jstize.js
@@ -5,9 +5,13 @@ let uglify_es = require('uglify-es')
 
 let clean_css = new CleanCSS({format: 'beautify'})
 
-let jstize = text => {
+let jstize = (text, options) => {
+  options = Object.assign(
+    {indent: 2, initial_indent: 0},
+    options || {}
+  )
   let tags = []
-  let indent = []
+  let indent = options.initial_indent
   let buffer = []
   let parse = text => {
     new html_minifier.HTMLParser(
@@ -48,30 +52,33 @@ let jstize = text => {
               suffix = ')'
             }
           }
-          out = out.replace(
-            /([.#])style$/,
-            (match, sep) => `${sep}'style'`
-          ).replace(
-            /([.#])([^.#]*-style)$/,
-            (match, sep, name) => `${sep}'${name}'`
-          )
           buffer.push(
-            indent.join('') + out + out1 + suffix + (unary ? ' {}\n' : ' {\n')
+            ' '.repeat(indent) +
+            out.replace(
+              /([.#])style$/,
+              (match, sep) => `${sep}'style'`
+            ).replace(
+              /([.#])([^.#]*-style)$/,
+              (match, sep, name) => `${sep}'${name}'`
+            ) +
+            out1 +
+            suffix +
+            (unary ? ' {}\n' : ' {\n')
           )
           if (!unary) {
             tags.push(tag)
-            indent.push('  ')
+            indent += options.indent
           }
         },
         end: tag => { //, attrs, autoGenerated) => {
           assert(tag === tags.pop())
-          indent.pop()
+          indent -= options.indent
           //buffer.push(`</${tag}>`)
           buffer.push(
             (
               buffer.length && buffer[buffer.length - 1].slice(-2) == '{\n' ?
                 buffer.pop().slice(0, -1) :
-                indent.join('')
+                ' '.repeat(indent)
             ) +
             '}\n'
           )
@@ -85,7 +92,7 @@ let jstize = text => {
                 compress: false,
                 output: {
                   beautify: true,
-                  indent_level: 2,
+                  indent_level: options.indent,
                   shebang: false
                 },
                 toplevel: true
@@ -111,7 +118,7 @@ let jstize = text => {
             //buffer.push(text)
             out = [`'${text}'`]
           for (let i = 0; i < out.length; ++i)
-            buffer.push(indent.join('') + out[i] + '\n')
+            buffer.push(' '.repeat(indent) + out[i] + '\n')
         },
         comment: (text, nonStandard) => {
           let prefix = nonStandard ? '<!' : '<!--'
@@ -119,28 +126,29 @@ let jstize = text => {
           let match = text.match(/^(\[if\s[^\]]+]>)([\s\S]*?)(<!\[endif])$/)
           if (match) {
             buffer.push(
-              `${indent.join('')}_out.push('${prefix}${match[1]}')\n`
+              `${' '.repeat(indent)}_out.push('${prefix}${match[1]}')\n`
             )
             parse(match[2])
             buffer.push(
-              `${indent.join('')}_out.push('${match[3]}${suffix}')\n`
+              `${' '.repeat(indent)}_out.push('${match[3]}${suffix}')\n`
             )
           }
           else
             buffer.push(
-              `${indent.join('')}_out.push('${prefix}${text}${suffix}')\n`
+              `${' '.repeat(indent)}_out.push('${prefix}${text}${suffix}')\n`
             )
         },
         doctype: doctype => {
           //buffer.push(doctype)
           buffer.push(
-            `${indent.join('')}_out.push('${doctype}')\n`
+            `${' '.repeat(indent)}_out.push('${doctype}')\n`
           )
         }
       }
     )
   }
   parse(html_minifier.minify(text, {collapseWhitespace: true}))
+  assert(tags.length === 0)
   return buffer.join('')
 }