diff --git a/Android/DynamicBible/assets/js/bible_ref_parsing.js b/Android/DynamicBible/assets/js/bible_ref_parsing.js index 30d4f5e2..086a34b3 100644 --- a/Android/DynamicBible/assets/js/bible_ref_parsing.js +++ b/Android/DynamicBible/assets/js/bible_ref_parsing.js @@ -3,581 +3,577 @@ // Jason@walljm.com // www.walljm.com // -String.prototype.trim = function() -{ +String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); -} -String.prototype.ltrim = function() -{ +}; + +String.prototype.ltrim = function() { return this.replace(/^\s+/, ""); -} -String.prototype.rtrim = function() -{ +}; + +String.prototype.rtrim = function() { return this.replace(/\s+$/, ""); +}; + +function parseReference(ref, r) { + var rest = parseBook(ref, r.start, r); + rest = parseFirstNum(rest, r.start, r); + rest = maybeParseSecondNum(rest, r.start, r); + rest = maybeParseRangeSep(rest, r, r); + rest = maybeParseBook(rest, r.end, r); + rest = maybeParseFirstNumOrVerse(rest, r.end, r); + rest = maybeParseSecondNum(rest, r.end, r); + return r; } -function Reference(ref) -{ +function parseBook(ref, data, errAcc) { ref = ref.toLowerCase().trim(); var bibleNames = new Object; var fbook = ref.substring(0, ref.search(/\w\s+\d/i)+1); + var rest = ref.slice(ref.search(/\w\s+\d/i)+1); - if (fbook.search(/\b(genesis|gen|ge|gn)\b/i) != -1) - { - this.book = 1; - this.bookname = "Genesis"; - this.longbookname = "Genesis"; - this.lastchapter = 50; + if (fbook.search(/\b(genesis|gen|ge|gn)\b/i) != -1) { + data.book = 1; + data.bookname = "Genesis"; + data.longbookname = "Genesis"; + data.lastchapter = 50; } - if (fbook.search(/\b(exodus|ex|exo|exod|exd)\b/i) != -1) - { - this.book = 2; - this.bookname = "Exodus"; - this.longbookname = "Exodus"; - this.lastchapter = 40; + if (fbook.search(/\b(exodus|ex|exo|exod|exd)\b/i) != -1) { + data.book = 2; + data.bookname = "Exodus"; + data.longbookname = "Exodus"; + data.lastchapter = 40; } - if (fbook.search(/\b(leviticus|lev|le|levi|lv)\b/i) != -1) - { - this.book = 3; - this.bookname = "Leviticus"; - this.longbookname = "Leviticus"; - this.lastchapter = 27; + if (fbook.search(/\b(leviticus|lev|le|levi|lv)\b/i) != -1) { + data.book = 3; + data.bookname = "Leviticus"; + data.longbookname = "Leviticus"; + data.lastchapter = 27; } - if (fbook.search(/\b(numbers|num|nu|numb|number)\b/i) != -1) - { - this.book = 4; - this.bookname = "Numbers"; - this.longbookname = "Book_of_Numbers"; - this.lastchapter = 36; + if (fbook.search(/\b(numbers|num|nu|numb|number)\b/i) != -1) { + data.book = 4; + data.bookname = "Numbers"; + data.longbookname = "Book_of_Numbers"; + data.lastchapter = 36; } if (fbook.search(/\b(deuteronomy|deut|de|dt|deu)\b/i) != -1) { - this.book = 5; - this.bookname = "Deuteronomy"; - this.longbookname = "Deuteronomy"; - this.lastchapter = 34; + data.book = 5; + data.bookname = "Deuteronomy"; + data.longbookname = "Deuteronomy"; + data.lastchapter = 34; } - if (fbook.search(/\b(joshua|josh|jos)\b/i) != -1) - { - this.book = 6; - this.bookname = "Joshua"; - this.longbookname = "Book_of_Joshua"; - this.lastchapter = 24; + if (fbook.search(/\b(joshua|josh|jos)\b/i) != -1) { + data.book = 6; + data.bookname = "Joshua"; + data.longbookname = "Book_of_Joshua"; + data.lastchapter = 24; } if (fbook.search(/\b(judges|jud|ju|jdg|judg)\b/i) != -1) { - this.book = 7; - this.bookname = "Judges"; - this.longbookname = "Book_of_Judges"; - this.lastchapter = 21; + data.book = 7; + data.bookname = "Judges"; + data.longbookname = "Book_of_Judges"; + data.lastchapter = 21; } - if (fbook.search(/\b(ruth|ru)\b/i) != -1) - { - this.book = 8; - this.bookname = "Ruth"; - this.longbookname = "Book_of_Ruth"; - this.lastchapter = 4; + if (fbook.search(/\b(ruth|ru)\b/i) != -1) { + data.book = 8; + data.bookname = "Ruth"; + data.longbookname = "Book_of_Ruth"; + data.lastchapter = 4; } if (fbook.search(/\b(1|i|1st|first)\s*(samuel|sa|sam|sml)\b/i) != -1) { - this.book = 9; - this.bookname = "1 Samuel"; - this.longbookname = "First_Samuel"; - this.lastchapter = 31; + data.book = 9; + data.bookname = "1 Samuel"; + data.longbookname = "First_Samuel"; + data.lastchapter = 31; } - if (fbook.search(/\b(2|ii|2nd|second|sec)\s*(samuel|sa|sam|sml)\b/i) != -1) - { - this.book = 10; - this.bookname = "2 Samuel"; - this.longbookname = "Second_Samuel"; - this.lastchapter = 24; + if (fbook.search(/\b(2|ii|2nd|second|sec)\s*(samuel|sa|sam|sml)\b/i) != -1) { + data.book = 10; + data.bookname = "2 Samuel"; + data.longbookname = "Second_Samuel"; + data.lastchapter = 24; } if (fbook.search(/\b(1|i|1st|first)\s*(kings|king|kgs|kn|k|ki)\b/i) != -1) { - this.book = 11; - this.bookname = "1 Kings"; - this.longbookname = "First_Kings"; - this.lastchapter = 22; + data.book = 11; + data.bookname = "1 Kings"; + data.longbookname = "First_Kings"; + data.lastchapter = 22; } - if (fbook.search(/\b(2|ii|2nd|second|sec)\s*(kings|king|kgs|kn|k|ki)\b/i) != -1) - { - this.book = 12; - this.bookname = "2 Kings"; - this.longbookname = "Second_Kings"; - this.lastchapter = 25; + if (fbook.search(/\b(2|ii|2nd|second|sec)\s*(kings|king|kgs|kn|k|ki)\b/i) != -1) { + data.book = 12; + data.bookname = "2 Kings"; + data.longbookname = "Second_Kings"; + data.lastchapter = 25; } if (fbook.search(/\b(1|i|1st|first)\s*(chronicles|chron|ch|chr)\b/i) != -1) { - this.book = 13; - this.bookname = "1 Chronicles"; - this.longbookname = "First_Chronicles"; - this.lastchapter = 29; + data.book = 13; + data.bookname = "1 Chronicles"; + data.longbookname = "First_Chronicles"; + data.lastchapter = 29; } - if (fbook.search(/\b(2|ii|2nd|second|sec)\s*(chronicles|chron|ch|chr)\b/i) != -1) - { - this.book = 14; - this.bookname = "2 Chronicles"; - this.longbookname = "Second_Chronicles"; - this.lastchapter = 36; + if (fbook.search(/\b(2|ii|2nd|second|sec)\s*(chronicles|chron|ch|chr)\b/i) != -1) { + data.book = 14; + data.bookname = "2 Chronicles"; + data.longbookname = "Second_Chronicles"; + data.lastchapter = 36; } if (fbook.search(/\b(ezra|ez|ezr)\b/i) != -1) { - this.book = 15; - this.bookname = "Ezra"; - this.longbookname = "Book_of_Ezra"; - this.lastchapter = 10; + data.book = 15; + data.bookname = "Ezra"; + data.longbookname = "Book_of_Ezra"; + data.lastchapter = 10; } - if (fbook.search(/\b(nehemiah|neh|ne|nehamiah)\b/i) != -1) - { - this.book = 16; - this.bookname = "Nehemiah"; - this.longbookname = "Book_of_Nehemiah"; - this.lastchapter = 13; + if (fbook.search(/\b(nehemiah|neh|ne|nehamiah)\b/i) != -1) { + data.book = 16; + data.bookname = "Nehemiah"; + data.longbookname = "Book_of_Nehemiah"; + data.lastchapter = 13; } - if (fbook.search(/\b(esther|est|es|esth)\b/i) != -1) - { - this.book = 17; - this.bookname = "Esther"; - this.longbookname = "Book_of_Esther"; - this.lastchapter = 10; + if (fbook.search(/\b(esther|est|es|esth)\b/i) != -1) { + data.book = 17; + data.bookname = "Esther"; + data.longbookname = "Book_of_Esther"; + data.lastchapter = 10; } - if (fbook.search(/\b(job|jo|jb)\b/i) != -1) - { - this.book = 18; - this.bookname = "Job"; - this.longbookname = "Book_of_Job"; - this.lastchapter = 42; + if (fbook.search(/\b(job|jo|jb)\b/i) != -1) { + data.book = 18; + data.bookname = "Job"; + data.longbookname = "Book_of_Job"; + data.lastchapter = 42; } - if (fbook.search(/\b(psalms|ps|psa|psalm|psm)\b/i) != -1) - { - this.book = 19; - this.bookname = "Psalm"; - this.longbookname = "Psalm"; - this.lastchapter = 150; + if (fbook.search(/\b(psalms|ps|psa|psalm|psm)\b/i) != -1) { + data.book = 19; + data.bookname = "Psalm"; + data.longbookname = "Psalm"; + data.lastchapter = 150; } - if (fbook.search(/\b(proverbs|prov|pr|pro|proverb|prv|prvbs)\b/i) != -1) - { - this.book = 20; - this.bookname = "Proverbs"; - this.longbookname = "Book_of_Proverbs"; - this.lastchapter = 31; + if (fbook.search(/\b(proverbs|prov|pr|pro|proverb|prv|prvbs)\b/i) != -1) { + data.book = 20; + data.bookname = "Proverbs"; + data.longbookname = "Book_of_Proverbs"; + data.lastchapter = 31; } - if (fbook.search(/\b(ecclesiastes|eccl|ecc|eccles|ec|ecl|ecclesiaste)\b/i) != -1) - { - this.book = 21; - this.bookname = "Ecclesiastes"; - this.longbookname = "Ecclesiastes"; - this.lastchapter = 12; + if (fbook.search(/\b(ecclesiastes|eccl|ecc|eccles|ec|ecl|ecclesiaste)\b/i) != -1) { + data.book = 21; + data.bookname = "Ecclesiastes"; + data.longbookname = "Ecclesiastes"; + data.lastchapter = 12; } - if (fbook.search(/\b(song\sof\ssolomon|song\sof\ssongs|sos|ss|son|so|song|songs)\b/i) != -1) - { - this.book = 22; - this.bookname = "Song of Solomon"; - this.longbookname = "Song_of_Solomon"; - this.lastchapter = 8; + if (fbook.search(/\b(song\sof\ssolomon|song\sof\ssongs|sos|ss|son|so|song|songs)\b/i) != -1) { + data.book = 22; + data.bookname = "Song of Solomon"; + data.longbookname = "Song_of_Solomon"; + data.lastchapter = 8; } - if (fbook.search(/\b(isaiah|is|is|isah|isai|ia)\b/i) != -1) - { - this.book = 23; - this.bookname = "Isaiah"; - this.longbookname = "Book_of_Isaiah"; - this.lastchapter = 66; + if (fbook.search(/\b(isaiah|is|is|isah|isai|ia)\b/i) != -1) { + data.book = 23; + data.bookname = "Isaiah"; + data.longbookname = "Book_of_Isaiah"; + data.lastchapter = 66; } - if (fbook.search(/\b(jerimiah|jeremiah|jer|je|jere)\b/i) != -1) - { - this.book = 24; - this.bookname = "Jeremiah"; - this.longbookname = "Book_of_Jeremiah"; - this.lastchapter = 52; + if (fbook.search(/\b(jerimiah|jeremiah|jer|je|jere)\b/i) != -1) { + data.book = 24; + data.bookname = "Jeremiah"; + data.longbookname = "Book_of_Jeremiah"; + data.lastchapter = 52; } - if (fbook.search(/\b(lamentations|lam|la|lamentation)\b/i) != -1) - { - this.book = 25; - this.bookname = "Lamentations"; - this.longbookname = "Book_of_Lamentations"; - this.lastchapter = 5; + if (fbook.search(/\b(lamentations|lam|la|lamentation)\b/i) != -1) { + data.book = 25; + data.bookname = "Lamentations"; + data.longbookname = "Book_of_Lamentations"; + data.lastchapter = 5; } - if (fbook.search(/\b(ezekiel|eze|ez|ezk|ezek)\b/i) != -1) - { - this.book = 26; - this.bookname = "Ezekiel"; - this.longbookname = "Book_of_Ezekiel"; - this.lastchapter = 48; + if (fbook.search(/\b(ezekiel|eze|ez|ezk|ezek)\b/i) != -1) { + data.book = 26; + data.bookname = "Ezekiel"; + data.longbookname = "Book_of_Ezekiel"; + data.lastchapter = 48; } - if (fbook.search(/\b(daniel|dan|dn|dl|da)\b/i) != -1) - { - this.book = 27; - this.bookname = "Daniel"; - this.longbookname = "Book_of_Daniel"; - this.lastchapter = 12; + if (fbook.search(/\b(daniel|dan|dn|dl|da)\b/i) != -1) { + data.book = 27; + data.bookname = "Daniel"; + data.longbookname = "Book_of_Daniel"; + data.lastchapter = 12; } - if (fbook.search(/\b(hosea|hos|ho)\b/i) != -1) - { - this.book = 28; - this.bookname = "Hosea"; - this.longbookname = "Book_of_Hosea"; - this.lastchapter = 14; + if (fbook.search(/\b(hosea|hos|ho)\b/i) != -1) { + data.book = 28; + data.bookname = "Hosea"; + data.longbookname = "Book_of_Hosea"; + data.lastchapter = 14; } - if (fbook.search(/\b(joel|joe|jl)\b/i) != -1) - { - this.book = 29; - this.bookname = "Joel"; - this.longbookname = "Book_of_Joel"; - this.lastchapter = 3; + if (fbook.search(/\b(joel|joe|jl)\b/i) != -1) { + data.book = 29; + data.bookname = "Joel"; + data.longbookname = "Book_of_Joel"; + data.lastchapter = 3; } - if (fbook.search(/\b(amos|am|amo)\b/i) != -1) - { - this.book = 30; - this.bookname = "Amos"; - this.longbookname = "Book_of_Amos"; - this.lastchapter = 9; + if (fbook.search(/\b(amos|am|amo)\b/i) != -1) { + data.book = 30; + data.bookname = "Amos"; + data.longbookname = "Book_of_Amos"; + data.lastchapter = 9; } - if (fbook.search(/\b(obadiah|oba|ob|obad)\b/i) != -1) - { - this.book = 31; - this.bookname = "Obadiah"; - this.longbookname = "Book_of_Obadiah"; - this.lastchapter = 1; + if (fbook.search(/\b(obadiah|oba|ob|obad)\b/i) != -1) { + data.book = 31; + data.bookname = "Obadiah"; + data.longbookname = "Book_of_Obadiah"; + data.lastchapter = 1; } - if (fbook.search(/\b(jonah|jnh|jon)\b/i) != -1) - { - this.book = 32; - this.bookname = "Jonah"; - this.longbookname = "Book_of_Jonah"; - this.lastchapter = 4; + if (fbook.search(/\b(jonah|jnh|jon)\b/i) != -1) { + data.book = 32; + data.bookname = "Jonah"; + data.longbookname = "Book_of_Jonah"; + data.lastchapter = 4; } - if (fbook.search(/\b(micah|mic|mi)\b/i) != -1) - { - this.book = 33; - this.bookname = "Micah"; - this.longbookname = "Book_of_Micah"; - this.lastchapter = 7; + if (fbook.search(/\b(micah|mic|mi)\b/i) != -1) { + data.book = 33; + data.bookname = "Micah"; + data.longbookname = "Book_of_Micah"; + data.lastchapter = 7; } - if (fbook.search(/\b(nahum|nah|na)\b/i) != -1) - { - this.book = 34; - this.bookname = "Nahum"; - this.longbookname = "Book_of_Nahum"; - this.lastchapter = 3; + if (fbook.search(/\b(nahum|nah|na)\b/i) != -1) { + data.book = 34; + data.bookname = "Nahum"; + data.longbookname = "Book_of_Nahum"; + data.lastchapter = 3; } - if (fbook.search(/\b(habakkuk|hab|ha|habakuk)\b/i) != -1) - { - this.book = 35; - this.bookname = "Habakkuk"; - this.longbookname = "Book_of_Habakkuk"; - this.lastchapter = 3; + if (fbook.search(/\b(habakkuk|hab|ha|habakuk)\b/i) != -1) { + data.book = 35; + data.bookname = "Habakkuk"; + data.longbookname = "Book_of_Habakkuk"; + data.lastchapter = 3; } - if (fbook.search(/\b(zephaniah|zeph|zep)\b/i) != -1) - { - this.book = 36; - this.bookname = "Zephaniah"; - this.longbookname = "Book_of_Zephaniah"; - this.lastchapter = 3; + if (fbook.search(/\b(zephaniah|zeph|zep)\b/i) != -1) { + data.book = 36; + data.bookname = "Zephaniah"; + data.longbookname = "Book_of_Zephaniah"; + data.lastchapter = 3; } - if (fbook.search(/\b(haggia|hag|hg|haggai)\b/i) != -1) - { - this.book = 37; - this.bookname = "Haggai"; - this.longbookname = "Book_of_Haggai"; - this.lastchapter = 2; + if (fbook.search(/\b(haggia|hag|hg|haggai)\b/i) != -1) { + data.book = 37; + data.bookname = "Haggai"; + data.longbookname = "Book_of_Haggai"; + data.lastchapter = 2; } - if (fbook.search(/\b(zechariah|zech|zch|zec)\b/i) != -1) - { - this.book = 38; - this.bookname = "Zechariah"; - this.longbookname = "Book_of_Zechariah"; - this.lastchapter = 14; + if (fbook.search(/\b(zechariah|zech|zch|zec)\b/i) != -1) { + data.book = 38; + data.bookname = "Zechariah"; + data.longbookname = "Book_of_Zechariah"; + data.lastchapter = 14; } - if (fbook.search(/\b(malachi|mal)\b/i) != -1) - { - this.book = 39; - this.bookname = "Malachi"; - this.longbookname = "Book_of_Malachi"; - this.lastchapter = 4; + if (fbook.search(/\b(malachi|mal)\b/i) != -1) { + data.book = 39; + data.bookname = "Malachi"; + data.longbookname = "Book_of_Malachi"; + data.lastchapter = 4; } - if (fbook.search(/\b(matthew|mt|matt|mat)\b/i) != -1) - { - this.book = 40; - this.bookname = "Matthew"; - this.longbookname = "Gospel_of_Matthew"; - this.lastchapter = 28; + if (fbook.search(/\b(matthew|mt|matt|mat)\b/i) != -1) { + data.book = 40; + data.bookname = "Matthew"; + data.longbookname = "Gospel_of_Matthew"; + data.lastchapter = 28; } - if (fbook.search(/\b(mark|mrk|mk|mr)\b/i) != -1) - { - this.book = 41; - this.bookname = "Mark"; - this.longbookname = "Gospel_of_Mark"; - this.lastchapter = 16; + if (fbook.search(/\b(mark|mrk|mk|mr)\b/i) != -1) { + data.book = 41; + data.bookname = "Mark"; + data.longbookname = "Gospel_of_Mark"; + data.lastchapter = 16; } - if (fbook.search(/\b(luke|lu|lke|luk|lk)\b/i) != -1) - { - this.book = 42; - this.bookname = "Luke"; - this.longbookname = "Gospel_of_Luke"; - this.lastchapter = 24; + if (fbook.search(/\b(luke|lu|lke|luk|lk)\b/i) != -1) { + data.book = 42; + data.bookname = "Luke"; + data.longbookname = "Gospel_of_Luke"; + data.lastchapter = 24; } - if (fbook.search(/\b(john|jn|jhn)\b/i) != -1) - { - this.book = 43; - this.bookname = "John"; - this.longbookname = "Gospel_of_John"; - this.lastchapter = 21; + if (fbook.search(/\b(john|jn|jhn)\b/i) != -1) { + data.book = 43; + data.bookname = "John"; + data.longbookname = "Gospel_of_John"; + data.lastchapter = 21; } - if (fbook.search(/\b(acts|ac|act)\b/i) != -1) - { - this.book = 44; - this.bookname = "Acts"; - this.longbookname = "Acts_of_the_Apostles"; - this.lastchapter = 28; + if (fbook.search(/\b(acts|ac|act)\b/i) != -1) { + data.book = 44; + data.bookname = "Acts"; + data.longbookname = "Acts_of_the_Apostles"; + data.lastchapter = 28; } - if (fbook.search(/\b(romans|rom|ro|rm|roman)\b/i) != -1) - { - this.book = 45; - this.bookname = "Romans"; - this.longbookname = "Epistle_to_the_Romans"; - this.lastchapter = 16; + if (fbook.search(/\b(romans|rom|ro|rm|roman)\b/i) != -1) { + data.book = 45; + data.bookname = "Romans"; + data.longbookname = "Epistle_to_the_Romans"; + data.lastchapter = 16; } - if (fbook.search(/\b(1|i|1st|first)\s*(corinthian|cor|corinthians|corinth|corin|corth|corint)\b/i) != -1) - { - this.book = 46; - this.bookname = "1 Corinthians"; - this.longbookname = "First_Epistle_to_the_Corinthians"; - this.lastchapter = 16; + if (fbook.search(/\b(1|i|1st|first)\s*(corinthian|cor|corinthians|corinth|corin|corth|corint)\b/i) != -1) { + data.book = 46; + data.bookname = "1 Corinthians"; + data.longbookname = "First_Epistle_to_the_Corinthians"; + data.lastchapter = 16; } - if (fbook.search(/\b(2|ii|2nd|second|sec)\s*(corinthian|cor|corinthians|corinth|corin|corth|corint)\b/i) != -1) - { - this.book = 47; - this.bookname = "2 Corinthians"; - this.longbookname = "Second_Epistle_to_the_Corinthians"; - this.lastchapter = 13; + if (fbook.search(/\b(2|ii|2nd|second|sec)\s*(corinthian|cor|corinthians|corinth|corin|corth|corint)\b/i) != -1) { + data.book = 47; + data.bookname = "2 Corinthians"; + data.longbookname = "Second_Epistle_to_the_Corinthians"; + data.lastchapter = 13; } - if (fbook.search(/\b(galatians|galatian|galations|gal|ga|gala|galation|galat)\b/i) != -1) - { - this.book = 48; - this.bookname = "Galatians"; - this.longbookname = "Epistle_to_the_Galatians"; - this.lastchapter = 6; + if (fbook.search(/\b(galatians|galatian|galations|gal|ga|gala|galation|galat)\b/i) != -1) { + data.book = 48; + data.bookname = "Galatians"; + data.longbookname = "Epistle_to_the_Galatians"; + data.lastchapter = 6; } - if (fbook.search(/\b(ephesians|eph|ep|ephes|ephe|ephs)\b/i) != -1) - { - this.book = 49; - this.bookname = "Ephesians"; - this.longbookname = "Epistle_to_the_Ephesians"; - this.lastchapter = 6; + if (fbook.search(/\b(ephesians|eph|ep|ephes|ephe|ephs)\b/i) != -1) { + data.book = 49; + data.bookname = "Ephesians"; + data.longbookname = "Epistle_to_the_Ephesians"; + data.lastchapter = 6; } - if (fbook.search(/\b(philippians|phi|phil|ph|philip)\b/i) != -1) - { - this.book = 50; - this.bookname = "Philippians"; - this.longbookname = "Epistle_to_the_Philippians"; - this.lastchapter = 4; + if (fbook.search(/\b(philippians|phi|phil|ph|philip)\b/i) != -1) { + data.book = 50; + data.bookname = "Philippians"; + data.longbookname = "Epistle_to_the_Philippians"; + data.lastchapter = 4; } - if (fbook.search(/\b(colossians|col|co|colossian|colos|coloss)\b/i) != -1) - { - this.book = 51; - this.bookname = "Colossians"; - this.longbookname = "Epistle_to_the_Colossians"; - this.lastchapter = 4; + if (fbook.search(/\b(colossians|col|co|colossian|colos|coloss)\b/i) != -1) { + data.book = 51; + data.bookname = "Colossians"; + data.longbookname = "Epistle_to_the_Colossians"; + data.lastchapter = 4; } - if (fbook.search(/\b(1|i|1st|first)\s*(thessalonians|the|thessa|thessalonian|thes|thess|th)\b/i) != -1) - { - this.book = 52; - this.bookname = "1 Thessalonians"; - this.longbookname = "First_Epistle_to_the_Thessalonians"; - this.lastchapter = 5; + if (fbook.search(/\b(1|i|1st|first)\s*(thessalonians|the|thessa|thessalonian|thes|thess|th)\b/i) != -1) { + data.book = 52; + data.bookname = "1 Thessalonians"; + data.longbookname = "First_Epistle_to_the_Thessalonians"; + data.lastchapter = 5; } - if (fbook.search(/\b(2|ii|2nd|second|sec)\s*(thessalonians|the|thessa|thessalonian|thes|thess|th)\b/i) != -1) - { - this.book = 53; - this.bookname = "2 Thessalonians"; - this.longbookname = "Second_Epistle_to_the_Thessalonians"; - this.lastchapter = 3; + if (fbook.search(/\b(2|ii|2nd|second|sec)\s*(thessalonians|the|thessa|thessalonian|thes|thess|th)\b/i) != -1) { + data.book = 53; + data.bookname = "2 Thessalonians"; + data.longbookname = "Second_Epistle_to_the_Thessalonians"; + data.lastchapter = 3; } - if (fbook.search(/\b(1|i|1st|first)\s*(timothy|tim|ti|timoth|tm)\b/i) != -1) - { - this.book = 54; - this.bookname = "1 Timothy"; - this.longbookname = "First_Epistle_to_Timothy"; - this.lastchapter = 6; + if (fbook.search(/\b(1|i|1st|first)\s*(timothy|tim|ti|timoth|tm)\b/i) != -1) { + data.book = 54; + data.bookname = "1 Timothy"; + data.longbookname = "First_Epistle_to_Timothy"; + data.lastchapter = 6; } - if (fbook.search(/\b(2|ii|2nd|second|sec)\s*(timothy|tim|timoth|tm)\b/i) != -1) - { - this.book = 55; - this.bookname = "2 Timothy"; - this.longbookname = "Second_Epistle_to_Timothy"; - this.lastchapter = 4; + if (fbook.search(/\b(2|ii|2nd|second|sec)\s*(timothy|tim|timoth|tm)\b/i) != -1) { + data.book = 55; + data.bookname = "2 Timothy"; + data.longbookname = "Second_Epistle_to_Timothy"; + data.lastchapter = 4; } - if (fbook.search(/\b(titus|tit)\b/i) != -1) - { - this.book = 56; - this.bookname = "Titus"; - this.longbookname = "Epistle_to_Titus"; - this.lastchapter = 3; + if (fbook.search(/\b(titus|tit)\b/i) != -1) { + data.book = 56; + data.bookname = "Titus"; + data.longbookname = "Epistle_to_Titus"; + data.lastchapter = 3; } - if (fbook.search(/\b(philemon|phlmn|phl|phm|phile|philem)\b/i) != -1) - { - this.book = 57; - this.bookname = "Philemon"; - this.longbookname = "Epistle_to_Philemon"; - this.lastchapter = 1; + if (fbook.search(/\b(philemon|phlmn|phl|phm|phile|philem)\b/i) != -1) { + data.book = 57; + data.bookname = "Philemon"; + data.longbookname = "Epistle_to_Philemon"; + data.lastchapter = 1; } - if (fbook.search(/\b(hebrews|heb|he|hebrew)\b/i) != -1) - { - this.book = 58; - this.bookname = "Hebrews"; - this.longbookname = "Epistle_to_the_Hebrews"; - this.lastchapter = 13; + if (fbook.search(/\b(hebrews|heb|he|hebrew)\b/i) != -1) { + data.book = 58; + data.bookname = "Hebrews"; + data.longbookname = "Epistle_to_the_Hebrews"; + data.lastchapter = 13; } - if (fbook.search(/\b(james|jam|ja|jas|jms|jame|jm)\b/i) != -1) - { - this.book = 59; - this.bookname = "James"; - this.longbookname = "Epistle_of_James"; - this.lastchapter = 5; + if (fbook.search(/\b(james|jam|ja|jas|jms|jame|jm)\b/i) != -1) { + data.book = 59; + data.bookname = "James"; + data.longbookname = "Epistle_of_James"; + data.lastchapter = 5; } - if (fbook.search(/\b(1|i|1st|first)\s*(peter|pe|pet|pete|pt|p)\b/i) != -1) - { - this.book = 60; - this.bookname = "1 Peter"; - this.longbookname = "First_Epistle_of_Peter"; - this.lastchapter = 5; + if (fbook.search(/\b(1|i|1st|first)\s*(peter|pe|pet|pete|pt|p)\b/i) != -1) { + data.book = 60; + data.bookname = "1 Peter"; + data.longbookname = "First_Epistle_of_Peter"; + data.lastchapter = 5; } - if (fbook.search(/\b(2|ii|2nd|second|sec)\s*(peter|pe|pet|pete|pt|p)\b/i) != -1) - { - this.book = 61; - this.bookname = "2 Peter"; - this.longbookname = "Second_Epistle_of_Peter"; - this.lastchapter = 3; + if (fbook.search(/\b(2|ii|2nd|second|sec)\s*(peter|pe|pet|pete|pt|p)\b/i) != -1) { + data.book = 61; + data.bookname = "2 Peter"; + data.longbookname = "Second_Epistle_of_Peter"; + data.lastchapter = 3; } - if (fbook.search(/\b(1|i|1st|first)\s*(john|jn|jo)\b/i) != -1) - { - this.book = 62; - this.bookname = "1 John"; - this.longbookname = "First_Epistle_of_John"; - this.lastchapter = 5; + if (fbook.search(/\b(1|i|1st|first)\s*(john|jn|jo)\b/i) != -1) { + data.book = 62; + data.bookname = "1 John"; + data.longbookname = "First_Epistle_of_John"; + data.lastchapter = 5; } - if (fbook.search(/\b(2|ii|2nd|second|sec)\s*(john|jn|jo)\b/i) != -1) - { - this.book = 63; - this.bookname = "2 John"; - this.longbookname = "Second_Epistle_of_John"; - this.lastchapter = 1; + if (fbook.search(/\b(2|ii|2nd|second|sec)\s*(john|jn|jo)\b/i) != -1) { + data.book = 63; + data.bookname = "2 John"; + data.longbookname = "Second_Epistle_of_John"; + data.lastchapter = 1; } - if (fbook.search(/\b(3|iii|3rd|third)\s*(john|jn|jo)\b/i) != -1) - { - this.book = 64; - this.bookname = "3 John"; - this.longbookname = "Third_Epistle_of_John"; - this.lastchapter = 1; + if (fbook.search(/\b(3|iii|3rd|third)\s*(john|jn|jo)\b/i) != -1) { + data.book = 64; + data.bookname = "3 John"; + data.longbookname = "Third_Epistle_of_John"; + data.lastchapter = 1; } - if (fbook.search(/\b(jude|jud|ju)\b/i) != -1) - { - this.book = 65; - this.bookname = "Jude"; - this.longbookname = "Epistle_of_Jude"; - this.lastchapter = 1; + if (fbook.search(/\b(jude|jud|ju)\b/i) != -1) { + data.book = 65; + data.bookname = "Jude"; + data.longbookname = "Epistle_of_Jude"; + data.lastchapter = 1; } - if (fbook.search(/\b(revelation|rev|re|revelations|rv)\b/i) != -1) - { - this.book = 66; - this.bookname = "Revelation"; - this.longbookname = "Book_of_Revelations"; - this.lastchapter = 22; + if (fbook.search(/\b(revelation|rev|re|revelations|rv)\b/i) != -1) { + data.book = 66; + data.bookname = "Revelation"; + data.longbookname = "Book_of_Revelations"; + data.lastchapter = 22; } - var rexp = new RegExp(fbook, "gi"); - var secondbook = ref.match(rexp); - // first, remove the book from the ref. - ref = ref.replace(rexp, "").trim(); + return rest; +} - // ok. we're going to deal with any of the following types of refernces: - /* - 1:1 - 2:3 // chapter/verse range. - 1:1 -2 // chapter/verse range. this one is special, because its indistinguisable from book 1:1 - book 2 when you remove the book, which i do. i'm ok with this. - 1-2 // chapter range - 1 // chapter - 2:1 // verse - 2:1 - 2:3 // verse range - */ - // first, see if we're dealing with a range. - if (ref.match(/\-/) != null) - { - var sref = ref.split("-")[0].trim(); - var eref = ref.split("-")[1].trim(); - - // if a colon is found, then its a chapter verse combo. - if (sref.match(/\:/) != null) - { - this.startchapter = sref.split(":")[0].trim(); - this.startverse = sref.split(":")[1].trim(); - } - else - { - // no : was found, must be whole chapter. - this.startchapter = sref.trim(); - this.startverse = 1; - } - - // if a colon is found, then its a chapter verse combo. - if (eref.match(/\:/) != null) - { - this.endchapter = eref.split(":")[0].trim(); - this.endverse = eref.split(":")[1].trim(); - } - else - { - // no : was found, must be whole chapter or a verse range. test secondbook array to find out. - if (secondbook.length == 2 || sref.match(/\:/) == null) - { - // chapter range. - this.endchapter = eref.trim(); - this.endverse = "*"; +function parseFirstNum(ref, r, errAcc) { + ref = ref.ltrim(); + var ch = ""; + var found = false; + for (var i = 0; i <= ref.length; i++) { + var c = ref.charAt(i); + // Grab characters until we hit a non digit. + if ("0".charAt(0) <= c && c <= "9".charAt(0)) { + found = true; + r.first = r.first.concat(c); + } else { + // if the chapter is longer than 3 digits it's an error + if (r.first.length > 3) { + errAcc.err = "Chapter too long\"" + r.first + "\"."; + return ""; + } else if (!found) { + errAcc.err = "No chapter found" + ref; } - else - { - // verse range. - this.endchapter = this.startchapter; - this.endverse = eref.trim(); + return ref.slice(i); + } + } + return ""; +} + +function parseSecondNum(ref, r, errAcc, skipColon) { + ref = ref.toLowerCase().ltrim(); + var vs = ""; + if (!skipColon) { + if (ref[0] != ":") { + return ref; + } + ref = ref.slice(1); + } + ref = ref.toLowerCase().ltrim(); + if (ref[0] == '*') { + r.second = '*'; + return ref.slice(1); + } + for (var i = 0; i <= ref.length; i++) { + var c = ref.charAt(i); + if ("0".charAt(0) <= c && c <= "9".charAt(0)) { + r.second = r.second.concat(c); + } else { + if (r.second.length > 3) { + errAcc.err = "Verse too long \"" + r.second + "\"."; + return ""; } + return ref.slice(i); } } - else - { - // if no - was found, then the ref can only be a single chapter or single verse. - if (ref.match(/\:/) != null) - { - this.startchapter = ref.split(":")[0].trim(); - this.endchapter = ref.split(":")[0].trim(); - this.startverse = ref.split(":")[1].trim(); ; - this.endverse = ref.split(":")[1].trim(); ; - } - else - { - // no : was found, must be whole chapter. - this.startchapter = ref.trim(); - this.endchapter = ref.trim(); - this.startverse = 1; - this.endverse = "*"; - } - - } - - // make sure you don't return an invalid chapter. - if (this.endchapter > this.lastchapter) - { - this.endchapter = this.lastchapter; + return ""; +} + +function maybeDo(ref, r, errAcc, f) { + ref = ref.toLowerCase().ltrim(); + if (ref != "") { + return f(ref, r, errAcc); } - if (this.startchapter > this.endchapter) - { - this.startchapter = this.endchapter; + return ""; +} + +function maybeParseBook(ref, r, errAcc) { + return maybeDo(ref, r, errAcc, parseBook); +} + +function maybeParseSecondNum(ref, r, errAcc, skipColon) { + return maybeDo(ref, r, errAcc, function (ref, r, errAcc) { + return parseSecondNum(ref, r, errAcc, skipColon); + }); +} + +function maybeParseFirstNumOrVerse(ref, r, errAcc) { + return maybeDo(ref, r, errAcc, function(ref, r, errAcc) { + if (typeof r.book != -1) { + if (ref.search(/:/) != -1) { + ref = parseFirstNum(ref, r, errAcc); + } + return parseSecondNum(ref, r, errAcc, true); + } + return ""; + }); +} + +function maybeParseRangeSep(ref, r, errAcc) { + return maybeDo(ref.ltrim(), r, errAcc, function(ref, r, errAcc) { + if (ref[0] == "-") { + return ref.slice(1).ltrim(); + } + return ""; + }); +} +function Reference(ref) +{ + r = { + start: { + first: '', + second: '' + }, + end: { + first: '', + second: '' + } + }; + // First collect all our tokens so to speak + parseReference(ref.toLowerCase().trim(), r); + this.book = r.start.book; + this.bookname = r.start.bookname; + this.longbookname = r.start.longbookname; + this.startchapter = r.start.first; + this.endchapter = r.start.first; + this.startverse = '1'; + // Then start our statemachine. + // Determine start verse + if (r.start.second != '') { + this.startverse = r.start.second; + this.endverse = '*'; } - if (this.startchapter == this.endchapter && this.startverse > this.endverse && this.endverse != "*") - { + // Determine possible end verse + if (r.end.second != '') { + this.endverse = r.end.second; + } + // Enforce some general data cleanliness + if (r.end.second != '*' && Number(r.end.second) < Number(r.start.second)) { this.endverse = this.startverse; } + // Determine end chapter and verse + if (r.end.first == '') { + if (r.start.second == '' || typeof r.end.book != 'undefined') { + this.endchapter = r.end.second || '1'; + this.endverse = '*'; + } + } else { + this.endchapter = r.end.first; + } } -function bookName(booknum) -{ +function bookName(booknum) { var book = new Array(); book[0] = ""; book[1] = "Genesis"; @@ -648,4 +644,4 @@ function bookName(booknum) book[66] = "Revelation"; return book[booknum]; -} \ No newline at end of file +}