Struct tpnote_lib::context::Context
source · pub struct Context {
pub path: PathBuf,
pub dir_path: PathBuf,
pub root_path: PathBuf,
/* private fields */
}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.
root_path: PathBufdir_path is a subdirectory of root_path. root_path is the
first directory, that upwards from dir_path, contains a file named
FILENAME_ROOT_PATH_MARKER (or, / if not marker file can be found).
The root directory is interpreted by Tp-Note’s viewer as its base
directory: only files within this directory are served.
Implementations§
source§impl 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
Constructor: path is the first positional command line parameter
<path> (see man page). path must point to a directory or
a file.
A copy of path is stored in self.ct as key TMPL_VAR_PATH. It
directory path as key TMPL_VAR_DIR_PATH.
use std::path::Path;
use tpnote_lib::settings::ENV_VAR_TPNOTE_EXTENSION_DEFAULT;
use tpnote_lib::settings::update_settings;
use tpnote_lib::config::TMPL_VAR_DIR_PATH;
use tpnote_lib::config::TMPL_VAR_PATH;
use tpnote_lib::context::Context;
std::env::remove_var(ENV_VAR_TPNOTE_EXTENSION_DEFAULT);
update_settings().unwrap();
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/"));
assert_eq!(&context.get(TMPL_VAR_PATH).unwrap().to_string(),
r#""/path/to/mynote.md""#);
assert_eq!(&context.get(TMPL_VAR_DIR_PATH).unwrap().to_string(),
r#""/path/to""#);sourcepub fn insert_content(
&mut self,
tmpl_var: &str,
tmpl_var_header: &str,
input: &impl Content
) -> Result<(), NoteError>
pub fn insert_content( &mut self, tmpl_var: &str, tmpl_var_header: &str, input: &impl 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::settings::ENV_VAR_TPNOTE_EXTENSION_DEFAULT;
use tpnote_lib::settings::update_settings;
use tpnote_lib::context::Context;
use tpnote_lib::content::Content;
use tpnote_lib::content::ContentString;
std::env::remove_var(ENV_VAR_TPNOTE_EXTENSION_DEFAULT);
update_settings().unwrap();
let mut context = Context::from(&Path::new("/path/to/mynote.md"));
context.insert_content("clipboard", "clipboard_header",
&ContentString::from(String::from("Data from clipboard.")));
assert_eq!(&context.get("clipboard").unwrap().to_string(),
"\"Data from clipboard.\"");
context.insert_content("stdin", "stdin_header",
&ContentString::from("---\ntitle: \"My Stdin.\"\n---\nbody".to_string()));
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.""#);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.