From 732a152f775205c53cfeff763da003f8caf6a6b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Bartelme=C3=9F?= Date: Mon, 9 Jul 2012 14:21:11 +0200 Subject: [PATCH] added settings2yml --- bin/settings2yml | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100755 bin/settings2yml diff --git a/bin/settings2yml b/bin/settings2yml new file mode 100755 index 000000000..d8847dc06 --- /dev/null +++ b/bin/settings2yml @@ -0,0 +1,58 @@ +#!/usr/bin/env node +var fs = require('fs'), + jsparser = require('uglify-js').parser; + + +if(process.argv.length != 3){ + console.error('Usage: ' + process.argv[1] + ' [filename]'); + process.exit(1); +} +var settingsStr = fs.readFileSync(process.argv[2]).toString(); + +var token = {}; +var t = jsparser.tokenizer(settingsStr); + + +indent = -1 + +var ymlStr = ''; + +var valueIsNext = false; + +function escapeString(s){ + if(s.indexOf('\n') != -1 || s.indexOf(':') != -1 || s.indexOf('\'') != -1 ){ + return JSON.stringify(s); + } + return s; +} + +function indentation(i){ + s = '' + while (i-- > 0) s += ' ' + return s; +} + + +while((token = t()).type !== 'eof'){ + var type = token.type, + value = token.value, + comments = token.comments_before.map(function(c){return c.value.split('\n') }) + if(type == 'punc' && value == '{') { indent++; ymlStr += "\n"; valueIsNext = false}; + if(type == 'punc' && value == '}') indent--; + if(type == 'punc' && value == ':') {ymlStr += ' : '; valueIsNext = true }; + if(type == 'punc' && value == ',') {ymlStr += '\n'; valueIsNext = false }; + + if(type == 'string' || type == 'name' || type == 'num'){ + if(!valueIsNext){ + if(comments.length > 0){ + + ymlStr += indentation(indent) + "\n" + + comments.map(function(s){return "\n" + indentation(indent) + '#' + s.join("\n" + indentation(indent) + '#')}).join('\n') +'\n'; + } + ymlStr += indentation(indent); + } + ymlStr += (type == 'string') ? escapeString(value) : value; + } +} + +console.log(ymlStr); \ No newline at end of file