What this command line utility does is to take a text file and update the content a bit before turning it into Markdown. For example, the first for loop searches all the straight quotes and replace them for curly quotes. The second loop searches for hr
tags and replace them for * * *
, then adds the noind
class to the p
that follows that hr
tag.
#!/usr/bin/env node
var fs = require('fs')
var program = require('commander')
var markdown = require('markdown').markdown
program
.version('0.0.1')
.usage('<keywords>')
.parse(process.argv)
if (!program.args.length)
program.help()
var filename = program.args[0]
fs.readFile(filename, 'utf8', function(err, data) {
if (err) throw err
var tree = markdown.parse(data)
;(function updateTree(jsonml) {
// replace straight quotes for curly quotes
for (i = 1; i < jsonml.length; i++) {
if (typeof jsonml[i][1] === "string" && jsonml[i][1].match(/"/g)) {
var p = jsonml[i]
for (j = 1; j < p.length; j++) {
if (typeof p[j] === "string" && p[j].match(/"/g)) {
p[j] = p[j].replace(/"(?=\b)/g, "“")
.replace(/"(?!\b)/g, "”")
}
}
}
if (typeof jsonml[i][1] === "string" && jsonml[i][1].match(/'/g)) {
var p = jsonml[i]
for (j = 1; j < p.length; j++) {
if (typeof p[j] === "string" && p[j].match(/'/g)) {
p[j] = p[j].replace(/'(?=\b)/g, "‘")
.replace(/'(?!\b)/g, "’")
}
}
}
}
// replace hr for * * * and add noind class to the p tag that follows it
for (i = 1; i < jsonml.length; i++) {
if (jsonml[i][0] === 'hr') {
var hr = jsonml[i]
var p = jsonml[i + 1]
hr.splice(0, 1, 'para', '* * *')
p.splice(1, 0, {'class': 'noind'})
}
}
})(tree)
var html = markdown.renderJsonML(markdown.toHTMLTree(tree))
console.log(html)
})
So my question is, how can I reduce the indentation level?