diff --git a/src/chiritori.rs b/src/chiritori.rs new file mode 100644 index 0000000..293f2c9 --- /dev/null +++ b/src/chiritori.rs @@ -0,0 +1,132 @@ +use std::collections::HashMap; + +use crate::{formatter::{self, Formatter}, parser, remover, tokenizer}; + +pub struct ChiritoriConfiguration { + pub delimiter_start: String, + pub delimiter_end: String, + pub time_limited_configuration: TimeLimitedConfiguration +} + +impl Default for ChiritoriConfiguration { + fn default() -> Self { + Self { + delimiter_start: String::from(""), + time_limited_configuration: TimeLimitedConfiguration::default() + } + } +} + +pub struct TimeLimitedConfiguration { + pub tag_name: String, + pub time_offset: String, + pub current: chrono::DateTime, +} + +impl Default for TimeLimitedConfiguration { + fn default() -> Self { + Self { + tag_name: String::from("time-limited"), + time_offset: String::from("+00:00"), + current: chrono::Local::now() + } + } +} + +pub fn clean(content: String, config: ChiritoriConfiguration) -> String { + let mut builder_map: HashMap< + &str, + Box, + > = HashMap::new(); + builder_map.insert( + &config.time_limited_configuration.tag_name, + Box::new(remover::time_limited_remover::TimeLimitedRemover { + current_time: config.time_limited_configuration.current, + time_offset: config.time_limited_configuration.time_offset, + }), + ); + + let tokens = tokenizer::tokenize( + &content, + &config.delimiter_start, + &config.delimiter_end, + ); + + let parsed = parser::parse(&tokens); + + let (removed, markers) = remover::remove(parsed, &content, &builder_map); + + let removed_pos = remover::get_removed_pos(&markers); + let formatter: Vec> = vec![ + Box::new(formatter::indent_remover::IndentRemover {}), + Box::new(formatter::empty_line_remover::EmptyLineRemover {}), + Box::new(formatter::prev_line_break_remover::PrevLineBreakRemover {}), + Box::new(formatter::next_line_break_remover::NextLineBreakRemover {}), + ]; + + formatter::format(&removed, &removed_pos, &formatter) +} + +#[cfg(test)] +mod tests { + use super::*; + use chrono::Local; + + fn create_test_config() -> ChiritoriConfiguration { + ChiritoriConfiguration { + delimiter_start: String::from(""), + time_limited_configuration: TimeLimitedConfiguration { + tag_name: String::from("time-limited"), + current: Local::now(), + time_offset: String::from("+00:00"), + }, + } + } + + #[test] + fn test_clean_removes_time_limited_code() { + let content = String::from(r#" + + + +

Hello World

+

This is a sample page with some code.

+ +

This content is only available until the end of the year.

+

After that, it will be removed from the page.

+ + + Campaign! + + 40% off! Only until the 2001/12/31! + + + + + Campaign! + + until the 2001/12/31! + + +"#); + + let expected = String::from(r#" + + + +

Hello World

+

This is a sample page with some code.

+ + Campaign! + + +"#); + + let config = create_test_config(); + let result = clean(content, config); + + assert_eq!(result, expected); + } +} diff --git a/src/main.rs b/src/main.rs index d2384ea..d29d405 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ +use chiritori::{ChiritoriConfiguration, TimeLimitedConfiguration}; use clap::Parser; -use formatter::Formatter; -use std::collections::HashMap; use std::fs::File; use std::io::prelude::*; mod element_parser; @@ -8,6 +7,7 @@ mod formatter; mod parser; mod remover; mod tokenizer; +mod chiritori; #[derive(Parser)] #[command(version, about, long_about = None)] @@ -61,37 +61,21 @@ fn main() { .expect("something went wrong reading the file"); } - let mut builder_map: HashMap< - &str, - Box, - > = HashMap::new(); - builder_map.insert( - args.time_limited_tag_name.as_str(), - Box::new(remover::time_limited_remover::TimeLimitedRemover { - current_time: args + let config = ChiritoriConfiguration { + delimiter_start: args.delimiter_start, + delimiter_end: args.delimiter_end, + time_limited_configuration: TimeLimitedConfiguration { + tag_name: args.time_limited_tag_name, + time_offset: args.time_limited_time_offset, + current: args .time_limited_current .parse::>() .unwrap_or(chrono::Local::now()), - time_offset: args.time_limited_time_offset.to_string(), - }), - ); - let tokens = tokenizer::tokenize( - &content, - args.delimiter_start.as_str(), - args.delimiter_end.as_str(), - ); - - let (removed, markers) = remover::remove(parser::parse(&tokens), &content, &builder_map); + } + }; - let removed_pos = remover::get_removed_pos(&markers); - let formatter: Vec> = vec![ - Box::new(formatter::indent_remover::IndentRemover {}), - Box::new(formatter::empty_line_remover::EmptyLineRemover {}), - Box::new(formatter::prev_line_break_remover::PrevLineBreakRemover {}), - Box::new(formatter::next_line_break_remover::NextLineBreakRemover {}), - ]; - let cleaned = formatter::format(&removed, &removed_pos, &formatter); + let cleaned = chiritori::clean(content, config); if let Some(output) = args.output { let mut f = File::create(output).expect("file not found");