aboutsummaryrefslogtreecommitdiff
path: root/src/content.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/content.rs')
-rw-r--r--src/content.rs59
1 files changed, 27 insertions, 32 deletions
diff --git a/src/content.rs b/src/content.rs
index d767f9c..ff01342 100644
--- a/src/content.rs
+++ b/src/content.rs
@@ -1,15 +1,15 @@
1extern crate reqwest;
2extern crate serde_json;
3extern crate cursive; 1extern crate cursive;
4extern crate regex; 2extern crate regex;
3extern crate reqwest;
4extern crate serde_json;
5extern crate urlencoding; 5extern crate urlencoding;
6 6
7use self::regex::Regex;
7use cursive::theme::Effect; 8use cursive::theme::Effect;
8use cursive::utils::markup::StyledString; 9use cursive::utils::markup::StyledString;
9use cursive::Cursive;
10use cursive::views::Dialog; 10use cursive::views::Dialog;
11use cursive::Cursive;
11use serde_json::Value; 12use serde_json::Value;
12use self::regex::Regex;
13use CONFIGURATION; 13use CONFIGURATION;
14 14
15pub fn query_url_gen(title: &str) -> String { 15pub fn query_url_gen(title: &str) -> String {
@@ -57,36 +57,33 @@ pub fn get_extract(v: &Value) -> Result<String, reqwest::Error> {
57 Ok(format!("{}", extract)) 57 Ok(format!("{}", extract))
58 } 58 }
59 // ignore non strings 59 // ignore non strings
60 _ => Ok(format!("This page does not exist anymore")) 60 _ => Ok(format!("This page does not exist anymore")),
61 } 61 }
62} 62}
63 63
64pub fn extract_formatter(extract: String) -> StyledString { 64pub fn extract_formatter(extract: String) -> StyledString {
65 let mut formatted = StyledString::new(); 65 let mut formatted = StyledString::new();
66 66
67 let heading= Regex::new(r"^== (?P<d>.*) ==$").unwrap(); 67 let heading = Regex::new(r"^== (?P<d>.*) ==$").unwrap();
68 let subheading= Regex::new(r"^=== (?P<d>.*) ===$").unwrap(); 68 let subheading = Regex::new(r"^=== (?P<d>.*) ===$").unwrap();
69 let subsubheading= Regex::new(r"^==== (?P<d>.*) ====$").unwrap(); 69 let subsubheading = Regex::new(r"^==== (?P<d>.*) ====$").unwrap();
70 70
71 for line in extract.lines() { 71 for line in extract.lines() {
72 if heading.is_match(line) { 72 if heading.is_match(line) {
73 formatted.append( 73 formatted.append(StyledString::styled(
74 StyledString::styled( 74 heading.replace(line, "$d"),
75 heading.replace(line, "$d"), Effect::Bold 75 Effect::Bold,
76 ) 76 ));
77 );
78 } else if subheading.is_match(line) { 77 } else if subheading.is_match(line) {
79 formatted.append( 78 formatted.append(StyledString::styled(
80 StyledString::styled( 79 subheading.replace(line, "$d"),
81 subheading.replace(line, "$d"), Effect::Italic 80 Effect::Italic,
82 ) 81 ));
83 );
84 } else if subsubheading.is_match(line) { 82 } else if subsubheading.is_match(line) {
85 formatted.append( 83 formatted.append(StyledString::styled(
86 StyledString::styled( 84 subsubheading.replace(line, "$d"),
87 subsubheading.replace(line, "$d"), Effect::Underline 85 Effect::Underline,
88 ) 86 ));
89 );
90 } else { 87 } else {
91 formatted.append(StyledString::plain(line)); 88 formatted.append(StyledString::plain(line));
92 } 89 }
@@ -100,10 +97,9 @@ pub fn extract_formatter(extract: String) -> StyledString {
100pub fn get_search_results(search: &str) -> Result<Vec<String>, reqwest::Error> { 97pub fn get_search_results(search: &str) -> Result<Vec<String>, reqwest::Error> {
101 let url = search_url_gen(search); 98 let url = search_url_gen(search);
102 let mut res = reqwest::get(&url[..])?; 99 let mut res = reqwest::get(&url[..])?;
103 let v: Value = serde_json::from_str(&res.text()?) 100 let v: Value = serde_json::from_str(&res.text()?).unwrap_or_else(|e| {
104 .unwrap_or_else( |e| { 101 panic!("Recieved error {:?}", e);
105 panic!("Recieved error {:?}", e); 102 });
106 } );
107 103
108 let mut results: Vec<String> = vec![]; 104 let mut results: Vec<String> = vec![];
109 for item in v[1].as_array().unwrap() { 105 for item in v[1].as_array().unwrap() {
@@ -129,19 +125,18 @@ pub fn get_links(v: &Value) -> Result<Vec<String>, reqwest::Error> {
129 for item in arr { 125 for item in arr {
130 match item["title"] { 126 match item["title"] {
131 Value::String(ref title) => links.push(title.to_string()), 127 Value::String(ref title) => links.push(title.to_string()),
132 _ => links.push(String::from("lol")) 128 _ => links.push(String::from("lol")),
133 } 129 }
134 } 130 }
135 }, 131 }
136 _ => links.push(String::from("lol")) 132 _ => links.push(String::from("lol")),
137 }; 133 };
138 134
139 Ok(links) 135 Ok(links)
140} 136}
141 137
142pub fn pop_error(s: &mut Cursive, msg: String) { 138pub fn pop_error(s: &mut Cursive, msg: String) {
143 s.add_layer(Dialog::text(format!("{}", msg)) 139 s.add_layer(Dialog::text(format!("{}", msg)).button("Ok", |s| s.quit()));
144 .button("Ok", |s| s.quit()));
145} 140}
146 141
147pub fn handler(e: reqwest::Error) -> String { 142pub fn handler(e: reqwest::Error) -> String {