-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
169 lines (130 loc) · 4.04 KB
/
index.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const bodyParser = require('body-parser')
const express = require('express')
const jsonfile = require('jsonfile')
const sortKeys = require('./lib/sortkeys')
const dataHelper = require('./lib/datahelper')
const app = express()
app.use(bodyParser.json())
app.use(express.static('public'))
// Get all data from disk.
app.get('/data', (req, res) => {
console.log('Getting data.')
const data = loadAllData()
res.send(data)
})
// Save data to source.
app.post('/save/:dataSourceName', (req, res) => {
const dataSourceName = req.params.dataSourceName
// Only support following dataSourceNames to be edited.
if (!['dictionary', 'terminology', 'translations'].includes(dataSourceName)) {
res.status(400).send('Invalid dataSourceName parameter!')
return
}
// Load data from file.
let sourceData = dataHelper.readFile(dataSourceName)
updateSourceData(sourceData, req.body)
// Write data to file.
// Sort items of terminology dataSourceName and dictionary dataSourceName.
dataHelper.writeFile(
dataSourceName,
sourceData,
['dictionary', 'terminology'].includes(dataSourceName)
)
// Ouput log.
console.log(
req.body.length === 1 && req.body[0].value === null
? `Delete ${dataSourceName}:`
: `Save to ${dataSourceName}:`
)
console.log(req.body)
res.send('OK')
})
app.listen(3000, () => {
console.log('App listening on port http://localhost:3000.')
})
function loadAllData() {
const allData = {}
for (let dataSourceName of [
'prototype_applications',
'translations',
'dictionary',
'terminology'
]) {
allData[dataSourceName] = dataHelper.readFile(dataSourceName, true)
}
const phabricatori18nFiles = dataHelper.readFile('phabricator_i18n_files')
const libphutili18nFiles = dataHelper.readFile('libphutil_i18n_files')
const discoverSimilars = dataHelper.readFile('discover_similars')
allData.categories = buildCategories(phabricatori18nFiles, libphutili18nFiles)
allData.similars = getSimilars(discoverSimilars, allData.translations)
return allData
}
// Update or delete items.
function updateSourceData(sourceData, items) {
items.forEach(item => {
// Value of item to be deleted is null, then delete it.
if (!item.value) {
delete sourceData[item.key]
return
}
// Trim value if key can be trimed.
sourceData[item.key] =
item.key !== item.key.trim() ? item.value : item.value.trim()
})
}
function buildCategories(phabricatori18nFiles, libphutili18nFiles) {
const categories = {}
getCategories(categories, phabricatori18nFiles, '')
if (dataHelper.INCLUDE_LIBPHUTIL) {
getCategories(categories, libphutili18nFiles, '[libphutil]')
}
return sortKeys(categories)
}
function getCategories(categories, i18nFiles, prefix) {
for (let file in i18nFiles.files) {
if (!dataHelper.INCLUDE_TEST_CASE && file.endsWith('TestCase.php')) {
continue
}
const pathFragments = file.split('/')
// Group category by applications.
const category =
prefix +
(pathFragments[0] === 'applications'
? pathFragments[0] + '/' + pathFragments[1]
: pathFragments[0])
// Init empty category.
if (categories[category] === undefined) {
categories[category] = {}
}
// Get strings by file id.
const fileId = i18nFiles.files[file]
const strings = i18nFiles.strings[fileId] || []
// Set strings to cateogries.
strings.forEach(item => {
// Filter some empty strings. They are pht('') in PHP code.
if (item.string === '') {
return
}
categories[category][item.string] = ''
})
}
}
function getSimilars(similars, translations) {
const results = []
let groupTranslated
for (let str in similars) {
for (let sameSize in similars[str]) {
groupTranslated = true
for (let words of similars[str][sameSize]) {
if (translations[words] === undefined) {
groupTranslated = false
break
}
}
if (!groupTranslated) {
results.push(similars[str][sameSize])
}
}
}
return results
}