forked from WeNeedHome/SummaryOfLoanSuspension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genProperties.ts
105 lines (89 loc) · 3.32 KB
/
genProperties.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import fs from "fs";
import {
PROJECT_DIR,
PROPERTIES_FLAT_PATH,
PROPERTIES_TREE_PATH,
README_PATH,
REG_CITY,
REG_END,
REG_ITEM_WITH_URI,
REG_PROV,
REG_START,
REG_TOTAL
} from "./const";
import { ITree } from "./itree/ds";
import { validateITree } from "./itree/validate";
import { tree2flat } from "./itree/reshape";
import { ArgumentParser } from "argparse";
import path from "path";
const parseItem = (s: string) => {
s = s.replace(/\s+/g, '')
if (!s) return; // no string to parse
// 首先确保要有一个city
if (tree.children.length === 0) return; // no provinces
let curProv = tree.children[tree.children.length - 1]; // curProv
if (curProv.children.length === 0) return; // no cities
let curCity = curProv.children[curProv.children.length - 1]; // curCity
s.split(',')
.forEach(s2 => {
s2 = s2.replace(/\s+/g, '')
if (!s2) return; // no string to parse
const mItemWithUri = s2.match(REG_ITEM_WITH_URI)
if (mItemWithUri)
curCity.children.push({name: mItemWithUri[1], uri: mItemWithUri[2]})
else {
curCity.children.push({name: s2, uri: ''})
}
})
}
/**
* 引入正向状态机机制,因此不需要回收,代码逻辑将更简洁
* @param line
*/
const parseLine = (line: string) => {
let mTotal = line.match(REG_TOTAL),
mProv = line.match(REG_PROV),
mCity = line.match(REG_CITY);
if (mTotal)
tree.count = parseInt(mTotal[1])
if (isStarted && REG_END.test(line))
isFinished = true
if (isFinished)
return;
if (!isStarted)
if (REG_START.test(line))
isStarted = true
else
return;
// 省份(单行)
if (mProv)
tree.children.push({name: mProv[1], count: parseInt(mProv[2]), children: []})
// 城市(原版:和楼盘连一起;新版:单行)
else if (mCity) {
tree.children[tree.children.length - 1] // curProvince
.children.push({name: mCity[1], count: parseInt(mCity[2]), children: []})
if (mCity[3]) parseItem(mCity[3])
}
// 可能是空行,或者是楼盘的换行,不管怎样,都可以解析为楼盘,没数据就不操作呗
else
parseItem(line)
}
export const genProperties = (readmeFilePath: string) => {
if (!readmeFilePath.startsWith('/'))
readmeFilePath = path.join(PROJECT_DIR, readmeFilePath)
console.log('reading readme from file://' + readmeFilePath)
fs.readFileSync(readmeFilePath, 'utf-8').split('\n').forEach(parseLine)
validateITree(tree)
console.log('passed validation √')
fs.writeFileSync(PROPERTIES_TREE_PATH, JSON.stringify(tree, undefined, 2), 'utf-8')
console.log('wrote tree-shape properties data into file://' + PROPERTIES_TREE_PATH)
fs.writeFileSync(PROPERTIES_FLAT_PATH, JSON.stringify(tree2flat(tree), undefined, 2), 'utf-8')
console.log('wrote flat-shape properties data into file://' + PROPERTIES_FLAT_PATH)
}
let tree: ITree = {name: '中华人民共和国', count: 0, children: []},
isStarted = false,
isFinished = false
const parser = new ArgumentParser()
parser.add_argument('-f', '--readmeFilePath', {default: README_PATH})
const args = parser.parse_args()
genProperties(args.readmeFilePath)