htdocs/node/makeblogs.js
2024-12-24 22:44:26 +00:00

162 lines
6.1 KiB
JavaScript
Executable File

function daysIntoYear(date){
return (Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) - Date.UTC(date.getFullYear(), 0, 0)) / 24 / 60 / 60 / 1000;
}
function secondsIntoDay(time) {
let hour = time.getUTCHours();
let minute = time.getUTCMinutes();
let second = time.getUTCSeconds();
return ((hour * 3600) + (minute * 60) + second)
}
const serverPath = require("./serverPath.js")
const blogs = require(serverPath+"/blogs/blogs.js");
const filePath = serverPath+"/blog/";
const fs = require("fs");
const { pageTop, pageBottom } = require(serverPath+"/html.js")
const comments = require(serverPath+"/blogs/comments.js")
console.log(blogs);
/////////////////////////////////
// Make each individual blog page
/////////////////////////////////
for (let length = 0; length < blogs.length; length++) {
console.log(length)
const blogFilePath = filePath + length + ".html";
let date = new Date(blogs[length][2])
let second = Math.round((secondsIntoDay(date) / (86400/65536))).toString(16);
let day = daysIntoYear(date).toString(16);
let year = date.getUTCFullYear().toString(16);
blogs[length][1] = blogs[length][1].replace(/(?:\r\n|\r|\n)/g, '<br>')
blogs[length][1] = blogs[length][1].replace(/(?:\t)/g, '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
let content = `<meta name="fediverse:creator" content="@DeaDvey@mastodon.social">
${pageTop}<div id="main">
<h3 class="blog-date">${second} ${day}/${year}</h3>
<h1 class="blog-title">${blogs[length][0]}</h1>
<p class="blog-content">${blogs[length][1]}</p>
${blogs[length][3] ?
`<div class="blog-images">
${blogs[length][3].map(image => `<img class="blog-image" height="200" src="/images/${image}" alt="${image}" />`).join('')}
</div>`
: ''}<br/>
<sub><i><small>${blogs[length][4]}</small></i></sub><hr/>
<h3>Comments section:</h3><br/>
<form action="/blog/submit-comment" id="commentForm" method="post">
<input name="name" class="form-control" id="name" placeholder="Enter your name"><br/>
<textarea height="40px" name="comment" class="form-control input-comment" id="comment" placeholder="Enter your Comment..."></textarea>
<input type="hidden" name="pageID" value="${length}">
<button type="submit">Submit</button>
</form>
<div class="${length}" id="comments">`
for (let comment = 0; comment<comments[length].length; comment++) {
content+=`<b>${comments[length][comment][0]}</b>: <i>${comments[length][comment][1]}</i><br/>`
}
content+=`</div></div>`
if (length > 0) { // Generate link to previous blog
content+=`<p class="previous"><a href="${length-1}.html"><-- Previous</a></p>`
}
if (length != blogs.length - 1) { // Generate link to next blog
content+=`<p class="next"><a href="${length+1}.html">Next --></a></p>`
}
content += `
<div id="links">
</div><script src="/blogs/comments.js"></script><script src="/blogs/displayComments.js"></script>${pageBottom}`
fs.writeFile(blogFilePath, content, (err) => {
if (err) {
console.error('Error creating file:', err);
} else {
console.log('File created successfully:', blogFilePath);
}
});
///////////////////////
// Make latest blog
//////////////////////
latestBlogPath = filePath + "latest.html"
console.log(latestBlogPath)
lastitem = blogs.length - 1
blogs[lastitem][1] = blogs[lastitem][1].replace(/(?:\r\n|\r|\n)/g, '<br>')
blogs[lastitem][1] = blogs[lastitem][1].replace(/(?:\t)/g, '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
let latestcontent = `${pageTop}
<h3 class="blog-date">${blogs[lastitem][2]}</h3>
<h1 class="blog-title">${blogs[lastitem][0]}</h1>
<p class="blog-content">${blogs[lastitem][1]}</p>
${blogs[lastitem][3] ?
`<div class="blog-images">
${blogs[lastitem][3].map(image => `<img class="blog-image" height="200" src="/images/${image}" alt="${image}" />`).join('')}
</div>`
: ''}<br/>
<sub><i><small>${blogs[lastitem][4]}</small></i></sub>
<p class="previous"><a href="${lastitem-1}.html"><-- Previous</a></p>
<div id="links">
</div><script src="https://code.jquery.com/jquery-3.6.1.min.js"></script><script src="/template.js"></script>${pageBottom}`
fs.writeFile(latestBlogPath, latestcontent, (err) => {
if (err) {
console.error('Error creating file:', err);
} else {
console.log('File created successfully:', latestBlogPath);
}
});
///////////////////////
// Make entire blog
//////////////////////
function entireBlogPost() {
let htmlPage = pageTop;
// Add each blog post to the HTML page
for (let length = blogs.length - 1; length >= 0; length--) {
const title = blogs[length][0];
const content = blogs[length][1];
const date = blogs[length][2];
blogs[length][1] = blogs[length][1].replace(/(?:\r\n|\r|\n)/g, '<br>')
blogs[length][1] = blogs[length][1].replace(/(?:\t)/g, '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
htmlPage += `
<h3 class="blog-date">${date}</h3>
<h1 class="blog-title">${title}</h1>
<p class="blog-content">${content}</p>
${blogs[length][3] ?
`<div class="blog-images">
${blogs[length][3].map(image => `<img class="blog-image" height="200" src="/images/${image}" alt="${image}" />`).join('')}
</div>`
: ''}
<div id="links"></div><hr/><script src="/template.js"></script>`;
}
//Remove the last <hr/>
htmlPage = htmlPage.slice(0, -5)
// Close the HTML page
htmlPage += pageBottom;
return htmlPage;
}
entireBlog = entireBlogPost()
entireFeedPath = filePath + "all.html"
fs.writeFile(entireFeedPath, entireBlog, (err) => {
if (err) {
console.error('Error writing to the file:', err);
} else {
console.log('Data has been written to the file successfully.');
}
});
}