New highlighting system

This commit is contained in:
bingus_violet 2024-05-31 01:05:50 -05:00
parent 6cf0541b72
commit 221b63b7cf
4 changed files with 2263 additions and 43 deletions

2153
output.json Normal file

File diff suppressed because it is too large Load diff

12
package-lock.json generated
View file

@ -12,6 +12,7 @@
"@node-minify/core": "^8.0.6", "@node-minify/core": "^8.0.6",
"@node-minify/uglify-js": "^8.0.6", "@node-minify/uglify-js": "^8.0.6",
"express": "^4.18.2", "express": "^4.18.2",
"himalaya": "^1.1.0",
"minify-html": "^0.0.2", "minify-html": "^0.0.2",
"ws": "^8.16.0", "ws": "^8.16.0",
"youtubei.js": "^9.0.2" "youtubei.js": "^9.0.2"
@ -486,6 +487,12 @@
"node": ">= 0.4" "node": ">= 0.4"
} }
}, },
"node_modules/himalaya": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/himalaya/-/himalaya-1.1.0.tgz",
"integrity": "sha512-LLase1dHCRMel68/HZTFft0N0wti0epHr3nNY7ynpLbyZpmrKMQ8YIpiOV77TM97cNpC8Wb2n6f66IRggwdWPw==",
"license": "ISC"
},
"node_modules/http-errors": { "node_modules/http-errors": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.0.tgz", "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.0.tgz",
@ -1341,6 +1348,11 @@
"function-bind": "^1.1.2" "function-bind": "^1.1.2"
} }
}, },
"himalaya": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/himalaya/-/himalaya-1.1.0.tgz",
"integrity": "sha512-LLase1dHCRMel68/HZTFft0N0wti0epHr3nNY7ynpLbyZpmrKMQ8YIpiOV77TM97cNpC8Wb2n6f66IRggwdWPw=="
},
"http-errors": { "http-errors": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.0.tgz", "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.0.tgz",

View file

@ -20,6 +20,7 @@
"@node-minify/core": "^8.0.6", "@node-minify/core": "^8.0.6",
"@node-minify/uglify-js": "^8.0.6", "@node-minify/uglify-js": "^8.0.6",
"express": "^4.18.2", "express": "^4.18.2",
"himalaya": "^1.1.0",
"minify-html": "^0.0.2", "minify-html": "^0.0.2",
"ws": "^8.16.0", "ws": "^8.16.0",
"youtubei.js": "^9.0.2" "youtubei.js": "^9.0.2"

View file

@ -5,7 +5,8 @@ const path = require('path'),
uglifyJs = require("@node-minify/uglify-js"), uglifyJs = require("@node-minify/uglify-js"),
htmlMinifier = require("minify-html"), htmlMinifier = require("minify-html"),
activityToHTML = require("./overcomplicatedStatuses.js"), activityToHTML = require("./overcomplicatedStatuses.js"),
randomThemer = require("./randomThemer.js") randomThemer = require("./randomThemer.js"),
himalaya = require("himalaya")
var constants = JSON.parse(fs.readFileSync(path.join(__dirname, 'constants.json'))) var constants = JSON.parse(fs.readFileSync(path.join(__dirname, 'constants.json')))
@ -44,31 +45,22 @@ function timeFormatter(seconds) {
} }
function converter(html, query) { function converter(html) {
var startTime = Date.now() var startTime = Date.now()
html = htmlMinifier.minify(html)
var config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config/config.json'))) var config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config/config.json')))
reloads += 1 reloads += 1
while (html.includes("{PATH_")) {
var pagePath = html.substring(html.indexOf("{PATH_"))
pagePath = pagePath.substring(6, pagePath.indexOf('}'))
var stringIndex = `{PATH_${pagePath}}`
pagePath = pagePath.toLowerCase()
var pageHTML = fs.readFileSync(path.join(__dirname, 'static', pagePath, 'index.html')).toString()
pageHTML = pageHTML.substring(pageHTML.indexOf('<main>') + 6, pageHTML.indexOf('</main>'))
html = html.replace(stringIndex, pageHTML)
}
var statusText = "" var statusText = ""
if (lanyardData) { if (lanyardData) {
var statusData = constants.discStatuses[lanyardData.discord_status] var statusData = constants.discStatuses[lanyardData.discord_status]
var username = lanyardData.discord_user.username var username = lanyardData.discord_user.username
if (lanyardData.activities[0] && lanyardData.activities[0].type == 4) { if (lanyardData.activities[0] && lanyardData.activities[0].type == 4) {
var statusText = `<hr><p>${lanyardData.activities[0].state}</p>` var statusText = `<hr/><p>${lanyardData.activities[0].state}</p>`
} }
} else { } else {
var statusData = constants.discStatuses.offline var statusData = constants.discStatuses.offline
@ -92,18 +84,18 @@ function converter(html, query) {
"COMMIT_COUNT": commitCount, "COMMIT_COUNT": commitCount,
"QUOTE_COUNT": quotes.length, "QUOTE_COUNT": quotes.length,
"RANDOM_TITLE": titles[Math.floor(Math.random() * titles.length)], "RANDOM_TITLE": titles[Math.floor(Math.random() * titles.length)],
"DISCORD_STATUS": "DISCORD_STATUS":
`<span style="color: ${statusData.color};" class="statusColor">${statusData.text}</span>` + `<span style="color: ${statusData.color};" class="statusColor">${statusData.text}</span>` +
`<style>.pfp { border-color: ${statusData.color} }</style>`, `<style>.pfp { border-color: ${statusData.color} }</style>`,
"UPTIME": uptime, "UPTIME": uptime,
"TOPBAR": `<div id="topbar"><h3><a href="/socials">Socials</a></h3></div>`, "TOPBAR": `<div id="topbar"><h3><a href="/socials">Socials</a></h3></div>`,
"DISCORD_USER": username, "DISCORD_USER": username,
"CUSTOM_STATUS": statusText, "CUSTOM_STATUS": statusText,
"SELECTED_VIDEO": () => { "SELECTED_VIDEO": () => {
if (config.dailyVideoURL) { if (config.dailyVideoURL) {
return `<h2><hr>Random video!</h2><p>I would call it random <em>daily</em> video but its not at all daily...</p> return `<h2><hr/>Random video!</h2><p>I would call it random <em>daily</em> video but its not at all daily...</p>
<br> <br/>
<video controls src="${config.dailyVideoURL}"></video>` <video controls="true" src="${config.dailyVideoURL}"></video>`
} }
return `` return ``
}, },
@ -113,33 +105,95 @@ function converter(html, query) {
"WEATHER_MODIFIER": randomThemer.returnTheme(), "WEATHER_MODIFIER": randomThemer.returnTheme(),
"WEATHER_TEXT": "", "WEATHER_TEXT": "",
"ANNOUNCEMENT": fs.readFileSync(path.join(__dirname, "config/announcement.html")), "ANNOUNCEMENT": fs.readFileSync(path.join(__dirname, "config/announcement.html")),
"CACHED_IMAGES": fs.readdirSync(path.join(__dirname, "cached")).length.toString() "CACHED_IMAGES": fs.readdirSync(path.join(__dirname, "cached")).length.toString(),
"ACTIVITIES": activityToHTML.activitiesToHTML(lanyardData, cachedImages)
} }
replacers.ALL_KEYWORDS = "{" + Object.keys(replacers).join("}{") + "} " replacers.ALL_KEYWORDS = "{" + Object.keys(replacers).join("}{") + "} "
var rpTable = Object.keys(replacers) var parsedHTML = himalaya.parse(html)
for (let index = 0; index < rpTable.length; index++) { function scanParsedHTML(json) {
const text = rpTable[index]; for (var i = 0; i < json.length; i++) {
html = html.replaceAll(`{${text}}`, replacers[text]) var element = json[i]
if (element.type == "element") {
if (element.children.length > 0) {
element.children = scanParsedHTML(element.children)
}
} else if (element.type == "text") {
while (element.content.includes("{PATH_")) {
var pagePath = element.content.substring(element.content.indexOf("{PATH_"))
pagePath = pagePath.substring(6, pagePath.indexOf('}'))
var stringIndex = `{PATH_${pagePath}}`
pagePath = pagePath.toLowerCase()
var pageHTML = fs.readFileSync(path.join(__dirname, 'static', pagePath, 'index.html')).toString()
pageHTML = pageHTML.substring(pageHTML.indexOf('<main>') + 6, pageHTML.indexOf('</main>'))
element.content = element.content.replaceAll(stringIndex, pageHTML)
}
var rpTable = Object.keys(replacers)
for (let index = 0; index < rpTable.length; index++) {
const text = rpTable[index];
element.content = element.content.replaceAll(`{${text}}`, replacers[text])
}
// console.log(element.content, himalaya.parse(element.content))
json[i] = element
}
}
return json
} }
var bodyHTML = html.substring(html.indexOf("<body>") + 6, html.lastIndexOf("</body>")) // var highTable = Object.keys(highlightedWords)
var highTable = Object.keys(highlightedWords)
for (let index = 0; index < highTable.length; index++) { // for (let index = 0; index < highTable.length; index++) {
var term = highTable[index]; // var term = highTable[index];
var replacement = `<span style="color: ${highlightedWords[term]}">${term}</span>` // var replacement = `<span style="color: ${highlightedWords[term]}">${term}</span>`
bodyHTML = bodyHTML.replaceAll(`{${term}}`, "TEMPORARY_REPLACE")
bodyHTML = bodyHTML.replaceAll(term, replacement) // element.content = element.content.replaceAll(`{${term}}`, "TEMPORARY_REPLACE")
bodyHTML = bodyHTML.replaceAll("TEMPORARY_REPLACE", `${term}`) // element.content = element.content.replaceAll(term, replacement)
// element.content = element.content.replaceAll("TEMPORARY_REPLACE", `${term}`)
// }
parsedHTML = scanParsedHTML(parsedHTML)
parsedHTML = himalaya.parse(himalaya.stringify(parsedHTML))
function highlighter(json) {
for (var i = 0; i < json.length; i++) {
var element = json[i]
if (element.type == "element") {
if (element.children.length > 0) {
element.children = highlighter(element.children)
}
} else if (element.type == "text") {
var highTable = Object.keys(highlightedWords)
for (let index = 0; index < highTable.length; index++) {
var term = highTable[index];
var replacement = `<span style="color: ${highlightedWords[term]}">${term}</span>`
element.content = element.content.replaceAll(`{${term}}`, "TEMPORARY_REPLACE")
element.content = element.content.replaceAll(term, replacement)
element.content = element.content.replaceAll("TEMPORARY_REPLACE", `${term}`)
}
}
}
return json
} }
bodyHTML = bodyHTML.replaceAll("{ACTIVITIES}", activityToHTML.activitiesToHTML(lanyardData, cachedImages)) parsedHTML = highlighter(parsedHTML)
html = html.substring(0, html.indexOf("<body>")) + bodyHTML + html.substring(html.indexOf("</body>") + 7) html = himalaya.stringify(parsedHTML)
html = html.replaceAll("{LOAD_TIME}", (Date.now() - startTime).toString() + "ms") html = html.replaceAll("{LOAD_TIME}", (Date.now() - startTime).toString() + "ms")
@ -166,7 +220,7 @@ module.exports = {
filePath = path.join(__dirname, 'static', filePath || 'index.html') filePath = path.join(__dirname, 'static', filePath || 'index.html')
if (fs.existsSync(filePath)) { if (fs.existsSync(filePath)) {
var data = fs.readFileSync(filePath).toString() var data = fs.readFileSync(filePath).toString()
res.contentType(path.basename(filePath)) res.contentType(path.basename(filePath))
// if (req.path.includes(".css")) { // if (req.path.includes(".css")) {
@ -177,7 +231,7 @@ module.exports = {
if (filePath.includes(".html")) { if (filePath.includes(".html")) {
data = converter(data, req.query) data = converter(data, req.query)
} }
if (!filePath.includes(".js")) { if (!filePath.includes(".js")) {
@ -206,7 +260,7 @@ async function updateCommits() {
commits = commits.substring(commits.lastIndexOf("<b>") + 3, commits.lastIndexOf("</b>")) commits = commits.substring(commits.lastIndexOf("<b>") + 3, commits.lastIndexOf("</b>"))
// ^ this works for Forgejo (basically everything i use that isnt Github E.G. Codeberg) // ^ this works for Forgejo (basically everything i use that isnt Github E.G. Codeberg)
// commits = commits.substring(commits.lastIndexOf(">") + 1) // commits = commits.substring(commits.lastIndexOf(">") + 1)
// ^ This works for Github (fuck you Github) // ^ This works for Github (fuck you Github)
@ -253,7 +307,7 @@ function get_img_url(activity, size = "large_image") {
function socketeer() { function socketeer() {
var lanyard = new WebSocket('https://api.violets-purgatory.dev') var lanyard = new WebSocket('https://api.violets-purgatory.dev')
lanyard.on("error", (error) =>{ lanyard.on("error", (error) => {
console.log(error) console.log(error)
}) })