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: PathBuf
First positional command line argument.
dir_path: PathBuf
The 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: PathBuf
dir_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
A thin wrapper around tera::Context
storing some additional
information.
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::set_test_default_settings;
use tpnote_lib::config::TMPL_VAR_DIR_PATH;
use tpnote_lib::config::TMPL_VAR_PATH;
use tpnote_lib::context::Context;
set_test_default_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::set_test_default_settings;
use tpnote_lib::context::Context;
use tpnote_lib::content::Content;
use tpnote_lib::content::ContentString;
set_test_default_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").unwrap()
.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)
pub fn insert<T, S>(&mut self, key: S, val: &T)
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>
pub fn try_insert<T, S>(&mut self, key: S, val: &T) -> Result<(), Error>
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.
Trait Implementations§
impl StructuralPartialEq for Context
Auto Trait Implementations§
impl Freeze for Context
impl RefUnwindSafe for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl UnwindSafe for Context
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more