/js/he_bcv_parser.js
JavaScript | 6251 lines | 6063 code | 140 blank | 48 comment | 875 complexity | bdc4fd4731461b5772dfb5570316e791 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- // Generated by CoffeeScript 1.3.3
- (function() {
- var bcv_parser, bcv_passage, bcv_utils, root,
- __hasProp = {}.hasOwnProperty;
- root = this;
- bcv_parser = (function() {
- bcv_parser.prototype.s = "";
- bcv_parser.prototype.entities = [];
- bcv_parser.prototype.passage = null;
- bcv_parser.prototype.regexps = {};
- bcv_parser.prototype.default_options = {};
- bcv_parser.prototype.options = {
- consecutive_combination_strategy: "combine",
- osis_compaction_strategy: "b",
- book_sequence_strategy: "ignore",
- invalid_sequence_strategy: "ignore",
- sequence_combination_strategy: "combine",
- invalid_passage_strategy: "ignore",
- zero_chapter_strategy: "error",
- zero_verse_strategy: "error",
- non_latin_digits_strategy: "ignore",
- book_alone_strategy: "ignore",
- captive_end_digits_strategy: "delete",
- end_range_digits_strategy: "verse",
- include_apocrypha: false,
- versification_system: "default"
- };
- function bcv_parser() {
- var key, val, _ref;
- this.options = {};
- _ref = bcv_parser.prototype.options;
- for (key in _ref) {
- if (!__hasProp.call(_ref, key)) continue;
- val = _ref[key];
- this.options[key] = val;
- }
- this.versification_system(this.options.versification_system);
- }
- bcv_parser.prototype.parse = function(s) {
- var accum, entities, entity, _i, _len, _ref;
- this.reset();
- this.s = s;
- s = this.replace_control_characters(s);
- _ref = this.match_books(s), s = _ref[0], this.passage.books = _ref[1];
- entities = this.match_passages(s);
- this.entities = [];
- for (_i = 0, _len = entities.length; _i < _len; _i++) {
- entity = entities[_i];
- accum = this.passage.handle_array([entity])[0];
- this.entities = this.entities.concat(accum);
- }
- return this;
- };
- bcv_parser.prototype.reset = function() {
- this.s = "";
- this.entities = [];
- if (this.passage) {
- this.passage.books = [];
- return this.passage.indices = {};
- } else {
- this.passage = new bcv_passage;
- this.passage.options = this.options;
- return this.passage.translations = this.translations;
- }
- };
- bcv_parser.prototype.set_options = function(options) {
- var key, val, _results;
- _results = [];
- for (key in options) {
- if (!__hasProp.call(options, key)) continue;
- val = options[key];
- if (key === "include_apocrypha" || key === "versification_system") {
- _results.push(this[key](val));
- } else {
- _results.push(this.options[key] = val);
- }
- }
- return _results;
- };
- bcv_parser.prototype.include_apocrypha = function(arg) {
- if (!((arg != null) && (arg === true || arg === false))) {
- return this;
- }
- this.options.include_apocrypha = arg;
- this.regexps.books = this.regexps.get_books(arg);
- if (arg === true) {
- this.translations["default"].chapters.Ps[150] = this.translations["default"].chapters.Ps151[0];
- } else if (arg === false) {
- if (this.translations["default"].chapters.Ps.length === 151) {
- this.translations["default"].chapters.Ps.pop();
- }
- }
- return this;
- };
- bcv_parser.prototype.versification_system = function(system) {
- var book, chapter_list, _base, _ref, _ref1;
- if (!((system != null) && (this.translations.alternates[system] != null))) {
- return this;
- }
- this.options.versification_system = system;
- if ((_ref = (_base = this.translations.alternates)["default"]) == null) {
- _base["default"] = {
- order: null,
- chapters: {}
- };
- }
- if (this.translations.alternates[system].order != null) {
- if (this.translations.alternates["default"].order == null) {
- this.translations.alternates["default"].order = bcv_utils.shallow_clone(this.translations["default"].order);
- }
- this.translations["default"].order = bcv_utils.shallow_clone(this.translations.alternates[system].order);
- }
- if (this.translations.alternates[system].chapters != null) {
- _ref1 = this.translations.alternates[system].chapters;
- for (book in _ref1) {
- if (!__hasProp.call(_ref1, book)) continue;
- chapter_list = _ref1[book];
- if (this.translations.alternates["default"].chapters[book] == null) {
- this.translations.alternates["default"].chapters[book] = bcv_utils.shallow_clone_array(this.translations["default"].chapters[book]);
- }
- this.translations["default"].chapters[book] = bcv_utils.shallow_clone_array(chapter_list);
- }
- }
- this.include_apocrypha(this.options.include_apocrypha);
- return this;
- };
- bcv_parser.prototype.replace_control_characters = function(s) {
- s = s.replace(this.regexps.control, " ");
- if (this.options.non_latin_digits_strategy === "replace") {
- s = s.replace(/[٠۰߀०০੦૦୦0౦೦൦๐໐༠၀႐០᠐᥆᧐᪀᪐᭐᮰᱀᱐꘠꣐꤀꧐꩐꯰0]/g, "0");
- s = s.replace(/[١۱߁१১੧૧୧௧౧೧൧๑໑༡၁႑១᠑᥇᧑᪁᪑᭑᮱᱁᱑꘡꣑꤁꧑꩑꯱1]/g, "1");
- s = s.replace(/[٢۲߂२২੨૨୨௨౨೨൨๒໒༢၂႒២᠒᥈᧒᪂᪒᭒᮲᱂᱒꘢꣒꤂꧒꩒꯲2]/g, "2");
- s = s.replace(/[٣۳߃३৩੩૩୩௩౩೩൩๓໓༣၃႓៣᠓᥉᧓᪃᪓᭓᮳᱃᱓꘣꣓꤃꧓꩓꯳3]/g, "3");
- s = s.replace(/[٤۴߄४৪੪૪୪௪౪೪൪๔໔༤၄႔៤᠔᥊᧔᪄᪔᭔᮴᱄᱔꘤꣔꤄꧔꩔꯴4]/g, "4");
- s = s.replace(/[٥۵߅५৫੫૫୫௫౫೫൫๕໕༥၅႕៥᠕᥋᧕᪅᪕᭕᮵᱅᱕꘥꣕꤅꧕꩕꯵5]/g, "5");
- s = s.replace(/[٦۶߆६৬੬૬୬௬౬೬൬๖໖༦၆႖៦᠖᥌᧖᪆᪖᭖᮶᱆᱖꘦꣖꤆꧖꩖꯶6]/g, "6");
- s = s.replace(/[٧۷߇७৭੭૭୭௭౭೭൭๗໗༧၇႗៧᠗᥍᧗᪇᪗᭗᮷᱇᱗꘧꣗꤇꧗꩗꯷7]/g, "7");
- s = s.replace(/[٨۸߈८৮੮૮୮௮౮೮൮๘໘༨၈႘៨᠘᥎᧘᪈᪘᭘᮸᱈᱘꘨꣘꤈꧘꩘꯸8]/g, "8");
- s = s.replace(/[٩۹߉९৯੯૯୯௯౯೯൯๙໙༩၉႙៩᠙᥏᧙᪉᪙᭙᮹᱉᱙꘩꣙꤉꧙꩙꯹9]/g, "9");
- }
- return s;
- };
- bcv_parser.prototype.match_books = function(s) {
- var book, books, _i, _len, _ref;
- books = [];
- _ref = this.regexps.books;
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- book = _ref[_i];
- s = s.replace(book.regexp, function(full, prev, bk) {
- var extra;
- books.push({
- value: bk,
- parsed: book.osis
- });
- extra = book.extra != null ? "/" + book.extra : "";
- return "" + prev + "\x1f" + (books.length - 1) + extra + "\x1f";
- });
- }
- s = s.replace(this.regexps.translations, function(match) {
- books.push({
- value: match,
- parsed: match.toLowerCase()
- });
- return "\x1e" + (books.length - 1) + "\x1e";
- });
- return [s, this.get_book_indices(books, s)];
- };
- bcv_parser.prototype.get_book_indices = function(books, s) {
- var add_index, match, re;
- add_index = 0;
- re = /([\x1f\x1e])(\d+)(?:\/[a-z])?\1/g;
- while (match = re.exec(s)) {
- books[match[2]].start_index = match.index + add_index;
- add_index += books[match[2]].value.length - match[0].length;
- }
- return books;
- };
- bcv_parser.prototype.match_passages = function(s) {
- var book_id, full, match, next_char, part, passage, passages, re, start_index_adjust;
- passages = [];
- while (match = this.regexps.escaped_passage.exec(s)) {
- full = match[0], part = match[1], book_id = match[2];
- match.index += full.length - part.length;
- if (/\s[2-9]\d\d\s*$|\s\d{4,}\s*$/.test(part)) {
- re = /\s+\d+\s*$/;
- part = part.replace(re, "");
- }
- if (!/[\d\x1f\x1e)]$/.test(part)) {
- part = this.replace_match_end(part);
- }
- if (this.options.captive_end_digits_strategy === "delete") {
- next_char = match.index + part.length;
- if (s.length > next_char && /^\w/.test(s.substr(next_char, 1))) {
- part = part.replace(/[\s*]+\d+$/, "");
- }
- part = part.replace(/(\x1e[)\]]?)[\s*]*\d+$/, "$1");
- }
- part = part.replace(/[A-Z]+/g, function(capitals) {
- return capitals.toLowerCase();
- });
- start_index_adjust = part.substr(0, 1 === "\x1f") ? 0 : part.split("\x1f")[0].length;
- passage = {
- value: grammar.parse(part),
- type: "base",
- start_index: this.passage.books[book_id].start_index - start_index_adjust,
- match: part
- };
- if (this.options.book_alone_strategy === "full" && passage.value.length === 1 && passage.value[0].type === "b" && start_index_adjust === 0 && this.passage.books[book_id].parsed.length === 1 && /^[234]/.test(this.passage.books[book_id].parsed[0])) {
- this.create_book_range(s, passage, book_id);
- }
- passages.push(passage);
- }
- return passages;
- };
- bcv_parser.prototype.replace_match_end = function(part) {
- var match, remove;
- remove = part.length;
- while (match = this.regexps.match_end_split.exec(part)) {
- remove = match.index + match[0].length;
- }
- if (remove < part.length) {
- part = part.substr(0, remove);
- }
- return part;
- };
- bcv_parser.prototype.create_book_range = function(s, passage, book_id) {
- var cases, i, limit, prev, range_regexp, _i;
- cases = [bcv_parser.prototype.regexps.first, bcv_parser.prototype.regexps.second, bcv_parser.prototype.regexps.third];
- limit = parseInt(this.passage.books[book_id].parsed[0].substr(0, 1), 10);
- for (i = _i = 1; 1 <= limit ? _i < limit : _i > limit; i = 1 <= limit ? ++_i : --_i) {
- range_regexp = i === limit - 1 ? bcv_parser.prototype.regexps.range_and : bcv_parser.prototype.regexps.range_only;
- prev = s.match(RegExp("(?:^|\\W)(" + cases[i - 1] + "\\s*" + range_regexp + "\\s*)\\x1f" + book_id + "\\x1f", "i"));
- if (prev != null) {
- return this.add_book_range_object(passage, prev, i);
- }
- }
- return false;
- };
- bcv_parser.prototype.add_book_range_object = function(passage, prev, start_book_number) {
- var length;
- length = prev[1].length;
- passage.value[0] = {
- type: "b_range_pre",
- value: [
- {
- type: "b_pre",
- value: start_book_number.toString(),
- indices: [prev.index, prev.index + length]
- }, passage.value[0]
- ],
- indices: [0, passage.value[0].indices[1] + length]
- };
- passage.value[0].value[1].indices[0] += length;
- passage.value[0].value[1].indices[1] += length;
- passage.start_index -= length;
- return passage.match = prev[1] + passage.match;
- };
- bcv_parser.prototype.osis = function() {
- var osis, out, _i, _len, _ref;
- out = [];
- _ref = this.parsed_entities();
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- osis = _ref[_i];
- if (osis.osis.length > 0) {
- out.push(osis.osis);
- }
- }
- return out.join(",");
- };
- bcv_parser.prototype.osis_and_translations = function() {
- var osis, out, _i, _len, _ref;
- out = [];
- _ref = this.parsed_entities();
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- osis = _ref[_i];
- if (osis.osis.length > 0) {
- out.push([osis.osis, osis.translations.join(",")]);
- }
- }
- return out;
- };
- bcv_parser.prototype.osis_and_indices = function() {
- var osis, out, _i, _len, _ref;
- out = [];
- _ref = this.parsed_entities();
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- osis = _ref[_i];
- if (osis.osis.length > 0) {
- out.push({
- osis: osis.osis,
- translations: osis.translations,
- indices: osis.indices
- });
- }
- }
- return out;
- };
- bcv_parser.prototype.parsed_entities = function() {
- var entity, entity_id, i, last_i, length, osis, osises, out, passage, strings, translation, translation_alias, translation_osis, translations, _i, _j, _k, _l, _len, _len1, _ref, _ref1, _ref2, _ref3, _ref4;
- out = [];
- for (entity_id = _i = 0, _ref = this.entities.length; 0 <= _ref ? _i < _ref : _i > _ref; entity_id = 0 <= _ref ? ++_i : --_i) {
- entity = this.entities[entity_id];
- if (entity.type && entity.type === "translation_sequence" && out.length > 0 && entity_id === out[out.length - 1].entity_id + 1) {
- out[out.length - 1].indices[1] = entity.absolute_indices[1];
- }
- if (entity.passages == null) {
- continue;
- }
- if ((entity.type === "b" || entity.type === "b_range") && this.options.book_alone_strategy === "ignore") {
- continue;
- }
- translations = [];
- translation_alias = null;
- if (entity.passages[0].translations != null) {
- _ref1 = entity.passages[0].translations;
- for (_j = 0, _len = _ref1.length; _j < _len; _j++) {
- translation = _ref1[_j];
- translation_osis = ((_ref2 = translation.osis) != null ? _ref2.length : void 0) > 0 ? translation.osis : "";
- if (translation_alias == null) {
- translation_alias = translation.alias;
- }
- translations.push(translation_osis);
- }
- } else {
- translations = [""];
- translation_alias = "default";
- }
- osises = [];
- length = entity.passages.length;
- for (i = _k = 0; 0 <= length ? _k < length : _k > length; i = 0 <= length ? ++_k : --_k) {
- passage = entity.passages[i];
- if ((_ref3 = passage.type) == null) {
- passage.type = entity.type;
- }
- if (passage.valid.valid === false) {
- if (this.options.invalid_sequence_strategy === "ignore" && entity.type === "sequence") {
- this.snap_sequence("ignore", entity, osises, i, length);
- }
- if (this.options.invalid_passage_strategy === "ignore") {
- continue;
- }
- }
- if ((passage.type === "b" || passage.type === "b_range") && this.options.book_sequence_strategy === "ignore" && entity.type === "sequence") {
- this.snap_sequence("book", entity, osises, i, length);
- continue;
- }
- if ((_ref4 = passage.absolute_indices) == null) {
- passage.absolute_indices = entity.absolute_indices;
- }
- osises.push({
- osis: passage.valid.valid ? this.to_osis(passage.start, passage.end, translation_alias) : "",
- type: passage.type,
- indices: passage.absolute_indices,
- translations: translations,
- start: passage.start,
- end: passage.end,
- enclosed_indices: passage.enclosed_absolute_indices,
- entity_id: entity_id,
- entities: [passage]
- });
- }
- if (osises.length === 0) {
- continue;
- }
- if (osises.length > 1 && this.options.consecutive_combination_strategy === "combine") {
- osises = this.combine_consecutive_passages(osises, translation_alias);
- }
- if (this.options.sequence_combination_strategy === "separate") {
- out = out.concat(osises);
- } else {
- strings = [];
- last_i = osises.length - 1;
- if ((osises[last_i].enclosed_indices != null) && osises[last_i].enclosed_indices[1] >= 0) {
- entity.absolute_indices[1] = osises[last_i].enclosed_indices[1];
- }
- for (_l = 0, _len1 = osises.length; _l < _len1; _l++) {
- osis = osises[_l];
- if (osis.osis.length > 0) {
- strings.push(osis.osis);
- }
- }
- out.push({
- osis: strings.join(","),
- indices: entity.absolute_indices,
- translations: translations,
- entity_id: entity_id,
- entities: osises
- });
- }
- }
- return out;
- };
- bcv_parser.prototype.to_osis = function(start, end, translation) {
- var osis, _ref, _ref1, _ref2, _ref3;
- if (!(end.c != null) && !(end.v != null) && start.b === end.b && !(start.c != null) && !(start.v != null) && this.options.book_alone_strategy === "first_chapter") {
- end.c = 1;
- }
- osis = {
- start: "",
- end: ""
- };
- if ((_ref = start.c) == null) {
- start.c = 1;
- }
- if ((_ref1 = start.v) == null) {
- start.v = 1;
- }
- if ((_ref2 = end.c) == null) {
- end.c = this.passage.translations[translation].chapters[end.b].length;
- }
- if ((_ref3 = end.v) == null) {
- end.v = this.passage.translations[translation].chapters[end.b][end.c - 1];
- }
- if (this.options.osis_compaction_strategy === "b" && start.c === 1 && start.v === 1 && end.c === this.passage.translations[translation].chapters[end.b].length && end.v === this.passage.translations[translation].chapters[end.b][end.c - 1]) {
- osis.start = start.b;
- osis.end = end.b;
- } else if (this.options.osis_compaction_strategy.length <= 2 && start.v === 1 && end.v === this.passage.translations[translation].chapters[end.b][end.c - 1]) {
- osis.start = start.b + "." + start.c.toString();
- osis.end = end.b + "." + end.c.toString();
- } else {
- osis.start = start.b + "." + start.c.toString() + "." + start.v.toString();
- osis.end = end.b + "." + end.c.toString() + "." + end.v.toString();
- }
- if (osis.start === osis.end) {
- return osis.start;
- }
- return osis.start + "-" + osis.end;
- };
- bcv_parser.prototype.combine_consecutive_passages = function(osises, translation) {
- var enclosed_sequence_start, i, last_i, osis, out, prev, prev_i, _i;
- out = [];
- prev = {};
- last_i = osises.length - 1;
- enclosed_sequence_start = -1;
- for (i = _i = 0; 0 <= last_i ? _i <= last_i : _i >= last_i; i = 0 <= last_i ? ++_i : --_i) {
- osis = osises[i];
- if (osis.osis.length > 0) {
- prev_i = out.length - 1;
- osis.is_enclosed_first = false;
- osis.is_enclosed_last = false;
- if (osis.enclosed_indices[0] !== enclosed_sequence_start) {
- enclosed_sequence_start = osis.enclosed_indices[0];
- if (enclosed_sequence_start >= 0) {
- osis.is_enclosed_first = true;
- }
- }
- if (enclosed_sequence_start >= 0 && (i === last_i || osises[i + 1].enclosed_indices[0] !== osis.enclosed_indices[0])) {
- osis.is_enclosed_last = true;
- }
- if (this.is_verse_consecutive(prev, osis.start, translation)) {
- out[prev_i].end = osis.end;
- out[prev_i].is_enclosed_last = osis.is_enclosed_last;
- out[prev_i].indices[1] = osis.indices[1];
- out[prev_i].enclosed_indices[1] = osis.enclosed_indices[1];
- out[prev_i].osis = this.to_osis(out[prev_i].start, osis.end, translation);
- } else {
- out.push(osis);
- }
- prev = {
- b: osis.end.b,
- c: osis.end.c,
- v: osis.end.v
- };
- } else {
- out.push(osis);
- prev = {};
- }
- }
- return this.snap_enclosed_indices(out);
- };
- bcv_parser.prototype.snap_enclosed_indices = function(osises) {
- var osis, _i, _len;
- for (_i = 0, _len = osises.length; _i < _len; _i++) {
- osis = osises[_i];
- if (osis.enclosed_indices[0] < 0 && osis.is_enclosed_last) {
- osis.indices[1] = osis.enclosed_indices[1];
- }
- delete osis.is_enclosed_first;
- delete osis.is_enclosed_last;
- }
- return osises;
- };
- bcv_parser.prototype.is_verse_consecutive = function(prev, check, translation) {
- var translation_order;
- if (prev.b == null) {
- return false;
- }
- translation_order = this.passage.translations[translation].order != null ? this.passage.translations[translation].order : this.passage.translations["default"].order;
- if (prev.b === check.b) {
- if (prev.c === check.c) {
- if (prev.v === check.v - 1) {
- return true;
- }
- } else if (check.v === 1 && prev.c === check.c - 1) {
- if (prev.v === this.passage.translations[translation].chapters[prev.b][prev.c - 1]) {
- return true;
- }
- }
- } else if (check.c === 1 && check.v === 1 && translation_order[prev.b] === translation_order[check.b] - 1) {
- if (prev.c === this.passage.translations[translation].chapters[prev.b].length && prev.v === this.passage.translations[translation].chapters[prev.b][prev.c - 1]) {
- return true;
- }
- }
- return false;
- };
- bcv_parser.prototype.snap_sequence = function(type, entity, osises, i, length) {
- var passage;
- passage = entity.passages[i];
- if (passage.absolute_indices[0] === entity.absolute_indices[0] && i < length - 1 && this.get_snap_sequence_i(entity.passages, i, length) !== i) {
- entity.absolute_indices[0] = entity.passages[i + 1].absolute_indices[0];
- this.remove_absolute_indices(entity.passages, i + 1);
- } else if (passage.absolute_indices[1] === entity.absolute_indices[1] && i > 0) {
- entity.absolute_indices[1] = osises.length > 0 ? osises[osises.length - 1].indices[1] : entity.passages[i - 1].absolute_indices[1];
- } else if (type === "book" && i < length - 1 && !this.starts_with_book(entity.passages[i + 1])) {
- entity.passages[i + 1].absolute_indices[0] = passage.absolute_indices[0];
- }
- return entity;
- };
- bcv_parser.prototype.get_snap_sequence_i = function(passages, i, length) {
- var j, _i, _ref;
- for (j = _i = _ref = i + 1; _ref <= length ? _i < length : _i > length; j = _ref <= length ? ++_i : --_i) {
- if (this.starts_with_book(passages[j])) {
- return j;
- }
- if (passages[j].valid.valid) {
- return i;
- }
- }
- return i;
- };
- bcv_parser.prototype.starts_with_book = function(passage) {
- if (passage.type.substr(0, 1) === "b") {
- return true;
- }
- if ((passage.type === "range" || passage.type === "ff") && passage.start.type.substr(0, 1) === "b") {
- return true;
- }
- return false;
- };
- bcv_parser.prototype.remove_absolute_indices = function(passages, i) {
- var end, passage, start, _i, _len, _ref, _ref1;
- if (passages[i].enclosed_absolute_indices[0] < 0) {
- return false;
- }
- _ref = passages[i].enclosed_absolute_indices, start = _ref[0], end = _ref[1];
- _ref1 = passages.slice(i);
- for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
- passage = _ref1[_i];
- if (passage.enclosed_absolute_indices[0] === start && passage.enclosed_absolute_indices[1] === end) {
- passage.enclosed_absolute_indices = [-1, -1];
- } else {
- break;
- }
- }
- return true;
- };
- return bcv_parser;
- })();
- root.bcv_parser = bcv_parser;
- bcv_passage = (function() {
- function bcv_passage() {}
- bcv_passage.prototype.books = [];
- bcv_passage.prototype.indices = {};
- bcv_passage.prototype.options = {};
- bcv_passage.prototype.translations = {};
- bcv_passage.prototype.handle_array = function(passages, accum, context) {
- var passage, _i, _len, _ref;
- if (accum == null) {
- accum = [];
- }
- if (context == null) {
- context = {};
- }
- for (_i = 0, _len = passages.length; _i < _len; _i++) {
- passage = passages[_i];
- if (passage.type === "stop") {
- break;
- }
- _ref = this.handle_obj(passage, accum, context), accum = _ref[0], context = _ref[1];
- }
- return [accum, context];
- };
- bcv_passage.prototype.handle_obj = function(passage, accum, context) {
- if ((passage.type != null) && (this[passage.type] != null)) {
- return this[passage.type](passage, accum, context);
- } else {
- return [accum, context];
- }
- };
- bcv_passage.prototype.b = function(passage, accum, context) {
- var alternates, b, obj, valid, _i, _len, _ref, _ref1;
- passage.start_context = bcv_utils.shallow_clone(context);
- passage.passages = [];
- alternates = [];
- _ref = this.books[passage.value].parsed;
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- b = _ref[_i];
- valid = this.validate_ref(passage.start_context.translations, {
- b: b
- });
- obj = {
- start: {
- b: b
- },
- end: {
- b: b
- },
- valid: valid
- };
- if (passage.passages.length === 0 && valid.valid) {
- passage.passages.push(obj);
- } else {
- alternates.push(obj);
- }
- }
- if (passage.passages.length === 0) {
- passage.passages.push(alternates.shift());
- }
- if (alternates.length > 0) {
- passage.passages[0].alternates = alternates;
- }
- if (passage.start_context.translations != null) {
- passage.passages[0].translations = passage.start_context.translations;
- }
- if ((_ref1 = passage.absolute_indices) == null) {
- passage.absolute_indices = this.get_absolute_indices(passage.indices);
- }
- accum.push(passage);
- context = {
- b: passage.passages[0].start.b
- };
- if (passage.start_context.translations != null) {
- context.translations = passage.start_context.translations;
- }
- return [accum, context];
- };
- bcv_passage.prototype.b_range = function(passage, accum, context) {
- return this.range(passage, accum, context);
- };
- bcv_passage.prototype.b_range_pre = function(passage, accum, context) {
- var alternates, book, end, start_obj, _ref, _ref1, _ref2;
- passage.start_context = bcv_utils.shallow_clone(context);
- passage.passages = [];
- alternates = [];
- book = this.pluck("b", passage.value);
- _ref = this.b(book, [], context), (_ref1 = _ref[0], end = _ref1[0]), context = _ref[1];
- if ((_ref2 = passage.absolute_indices) == null) {
- passage.absolute_indices = this.get_absolute_indices(passage.indices);
- }
- start_obj = {
- b: passage.value[0].value + end.passages[0].start.b.substr(1),
- type: "b"
- };
- passage.passages = [
- {
- start: start_obj,
- end: end.passages[0].end,
- valid: end.passages[0].valid
- }
- ];
- if (passage.start_context.translations != null) {
- passage.passages[0].translations = passage.start_context.translations;
- }
- accum.push(passage);
- return [accum, context];
- };
- bcv_passage.prototype.base = function(passage, accum, context) {
- this.indices = this.calculate_indices(passage.match, passage.start_index);
- return this.handle_array(passage.value, accum, context);
- };
- bcv_passage.prototype.bc = function(passage, accum, context) {
- var alternates, b, c, context_key, obj, type, valid, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3, _ref4;
- passage.start_context = bcv_utils.shallow_clone(context);
- passage.passages = [];
- _ref = ["b", "c", "v"];
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- type = _ref[_i];
- delete context[type];
- }
- c = this.pluck("c", passage.value).value;
- alternates = [];
- _ref1 = this.books[this.pluck("b", passage.value).value].parsed;
- for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
- b = _ref1[_j];
- context_key = "c";
- valid = this.validate_ref(passage.start_context.translations, {
- b: b,
- c: c
- });
- obj = {
- start: {
- b: b
- },
- end: {
- b: b
- },
- valid: valid
- };
- if (valid.messages.start_chapter_not_exist_in_single_chapter_book) {
- obj.valid = this.validate_ref(passage.start_context.translations, {
- b: b,
- v: c
- });
- obj.start.c = 1;
- obj.end.c = 1;
- context_key = "v";
- }
- obj.start[context_key] = c;
- _ref2 = this.fix_start_zeroes(obj.valid, obj.start.c, obj.start.v), obj.start.c = _ref2[0], obj.start.v = _ref2[1];
- obj.end[context_key] = obj.start[context_key];
- if (passage.passages.length === 0 && obj.valid.valid) {
- passage.passages.push(obj);
- } else {
- alternates.push(obj);
- }
- }
- if (passage.passages.length === 0) {
- passage.passages.push(alternates.shift());
- }
- if (alternates.length > 0) {
- passage.passages[0].alternates = alternates;
- }
- if (passage.start_context.translations != null) {
- passage.passages[0].translations = passage.start_context.translations;
- }
- if ((_ref3 = passage.absolute_indices) == null) {
- passage.absolute_indices = this.get_absolute_indices(passage.indices);
- }
- _ref4 = ["b", "c", "v"];
- for (_k = 0, _len2 = _ref4.length; _k < _len2; _k++) {
- type = _ref4[_k];
- if (passage.passages[0].start[type] != null) {
- context[type] = passage.passages[0].start[type];
- }
- }
- accum.push(passage);
- return [accum, context];
- };
- bcv_passage.prototype.bc_title = function(passage, accum, context) {
- var bc, i, title, _i, _ref, _ref1, _ref2;
- passage.start_context = bcv_utils.shallow_clone(context);
- _ref = this.bc(this.pluck("bc", passage.value), [], context), (_ref1 = _ref[0], bc = _ref1[0]), context = _ref[1];
- if (bc.passages[0].start.b !== "Ps" && (bc.passages[0].alternates != null)) {
- for (i = _i = 0, _ref2 = bc.passages[0].alternates.length; 0 <= _ref2 ? _i < _ref2 : _i > _ref2; i = 0 <= _ref2 ? ++_i : --_i) {
- if (bc.passages[0].alternates[i].start.b !== "Ps") {
- continue;
- }
- bc.passages[0] = bc.passages[0].alternates[i];
- break;
- }
- }
- if (bc.passages[0].start.b !== "Ps") {
- accum.push(bc);
- return [accum, context];
- }
- this.books[this.pluck("b", bc.value).value].parsed = ["Ps"];
- title = this.pluck("title", passage.value);
- passage.value[1] = {
- type: "v",
- value: [
- {
- type: "integer",
- value: 1,
- indices: title.indices
- }
- ],
- indices: title.indices
- };
- passage.original_type = "bc_title";
- passage.type = "bcv";
- return this.bcv(passage, accum, passage.start_context);
- };
- bcv_passage.prototype.bcv = function(passage, accum, context) {
- var alternates, b, bc, c, obj, type, v, valid, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3, _ref4;
- passage.start_context = bcv_utils.shallow_clone(context);
- passage.passages = [];
- _ref = ["b", "c", "v"];
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- type = _ref[_i];
- delete context[type];
- }
- bc = this.pluck("bc", passage.value);
- c = this.pluck("c", bc.value).value;
- v = this.pluck("v", passage.value).value;
- alternates = [];
- _ref1 = this.books[this.pluck("b", bc.value).value].parsed;
- for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
- b = _ref1[_j];
- valid = this.validate_ref(passage.start_context.translations, {
- b: b,
- c: c,
- v: v
- });
- _ref2 = this.fix_start_zeroes(valid, c, v), c = _ref2[0], v = _ref2[1];
- obj = {
- start: {
- b: b,
- c: c,
- v: v
- },
- end: {
- b: b,
- c: c,
- v: v
- },
- valid: valid
- };
- if (passage.passages.length === 0 && valid.valid) {
- passage.passages.push(obj);
- } else {
- alternates.push(obj);
- }
- }
- if (passage.passages.length === 0) {
- passage.passages.push(alternates.shift());
- }
- if (alternates.length > 0) {
- passage.passages[0].alternates = alternates;
- }
- if (passage.start_context.translations != null) {
- passage.passages[0].translations = passage.start_context.translations;
- }
- if ((_ref3 = passage.absolute_indices) == null) {
- passage.absolute_indices = this.get_absolute_indices(passage.indices);
- }
- _ref4 = ["b", "c", "v"];
- for (_k = 0, _len2 = _ref4.length; _k < _len2; _k++) {
- type = _ref4[_k];
- if (passage.passages[0].start[type] != null) {
- context[type] = passage.passages[0].start[type];
- }
- }
- accum.push(passage);
- return [accum, context];
- };
- bcv_passage.prototype.bv = function(passage, accum, context) {
- var b, bcv, v, _ref, _ref1, _ref2, _ref3;
- passage.start_context = bcv_utils.shallow_clone(context);
- _ref = passage.value, b = _ref[0], v = _ref[1];
- bcv = {
- indices: passage.indices,
- value: [
- {
- type: "bc",
- value: [
- b, {
- type: "c",
- value: [
- {
- type: "integer",
- value: 1
- }
- ]
- }
- ]
- }, v
- ]
- };
- _ref1 = this.bcv(bcv, [], context), (_ref2 = _ref1[0], bcv = _ref2[0]), context = _ref1[1];
- passage.passages = bcv.passages;
- if (passage.start_context.translations != null) {
- passage.passages[0].translations = passage.start_context.translations;
- }
- if ((_ref3 = passage.absolute_indices) == null) {
- passage.absolute_indices = this.get_absolute_indices(passage.indices);
- }
- accum.push(passage);
- return [accum, context];
- };
- bcv_passage.prototype.c = function(passage, accum, context) {
- var c, valid, _ref;
- passage.start_context = bcv_utils.shallow_clone(context);
- c = passage.type === "integer" ? passage.value : this.pluck("integer", passage.value).value;
- valid = this.validate_ref(passage.start_context.translations, {
- b: context.b,
- c: c
- });
- if (!valid.valid && valid.messages.start_chapter_not_exist_in_single_chapter_book) {
- return this.v(passage, accum, context);
- }
- c = this.fix_start_zeroes(valid, c)[0];
- passage.passages = [
- {
- start: {
- b: context.b,
- c: c
- },
- end: {
- b: context.b,
- c: c
- },
- valid: valid
- }
- ];
- if (passage.start_context.translations != null) {
- passage.passages[0].translations = passage.start_context.translations;
- }
- accum.push(passage);
- context.c = c;
- delete context.v;
- if ((_ref = passage.absolute_indices) == null) {
- passage.absolute_indices = this.get_absolute_indices(passage.indices);
- }
- return [accum, context];
- };
- bcv_passage.prototype.c_psalm = function(passage, accum, context) {
- var c;
- passage.original_type = passage.type;
- passage.original_value = passage.value;
- passage.type = "bc";
- c = this.books[passage.value].value.match(/^\d+/)[0];
- passage.value = [
- {
- type: "b",
- value: passage.original_value,
- indices: passage.indices
- }, {
- type: "c",
- value: [
- {
- type: "integer",
- value: c,
- indices: passage.indices
- }
- ],
- indices: passage.indices
- }
- ];
- return this.bc(passage, accum, context);
- };
- bcv_passage.prototype.c_title = function(passage, accum, context) {
- var title;
- passage.start_context = bcv_utils.shallow_clone(context);
- if (context.b !== "Ps") {
- return this.c(passage.value[0], accum, context);
- }
- title = this.pluck("title", passage.value);
- passage.value[1] = {
- type: "v",
- value: [
- {
- type: "integer",
- value: 1,
- indices: title.indices
- }
- ],
- indices: title.indices
- };
- passage.original_type = "c_title";
- passage.type = "cv";
- return this.cv(passage, accum, passage.start_context);
- };
- bcv_passage.prototype.cv = function(passage, accum, context) {
- var c, v, valid, _ref, _ref1;
- passage.start_context = bcv_utils.shallow_clone(context);
- c = this.pluck("c", passage.value).value;
- v = this.pluck("v", passage.value).value;
- valid = this.validate_ref(passage.start_context.translations, {
- b: context.b,
- c: c,
- v: v
- });
- _ref = this.fix_start_zeroes(valid, c, v), c = _ref[0], v = _ref[1];
- passage.passages = [
- {
- start: {
- b: context.b,
- c: c,
- v: v
- },
- end: {
- b: context.b,
- c: c,
- v: v
- },
- valid: valid
- }
- ];
- if (passage.start_context.translations != null) {
- passage.passages[0].translations = passage.start_context.translations;
- }
- accum.push(passage);
- context.c = c;
- context.v = v;
- if ((_ref1 = passage.absolute_indices) == null) {
- passage.absolute_indices = this.get_absolute_indices(passage.indices);
- }
- return [accum, context];
- };
- bcv_passage.prototype.cb_range = function(passage, accum, context) {
- var b, end_c, start_c, _ref;
- passage.original_type = passage.type;
- passage.type = "range";
- _ref = passage.value, b = _ref[0], start_c = _ref[1], end_c = _ref[2];
- passage.original_value = [b, start_c, end_c];
- passage.value = [
- {
- type: "bc",
- value: [b, start_c],
- indices: passage.indices
- }, end_c
- ];
- end_c.indices[1] = passage.indices[1];
- return this.range(passage, accum, context);
- };
- bcv_passage.prototype.cv_psalm = function(passage, accum, context) {
- var bc, c_psalm, v, _ref;
- passage.start_context = bcv_utils.shallow_clone(context);
- passage.original_type = passage.type;
- passage.original_value = passage.value;
- _ref = passage.value, c_psalm = _ref[0], v = _ref[1];
- passage.type = "bcv";
- bc = this.c_psalm(c_psalm, [], passage.start_context)[0][0];
- passage.value = [bc, v];
- return this.bcv(passage, accum, context);
- };
- bcv_passage.prototype.ff = function(passage, accum, context) {
- var _ref, _ref1, _ref2;
- passage.start_context = bcv_utils.shallow_clone(context);
- passage.value.push({
- type: "integer",
- indices: passage.indices,
- value: 999
- });
- _ref = this.range(passage, [], passage.start_context), (_ref1 = _ref[0], passage = _ref1[0]), context = _ref[1];
- passage.value.pop();
- if (passage.passages[0].valid.end_verse_not_exist != null) {
- delete passage.passages[0].valid.end_verse_not_exist;
- }
- if (passage.passages[0].valid.end_chapter_not_exist != null) {
- delete passage.passages[0].valid.end_chapter_not_exist;
- }
- if (passage.passages[0].end.original_c != null) {
- delete passage.passages[0].end.original_c;
- }
- accum.push(passage);
- if ((_ref2 = passage.absolute_indices) == null) {
- passage.absolute_indices = this.get_absolute_indices(passage.indices);
- }
- return [accum, context];
- };
- bcv_passage.prototype.integer_title = function(passage, accum, context) {
- var v_indices;
- passage.start_context = bcv_utils.shallow_clone(context);
- if (context.b !== "Ps") {
- return this.integer(passage.value[0], accum, context);
- }
- passage.value[0] = {
- type: "c",
- value: [passage.value[0]],
- indices: [passage.value[0].indices[0], passage.value[0].indices[1]]
- };
- v_indices = [passage.indices[1] - 5, passage.indices[1]];
- passage.value[1] = {
- type: "v",
- value: [
- {
- type: "integer",
- value: 1,
- indices: v_indices
- }
- ],
- indices: v_indices
- };
- passage.original_type = "integer_title";
- passage.type = "cv";
- return this.cv(passage, accum, passage.start_context);
- };
- bcv_passage.prototype.integer = function(passage, accum, context) {
- if (context.v != null) {
- return this.v(passage, accum, context);
- }
- return this.c(passage, accum, context);
- };
- bcv_passage.prototype.sequence = function(passage, accum, context) {
- var obj, psg, sub_psg, _i, _j, _len, _len1, _ref, _ref1, _ref2, _ref3, _ref4, _ref5;
- passage.start_context = bcv_utils.shallow_clone(context);
- passage.passages = [];
- _ref = passage.value;
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- obj = _ref[_i];
- _ref1 = this.handle_array(obj, [], context), (_ref2 = _ref1[0], psg = _ref2[0]), context = _ref1[1];
- _ref3 = psg.passages;
- for (_j = 0, _len1 = _ref3.length; _j < _len1; _j++) {
- sub_psg = _ref3[_j];
- if ((_ref4 = sub_psg.type) == null) {
- sub_psg.type = psg.type;
- }
- if ((_ref5 = sub_psg.absolute_indices) == null) {
- sub_psg.absolute_indices = psg.absolute_indices;
- }
- if (psg.start_context.translations != null) {
- sub_psg.translations = psg.start_context.translations;
- }
- sub_psg.enclosed_absolute_indices = psg.type === "sequence_post_enclosed" ? psg.absolute_indices : [-1, -1];
- passage.passages.push(sub_psg);
- }
- }
- if (passage.absolute_indices == null) {
- if (passage.passages.length > 0 && passage.type === "sequence") {
- passage.absolute_indices = [passage.passages[0].absolute_indices[0], passage.passages[passage.passages.length - 1].absolute_indices[1]];
- } else {
- passage.absolute_indices = this.get_absolute_indices(passage.indices);
- }
- }
- accum.push(passage);
- return [accum, context];
- };
- bcv_passage.prototype.sequence_post_enclosed = function(passage, accum, context) {
- return this.sequence(passage, accum, context);
- };
- bcv_passage.prototype.v = function(passage, accum, context) {
- var c, no_c, v, valid, _ref, _ref1;
- v = passage.type === "integer" ? passage.value : this.pluck("integer", passage.value).value;
- passage.start_context = bcv_utils.shallow_clone(context);
- c = context.c != null ? context.c : 1;
- valid = this.validate_ref(passage.start_context.translations, {
- b: context.b,
- c: c,
- v: v
- });
- _ref = this.fix_start_zeroes(valid, 0, v), no_c = _ref[0], v = _ref[1];
- passage.passages = [
- {
- start: {
- b: context.b,
- c: c,
- v: v
- },
- end: {
- b: context.b,
- c: c,
- v: v
- },
- valid: valid
- }
- ];
- if (passage.start_context.translations != null) {
- passage.passages[0].translations = passage.start_context.translations;
- }
- if ((_ref1 = passage.absolute_indices) == null) {
- passage.absolute_indices = this.get_absolute_indices(passage.indices);
- }
- accum.push(passage);
- context.v = v;
- return [accum, context];
- };
- bcv_passage.prototype.range = function(passage, accum, context) {
- var end, end_obj, new_end, start, start_obj, temp_valid, temp_value, valid, _ref, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7;
- passage.start_context = bcv_utils.shallow_clone(context);
- _ref = passage.value, start = _ref[0], end = _ref[1];
- if (end.type === "v" && (start.type === "bc" || start.type === "c") && this.options.end_range_digits_strategy === "verse") {
- return this.range_change_integer_end(passage, accum);
- }
- _ref1 = this.handle_obj(start, [], context), (_ref2 = _ref1[0], start = _ref2[0]), context = _ref1[1];
- _ref3 = this.handle_obj(end, [], context), (_ref4 = _ref3[0], end = _ref4[0]), context = _ref3[1];
- passage.value = [start, end];
- passage.indices = [start.indices[0], end.indices[1]];
- delete passage.absolute_indices;
- start_obj = {
- b: start.passages[0].start.b,
- c: start.passages[0].start.c,
- v: start.passages[0].start.v,
- type: start.type
- };
- end_obj = {
- b: end.passages[0].end.b,
- c: end.passages[0].end.c,
- v: end.passages[0].end.v,
- type: end.type
- };
- if (end.passages[0].valid.messages.start_chapter_is_zero) {
- end_obj.c = 0;
- }
- if (end.passages[0].valid.messages.start_verse_is_zero) {
- end_obj.v = 0;
- }
- valid = this.validate_ref(passage.start_context.translations, start_obj, end_obj);
- if (valid.valid) {
- if (valid.messages.end_chapter_not_exist && this.options.end_range_digits_strategy === "verse" && !(start_obj.v != null) && (end.type === "integer" || end.type === "v")) {
- temp_value = end.type === "v" ? this.pluck("integer", end.value) : end.value;
- temp_valid = this.validate_ref(passage.start_context.translations, {
- b: start_obj.b,
- c: start_obj.c,
- v: temp_value
- });
- if (temp_valid.valid) {
- return this.range_change_integer_end(passage, accum);
- }
- }
- if (valid.messages.end_chapter_not_exist && this.options.end_range_digits_strategy === "verse" && (start_obj.v != null) && end.type === "cv") {
- temp_valid = this.validate_ref(passage.start_context.translations, {
- b: end_obj.b,
- c: start_obj.c,
- v: end_obj.c
- });
- if (temp_valid.valid) {
- temp_valid = this.validate_ref(passage.start_context.translations, {
- b: end_obj.b,
- c: start_obj.c,
- v: end_obj.v
- });
- }
- if (temp_valid.valid) {
- return this.range_change_cv_end(passage, accum);
- }
- }
- this.range_validate(valid, start_obj, end_obj, passage);
- } else {
- if ((valid.messages.end_chapter_before_start || valid.messages.end_verse_before_start) && (end.type === "integer" || end.type === "v") || (valid.messages.end_chapter_before_start && end.type === "cv")) {
- new_end = this.range_check_new_end(passage.start_context.translations, start_obj, end_obj, valid);
- if (new_end > 0) {
- return this.range_change_end(passage, accum, new_end);
- }
- }
- if (this.options.end_range_digits_strategy === "verse" && start_obj.v === void 0 && (end.type === "integer" || end.type === "v")) {
- temp_value = end.type === "v" ? this.pluck("integer", end.value) : end.value;
- temp_valid = this.validate_ref(passage.start_context.translations, {
- b: start_obj.b,
- c: start_obj.c,
- v: temp_value
- });
- if (temp_valid.valid) {
- return this.range_change_integer_end(passage, accum);
- }
- }
- _ref5 = [passage.type, "sequence"], passage.original_type = _ref5[0], passage.type = _ref5[1];
- _ref6 = [[start, end], [[start], [end]]], passage.original_value = _ref6[0], passage.value = _ref6[1];
- return this.handle_obj(passage, accum, passage.start_context);
- }
- if ((_ref7 = passage.absolute_indices) == null) {
- passage.absolute_indices = this.get_absolute_indices(passage.indices);
- }
- passage.passages = [
- {
- start: start_obj,
- end: end_obj,
- valid: valid
- }
- ];
- if (passage.start_context.translations != null) {
- passage.passages[0].translations = passage.start_context.translations;
- }
- accum.push(passage);
- return [accum, context];
- };
- bcv_passage.prototype.range_change_end = function(passage, accum, new_end) {
- var end, new_obj, start, _ref;
- _ref = passage.value, start = _ref[0], end = _ref[1];
- if (end.type === "integer") {
- end.original_value = end.value;
- end.value = new_end;
- } else if (end.type === "v") {
- new_obj = this.pluck("integer", end.value);
- new_obj.original_value = new_obj.value;
- new_obj.value = new_end;
- } else if (end.type === "cv") {
- new_obj = this.pluck("c", end.value);
- new_obj.original_value = new_obj.value;
- new_obj.value = new_end;
- }
- return this.handle_obj(passage, accum, passage.start_context);
- };
- bcv_passage.prototype.range_change_integer_end = function(passage, accum) {
- var end, start, _ref;
- _ref = passage.value, start = _ref[0], end = _ref[1];
- passage.original_type = passage.type;
- passage.original_value = [start, end];
- passage.type = start.type === "integer" ? "cv" : start.type + "v";
- if (start.type === "integer") {
- passage.value[0] = {
- type: "c",
- value: [start],
- indices: start.indices
- };
- }
- if (end.type === "integ…
Large files files are truncated, but you can click here to view the full file