Skip to content

Instantly share code, notes, and snippets.

@LuoZijun
Last active February 17, 2016 16:08
Show Gist options
  • Save LuoZijun/1765e34de760588e8e3c to your computer and use it in GitHub Desktop.
Save LuoZijun/1765e34de760588e8e3c to your computer and use it in GitHub Desktop.
JSX Parse

JSX 字符串提取

代码依赖 Node.js 运行环境以及 Python 运行环境。

下面将安装 Node.js 和 Python 运行时:

brew install node
brew install python
brew install npm

具体使用范例:

# 克隆本脚本
git clone https://gist.github.com/1765e34de760588e8e3c.git workspace
cd workspace
# 使用 Npm 安装 Babel 语法解析库
npm install

# 创建数据目录
mkdir data && cd data
# 克隆待处理的项目代码
git clone https://github.com/Graylog2/graylog2-server.git
cd ../
# 开始分析JSX语法树,这将需要一些时间 ...
python parse.py ./data
{
"name": "test",
"version": "0.0.1",
"description": "",
"author": "",
"devDependencies": {
"babel-core": "^6.3.21",
"babel-loader": "^6.2.0",
"babel-plugin-syntax-decorators": "^6.3.13",
"babel-plugin-transform-decorators": "^6.3.13",
"babel-polyfill": "^6.3.14",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-stage-0": "^6.3.13",
"babel-preset-stage-1": "^6.3.13",
"babel-preset-stage-2": "^6.3.13",
"babel-preset-stage-3": "^6.3.13"
}
}
#!/usr/local/bin/node
var fs = require('fs');
var babel = require("babel-core");
function readFile(filename) {
return fs.readFileSync(filename).toString();
}
function parse (code, _label){
_label = _label ? _label : 'jsxText';
var result = babel.transform(code, {
presets: ['es2015', 'react', 'stage-0', 'stage-1', 'stage-2', 'stage-3']
});
var tokens = result.ast.tokens;
return tokens.map(function (token, index){
var label = token.type.label;
var value = token.value;
if ( token.type.isLoop ) {
console.info(token);
}
if ( label == _label) {
return value;
}
}).filter(function (value, index){
if ( typeof value === "string" ) {
return /\S+/.test(value);
} else {
return false;
}
});
}
function main () {
var filename = undefined;
if ( process.argv.length > 2 ) {
filename = process.argv[2];
}
if ( !filename ) {
console.error("$ node parse.js app.jsx ...")
return false;
}
var code = readFile(filename);
var lines = parse(code, 'jsxText');
process.stdout.write(JSON.stringify(lines));
}
main();
#!/usr/bin/env python
#coding: utf8
import os,sys,subprocess,json
def get_files(target):
result = []
for root, dirs, files in os.walk(target):
# # or fname[-3:] == ".js"
files = filter(lambda fname: fname[-4:] == ".jsx", files)
result += map(lambda fname: os.path.join(root, fname), files)
return list(set(result))
def parse(filename):
print("\t@ $ node parse.js %s" % filename)
p = subprocess.Popen(["node", "parse.js", filename], stdout=subprocess.PIPE)
output = p.communicate()
try:
lines = json.loads(output[0])
except:
lines = []
if len(lines) > 0:
print "\t", lines
return lines
if __name__ == '__main__':
if len(sys.argv) < 2:
print(u"$ python parse.py .")
sys.exit()
name = sys.argv[1]
if os.path.isfile(name): files = [name]
else: files = get_files(name)
result = map(lambda filename: parse(filename), files)
print "\n", ("*"*50+"\n")*3
print "**\tLines:"
print "\t\t ", result
print "\n\n**\tDone."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment