Skip to content

Commit

Permalink
Add template
Browse files Browse the repository at this point in the history
  • Loading branch information
shinshin86 committed Apr 13, 2022
1 parent af38a8c commit 157c433
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 5 deletions.
187 changes: 187 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ edition = "2021"
[dependencies]
pulldown-cmark = "0.9.1"
notify = "4.0.16"
clap = { version = "3.1.8", features = ["derive"] }
clap = { version = "3.1.8", features = ["derive"] }
handlebars = "4.2.2"
serde_json = "1.0.79"
33 changes: 29 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use clap::Parser as ClapParser;
use handlebars::Handlebars;
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use pulldown_cmark::{html, Options, Parser};
use serde_json::json;
use std::fs;
use std::fs::File;
use std::io::Write;
Expand All @@ -19,6 +21,10 @@ struct Args {
/// Specify the output destination for the converted HTML file (optional)
#[clap(short, long, default_value = ".")]
output: String,

/// Specify the file path of the template file
#[clap(short, long, default_value = "")]
template: String,
}

fn read_md_file(file_path: &std::path::Path) -> Result<String, Box<dyn std::error::Error>> {
Expand All @@ -29,13 +35,27 @@ fn read_md_file(file_path: &std::path::Path) -> Result<String, Box<dyn std::erro
fn write_html_file(
file_path: &std::path::Path,
html: &str,
template: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let mut file = File::create(file_path)?;
write!(file, "{}", html)?;

if template != "" {
let template_path = Path::new(template);
let mut handlebars = Handlebars::new();
match handlebars.register_template_file("template", template_path) {
Err(err) => {
eprintln!("ERROR: {}", err.reason);
std::process::exit(1);
}
_ => handlebars.render_to_write("template", &json!({ "html": html }), &mut file)?,
}
} else {
write!(file, "{}", html)?;
}
Ok(())
}

fn markdown_to_html(input_path: &std::path::Path, output_path: &std::path::Path) {
fn markdown_to_html(input_path: &std::path::Path, output_path: &std::path::Path, template: &str) {
let markdown_input = read_md_file(input_path).unwrap();

let mut options = Options::empty();
Expand All @@ -45,13 +65,14 @@ fn markdown_to_html(input_path: &std::path::Path, output_path: &std::path::Path)
let mut html_output = String::new();
html::push_html(&mut html_output, parser);

write_html_file(output_path, &html_output).unwrap();
write_html_file(output_path, &html_output, template).unwrap();
}

fn main() -> notify::Result<()> {
let args = Args::parse();
let input = &args.input;
let output = &args.output;
let template = &args.template;

let (tx, rx) = channel();
let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(1))?;
Expand Down Expand Up @@ -84,7 +105,11 @@ fn main() -> notify::Result<()> {
md_file_name.to_str().unwrap().replace(".md", ".html");
let output_file_path = output_dir_path.join(html_file_name);

markdown_to_html(input_file_path, output_file_path.as_path());
markdown_to_html(
input_file_path,
output_file_path.as_path(),
template,
);

println!("=== Generated HTML ===");
println!("Input file path: {:?}", input_file_path);
Expand Down

0 comments on commit 157c433

Please sign in to comment.