Struct Context

Source
pub struct Context<S: ContextState + ?Sized> { /* private fields */ }
Expand description

Tiny wrapper around “Tera context” with some additional information.

Implementations§

Source§

impl<S: ContextState> Context<S>

The methods below are available in all ContentState states.

Source

pub fn get_path(&self) -> &Path

Getter for self.path.

Source

pub fn get_dir_path(&self) -> &Path

Getter for self.dir_path.

Source

pub fn get_root_path(&self) -> &Path

Getter for self.root_path.

Source

pub fn get_doc_file_date(&self) -> Option<SystemTime>

Getter for self.doc_file_date.

Source

pub fn mark_as_invalid(self) -> Context<Invalid>

Transition to the fault state.

Source

pub fn from_context_path(context: &Context<S>) -> Context<HasSettings>

Constructor. Unlike from() this constructor does not access the file system in order to detect dir_path, root_path and doc_file_date. It copies these values from the passed context. Use this constructor when you are sure that the above date has not changed since you instantiated context. In this case you can avoid repeated file access.

Source

pub fn insert_raw_text_from_existing_content(&mut self, content: &impl Content)

Inserts a Content in Context. The content appears as key in context.ct with its name taken from content.name(). Its value is a tera::Map with two keys TMPL_VAR_HEADER and TMPL_VAR_BODY. The corresponding values are copied from conten.header() and content.body().

Source

pub fn insert_front_matter_and_raw_text_from_existing_content2( &mut self, clipboards: &Vec<&impl Content>, ) -> Result<(), NoteError>

See function of the same name in impl Context<HasSettings>.

Source§

impl Context<Invalid>

The start state of all Context objects.

Source

pub fn from(path: &Path) -> Result<Context<HasSettings>, FileError>

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. The root directory, where the marker file tpnote.toml was found, is stored with the key TMPL_VAR_ROOT_PATH. If path points to a file, its file creation date is stored with the key TMPL_VAR_DOC_FILE_DATE.

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

assert_eq!(context.get_path(), Path::new("/path/to/mynote.md"));
assert_eq!(context.get_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""#);
Source§

impl Context<HasSettings>

Source

pub fn insert_front_matter( self, fm: &FrontMatter, ) -> Context<ReadyForFilenameTemplate>

Merges fm into self.ct.

Source

pub fn insert_front_matter_and_raw_text_from_existing_content( self, clipboards: &Vec<&impl Content>, ) -> Result<Context<HasExistingContent>, NoteError>

Inserts clipboard data, stdin data and/or existing note file content into the context. The data may contain some copied text with or without a YAML header. The latter usually carries front matter variables. The input data below is registered with the key name given by tmpl_var_body_name. 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_name. The templates expect the key names clipboard_header or std_header. The raw header text will be inserted with this key name.

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")).unwrap();
let c1 =  ContentString::from_string(String::from("Data from clipboard."),
         "txt_clipboard".to_string(),
);
let c2 = ContentString::from_string(
         "---\ntitle: My Stdin.\n---\nbody".to_string(),
         "stdin".to_string(),
);
let c = vec![&c1, &c2];

let context = context
    .insert_front_matter_and_raw_text_from_existing_content(&c).unwrap();

assert_eq!(
    &context.get("txt_clipboard").unwrap().get("body").unwrap().to_string(),
    "\"Data from clipboard.\"");
assert_eq!(
    &context.get("stdin").unwrap().get("body").unwrap().to_string(),
    "\"body\"");
assert_eq!(
    &context.get("stdin").unwrap().get("header").unwrap().to_string(),
    "\"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(),
     "\"My Stdin.\"");
Source

pub fn insert_error_content( self, note_erroneous_content: &impl Content, error_message: &str, viewer_doc_js: &str, ) -> Context<ReadyForHtmlErrorTemplate>

This adds the following variables to self:

  • TMPL_HTML_VAR_VIEWER_DOC_JS from viewer_doc_js
  • TMPL_HTML_VAR_DOC_ERROR from error_message
  • TMPL_HTML_VAR_DOC_TEXT from note_erroneous_content
Source§

impl Context<HasExistingContent>

Source

pub fn insert_front_matter_and_raw_text_from_existing_content( self, clipboards: &Vec<&impl Content>, ) -> Result<Context<HasExistingContent>, NoteError>

See same method in Context<HasSettings>.

Source

pub fn set_state_ready_for_content_template( self, ) -> Context<ReadyForContentTemplate>

Mark this as ready for a content template.

Source§

impl Context<ReadyForFilenameTemplate>

Source

pub fn assert_precoditions(&self) -> Result<(), NoteError>

Checks if the front matter variables satisfy preconditions. self.path is the path to the current document.

Source

pub fn insert_raw_content_and_css( self, content: &impl Content, viewer_doc_js: &str, ) -> Context<ReadyForHtmlTemplate>

Inserts the following variables into self:

  • TMPL_HTML_VAR_VIEWER_DOC_JS from viewer_doc_js
  • TMPL_VAR_DOC from content.header() and content.body()
  • TMPL_HTML_VAR_EXPORTER_DOC_CSS
  • TMPL_HTML_VAR_EXPORTER_HIGHLIGHTING_CSS
  • TMPL_HTML_VAR_EXPORTER_HIGHLIGHTING_CSS
  • TMPL_HTML_VAR_VIEWER_DOC_CSS_PATH
  • TMPL_HTML_VAR_VIEWER_DOC_CSS_PATH_VALUE
  • TMPL_HTML_VAR_VIEWER_HIGHLIGHTING_CSS_PATH
  • TMPL_HTML_VAR_VIEWER_HIGHLIGHTING_CSS_PATH_VALUE

Methods from Deref<Target = Context>§

Source

pub fn get(&self, index: &str) -> Option<&Value>

Returns the value at a given key index.

Source

pub fn contains_key(&self, index: &str) -> bool

Checks if a value exists at a specific index.

Trait Implementations§

Source§

impl<S: Clone + ContextState + ?Sized> Clone for Context<S>

Source§

fn clone(&self) -> Context<S>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S: Debug + ContextState + ?Sized> Debug for Context<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: ContextState> Deref for Context<S>

Auto dereferences for convenient access to tera::Context.

Source§

type Target = Context

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<S: PartialEq + ContextState + ?Sized> PartialEq for Context<S>

Source§

fn eq(&self, other: &Context<S>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<S: ContextState + ?Sized> StructuralPartialEq for Context<S>

Auto Trait Implementations§

§

impl<S> Freeze for Context<S>
where S: ?Sized,

§

impl<S> RefUnwindSafe for Context<S>
where S: RefUnwindSafe + ?Sized,

§

impl<S> Send for Context<S>
where S: Send + ?Sized,

§

impl<S> Sync for Context<S>
where S: Sync + ?Sized,

§

impl<S> Unpin for Context<S>
where S: Unpin + ?Sized,

§

impl<S> UnwindSafe for Context<S>
where S: UnwindSafe + ?Sized,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T