Skip to content

Commit f08894a

Browse files
feat: {{include}}
1 parent dd24093 commit f08894a

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/main.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,34 @@ impl PreProcessor for LinkOrInclude {
106106
}
107107
}
108108

109+
/**
110+
* insert arbitrary file content directly
111+
* tag syntax: {{include|<relative path from root document>}}
112+
*/
113+
struct AlwaysInclude;
114+
115+
impl PreProcessor for LinkOrInclude {
116+
fn transform(&self, build_context: &BuildContext<'_>, content: String) -> String {
117+
let pattern = Regex::from_str(r#"\{\{include\|./((?:\w+/)+)(\w+)\}\}"#).unwrap();
118+
pattern.replace_all(content.as_str(), |captures: &Captures| {
119+
let file_path = captures.index(1);
120+
let file_name = captures.index(2);
121+
println!("including: {file_path}/{file_name}");
122+
let mut cloned_path = build_context.input_file.to_path_buf();
123+
cloned_path.pop();
124+
let target_path = cloned_path.join(file_path).join(file_name);
125+
println!("{target_path}", target_path = &target_path.to_str().unwrap());
126+
let target_path = target_path.canonicalize().unwrap();
127+
println!("{path}", path = &target_path.to_str().unwrap());
128+
let mut fd = BufReader::new(File::open(target_path).expect("Inclusion target file open error"));
129+
let mut including_text = String::new();
130+
fd.read_to_string(&mut including_text).unwrap();
131+
132+
including_text
133+
}).to_string()
134+
}
135+
}
136+
109137
fn main() {
110138
let mut args: Args = Args::parse().validate().unwrap();
111139
println!("{args:?}", args = &args);
@@ -122,7 +150,7 @@ fn main() {
122150
};
123151

124152
let input_content = LinkOrInclude.transform(&build_context, input_content);
125-
let input_content = LinkOrInclude.transform(&build_context, input_content);
153+
let input_content = AlwaysInclude.transform(&build_context, input_content);
126154

127155
let mut output = BufWriter::new(File::options().write(true).create(true).truncate(true).open(args.output_file).unwrap());
128156
output.write_all(input_content.as_bytes()).expect("could not write output to destination");

0 commit comments

Comments
 (0)