#!/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'})
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(
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'
)
compress: false,
output: {
beautify: true,
- indent_level: 2,
+ indent_level: options.indent,
shebang: false
},
toplevel: true
//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 ? '<!' : '<!--'
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('')
}