Repository navigation
Expand file tree
/
Copy pathfontAdded.js
More file actions
152 lines (135 loc) · 5.17 KB
/
Copy pathfontAdded.js
File metadata and controls
152 lines (135 loc) · 5.17 KB
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
const fs = require('fs')
const pre_path = '../Fonts/'
const css_path = "./static/css/fonts.css"
const fonts_json = "fonts.json"
const {
font_extentions,
old_fonts
} = require('./configuration')
const getFiles = path => {
const files = []
for (const file of fs.readdirSync(path)) {
const fullPath = path + '/' + file
if(fs.lstatSync(fullPath).isDirectory())
getFiles(fullPath).forEach(x => files.push(file + '/' + x))
else files.push(file)
}
return files
}
var fontName = file => {
let fileAr = file.split('')
for(let i=fileAr.length-1; i>=0; i--){
if(fileAr[i] == '/')
return file.slice(i+1, fileAr.length)
}
return file
}
var fontFamily = file => {
let fileAr = file.split('')
const ind = fileAr.findIndex( ele => ele == '/')
return file.slice(0, ind)
}
var fontInitialize = () =>{
const old_data = fs.existsSync(fonts_json)
? JSON.parse(fs.readFileSync(fonts_json, 'utf8'))
: {}
const old_css = fs.existsSync(css_path)
? fs.readFileSync(css_path, 'utf8')
: ''
const all_files = getFiles('./static/Fonts/')
const new_obj = {}
const new_css_entries = []
all_files.forEach( file =>{
extention = font_extentions.find( a => {
if(file.includes(a)){
return a
}
return false
})
if(extention){
let font_name = fontName(file).replace(/-/g, ' ').replace(/_/g, ' ').replace(extention, '')
const font_family = fontFamily(file).replace(/-/g, ' ').replace(/_/g, ' ')
let font_name_link = fontName(file).replace(/ /g, '-').replace(/_/g, '-').replace(extention, '')
const font_family_link = fontFamily(file).replace(/ /g, '-').replace(/_/g, '-')
if(old_fonts.includes(extention)){
font_name = "old " + extention + " " + font_name
font_name_link = "old-" + extention + "-" + font_name_link
}
font_address = pre_path + file
if(!new_obj[font_family_link]){
new_obj[font_family_link] = {
family: font_family,
link: font_family_link,
fonts: [{font: font_name, link: font_name_link}]
}
}
else{
new_obj[font_family_link]['fonts'].push({font: font_name, link: font_name_link})
}
}
console.log("File:", file, "Extension:", extention);
})
// Build set of all existing font names from old_data
const existing_font_links = new Set()
for (const entry of Object.values(old_data)) {
for (const f of entry.fonts) {
if (f.link) existing_font_links.add(f.link)
}
}
// Merge: keep existing curated entries, add new families whose fonts aren't already referenced
const merged = { ...old_data }
for (const key of Object.keys(new_obj)) {
if (!(key in old_data)) {
const is_new = !new_obj[key].fonts.some(f => existing_font_links.has(f.link))
if (is_new) {
merged[key] = new_obj[key]
}
}
}
fs.writeFileSync(fonts_json, JSON.stringify(merged, null, '\t'), 'utf8')
// Merge CSS: keep old declarations, append new ones for new families
const existing_families = new Set()
if (old_css) {
const family_regex = /font-family:\s*"([^"]+)"/g
let match
while ((match = family_regex.exec(old_css)) !== null) {
existing_families.add(match[1])
}
}
let new_css = old_css
const generated_css_entries = {}
// Build CSS entries grouped by font family
for (const file of all_files) {
extention = font_extentions.find(a => {
if(file.includes(a)) return a
return false
})
if(extention){
let font_name = fontName(file).replace(/-/g, ' ').replace(/_/g, ' ').replace(extention, '')
const font_family_str = fontFamily(file).replace(/-/g, ' ').replace(/_/g, ' ')
let font_name_link = fontName(file).replace(/ /g, '-').replace(/_/g, '-').replace(extention, '')
const font_family_link = fontFamily(file).replace(/ /g, '-').replace(/_/g, '-')
if(old_fonts.includes(extention)){
font_name = "old " + extention + " " + font_name
font_name_link = "old-" + extention + "-" + font_name_link
}
const font_address = pre_path + file
if (!generated_css_entries[font_family_link]) {
generated_css_entries[font_family_link] = ''
}
generated_css_entries[font_family_link] += '@font-face {\n\tfont-family: "'+font_name+'";\n\tsrc: url("'+font_address+'");\n\n}'
}
}
// Append only truly new families to CSS
for (const key of Object.keys(generated_css_entries)) {
if (!(key in old_data)) {
const is_new = !existing_font_links.has(new_obj[key]?.fonts?.[0]?.link)
if (is_new) {
new_css += generated_css_entries[key]
}
}
}
fs.writeFileSync(css_path, new_css)
console.log('completed')
}
fontInitialize()