-
-
Notifications
You must be signed in to change notification settings - Fork 433
/
processUsers.js
149 lines (128 loc) · 4.07 KB
/
processUsers.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
const fs = require('fs');
const crypto = require("crypto")
const puppeteer = require("puppeteer");
const configDir = "./exampleSite/config/_default";
const defaultLang = "en";
const usersFolderPath = "./exampleSite/content/users/"
const translate = require('@iamtraction/google-translate');
var targetLangs = []
const configFiles = fs.readdirSync(configDir);
configFiles.forEach(file => {
if (file.indexOf("languages.") > -1) {
var lang = file.split(".")[1];
if (lang != defaultLang) {
targetLangs.push(lang);
}
}
});
const indexFiles = fs.readdirSync(usersFolderPath);
for (var i in targetLangs) {
var targetFile = '_index.' + targetLangs[i] + '.md';
if (indexFiles.indexOf(targetFile) == -1) {
fs.copyFileSync(usersFolderPath + '_index.md', usersFolderPath + targetFile);
}
}
let rawdata = fs.readFileSync(usersFolderPath + 'users.json');
let users = JSON.parse(rawdata);
let userDict = {}
for (var i in users) {
userDict[generateDirName(users[i].url)] = true;
}
const files = fs.readdirSync(usersFolderPath);
for (file in files) {
let stats = fs.statSync(usersFolderPath + files[file]);
if (files[file] != 'users.json' && files[file].indexOf("_index.") == -1) {
if (stats.isDirectory()) {
if (!userDict[files[file]]) {
console.log('deleting: ', files[file]);
fs.rmdirSync(usersFolderPath + files[file], { recursive: true, force: true });
}
} else {
console.log('deleting: ', files[file]);
fs.unlinkSync(usersFolderPath + files[file]);
}
}
}
var cache = {};
function generateDirName(seed) {
var hash = crypto.createHash('md5');
hash.update(seed);
var dir = hash.digest('hex');
return dir;
}
async function convert(text, from, to) {
var options = {
from: from,
to: to
};
if (!cache[to])
cache[to] = {};
if (cache[to][text])
return cache[to][text];
var translated_text = await translate(text, options);
cache[to][text] = translated_text.text;
return translated_text.text;
}
async function translateFrontMatterTags(block, targetLang, tags) {
var array = block.split("\n");
var translatedBlock = "";
for (var i = 0; i < array.length; i++) {
if (array[i].indexOf(":") > -1) {
var elements = array[i].split(":");
var newElement = "";
if (elements[0].indexOf("tags") != -1) {
translatedTags = []
for (var j in tags) {
var tempTag = await convert(tags[j], defaultLang, targetLang);
translatedTags.push(tempTag);
}
var trasnlatedTagsString = translatedTags.join(", ");
newElement = elements[0] + ": [" + trasnlatedTagsString + "]";
} else {
newElement = array[i];
}
} else {
newElement = array[i];
}
translatedBlock += newElement + "\n";
}
return translatedBlock;
}
puppeteer
.launch({
defaultViewport: {
width: 1280,
height: 800,
},
})
.then(async (browser) => {
const page = await browser.newPage();
for (var i in users) {
var userMDFile = "---\n\
title: \""+ users[i].title + "\"\n\
tags: ["+ users[i].tags + "]\n\
externalUrl: \""+ users[i].url + "\"\n\
weight: "+ (i + 1) + "\n\
showDate: false\n\
showAuthor: false\n\
showReadingTime: false\n\
showEdit: false\n\
showLikes: false\n\
showViews: false\n\
layoutBackgroundHeaderSpace: false\n\
\r---\n";
var dir = usersFolderPath + generateDirName(users[i].url);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
console.log(i, users[i].title);
fs.writeFileSync(dir + '/index.md', userMDFile);
for (var j in targetLangs) {
var content = await translateFrontMatterTags(userMDFile, targetLangs[j], users[i].tags);
fs.writeFileSync(dir + '/index.' + targetLangs[j] + '.md', content);
}
await page.goto(users[i].url);
await page.screenshot({ path: dir + "/feature.jpg" });
}
}
await browser.close();
});