Skip to content

Instantly share code, notes, and snippets.

@tedzhou
Created June 11, 2014 05:32
Show Gist options
  • Save tedzhou/7bcc1565e5273c985ddf to your computer and use it in GitHub Desktop.
Save tedzhou/7bcc1565e5273c985ddf to your computer and use it in GitHub Desktop.
为mac automator写的把词法定义的的字典改成用setValue的方式
#!/usr/bin/env node
var stdin = process.stdin,
stdout = process.stdout;
inputChunks = [];
var reg = /(.*?)\s?=?\s?@\{([\s\S]*)\}/;
stdin.resume();
stdin.setEncoding('utf8');
stdin.on('data', function (chunk) {
// console.log(chunk);
inputChunks.push(chunk);
});
stdin.on('end', function () {
var expr = inputChunks.join('');
try {
// console.log(inputChunks);
var result = [];
var matches = expr.match(reg);
var dictionaryStr = matches[2];
var emptySpace = '';
var prefixExp = matches[1];
var finded = false;
if(prefixExp) {
[].forEach.call(prefixExp, function(s){
if(!finded) {
if(s == ' '){
emptySpace += ' ';
} else {
finded = true;
}
}
})
} else {
[].forEach.call(calldictionaryStr, function(s){
if(!finded) {
if(s == ' '){
emptySpace += ' ';
} else {
finded = true;
}
}
})
}
var keyAndValue = dictionaryStr.split(',');
var result = [];
result.push(emptySpace + 'NSMutableDictionary* userDict = [NSMutableDictionary dictionary];');
keyAndValue.forEach(function(keyValueStr){
if (keyValueStr.trim().length > 0) {
var keyValueArr = keyValueStr.split(':');
var key = keyValueArr[0].trim();
var value = keyValueArr[1].trim();
result.push(emptySpace + '[userDict setValue:'+value+' forKey:'+key+'];');
}
});
if(prefixExp) {
result = result.concat([prefixExp + ' = userDict;']);
}
stdout.write(result.join('\n'));
stdout.write('\n\n');
} catch(ex){
stdout.write(expr+'\n//l2s error:' + ex);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment