pub struct Context {
    pub path: PathBuf,
    pub dir_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.

Implementations

A thin wrapper around tera::Context storing some additional information.

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/"));

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.""#);

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.""#);

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>

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);

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
}

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);

Returns the value at a given key index.

Remove a key from the context, returning the value at the key if the key was previously inserted into the context.

Checks if a value exists at a specific index.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Auto-dereference for convenient access to tera::Content.

The resulting type after dereferencing.
Dereferences the value.

Auto-dereference for convenient access to tera::Content.

Mutably dereferences the value.
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.