From d7f9120c121f8957edd1d6c4634e584ec145bbbc Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Thu, 28 Jun 2018 18:19:36 +0530 Subject: Start formatting article --- src/content.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/content.rs b/src/content.rs index 585f6b2..e31531b 100644 --- a/src/content.rs +++ b/src/content.rs @@ -1,11 +1,15 @@ extern crate reqwest; extern crate serde_json; extern crate cursive; +extern crate regex; +use cursive::theme::{ BaseColor, Color, Effect, Style }; +use cursive::utils::markup::StyledString; use cursive::Cursive; use cursive::views::Dialog; use serde_json::Value; use reqwest::Response; +use self::regex::Regex; pub fn query_url_gen(title: &str) -> String { // query config @@ -61,6 +65,33 @@ pub fn get_extract(mut res: Response) -> Result { } } +pub fn extract_formatter(extract: String) -> StyledString { + let mut formatted = StyledString::new(); + + let heading= Regex::new(r"^== (?P.*) ==$").unwrap(); + let subheading= Regex::new(r"^=== (?P.*) ===$").unwrap(); + + for line in extract.lines() { + if heading.is_match(line) { + formatted.append( + StyledString::styled( + heading.replace(line, "$d"), Effect::Bold + ) + ); + } else if subheading.is_match(line) { + formatted.append( + StyledString::styled( + subheading.replace(line, "$d"), Effect::Italic + ) + ); + } else { + formatted.append(StyledString::plain(line)); + } + } + + formatted +} + pub fn get_search_results(search: &str) -> Result, reqwest::Error> { let url = search_url_gen(search); let mut res = reqwest::get(&url[..])?; -- cgit v1.2.3