Skip to content

Commit

Permalink
Merge pull request #9 from piyoppi/chiritori-core
Browse files Browse the repository at this point in the history
split main logic from entry function
  • Loading branch information
piyoppi authored Oct 12, 2024
2 parents d67cd75 + c91c30c commit c32adfe
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 28 deletions.
132 changes: 132 additions & 0 deletions src/chiritori.rs
Original file line number Diff line number Diff line change
@@ -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("<!--"),
delimiter_end: String::from("-->"),
time_limited_configuration: TimeLimitedConfiguration::default()
}
}
}

pub struct TimeLimitedConfiguration {
pub tag_name: String,
pub time_offset: String,
pub current: chrono::DateTime<chrono::Local>,
}

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<dyn remover::remove_marker_builder::RemoveMarkerBuilder>,
> = 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<Box<dyn Formatter>> = 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("<!--"),
delimiter_end: 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#"
<!DOCTYPE html>
<html>
<body>
<h1>Hello World</h1>
<p>This is a sample page with some code.</p>
<!-- time-limited to="2019-12-31 23:59:59" -->
<p>This content is only available until the end of the year.</p>
<p>After that, it will be removed from the page.</p>
<!-- /time-limited -->
<!-- time-limited to="2999-12-31 23:59:59" -->
Campaign!
<!-- time-limited to="2001-12-31 23:59:59" -->
40% off! Only until the 2001/12/31!
<!-- /time-limited -->
<!-- /time-limited -->
<!-- time-limited to="2001-12-31 23:59:59" -->
<!-- time-limited to="2999-12-31 23:59:59" -->
Campaign!
<!-- /time-limited -->
until the 2001/12/31!
<!-- /time-limited -->
</body>
</html>"#);

let expected = String::from(r#"
<!DOCTYPE html>
<html>
<body>
<h1>Hello World</h1>
<p>This is a sample page with some code.</p>
<!-- time-limited to="2999-12-31 23:59:59" -->
Campaign!
<!-- /time-limited -->
</body>
</html>"#);

let config = create_test_config();
let result = clean(content, config);

assert_eq!(result, expected);
}
}
40 changes: 12 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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;
mod formatter;
mod parser;
mod remover;
mod tokenizer;
mod chiritori;

#[derive(Parser)]
#[command(version, about, long_about = None)]
Expand Down Expand Up @@ -61,37 +61,21 @@ fn main() {
.expect("something went wrong reading the file");
}

let mut builder_map: HashMap<
&str,
Box<dyn remover::remove_marker_builder::RemoveMarkerBuilder>,
> = 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::<chrono::DateTime<chrono::Local>>()
.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<Box<dyn Formatter>> = 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");
Expand Down

0 comments on commit c32adfe

Please sign in to comment.