2015-06-01 12:52:33 -06:00
|
|
|
#!/usr/bin/env coffee
|
|
|
|
|
|
|
|
# Dependencies
|
|
|
|
Handlebars = require('handlebars')
|
|
|
|
Beautifiers = require("../src/beautifiers")
|
|
|
|
fs = require('fs')
|
2016-03-21 10:46:52 -06:00
|
|
|
_ = require('lodash')
|
2016-07-02 16:28:01 -06:00
|
|
|
path = require('path')
|
|
|
|
pkg = require('../package.json')
|
2015-06-01 12:52:33 -06:00
|
|
|
|
|
|
|
console.log('Generating options...')
|
|
|
|
beautifier = new Beautifiers()
|
2015-06-01 13:50:09 -06:00
|
|
|
languageOptions = beautifier.options
|
|
|
|
packageOptions = require('../src/config.coffee')
|
2015-06-08 16:05:03 -06:00
|
|
|
# Build options by Beautifier
|
2016-07-02 16:28:01 -06:00
|
|
|
beautifiersMap = _.keyBy(beautifier.beautifiers, 'name')
|
|
|
|
languagesMap = _.keyBy(beautifier.languages.languages, 'name')
|
2015-06-08 16:05:03 -06:00
|
|
|
beautifierOptions = {}
|
2016-03-21 10:46:52 -06:00
|
|
|
for lo, optionGroup of languageOptions
|
|
|
|
for optionName, optionDef of optionGroup.properties
|
2015-06-08 16:05:03 -06:00
|
|
|
beautifiers = optionDef.beautifiers ? []
|
|
|
|
for beautifierName in beautifiers
|
2016-03-21 10:46:52 -06:00
|
|
|
beautifierOptions[beautifierName] ?= {}
|
|
|
|
beautifierOptions[beautifierName][optionName] = optionDef
|
2015-06-01 12:52:33 -06:00
|
|
|
|
|
|
|
console.log('Loading options template...')
|
2016-07-02 16:28:01 -06:00
|
|
|
readmeTemplatePath = path.resolve(__dirname, '../README-template.md')
|
|
|
|
readmePath = path.resolve(__dirname, '../README.md')
|
2015-06-01 13:50:09 -06:00
|
|
|
optionsTemplatePath = __dirname + '/options-template.md'
|
|
|
|
optionTemplatePath = __dirname + '/option-template.md'
|
2016-03-21 10:46:52 -06:00
|
|
|
optionGroupTemplatePath = __dirname + '/option-group-template.md'
|
2015-06-01 12:52:33 -06:00
|
|
|
optionsPath = __dirname + '/options.md'
|
2016-07-02 16:28:01 -06:00
|
|
|
|
2015-06-01 13:50:09 -06:00
|
|
|
optionsTemplate = fs.readFileSync(optionsTemplatePath).toString()
|
2016-03-21 10:46:52 -06:00
|
|
|
optionGroupTemplate = fs.readFileSync(optionGroupTemplatePath).toString()
|
2015-06-01 13:50:09 -06:00
|
|
|
optionTemplate = fs.readFileSync(optionTemplatePath).toString()
|
2016-07-02 16:28:01 -06:00
|
|
|
readmeTemplate = fs.readFileSync(readmeTemplatePath).toString()
|
2015-06-01 13:50:09 -06:00
|
|
|
|
2015-06-01 12:52:33 -06:00
|
|
|
console.log('Building documentation from template and options...')
|
2015-06-01 13:50:09 -06:00
|
|
|
Handlebars.registerPartial('option', optionTemplate)
|
2016-03-21 10:46:52 -06:00
|
|
|
Handlebars.registerPartial('option-group', optionGroupTemplate)
|
2015-06-01 13:50:09 -06:00
|
|
|
template = Handlebars.compile(optionsTemplate)
|
2016-07-02 16:28:01 -06:00
|
|
|
readmeTemplate = Handlebars.compile(readmeTemplate)
|
2015-06-09 09:02:52 -06:00
|
|
|
|
|
|
|
linkifyTitle = (title) ->
|
2016-03-21 10:46:52 -06:00
|
|
|
title = title.toLowerCase()
|
|
|
|
p = title.split(/[\s,+#;,\/?:@&=+$]+/) # split into parts
|
|
|
|
sep = "-"
|
|
|
|
p.join(sep)
|
2015-06-09 09:02:52 -06:00
|
|
|
|
|
|
|
Handlebars.registerHelper('linkify', (title, options) ->
|
2016-03-21 10:46:52 -06:00
|
|
|
return new Handlebars.SafeString(
|
|
|
|
"[#{options.fn(this)}](\##{linkifyTitle(title)})"
|
|
|
|
)
|
2015-06-09 09:02:52 -06:00
|
|
|
)
|
|
|
|
|
2015-08-31 16:25:40 -06:00
|
|
|
exampleConfig = (option) ->
|
|
|
|
# console.log(option)
|
|
|
|
t = option.type
|
|
|
|
d = switch
|
|
|
|
when option.default? then option.default
|
|
|
|
when t is "string" then ""
|
|
|
|
when t is "integer" then 0
|
|
|
|
when t is "boolean" then false
|
|
|
|
else null
|
|
|
|
|
|
|
|
json = {}
|
|
|
|
namespace = option.language.namespace
|
|
|
|
k = option.key
|
|
|
|
c = {}
|
|
|
|
c[k] = d
|
|
|
|
json[namespace] = c
|
|
|
|
return """```json
|
|
|
|
#{JSON.stringify(json, undefined, 4)}
|
|
|
|
```"""
|
|
|
|
|
|
|
|
Handlebars.registerHelper('example-config', (key, option, options) ->
|
|
|
|
results = exampleConfig(key, option)
|
|
|
|
# console.log(results)
|
|
|
|
return new Handlebars.SafeString(results)
|
|
|
|
)
|
2015-06-09 09:02:52 -06:00
|
|
|
|
2016-07-02 16:28:01 -06:00
|
|
|
Handlebars.registerHelper('language-beautifiers-support', (languageOptions, options) ->
|
|
|
|
|
|
|
|
###
|
|
|
|
| Language | Supported Beautifiers |
|
|
|
|
| --- | ---- |
|
|
|
|
| JavaScript | Js-Beautify, Pretty Diff |
|
|
|
|
###
|
|
|
|
|
|
|
|
rows = _.map(languageOptions, (val, k) ->
|
|
|
|
name = val.title
|
|
|
|
defaultBeautifier = _.get(val, "properties.default_beautifier.default")
|
|
|
|
beautifiers = _.map(val.beautifiers, (b) ->
|
|
|
|
beautifier = beautifiersMap[b]
|
|
|
|
isDefault = b is defaultBeautifier
|
|
|
|
if beautifier.link
|
|
|
|
r = "[`#{b}`](#{beautifier.link})"
|
|
|
|
else
|
|
|
|
r = "`#{b}`"
|
|
|
|
if isDefault
|
|
|
|
r += " (Default)"
|
|
|
|
return r
|
|
|
|
)
|
|
|
|
grammars = _.map(val.grammars, (b) -> "`#{b}`")
|
|
|
|
extensions = _.map(val.extensions, (b) -> "`.#{b}`")
|
|
|
|
|
|
|
|
return "| #{name} | #{grammars.join(', ')} |#{extensions.join(', ')} | #{beautifiers.join(', ')} |"
|
|
|
|
)
|
|
|
|
results = """
|
|
|
|
| Language | Grammars | File Extensions | Supported Beautifiers |
|
|
|
|
| --- | --- | --- | ---- |
|
|
|
|
#{rows.join('\n')}
|
|
|
|
"""
|
|
|
|
return new Handlebars.SafeString(results)
|
|
|
|
)
|
|
|
|
|
2016-07-04 20:42:22 -06:00
|
|
|
Handlebars.registerHelper('language-options-support', (languageOptions, options) ->
|
|
|
|
|
|
|
|
###
|
|
|
|
| Option | PrettyDiff | JS-Beautify |
|
|
|
|
| --- | --- | --- |
|
|
|
|
| `brace_style` | ? | ? |
|
|
|
|
| `break_chained_methods` | ? | ? |
|
|
|
|
| `end_with_comma` | ? | ? |
|
|
|
|
| `end_with_newline` | ? | ? |
|
|
|
|
| `eval_code` | ? | ? |
|
|
|
|
| `indent_size` | :white_check_mark: | :white_check_mark: |
|
|
|
|
| `indent_char` | :white_check_mark: | :white_check_mark: |
|
|
|
|
###
|
|
|
|
|
|
|
|
rows = []
|
|
|
|
beautifiers = languageOptions.beautifiers.sort()
|
|
|
|
headers = ['Option'].concat(beautifiers)
|
|
|
|
rows.push(headers)
|
|
|
|
rows.push(_.map(headers, () -> '---'))
|
|
|
|
# console.log(languageOptions)
|
|
|
|
_.each(Object.keys(languageOptions.properties), (op) ->
|
|
|
|
field = languageOptions.properties[op]
|
|
|
|
support = _.map(beautifiers, (b) ->
|
|
|
|
if (_.includes(field.beautifiers, b) or _.includes(["disabled", "default_beautifier", "beautify_on_save"], op))
|
|
|
|
return ':white_check_mark:'
|
|
|
|
else
|
|
|
|
return ':x:'
|
|
|
|
)
|
|
|
|
rows.push(["`#{op}`"].concat(support))
|
|
|
|
)
|
|
|
|
|
|
|
|
results = _.map(rows, (r) -> "| #{r.join(' | ')} |").join('\n')
|
|
|
|
return new Handlebars.SafeString(results)
|
|
|
|
)
|
|
|
|
|
2016-03-21 10:46:52 -06:00
|
|
|
sortKeysBy = (obj, comparator) ->
|
|
|
|
keys = _.sortBy(_.keys(obj), (key) ->
|
|
|
|
return if comparator then comparator(obj[key], key) else key
|
|
|
|
)
|
|
|
|
return _.zipObject(keys, _.map(keys, (key) ->
|
|
|
|
return obj[key]
|
|
|
|
))
|
|
|
|
|
|
|
|
sortSettings = (settings) ->
|
|
|
|
# TODO: Process object type options
|
|
|
|
r = _.mapValues(settings, (op) ->
|
|
|
|
if op.type is "object" and op.properties
|
|
|
|
op.properties = sortSettings(op.properties)
|
|
|
|
return op
|
|
|
|
)
|
|
|
|
# Process these settings
|
|
|
|
r = sortKeysBy(sortKeysBy(r), (op) -> op.order)
|
|
|
|
# r = _.chain(r).sortBy((op) -> op.key).sortBy((op) -> settings[op.key]?.order).value()
|
|
|
|
# console.log('sort', settings, r)
|
|
|
|
return r
|
|
|
|
|
2015-06-01 12:52:33 -06:00
|
|
|
context = {
|
2016-07-02 16:28:01 -06:00
|
|
|
package: pkg,
|
2016-03-21 10:46:52 -06:00
|
|
|
packageOptions: sortSettings(packageOptions)
|
|
|
|
languageOptions: sortSettings(languageOptions)
|
|
|
|
beautifierOptions: sortSettings(beautifierOptions)
|
2015-06-01 12:52:33 -06:00
|
|
|
}
|
|
|
|
result = template(context)
|
2016-07-02 16:28:01 -06:00
|
|
|
readmeResult = readmeTemplate(context)
|
2015-06-01 12:52:33 -06:00
|
|
|
|
|
|
|
console.log('Writing documentation to file...')
|
|
|
|
fs.writeFileSync(optionsPath, result)
|
2016-07-02 16:28:01 -06:00
|
|
|
fs.writeFileSync(readmePath, readmeResult)
|
|
|
|
# fs.writeFileSync(__dirname+'/context.json', JSON.stringify(context, undefined, 2))
|
|
|
|
|
|
|
|
console.log('Updating package.json')
|
|
|
|
# Add Language keywords
|
|
|
|
ls = _.map(Object.keys(languagesMap), (a)->a.toLowerCase())
|
|
|
|
|
|
|
|
# Add Beautifier keywords
|
|
|
|
bs = _.map(Object.keys(beautifiersMap), (a)->a.toLowerCase())
|
|
|
|
keywords = _.union(pkg.keywords, ls, bs)
|
|
|
|
pkg.keywords = keywords
|
|
|
|
fs.writeFileSync(path.resolve(__dirname,'../package.json'), JSON.stringify(pkg, undefined, 2))
|
2015-06-01 12:52:33 -06:00
|
|
|
|
2015-06-09 09:02:52 -06:00
|
|
|
console.log('Done.')
|