-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakepages.js
138 lines (116 loc) · 4.85 KB
/
makepages.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
const blogs = require("/srv/www/htdocs/blog/blogs.js");
const filePath = "/srv/www/htdocs/blog/blogs/";
const fs = require("fs");
const { pageTop, pageBottom } = require("/srv/www/htdocs/html.js")
const comments = require("/srv/www/htdocs/blog/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 commentsHTML = ""
for (let commentsCounter = comments[length].length - 1; commentsCounter >= 0; commentsCounter--) {
commentsHTML += comments[length][commentsCounter] + "<br/>"
}
let content = `${pageTop}<div id="main">
<h3 class="blog-date">${blogs[length][2]}</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 (under construction):</h3><br/>
<form action="" id="commentForm">
<input name="${length}" class="form-control" id="name" placeholder="Enter your name/alias (no login required)..."><br/>
<input height="40px" name="${length}" class="form-control input-comment" id="comment" placeholder="Enter your Comment...">
<button type="submit">Submit</button>
</form>
<div class="comments"-->
<br/>
${commentsHTML}
</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="/blog/comments.js"></script><script src="/blog/submit-comment.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
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];
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.');
}
});
}