-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbible
executable file
·213 lines (183 loc) · 5.24 KB
/
bible
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env node
// Dependencies
var BibleJS = require("bible.js")
, Couleurs = require("couleurs")()
, Debug = require("bug-killer")
, RegexParser = require("regex-parser")
, ReadJson = require("r-json")
, WriteJson = require("w-json")
, Abs = require("abs")
, Yargs = require("yargs")
, OS = require("os")
, LeTable = require("le-table")
, IsThere = require("is-there")
, config = null
;
// Help output
const HELP = require(__dirname + "/docs/help")
, SAMPLE_CONFIGURATION = require(__dirname + "/docs/sample-conf")
, CONFIG_FILE_PATH = Abs("~/.bible-config.json")
, SUBMODULES_DIR = Abs("~/.bible")
;
// CLI args parser
Yargs = Yargs.usage(HELP);
var Argv = Yargs.argv
, language = Argv.lang || Argv.language
, search = Argv.s || Argv.search
, searchResultColor = null
;
// Read the configuration file
try {
config = ReadJson(CONFIG_FILE_PATH);
} catch (e) {
if (e.code === "ENOENT") {
Debug.log(
"No configuration file was found. Initing the configuration file."
, "warn"
);
WriteJson(CONFIG_FILE_PATH, SAMPLE_CONFIGURATION)
Debug.log(
"The configuration file created successfully at the following location: "
+ CONFIG_FILE_PATH
, "warn"
);
config = require(CONFIG_FILE_PATH);
} else {
Debug.log(
"Cannot read the configuration file. Reason: " + e.code
, "warn"
);
}
}
// Show the version
if (Argv.v || Argv.version) {
return console.log(
"Bible " + require(__dirname + "/../package").version
+ "\nBible.JS " + require(__dirname + "/../package").dependencies["bible.js"]
);
}
// Show help
var references = Argv._;
if (Argv.help || (!language && !references.length && !search)) {
return console.log(Yargs.help());
}
// Try to get options from config as well
language = language || config.language;
searchResultColor = (
Argv.rc || Argv.resultColor || config.resultColor
).split(/[ ,]+/)
config.searchLimit = config.searchLimit || 10;
// Table defaults
LeTable.defaults.marks = {
nw: "┌"
, n: "─"
, ne: "┐"
, e: "│"
, se: "┘"
, s: "─"
, sw: "└"
, w: "│"
, b: " "
, mt: "┬"
, ml: "├"
, mr: "┤"
, mb: "┴"
, mm: "┼"
};
// Parse result color
for (var i = 0; i < 3; ++i) {
if (!searchResultColor[i]) {
return console.log(
"Invalid result color. Please provide a string in this format:"
+ "'r, g, b'. Example: --resultColor '255, 0, 0'"
);
}
searchResultColor[i] = parseInt(searchResultColor[i])
}
/**
* printOutput
* This function is called when the response from the
* search or get request comes
*
* @param {Error} err An error that ocured while fetching the verses.
* @param {Array} verses The verses array that was returned by bible.js module.
* @return {undefined} Returns undefined
*/
function printOutput (err, verses) {
// Handle error
if (err) {
console.log("Error: ", err);
return;
}
// No verses
if (!verses || !verses.length) {
console.log("Verses not found");
}
var tbl = new LeTable();
// Output each verse
for (var i in verses) {
// get the current verse and its reference
var cVerse = verses[i]
, cVerseRef = cVerse.bookname + " " + cVerse.chapter + ":" + cVerse.verse
;
if (search) {
// Highlight search results
var re = typeof search === "string" ? RegexParser(search) : search
, match = cVerse.text.match(re) || []
;
for (var ii = 0; ii < match.length; ++ii) {
cVerse.text = cVerse.text.replace (
new RegExp(match[ii])
, Couleurs.fg(match[ii], searchResultColor)
);
}
}
if (Argv.onlyVerses) {
console.log(cVerse.text);
} else {
tbl.addRow([
{text: cVerseRef, data: {hAlign: "right"}}
, {
text: cVerse.text.match(/.{1,80}(\s|$)|\S+?(\s|$)/g).join("\n")
, data: {hAlign: "left"}
}
]);
}
// Search limit
if (search && --config.searchLimit <= 0) {
break;
}
}
// Output
if (!Argv.onlyVerses) {
console.log(tbl.toString());
}
}
// Check if the ~/.bible dir exists
if (!IsThere(SUBMODULES_DIR)) {
Debug.log("~/.bible directory was not found. Downloading packages. This may take a while.", "info");
}
// Init submodules
BibleJS.init(config, function (err) {
if (err) { throw err; }
// Create Bible instance
var bibleIns = new BibleJS({language: language});
// Get the verses
if (references.length) {
for (var i = 0; i < references.length; ++i) {
(function (cR) {
if (!Argv.onlyVerses) {
console.log("Reference: " + cR);
}
bibleIns.get(cR, printOutput);
})(references[i]);
}
}
// Search verses
if (search) {
if (!Argv.onlyVerses) {
console.log("Results for search: " + search);
}
bibleIns.search(search, printOutput);
}
});