Struct tpnote_lib::context::Context
source · [−]Expand description
Tiny wrapper around “Tera context” with some additional information.
Fields
path: PathBufFirst positional command line argument.
dir_path: PathBufThe directory (only) path corresponding to the first positional command line argument. The is our working directory and the directory where the note file is (will be) located.
Implementations
sourceimpl Context
impl Context
A thin wrapper around tera::Context storing some additional
information.
sourcepub fn from(path: &Path) -> Self
pub fn from(path: &Path) -> Self
path is the first positional command line parameter <path> (see
man page).
The path parameter must be a canonicalized fully qualified file name.
use std::path::Path;
use tpnote_lib::context::Context;
let mut context = Context::from(&Path::new("/path/to/mynote.md"));
assert_eq!(context.path, Path::new("/path/to/mynote.md"));
assert_eq!(context.dir_path, Path::new("/path/to/"));sourcepub fn insert_front_matter(&mut self, fm: &FrontMatter)
pub fn insert_front_matter(&mut self, fm: &FrontMatter)
Inserts the YAML front header variables in the context for later use with templates.
use std::path::Path;
use tpnote_lib::context::Context;
use tpnote_lib::front_matter::FrontMatter;
let mut context = Context::from(&Path::new("/path/to/mynote.md"));
context.insert_front_matter(
&FrontMatter::try_from("title: \"My Stdin.\"").unwrap());
assert_eq!(&context.get("fm_title").unwrap().to_string(),
r#""My Stdin.""#);sourcepub fn insert_content(
&mut self,
tmpl_var: &str,
tmpl_var_header: &str,
input: &Content
) -> Result<(), NoteError>
pub fn insert_content(
&mut self,
tmpl_var: &str,
tmpl_var_header: &str,
input: &Content
) -> Result<(), NoteError>
Inserts clipboard or stdin data into the context. The data may
contain some copied text with or without a YAML header.
The latter usually carries front matter variable.
These are added separately via insert_front_matter().
The input data below is registered with the key name given
by tmpl_var. Typical names are "clipboard" or "stdin".
If the below input contains a valid YAML header, it will
be registered in the context with the key name given by
tmpl_var_header. This string is typically one of
clipboard_header or std_header.
The raw data that will be inserted into the context.
use std::path::Path;
use tpnote_lib::context::Context;
use tpnote_lib::content::Content;
let mut context = Context::from(&Path::new("/path/to/mynote.md"));
context.insert_content("clipboard", "clipboard_header",
&Content::from(String::from("Data from clipboard.")));
assert_eq!(&context.get("clipboard").unwrap().to_string(),
"\"Data from clipboard.\"");
context.insert_content("stdin", "stdin_header",
&Content::from(String::from("---\ntitle: \"My Stdin.\"\n---\nbody")));
assert_eq!(&context.get("stdin").unwrap().to_string(),
r#""body""#);
assert_eq!(&context.get("stdin_header").unwrap().to_string(),
r#""title: \"My Stdin.\"""#);
// "fm_title" is dynamically generated from the header variable "title".
assert_eq!(&context.get("fm_title").unwrap().to_string(),
r#""My Stdin.""#);sourcepub fn insert_environment(&mut self) -> Result<(), NoteError>
pub fn insert_environment(&mut self) -> Result<(), NoteError>
Captures Tp-Note’s environment and stores it as variables in a
context collection. The variables are needed later to populate
a context template and a filename template.
This function add the keys: TMPL_VAR_EXTENSION_DEFAULT, TMPL_VAR_USERNAME and TMPL_VAR_LANG.
use std::path::Path;
use tpnote_lib::context::Context;
use tpnote_lib::config::TMPL_VAR_EXTENSION_DEFAULT; // `extension_default`
use tpnote_lib::config::FILENAME_EXTENSION_DEFAULT; // usually `md`
let mut context = Context::from(&Path::new("/path/to/mynote.md"));
context.insert_environment();
// For most platforms `context.get("extension_default")` is `md`
assert_eq!(&context.get(TMPL_VAR_EXTENSION_DEFAULT).unwrap().to_string(),
&format!("\"{FILENAME_EXTENSION_DEFAULT}\""));Methods from Deref<Target = Context>
sourcepub fn insert<T, S>(&mut self, key: S, val: &T)where
T: Serialize + ?Sized,
S: Into<String>,
pub fn insert<T, S>(&mut self, key: S, val: &T)where
T: Serialize + ?Sized,
S: Into<String>,
Converts the val parameter to Value and insert it into the context.
Panics if the serialization fails.
let mut context = tera::Context::new();
context.insert("number_users", &42);sourcepub fn try_insert<T, S>(&mut self, key: S, val: &T) -> Result<(), Error>where
T: Serialize + ?Sized,
S: Into<String>,
pub fn try_insert<T, S>(&mut self, key: S, val: &T) -> Result<(), Error>where
T: Serialize + ?Sized,
S: Into<String>,
Converts the val parameter to Value and insert it into the context.
Returns an error if the serialization fails.
let mut context = Context::new();
// user is an instance of a struct implementing `Serialize`
if let Err(_) = context.try_insert("number_users", &user) {
// Serialization failed
}sourcepub fn extend(&mut self, source: Context)
pub fn extend(&mut self, source: Context)
Appends the data of the source parameter to self, overwriting existing keys.
The source context will be dropped.
let mut target = Context::new();
target.insert("a", &1);
target.insert("b", &2);
let mut source = Context::new();
source.insert("b", &3);
source.insert("d", &4);
target.extend(source);sourcepub fn remove(&mut self, index: &str) -> Option<Value>
pub fn remove(&mut self, index: &str) -> Option<Value>
Remove a key from the context, returning the value at the key if the key was previously inserted into the context.
sourcepub fn contains_key(&self, index: &str) -> bool
pub fn contains_key(&self, index: &str) -> bool
Checks if a value exists at a specific index.