forked from SeanMcP/word-frequency-by-chapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
159 lines (127 loc) · 4.67 KB
/
build.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// This will generate an HTML file based on `word-frequency-by-chapter.json`
const fs = require('fs')
const path = require('path')
const mkdirp = require('mkdirp')
const orderedBooks = JSON.parse(fs.readFileSync(path.join(__dirname, 'kjv', 'ordered-books.json'), 'utf8'))
const byChapterData = JSON.parse(fs.readFileSync(path.join(__dirname, 'word-frequency-by-chapter.json'), 'utf8'))
function buildIndex() {
let body = `
<h1>Word Frequency by Bible Chapter</h1>
<p>The most common words in every chapter of the Bible</p>
<nav>
<section>
<h2>Old Testament</h2>
<ol>
`
for (let book of orderedBooks) {
if (book === 'Matthew') {
body += `
</ol>
</section>
<section>
<h2>New Testament</h2>
<ol>
`
}
body += `
<li><a href="${urlSafeName(book)}">${book}</a></li>
`
}
body += '</ol></section></nav>'
body += `
<section id="about">
<h2>Why?</h2>
<p>When reading the Bible with my family, I like to pick out a word that my young son should listen for. I want it to be relevant to the passage and common enough to keep him engaged throughout the reading.</p>
<p>While it is sometimes easy to scan a chapter and pick a suitable word, often times it is not. This website makes it easier to find the right word for each chapter of the Bible.</p>
<h2>Why old English?</h2>
<p>The computer program behind this website reads every chapter of the Bible and counts the word frequency. The only Bible translations that are free and available to use are in the public domain. I chose the King James Version because it meets that criterion and is still used today.</p>
<p>In my experience, finding the word in my preferred translation has been easy, especially if you are choosing a word that occurs frequently in a chapter.</p>
</section>
`
let html = htmlHead(`<title>Word Frequency by Bible Chapter</title><link rel="stylesheet" href="./styles.css" />`) + htmlBody(body, "home") + htmlTail()
fs.writeFileSync(path.join(__dirname, 'docs', 'index.html'), html)
}
buildIndex()
// Build book pages
for (let book of orderedBooks) {
let body = `
<section id="nav">
<div id="sticky-nav">
<h1 id="top">${book}</h1>
<h2 id="toc">Table of Contents</h2>
<nav>
<ol>`
let chapters = byChapterData[book]
let chaptersHtml = `
<section id="chapters">
<h2>Chapters</h2>`
for (let chapter in chapters) {
let chapterAnchor = `ch-${chapter}`
let chapterName = `${book} ${chapter}`
body += `
<li><a href="#${chapterAnchor}" aria-label="${chapterName}">${chapter}</a></li>` // Build TOC
chaptersHtml += `
<article>
<h3 id="ch-${chapter}">${chapterName}</h3>
<ol>`
let words = chapters[chapter]
for (let [word, count] of words) {
chaptersHtml += `
<li>${word} (${count})</li>`
}
chaptersHtml += `
</ol>
</article>`
}
body += `
</ol>
<a href="../">View all books</a>
<form id="limit-form">
<label for="limit-input">Limit to top</label>
<input id="limit-input" name="top" type="number" pattern="[0-9]*" />
<button>Go</button>
</form>
<script src="../limit.js"></script>
</nav>
</div>
</section>` // Close TOC
body += chaptersHtml + '</section>'
body += `<a href="#main" id="back-to-top">Back to top</a>`
const html = htmlHead(`<title>Most frequent words in ${book}</title><link rel="stylesheet" href="../styles.css" />`) + htmlBody(body, "book") + htmlTail()
let targetDir = path.join(__dirname, 'docs', urlSafeName(book))
mkdirp.sync(targetDir)
fs.writeFileSync(path.join(targetDir, 'index.html'), html)
}
//#region Hoisted utils
function urlSafeName(name) {
return name.toLowerCase().replace(/\ /g, '-')
}
function htmlHead(head = '') {
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
${head}
</head>
`
}
function htmlBody(body = '', mainClass = '') {
return `
<body>
<main class="${mainClass}" id="main">
${body}
</main>
<footer>
<p>Made by <a href="https://seanmcp.com">SeanMcP</a></p>
</footer>
</body>
`
}
function htmlTail() {
return `
</html>
<!-- SDG -->
`
}
//#endregion