Created
October 20, 2011 06:51
-
-
Save yuanchuan/1300578 to your computer and use it in GitHub Desktop.
一个两小时的编程实践
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var http = require('http') | |
, fs = require('fs') | |
, url = require('url') | |
, exec = require('child_process').exec | |
, qs = require('querystring') | |
, path = require('path'); | |
var static = { | |
xmlInput: 'input/IB.xml', | |
xmlOutput: 'output/IB.xml', | |
json: 'output/rules.json' | |
}; | |
/** | |
* 根据接收到的JSON文件里的数据,在浏览器中更新XMl节点的值, | |
* 然后把更新之后的XML的内容传递到服务器. | |
*/ | |
function jsSrc(fn) { | |
return ('<script>('+ fn.toString()+')()</script>'); | |
} | |
var clientjs = jsSrc(function() { | |
$(function(){ | |
$.getJSON('/json?s='+ (+new Date), function(rules) { | |
var ibs = $('ib'); | |
$.each(rules, function(i, s){ | |
var ib = ibs.filter(function(){ | |
return $(this).find('id').html() == s.id; | |
}); | |
ib.find('price').html(s.price); | |
ib.find('pricez').html( | |
/^不/.test(s.pricez) ? (s.price) : (s.pricez) | |
); | |
}); | |
$.post('/', {xml: $('#xml').html()}, function(msg) { | |
alert(msg); | |
}); | |
}); | |
}); | |
}); | |
/** | |
* 从浏览器传回来的XML标签都变成了小写,因此在保存前需要转换一下. | |
*/ | |
var replaceTag = (function() { | |
var tags = { | |
lowercase: [ | |
'ibinfo', 'ib', 'id', 'name', 'desc', 'icon', 'type', 'price', | |
'pricez', 'classid', 'pos', 'uselv', 'userank', 'bind', 'sell', | |
'sellprice', 'getway', 'state', 'dig', 'outfit', 'effect', 'ibeffect' | |
], | |
original: [ | |
'IBInfo', 'IB', 'Id', 'Name', 'Desc', 'Icon', 'Type', 'Price', | |
'Pricez', 'ClassId', 'Pos', 'UseLv', 'UseRank', 'Bind', 'Sell', | |
'SellPrice', 'GetWay', 'State', 'Dig', 'Outfit', 'Effect', 'IBEffect' | |
] | |
}; | |
function _replace(str, s, t) { | |
var reg = RegExp('(<|<\/)' +s+ '(>)', 'g'); | |
return str.replace(reg, '$1'+t+'$2'); | |
} | |
return function(xml) { | |
tags.lowercase.forEach(function(n, i) { | |
xml = _replace(xml, n, tags.original[i]); | |
}); | |
return xml; | |
} | |
}()); | |
/** | |
* 读取原始的XML文件,把它的内容嵌入html文件当中. | |
* 建立web服务器,返回指定的资源文件. | |
*/ | |
fs.readFile(static.xmlInput, function(err, xml) { | |
var server = http.createServer(function(req, res) { | |
var pathname = url.parse(req.url).pathname | |
, fname = path.join(process.cwd(), pathname) | |
, buffer = []; | |
// 接收从浏览器传回来的数据. | |
if (req.method == 'POST') { | |
req.on('data', function(data) { | |
buffer.push(data); | |
}); | |
req.on('end', function() { | |
var post = qs.parse(buffer.join('')) | |
, msg = 'saved'; | |
// 写入到文件当中. | |
fs.writeFile(static.xmlOutput, replaceTag(post.xml), function(err) { | |
console.log(msg); | |
res.end(msg); | |
process.exit(); | |
}); | |
}); | |
// 返回指定的资源. | |
} else if (req.method == 'GET') { | |
if (pathname == '/') { | |
res.writeHead(200, {'Content-Type': 'text/html'}); | |
res.end([ | |
'<html>', | |
'<head>', | |
'<meta charset="utf-8"/>', | |
'<script src="lib/jquery.js"></script>', | |
'</head>', | |
'<body>', | |
'<div id="xml">', | |
xml, | |
'</div>', | |
clientjs, | |
'</body>', | |
'</html>' | |
].join('')); | |
// 我知道这里有重复:D | |
} else if(/\.js$/.test(pathname)) { | |
fs.readFile(fname, function(err, output) { | |
res.writeHead(200, {'Content-Type': 'text/javascript'}); | |
res.end(output); | |
}); | |
} else if (/\/json$/.test(pathname)) { | |
fs.readFile(static.json, function(err, output) { | |
res.writeHead(200, {'Content-Type': 'application/json'}); | |
res.end(output); | |
}); | |
} | |
} | |
}); | |
// 打开服务器端口,运行浏览器并把url地址指向它. | |
server.listen(3000, 'localhost', function(){ | |
console.log('opening..'); | |
exec('google-chrome http://localhost:3000/'); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
#从Excel文件导出的文本文件 | |
input=input/rules.txt | |
#用awk获取必要的字段,并格式化为json文件 | |
output=output/rules.json | |
awk ' | |
BEGIN { print "[" } | |
NR>3 { print "{\"id\":\"",$1,"\",\"price\":\"",$4,"\",\"pricez\":\"",$5,"\"}," } | |
END { print "]" } | |
' $input | tr -d '\r\n ' | sed -e 's/\,\]/\]/' | cat > $output | |
echo 'done' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment