forked from yql/yql-tables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbible.bible.xml
268 lines (262 loc) · 11.6 KB
/
bible.bible.xml
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?xml version="1.0" encoding="UTF-8"?>
<table xmlns="http://query.yahooapis.com/v1/schema/table.xsd">
<meta>
<author>Vic Mortelmans</author>
<description>The table contains the complete bible. Each passage is a record, identified by its citation reference.</description>
<sampleQuery>select * from bible.bible where language='en' and bibleref='Luke 11:10'</sampleQuery>
<documentationURL>http://docs.google.com/View?id=ddq89pzk_155t6rwmkhp</documentationURL>
</meta>
<bindings>
<select produces="XML" itemPath="">
<urls>
<url></url>
</urls>
<inputs>
<key id="language" type="xs:string" paramType="variable" required="false"/>
<key id="edition" type="xs:string" paramType="variable" required="false"/>
<key id="book" type="xs:string" paramType="variable" required="false"/>
<key id="chapter" type="xs:integer" paramType="variable" required="false"/>
<key id="passage" type="xs:integer" paramType="variable" required="false"/>
<key id="bibleref" type="xs:string" paramType="variable" required="false"/>
</inputs>
<execute><![CDATA[
String.prototype.trim = function() {
// Strip leading and trailing white-space
return this.replace(/^[ ]*|[ ]*$/g, "");
}
var debug = 0;
if (!edition && !language) {
language = "en";
}
var editionsquery = 'select edition from xml where url="https://raw.github.com/vicmortelmans/BibleConfiguration/master/editions.xml" and edition.input like "%$edition%" and edition.language matches "(?i)$language"';
editionsquery = editionsquery.replace("$language",language?language:'.*');
editionsquery = editionsquery.replace("$edition",edition?edition:'');
var editions = y.query(editionsquery).results.editions;
var edition;
if (editions.edition.length() < 1) {
var supported_editionsquery = 'select edition from xml where url="https://raw.github.com/vicmortelmans/BibleConfiguration/master/editions.xml"';
var supported_editions = y.query(supported_editionsquery).results.editions;
y.log('No bible edition found matching query parameters. Supported editions and languages:');
y.log(listEditions(supported_editions));
y.exit();
} else if (editions.edition.length() > 1) {
edition = editions.edition.(default_ == "true")[0];
if ( ! edition ) {
edition = editions.edition[0];
}
y.log('Multiple bible editions found matching query parameters: ');
y.log(listEditions(editions));
} else {
edition = editions.edition[0];
}
y.log('Using: ');
y.log(listEditions(<editions>{edition}</editions>));
if (edition.copyright.toString()) {
y.log('Copyright: ');
y.log(edition.copyright.toString());
}
debug && y.log(edition);
var biblerefsxml;
if (!bibleref) {
if (!book && !chapter) {
y.log('At least a book and a chapter number should be provided.');
y.exit();
}
biblerefsxml = <biblerefs>
<bibleref>
<book>{book}</book>
<chapter>{chapter}</chapter>
<verse>{verse}</verse>
<phrase/>
</bibleref>
</biblerefs>;
} else {
var biblerefsquery = "use 'https://raw.github.com/vicmortelmans/yql-tables/master/bible/bibleref.xml' as bible.bibleref;select * from bible.bibleref where bibleref='$bibleref'";
biblerefsquery = biblerefsquery.replace("$bibleref",bibleref);
biblerefsxml = y.query(biblerefsquery).results.biblerefs;
if (biblerefsxml.length() == 0) {
y.log('Invalid bible reference.');
y.exit()
}
}
debug && y.log("Bible references: ");
debug && y.log(biblerefsxml);
book = biblerefsxml.bibleref[0].book.toString();
var booksquery = 'select book from xml where url="https://raw.github.com/vicmortelmans/BibleConfiguration/master/books.xml" and book.input matches "(?i)$book"';
booksquery = booksquery.replace("$book",book);
var books = y.query(booksquery).results.books;
if (books.book.length() < 1) {
var error_message = '$book is not a bible book. Common bible book abbreviations : $supported';
error_message = error_message.replace("$book",book);
error_message = error_message.replace("$supported","http://biblewiki.net/books/index");
y.log(error_message);
y.exit();
}
book = books.book[0];
debug && y.log(book);
var servicesquery = 'select service from xml where url="https://raw.github.com/vicmortelmans/BibleConfiguration/master/services.xml" and service.name matches "(?i)$service"';
servicesquery = servicesquery.replace("$service",edition.code.(@default_ == "true")[email protected]());
var services = y.query(servicesquery).results.services;
if (services.service.length() < 1) {
var error_message = 'Internal error';
y.log(error_message);
y.exit();
}
service = services.service[0];
debug && y.log(service);
var bookcode = book.code.(@service == service.bookskey.toString()).toString();
var editioncode = edition.code.(@service == service.editionskey.toString()).toString();
var queryoutput = new String();
var output = new String();
for each (var biblerefxml in biblerefsxml.*) {
debug && y.log(biblerefxml);
var url = new String();
var finalquery = new String();
for each (var step in service.step) {
if (step.@nr < service.step.length()) {
if ([email protected]() == "true") { // yql query
var query = step.toString();
query = query.replace(/\$book/g,bookcode);
query = query.replace(/\$edition/g,editioncode);
query = query.replace(/\$chapter/g,biblerefxml.chapter.toString());
query = query.replace(/\$passage/g,biblerefxml.verse.toString());
query = query.replace(/\$url/g,url);
var rest = 'http://query.yahooapis.com/v1/public/yql?q=$query&diagnostics=true';
rest = rest.replace("$query",escape(query));
url = rest;
debug && y.log(url);
} else { // rest call
var rest = step.toString();
rest = rest.replace(/\$book/g,escape(bookcode));
rest = rest.replace(/\$edition/g,escape(editioncode));
rest = rest.replace(/\$chapter/g,escape(biblerefxml.chapter.toString()));
rest = rest.replace(/\$passage/g,escape(biblerefxml.verse.toString()));
rest = rest.replace(/\$url/g,escape(url));
url = rest;
debug && y.log(url);
}
} else { // last step is always yql query
finalquery = step.toString();
finalquery = finalquery.replace(/\$book/g,bookcode);
finalquery = finalquery.replace(/\$edition/g,editioncode);
finalquery = finalquery.replace(/\$chapter/g,biblerefxml.chapter.toString());
finalquery = finalquery.replace(/\$passage/g,biblerefxml.verse.toString());
finalquery = finalquery.replace(/\$url/g,url);
debug && y.log(finalquery);
}
}
queryoutput = stringListValue(y.query(finalquery).results);
queryoutput = queryoutput.trim();
var verseIndication = queryoutput.match(/^[0-9]+/);
if ( verseIndication && verseIndication[0] === biblerefxml.verse.toString()) {
queryoutput = queryoutput.replace(/^[0-9]+/,'');
}
debug && y.log(queryoutput);
if (biblerefxml.phrase != '') {
queryoutput = extractPhrase(queryoutput,biblerefxml.phrase.toString());
}
debug && y.log(queryoutput);
output += (queryoutput + ' ');
}
output = output.trim();
var punctuation = output.match(/[.?!,:;]$/);
if (!punctuation) {
output += '.';
}
else if (punctuation[0].match(/[,:;]/)) {
output = output.replace(/[,:;]$/,'.');
}
if ( -1 != output.indexOf("’") &&
-1 == output.substring(0,output.indexOf("’")).indexOf("‘") ) {
output = '‘' + output;
}
if ( -1 != output.indexOf("‘") &&
-1 == output.substring(output.lastIndexOf("‘"),output.length-1).indexOf("’") ) {
output += '’';
}
response.object = <passage>{output}</passage>;
function stringValue(node) {
var value = new String();
if (node.hasSimpleContent()) {
value = node.toString();
} else {
for each (var c in node.children()) {
value += stringValue(c);
}
}
return value;
}
function stringListValue(node) {
var value = new String();
if (node.hasSimpleContent()) {
value = node.toString();
} else {
for each (var c in node.children()) {
value += stringValue(c) + " ";
}
}
return value;
}
function listEditions(node) {
var value = new String();
for each (var edition in node.children()) {
value += edition.input.(@name == 'true').toString();
if (edition.input.(@name != 'true').length() > 0) {
value += " (";
for each (var input in edition.input.(@name != 'true')) {
value += input.toString();
value += ",";
}
value = value.replace(/,$/,'');
value += ")";
}
value += " - ";
value += edition.language.toString();
value += ", ";
}
value = value.replace(/, $/,'');
return value;
}
function extractPhrase(passage,phrase) {
phrasechar = phrase.replace('-','');
var phrases = new Array();
var remainingpassage = passage.trim();
while (remainingpassage.length > 0) {
y.log(remainingpassage.length);
y.log(remainingpassage);
var search = remainingpassage.search(/[.!?]["' ,]*\w|;|: *\W/);
/* /[.!?]["' ,]*[[A-Z]]|;/ */
y.log(search);
if (search < 0) {
phrases.push(remainingpassage);
remainingpassage = "";
} else {
phrases.push(remainingpassage.slice(0,search+2).trim());
remainingpassage = remainingpassage.substr(search+1).trim();
}
debug && y.log(phrases[phrases.length-1]);
}
var index = "abcdefghijkl".indexOf(phrasechar);
var i = 0;
var result = new String();
var before = phrase.match(/^-/);
debug && y.log("before: " + before);
var after = phrase.match(/-$/);
debug && y.log("after: " + after);
for each (var p in phrases) {
var b;
b = (i == index);
b = b || (before && i < index);
b = b || (after && i > index);
debug && y.log(b);
if (b) {
result += (p + ' ');
}
i++;
}
return result;
}
]]></execute>
</select>
</bindings>
</table>