Skip to content

Commit

Permalink
Ugly but works
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelMon committed Jul 4, 2024
1 parent 4c7463f commit 680eef1
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 111 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "estienne"
version = "0.5.0-alpha"
version = "0.6.0-alpha"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -11,10 +11,10 @@ name = "est"
[dependencies]
thiserror ="1.0"
regex = "1"
lazy_static = "1.4.0"
lazy_static = "1.5.0"

[dev-dependencies]
pretty_assertions = "1.3.0"
pretty_assertions = "1.4.0"
#criterion = { version= "0.4", features = ["html_reports"] }

#[[bench]]
Expand Down
34 changes: 29 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,33 @@ pub fn url<S: Into<String> + Clone>(site: &Site, text: S) -> String {
}

#[cfg(test)]
mod test {
mod lib_test {
use crate::parsers::surround::Script;

use super::*;
use pretty_assertions::assert_eq;

#[test]
fn t_revelations_url() {
let text: &str = "A popular scriptures is Rev 12:12. It is quoted often.";
let expect: String = "A popular scriptures is [Rev 12:12](https://www.jw.org/en/library/bible/study-bible/books/revelation/12/#v66012012). It is quoted often.".to_string();
let got: String = Script::new(text).url( &Site::JwOrg).get_text();
fn t_single_url() {
let input: &str = "A popular scriptures is Re 12:12. It is quoted often.";
let expect: String = "A popular scriptures is [Re 12:12](https://www.jw.org/en/library/bible/study-bible/books/revelation/12/#v66012012). It is quoted often.".to_string();
let got: String = Script::new(input).url( &Site::JwOrg).get_text();
assert_eq!(got, expect)
}

#[test]
fn t_single_ranged_url() {
let input: &str = "A popular scriptures is Job 36:26-28. It is quoted often.";
let expect: String = "A popular scriptures is [Job 36:26-28](https://www.jw.org/en/library/bible/study-bible/books/job/36/#v18036026-v18036028). It is quoted often.".to_string();
let got: String = Script::new(input).url( &Site::JwOrg).get_text();
assert_eq!(got, expect)
}

#[test]
fn t_multipal_url() {
let input: &str = "Three well-known Bible scriptures are Proverbs 3:5, John 3:16, and Romans 8:28";
let expect: String = "Three well-known Bible scriptures are [Proverbs 3:5](https://www.jw.org/en/library/bible/study-bible/books/proverbs/3/#v20003005), [John 3:16](https://www.jw.org/en/library/bible/study-bible/books/john/3/#v43003016), and [Romans 8:28](https://www.jw.org/en/library/bible/study-bible/books/romans/8/#v45008028)".to_string();
let got: String = Script::new(input).url( &Site::JwOrg).get_text();
assert_eq!(got, expect)
}

Expand Down Expand Up @@ -98,6 +114,14 @@ mod test {
assert_eq!(got, expect);
}

#[test]
fn t_add_element_prefix_ranged_multi() {
let input:&str = "Other popular scriptures include John 3:16, 17, Matthew 24:14-16, and Psalm 83:18, 17-20, they are quoted often.";
let expect:&str = "Other popular scriptures include **John 3:16, 17]], **Matthew 24:14-16]], and **Psalm 83:18, 17-20]], they are quoted often.";
let got = surround(input, "**", "]]");
assert_eq!(got, expect);
}

#[test]
fn t_add_element_prefix_multi_no_scriptures() {
let input: &str = "There are no scriptures in this line.";
Expand Down
160 changes: 91 additions & 69 deletions src/locales/en_us.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,74 +74,76 @@ pub enum Book {
impl TryFrom<&str> for Book {
type Error = String;

// Naming as it appeares on the NWT.
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value.to_lowercase().as_str() {
"genesis" | "gn" => Ok(Book::Genesis),
"exodus" => Ok(Book::Exodus),
"leviticus" => Ok(Book::Leviticus),
"numbers" => Ok(Book::Numbers),
"deuteronomy" => Ok(Book::Deuteronomy),
"joshua" => Ok(Book::Joshua),
"judges" => Ok(Book::Judges),
"ruth" => Ok(Book::Ruth),
"1 samuel" => Ok(Book::FirstSamuel),
"2 samuel" => Ok(Book::SecondSamuel),
"1 kings" => Ok(Book::FirstKings),
"2 kings" => Ok(Book::SecondKings),
"1 chronicles" => Ok(Book::FirstChronicles),
"2 chronicles" => Ok(Book::SecondChronicles),
"ezra" => Ok(Book::Ezra),
"nehemiah" => Ok(Book::Nehemiah),
"esther" => Ok(Book::Esther),
"exodus" | "ex" => Ok(Book::Exodus),
"leviticus" | "le" => Ok(Book::Leviticus),
"numbers" | "nu" => Ok(Book::Numbers),
"deuteronomy" | "de" => Ok(Book::Deuteronomy),
"joshua" | "jos" => Ok(Book::Joshua),
"judges" | "jg" => Ok(Book::Judges),
"ruth" | "ru" => Ok(Book::Ruth),
"1 samuel" | "1sa" | "1 sa" => Ok(Book::FirstSamuel),
"2 samuel" | "2sa" | "2 sa" => Ok(Book::SecondSamuel),
"1 kings" | "1ki" | "1 ki" => Ok(Book::FirstKings),
"2 kings" | "2ki" | "2 ki" => Ok(Book::SecondKings),
"1 chronicles" | "1ch" | "1 ch" => Ok(Book::FirstChronicles),
"2 chronicles" | "2ch" | "2 ch" => Ok(Book::SecondChronicles),
"ezra" | "ezr" => Ok(Book::Ezra),
"nehemiah" | "ne" => Ok(Book::Nehemiah),
"esther" | "es" => Ok(Book::Esther),
"job" => Ok(Book::Job),
"psalms" => Ok(Book::Psalms),
"proverbs" => Ok(Book::Proverbs),
"ecclesiastes" => Ok(Book::Ecclesiastes),
"song of solomon" => Ok(Book::SongOfSolomon),
"isaiah" => Ok(Book::Isaiah),
"jeremiah" => Ok(Book::Jeremiah),
"lamentations" => Ok(Book::Lamentations),
"ezekiel" => Ok(Book::Ezekiel),
"daniel" => Ok(Book::Daniel),
"hosea" => Ok(Book::Hosea),
"joel" => Ok(Book::Joel),
"amos" => Ok(Book::Amos),
"obadiah" => Ok(Book::Obadiah),
"jonah" => Ok(Book::Jonah),
"micah" => Ok(Book::Micah),
"nahum" => Ok(Book::Nahum),
"habakkuk" => Ok(Book::Habakkuk),
"zephaniah" => Ok(Book::Zephaniah),
"haggai" => Ok(Book::Haggai),
"zechariah" => Ok(Book::Zechariah),
"malachi" => Ok(Book::Malachi),
"matthew" => Ok(Book::Matthew),
"mark" => Ok(Book::Mark),
"luke" => Ok(Book::Luke),
"psalms" | "ps" => Ok(Book::Psalms),
"proverbs" | "pr" => Ok(Book::Proverbs),
"ecclesiastes" | "ec" => Ok(Book::Ecclesiastes),
"song of solomon" | "ca" => Ok(Book::SongOfSolomon),
"isaiah" | "isa" => Ok(Book::Isaiah),
"jeremiah" | "jer" => Ok(Book::Jeremiah),
"lamentations" | "la" => Ok(Book::Lamentations),
"ezekiel" | "eze" => Ok(Book::Ezekiel),
"daniel" | "da" => Ok(Book::Daniel),
"hosea" | "ho" => Ok(Book::Hosea),
"joel" | "joe" => Ok(Book::Joel),
"amos" | "am" => Ok(Book::Amos),
"obadiah" | "ob" => Ok(Book::Obadiah),
"jonah" | "jon" => Ok(Book::Jonah),
"micah" | "mic" => Ok(Book::Micah),
"nahum" | "na" => Ok(Book::Nahum),
"habakkuk" | "hab" => Ok(Book::Habakkuk),
"zephaniah" | "zep" => Ok(Book::Zephaniah),
"haggai" | "hag" => Ok(Book::Haggai),
"zechariah" | "zec" => Ok(Book::Zechariah),
"malachi" | "mal" => Ok(Book::Malachi),
"matthew" | "mt" => Ok(Book::Matthew),
"mark" | "mr" => Ok(Book::Mark),
"luke" | "lu" => Ok(Book::Luke),
"john" | "joh" => Ok(Book::John),
"acts" => Ok(Book::Acts),
"romans" => Ok(Book::Romans),
"1 corinthians" => Ok(Book::FirstCorinthians),
"2 corinthians" => Ok(Book::SecondCorinthians),
"galatians" => Ok(Book::Galatians),
"ephesians" => Ok(Book::Ephesians),
"philippians" => Ok(Book::Philippians),
"colossians" => Ok(Book::Colossians),
"1 thessalonians" => Ok(Book::FirstThessalonians),
"2 thessalonians" => Ok(Book::SecondThessalonians),
"1 timothy" => Ok(Book::FirstTimothy),
"2 timothy" => Ok(Book::SecondTimothy),
"titus" => Ok(Book::Titus),
"philemon" => Ok(Book::Philemon),
"hebrews" => Ok(Book::Hebrews),
"james" => Ok(Book::James),
"1 peter" => Ok(Book::FirstPeter),
"2 peter" => Ok(Book::SecondPeter),
"1 john" => Ok(Book::FirstJohn),
"2 john" => Ok(Book::SecondJohn),
"3 john" => Ok(Book::ThirdJohn),
"acts" | "ac" => Ok(Book::Acts),
"romans" | "ro" => Ok(Book::Romans),
"1 corinthians" | "1co" | "1 co" => Ok(Book::FirstCorinthians),
"2 corinthians" | "2co" | "2 co" => Ok(Book::SecondCorinthians),
"galatians" | "ga" => Ok(Book::Galatians),
"ephesians" | "eph" => Ok(Book::Ephesians),
"philippians" | "php" => Ok(Book::Philippians),
"colossians" | "col" => Ok(Book::Colossians),
"1 thessalonians" | "1th" | "1 th" => Ok(Book::FirstThessalonians),
"2 thessalonians" | "2th" | "2 th" => Ok(Book::SecondThessalonians),
"1 timothy" | "1ti" | "1 ti" => Ok(Book::FirstTimothy),
"2 timothy" | "2ti" | "2 ti" => Ok(Book::SecondTimothy),
"titus" | "tit" => Ok(Book::Titus),
"philemon" | "phm" => Ok(Book::Philemon),
"hebrews" | "heb" => Ok(Book::Hebrews),
"james" | "jas" => Ok(Book::James),
"1 peter" | "1pe" | "1 pe" => Ok(Book::FirstPeter),
"2 peter" | "2pe" | "2 pe" => Ok(Book::SecondPeter),
"1 john" | "1jo" | "1 jo" => Ok(Book::FirstJohn),
"2 john" | "2jo" | "2 jo" => Ok(Book::SecondJohn),
"3 john" | "3jo" | "3 jo" => Ok(Book::ThirdJohn),
"jude" => Ok(Book::Jude),
"revelation" | "rev" => Ok(Book::Revelation),
"revelation" | "re" => Ok(Book::Revelation),

_ => Err(value.to_string()),
}
}
Expand Down Expand Up @@ -233,11 +235,7 @@ impl BibleRef for Book {
/// True if `&str` is a valid book of the Bible.
fn is_valid(book: &str) -> bool {
let v: Result<Book, String> = book.try_into();

match v {
Ok(_) => true,
Err(_) => false,
}
v.is_ok()
}
}

Expand All @@ -246,11 +244,35 @@ pub enum Site {
JwOrg,
}

/// The UrlTemplate holds the URL structure for single and ranged verse scriptures.
pub(crate) struct UrlTemplate {
single: String,
range: String,
}

impl Url for Site {
fn get_template(&self) -> String {
fn get_template(&self) -> UrlTemplate {
match self {
Site::JwOrg => UrlTemplate {single: "https://www.jw.org/en/library/bible/study-bible/books/{BOOKNAME}/{CHAPTER}/#v{BOOKNUM}{CHAPTER}{VERSE}".into(),
range: "https://www.jw.org/en/library/bible/study-bible/books/{BOOKNAME}/{CHAPTER}/#v{BOOKNUM}{CHAPTER}{VERSE}-v{BOOKNUM}{CHAPTER}{VERSE}".into()}
}
}
fn get_single(&self) -> String {
match self {
Site::JwOrg => "https://www.jw.org/en/library/bible/study-bible/books/{BOOKNAME}/{CHAPTER}/#v{BOOKNUM}{CHAPTER}{VERSE}".into(),
Site::JwOrg => {
let temp = self.get_template();
temp.single
}
}
}

fn get_range(&self) -> String {
match self {
Site::JwOrg => {
let temp = self.get_template();
temp.range
}
}
}
}

Expand Down
37 changes: 28 additions & 9 deletions src/parsers/scripture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use regex::Regex;

lazy_static! {
static ref RE: regex::Regex =
// Regular expression matches the pattern for the name of the book or letter, chapter, and verse. Single verse at the moment,
// ranges are not taken into account.
Regex::new(r"(?P<book>([1234]\s)?[a-zA-Z]+)(?:\s+)(?P<chapter>\d+)(:)(?P<verse>\d+)").expect("error while compiling the FIND_BOOK regex in scripture");
// Regular expression matches the pattern for the name of the book or letter, chapter, and verse.
Regex::new(r"(?<book>(?:[1234]\s?)?[a-zA-Z]+)\s*(?<chapter>\d+)(?::(?<verse>\d+(?:[—–-]\d+)?(?:,\s*\d+(?:[—–-]\d+)?)*(?:;\s*\d+(?::\d+(?:[—–-]\d+)?(?:,\s*\d+(?:[—–-]\d+)?)*))*)?)").expect("error while compiling the FIND_BOOK regex in scripture");
}

#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
Expand All @@ -28,7 +27,7 @@ impl<'a> Bible<'a> {
booknum: Default::default(),
}
}
//FIX

pub(crate) fn get_book(&self) -> &'a str {
self.book
}
Expand All @@ -41,21 +40,28 @@ impl<'a> Bible<'a> {
self.verse
}

pub(crate) fn get_i(&self) -> u8 {
pub(crate) fn get_idx(&self) -> u8 {
Book::get_index(self.book).expect("expected a valid book")
}

pub(crate) fn is_range(&self) -> bool {
match self.get_verse().contains('-') {
true => true,
false => false,
}
}

pub(crate) fn parse(scripture: &'a str) -> Bible {
let re: &RE = &RE;

let scripture = match Book::is_valid(
re.captures(scripture)
let scripture: regex::Captures = match Book::is_valid(
RE.captures(scripture)
.unwrap()
.name("book")
.unwrap()
.as_str(),
) {
true => re.captures(scripture).unwrap(),
true => RE.captures(scripture).unwrap(),

false => {
//TODO: Improve this with Errors
Expand All @@ -75,11 +81,11 @@ impl<'a> Bible<'a> {
}
}


// Unit tests
#[cfg(test)]
mod test {
use super::*;
use pretty_assertions::assert_eq;

#[test]
#[should_panic]
Expand Down Expand Up @@ -121,4 +127,17 @@ mod test {
let result: Bible = Bible::parse(input);
assert_eq!(result, expect);
}

#[test]
fn t_find_book_ranged() {
let input: &str = "1 Timothy 3:16-20";
let expect: Bible = Bible {
book: "1 Timothy",
chapter: "3",
verse: "16-20",
booknum: "54".into(),
};
let result: Bible = Bible::parse(input);
assert_eq!(result, expect);
}
}
Loading

0 comments on commit 680eef1

Please sign in to comment.