Skip to content

Commit 99bd439

Browse files
feat: add <summary> mode to build mode
1 parent 96b5df6 commit 99bd439

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/main.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ impl Args {
4545
enum BuildMode {
4646
Dynamic,
4747
Static,
48+
Spoiler
4849
}
4950

5051
struct BuildContext<'ctx> {
@@ -72,6 +73,42 @@ impl PreProcessor for LinkOrInclude {
7273
let full_file_path = format!("{file_path}{file_name}");
7374
match build_context.mode {
7475
BuildMode::Dynamic => format!("This section is migrated. Please see [{file_name}](./{full_file_path})"),
76+
BuildMode::Spoiler => {
77+
let mut cloned_path = build_context.input_file.to_path_buf();
78+
cloned_path.pop();
79+
let target_path = cloned_path.join(file_path).join(file_name);
80+
let mut pasting_text = String::from("<details><summary>");
81+
pasting_text.push_str("content of ");
82+
pasting_text.push_str(target_path.to_str().unwrap_or("included file"));
83+
pasting_text.push_str("</summary>\n\n");
84+
85+
println!("{target_path}", target_path = &target_path.to_str().unwrap());
86+
let target_path = target_path.canonicalize().unwrap();
87+
println!("{path}", path = &target_path.to_str().unwrap());
88+
let mut fd = BufReader::new(File::open(target_path).expect("Inclusion target file open error"));
89+
let mut buf = String::new();
90+
fd.read_to_string(&mut buf).unwrap();
91+
92+
let including_text = {
93+
let include_pat = Regex::from_str(r#"<!-- START -->\n?((.|\n)*)<!-- END -->"#).unwrap();
94+
let ret = include_pat.captures_iter(buf.as_str()).map(|a| {
95+
let to_include = a.index(1).to_string();
96+
// lower header level by one
97+
let header_pat = Regex::from_str("(?m)^(#{1,5})(.*)$").unwrap();
98+
let to_include = header_pat.replace_all(to_include.as_str(), |cap: &Captures| {
99+
format!("#{header}{headline_text}", header = cap.index(1), headline_text = cap.index(2))
100+
}).to_string();
101+
102+
to_include
103+
}).join("");
104+
105+
ret
106+
};
107+
108+
pasting_text.push_str("\n</details>");
109+
110+
including_text
111+
}
75112
BuildMode::Static => {
76113
let mut cloned_path = build_context.input_file.to_path_buf();
77114
cloned_path.pop();

0 commit comments

Comments
 (0)