fix: improve scripts and js

This commit is contained in:
Nuno Coração
2025-06-20 23:24:40 +01:00
parent 1f49f8f89f
commit 8c42d127ff
7 changed files with 228 additions and 207 deletions

View File

@@ -7,7 +7,7 @@ const oid_likes =
pageScriptElement && pageScriptElement.getAttribute("data-oid-likes")
? pageScriptElement.getAttribute("data-oid-likes")
: (console.error("data-oid-likes is null"), null);
const liked_page = false;
let liked_page = false;
const id = oid ? oid.replaceAll("/", "-") : oid;
const id_likes = oid_likes ? oid_likes.replaceAll("/", "-") : oid_likes;

View File

@@ -156,7 +156,6 @@ function executeQuery(term) {
if (results.length > 0) {
results.forEach(function (value, key) {
console.log(value.item.summary);
var html = value.item.summary;
var div = document.createElement("div");
div.innerHTML = html;

View File

@@ -6,14 +6,33 @@ function _getDefaultPackeryOptions() {
};
}
function _getPackeryOptions(nodeGallery) {
const defaults = _getDefaultPackeryOptions();
const {
packeryGutter,
packeryPercentPosition,
packeryResize,
} = nodeGallery.dataset;
return {
percentPosition:
packeryPercentPosition !== undefined
? packeryPercentPosition === "true"
: defaults.percentPosition,
gutter:
packeryGutter !== undefined ? parseInt(packeryGutter, 10) : defaults.gutter,
resize:
packeryResize !== undefined ? packeryResize === "true" : defaults.resize,
};
}
(function init() {
$(window).on("load", function () {
let packeries = [];
let nodeGalleries = document.querySelectorAll(".gallery");
nodeGalleries.forEach((nodeGallery) => {
// TODO : implement a reader of Packery configuration _getPackeryOptions; for example by reading data-attribute
let packery = new Packery(nodeGallery, _getDefaultPackeryOptions());
let packery = new Packery(nodeGallery, _getPackeryOptions(nodeGallery));
packeries.push(packery);
});
console.groupEnd();

View File

@@ -1,4 +1,4 @@
const fs = require("fs");
const fs = require("fs/promises");
const translate = require("@iamtraction/google-translate");
const defaultLang = "en";
@@ -8,11 +8,8 @@ const targetLangIso = targetLang == "pt" ? "pt-pt" : targetLang;
const targetFilePath = filePath.replace(".md", "." + targetLangIso + ".md");
async function convert(text, from, to) {
var options = {
from: from,
to: to,
};
var translated_text = await translate(text, options);
const options = { from, to };
const translated_text = await translate(text, options);
return translated_text.text;
}
@@ -20,12 +17,12 @@ console.log(filePath);
console.log(targetFilePath);
async function processFrontMatter(block) {
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 = "";
const array = block.split("\n");
let translatedBlock = "";
for (const line of array) {
let newElement = line;
if (line.indexOf(":") > -1) {
const elements = line.split(":");
if (
elements[0] == "title" ||
elements[0] == "description" ||
@@ -34,13 +31,11 @@ async function processFrontMatter(block) {
elements[0] == "categories" ||
elements[0] == "tags"
) {
var translatedElement = elements[1] ? await convert(elements[1], defaultLang, targetLang) : elements[1];
const translatedElement = elements[1]
? await convert(elements[1], defaultLang, targetLang)
: elements[1];
newElement = elements[0] + ": " + translatedElement;
} else {
newElement = array[i];
}
} else {
newElement = array[i];
}
translatedBlock += newElement + "\n";
}
@@ -48,17 +43,19 @@ async function processFrontMatter(block) {
}
async function main() {
const fileContent = fs.readFileSync(filePath, "utf8");
const fileContent = await fs.readFile(filePath, "utf8");
var array = fileContent.split("---\n");
var frontMatter = array[1];
var content = array[2];
const array = fileContent.split("---\n");
const frontMatter = array[1];
const content = array[2];
var translatedFrontMatter = await processFrontMatter(frontMatter);
var translatedContent = await convert(content, defaultLang, targetLang);
const translatedFrontMatter = await processFrontMatter(frontMatter);
const translatedContent = await convert(content, defaultLang, targetLang);
var newFileContent = "---\n" + translatedFrontMatter + "---\n" + translatedContent;
fs.writeFileSync(targetFilePath, newFileContent, "utf8");
const newFileContent =
"---\n" + translatedFrontMatter + "---\n" + translatedContent;
await fs.writeFile(targetFilePath, newFileContent, "utf8");
}
main();

View File

@@ -1,4 +1,4 @@
const fs = require("fs");
const fs = require("fs/promises");
const translate = require("@iamtraction/google-translate");
const configDir = "./exampleSite/config/_default";
@@ -7,35 +7,39 @@ const defaultLang = "en";
const targetLang = process.argv[2] || "en";
const targetLangIso = targetLang == "pt" ? "pt-pt" : targetLang;
function createConfigs() {
const files = fs.readdirSync(configDir);
files.forEach((file) => {
async function createConfigs() {
const files = await fs.readdir(configDir);
for (const file of files) {
const filePath = `${configDir}/${file}`;
if (filePath.indexOf("languages.en.toml") > -1 || filePath.indexOf("menus.en.toml") > -1) {
var fileContent = fs.readFileSync(filePath, "utf8");
fs.writeFileSync(filePath.replace(".en.toml", "." + targetLangIso + ".toml"), fileContent, "utf8");
if (
filePath.indexOf("languages.en.toml") > -1 ||
filePath.indexOf("menus.en.toml") > -1
) {
const fileContent = await fs.readFile(filePath, "utf8");
await fs.writeFile(
filePath.replace(".en.toml", "." + targetLangIso + ".toml"),
fileContent,
"utf8"
);
}
}
});
}
async function convert(text, from, to) {
var options = {
from: from,
to: to,
};
var translated_text = await translate(text, options).catch((err) => {
const options = { from, to };
const translated_text = await translate(text, options).catch((err) => {
console.error(err);
});
return translated_text && translated_text.text ? translated_text.text : "";
}
async function processFrontMatter(block) {
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 = "";
const array = block.split("\n");
let translatedBlock = "";
for (const line of array) {
let newElement = line;
if (line.indexOf(":") > -1) {
const elements = line.split(":");
if (
elements[0] == "title" ||
elements[0] == "description" ||
@@ -44,13 +48,11 @@ async function processFrontMatter(block) {
elements[0] == "categories" ||
elements[0] == "tags"
) {
var translatedElement = elements[1] ? await convert(elements[1], defaultLang, targetLang) : elements[1];
const translatedElement = elements[1]
? await convert(elements[1], defaultLang, targetLang)
: elements[1];
newElement = elements[0] + ": " + translatedElement;
} else {
newElement = array[i];
}
} else {
newElement = array[i];
}
translatedBlock += newElement + "\n";
}
@@ -60,29 +62,34 @@ async function processFrontMatter(block) {
async function processFile(filePath) {
console.log("translating", filePath);
if (filePath.indexOf("index.md") > -1) {
const targetFilePath = filePath.replace(".md", "." + targetLangIso + ".md");
const targetFilePath = filePath.replace(
".md",
"." + targetLangIso + ".md"
);
const fileContent = fs.readFileSync(filePath, "utf8");
const fileContent = await fs.readFile(filePath, "utf8");
var array = fileContent.split("---\n");
var frontMatter = array[1];
var content = array[2];
const array = fileContent.split("---\n");
const frontMatter = array[1];
const content = array[2];
var translatedFrontMatter = await processFrontMatter(frontMatter);
var translatedContent = await convert(content, defaultLang, targetLang);
const translatedFrontMatter = await processFrontMatter(frontMatter);
const translatedContent = await convert(content, defaultLang, targetLang);
var newFileContent = "---\n" + translatedFrontMatter + "---\n" + translatedContent;
fs.writeFileSync(targetFilePath, newFileContent, "utf8");
} else return;
const newFileContent =
"---\n" + translatedFrontMatter + "---\n" + translatedContent;
await fs.writeFile(targetFilePath, newFileContent, "utf8");
} else {
return;
}
}
async function processFolder(folderPath) {
const files = fs.readdirSync(folderPath);
const files = await fs.readdir(folderPath);
for (var i in files) {
const file = files[i];
for (const file of files) {
const filePath = `${folderPath}/${file}`;
const isDir = fs.lstatSync(filePath).isDirectory();
const isDir = (await fs.lstat(filePath)).isDirectory();
console.log(filePath, isDir);
if (isDir) {
await processFolder(filePath);
@@ -93,8 +100,11 @@ async function processFolder(folderPath) {
}
async function createContent() {
processFolder(contentDir);
await processFolder(contentDir);
}
createConfigs();
createContent();
(async () => {
await createConfigs();
await createContent();
})();

View File

@@ -1,52 +1,57 @@
const fs = require("fs");
const fs = require("fs/promises");
const configDir = "./exampleSite/config/_default";
const contentDir = "./exampleSite/content";
const defaultLang = "en";
var targetLangs = [];
const targetLangs = [];
function readConfigs() {
const files = fs.readdirSync(configDir);
files.forEach((file) => {
async function readConfigs() {
const files = await fs.readdir(configDir);
for (const file of files) {
console.log(file);
if (file.indexOf("languages.") > -1) {
var lang = file.split(".")[1];
const lang = file.split(".")[1];
console.log(lang);
if (lang != defaultLang) {
targetLangs.push(lang);
}
}
});
}
}
async function processFile(filePath, file) {
if (filePath.indexOf("index.md") > -1) {
console.log("processing", filePath);
for (var i in targetLangs) {
const targetLang = targetLangs[i];
var targetFilePath = filePath.replace(".md", "." + targetLang + ".md");
//var targetFileName = file.replace(".md", "." + targetLang + ".md");
for (const targetLang of targetLangs) {
const targetFilePath = filePath.replace(".md", "." + targetLang + ".md");
if (fs.existsSync(targetFilePath)) {
let exists = true;
try {
await fs.access(targetFilePath);
} catch {
exists = false;
}
if (exists) {
console.log("file already exists", targetFilePath);
} else {
console.log("creating file", targetFilePath);
//fs.symlinkSync(file, targetFilePath, 'junction');
fs.copyFileSync(filePath, targetFilePath);
await fs.copyFile(filePath, targetFilePath);
}
}
} else return;
} else {
return;
}
}
async function processFolder(folderPath) {
const files = fs.readdirSync(folderPath);
const files = await fs.readdir(folderPath);
for (var i in files) {
const file = files[i];
for (const file of files) {
const filePath = `${folderPath}/${file}`;
const isDir = fs.lstatSync(filePath).isDirectory();
const isDir = (await fs.lstat(filePath)).isDirectory();
if (isDir) {
await processFolder(filePath);
} else {
@@ -56,8 +61,11 @@ async function processFolder(folderPath) {
}
async function createLinks() {
processFolder(contentDir);
await processFolder(contentDir);
}
readConfigs();
createLinks();
(async () => {
await readConfigs();
await createLinks();
})();

View File

@@ -1,151 +1,139 @@
const fs = require("fs");
const fs = require("fs/promises");
const crypto = require("crypto");
const puppeteer = require("puppeteer");
const translate = require("@iamtraction/google-translate");
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 = {};
let cache = {};
function generateDirName(seed) {
var hash = crypto.createHash("md5");
const hash = crypto.createHash("md5");
hash.update(seed);
var dir = hash.digest("hex");
return dir;
return hash.digest("hex");
}
async function convert(text, from, to) {
var options = {
from: from,
to: to,
};
const options = { from, to };
if (!cache[to]) cache[to] = {};
if (cache[to][text]) return cache[to][text];
var translated_text = await translate(text, options);
const 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 = "";
const array = block.split("\n");
let translatedBlock = "";
for (const line of array) {
let newElement = line;
if (line.indexOf(":") > -1) {
const elements = line.split(":");
if (elements[0].indexOf("tags") != -1) {
translatedTags = [];
for (var j in tags) {
var tempTag = await convert(tags[j], defaultLang, targetLang);
const translatedTags = [];
for (const tag of tags) {
const tempTag = await convert(tag, defaultLang, targetLang);
translatedTags.push(tempTag);
}
var trasnlatedTagsString = translatedTags.join(", ");
const trasnlatedTagsString = translatedTags.join(", ");
newElement = elements[0] + ": [" + trasnlatedTagsString + "]";
} else {
newElement = array[i];
}
} else {
newElement = array[i];
}
translatedBlock += newElement + "\n";
}
return translatedBlock;
}
puppeteer
.launch({
(async () => {
const targetLangs = [];
const configFiles = await fs.readdir(configDir);
for (const file of configFiles) {
if (file.indexOf("languages.") > -1) {
const lang = file.split(".")[1];
if (lang !== defaultLang) {
targetLangs.push(lang);
}
}
}
const indexFiles = await fs.readdir(usersFolderPath);
for (const lang of targetLangs) {
const targetFile = `_index.${lang}.md`;
if (!indexFiles.includes(targetFile)) {
await fs.copyFile(
usersFolderPath + "_index.md",
usersFolderPath + targetFile
);
}
}
const rawdata = await fs.readFile(usersFolderPath + "users.json", "utf8");
const users = JSON.parse(rawdata);
const userDict = {};
for (const user of users) {
userDict[generateDirName(user.url)] = true;
}
const files = await fs.readdir(usersFolderPath);
for (const file of files) {
const stats = await fs.stat(usersFolderPath + file);
if (file !== "users.json" && file.indexOf("_index.") === -1) {
if (stats.isDirectory()) {
if (!userDict[file]) {
console.log("deleting: ", file);
await fs.rm(usersFolderPath + file, { recursive: true, force: true });
}
} else {
console.log("deleting: ", file);
await fs.unlink(usersFolderPath + file);
}
}
}
const browser = await 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";
for (let i = 0; i < users.length; i++) {
const user = users[i];
const userMDFile =
"---\n" +
` title: "${user.title}"\n` +
` tags: [${user.tags}]\n` +
` externalUrl: "${user.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);
const dir = usersFolderPath + generateDirName(user.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);
try {
await fs.access(dir);
} catch {
await fs.mkdir(dir);
}
await page.goto(users[i].url);
console.log(i, user.title);
await fs.writeFile(dir + "/index.md", userMDFile);
for (const lang of targetLangs) {
const content = await translateFrontMatterTags(userMDFile, lang, user.tags);
await fs.writeFile(dir + `/index.${lang}.md`, content);
}
await page.goto(user.url);
await page.screenshot({ path: dir + "/feature.jpg", type: "webp", quality: 50 });
}
}
await browser.close();
});
})();