-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from iambenzo/ft/output
- Loading branch information
Showing
8 changed files
with
554 additions
and
37 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use std::fs::File; | ||
use std::path::{Path, PathBuf}; | ||
|
||
use tera::{Context, Tera}; | ||
|
||
use crate::model::{Book, Highlight}; | ||
|
||
/// Uses a [Book] and optionally a [PathBuf] to a custom template file to render the highlights and | ||
/// notes captured whilst reading to a file in the output [Path]. | ||
/// | ||
/// Will use a default template if a custom template isn't provided. | ||
pub fn render_output(book: &Book, template: &Option<PathBuf>, output_dir: &Path) { | ||
let mut tera = Tera::default(); | ||
tera.add_raw_template("default", include_str!("templates/default.md")) | ||
.unwrap(); | ||
|
||
let mut ctx = Context::new(); | ||
ctx.insert( | ||
"highlights", | ||
&book | ||
.highlights() | ||
.values() | ||
.cloned() | ||
.collect::<Vec<Highlight>>(), | ||
); | ||
ctx.insert("quotes", &book.quotes()); | ||
|
||
let output: tera::Result<()>; | ||
let mut file_path = output_dir.to_path_buf(); | ||
file_path.push(format!("{}. {}.md", book.author(), book.title())); | ||
|
||
let file: File = match File::create(file_path.as_path()) { | ||
Ok(f) => f, | ||
Err(e) => { | ||
eprintln!("Error creating output file: {}", e); | ||
// dbg!(e); | ||
::std::process::exit(1); | ||
} | ||
}; | ||
|
||
// use default template unless user has provided one | ||
if let Some(t) = template { | ||
tera.add_template_file(t, Some("user")).unwrap(); | ||
output = tera.render_to("user", &ctx, file); | ||
} else { | ||
output = tera.render_to("default", &ctx, file); | ||
} | ||
|
||
if let Err(e) = output { | ||
eprintln!("Parsing error(s): {}", e); | ||
// dbg!(e); | ||
::std::process::exit(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
tags: | ||
created: | ||
"{ date }": | ||
type: kindle | ||
--- | ||
|
||
| Page | Description | Theme | | ||
| ---- | ----------- | ----- | | ||
{%- for hl in highlights %} | ||
| {{ hl.page }} | {{ hl.note.content | default (value="") }} [^{{ loop.index }}]| | | ||
{%- endfor %} | ||
|
||
{% for quote in quotes %} | ||
[^{{ loop.index }}]: {{ quote }} | ||
{% endfor %} |