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.
impl<S: ContextState> Context<S>
The methods below are available in all ContentState states.
Sourcepub fn get_dir_path(&self) -> &Path
pub fn get_dir_path(&self) -> &Path
Getter for self.dir_path.
Sourcepub fn get_root_path(&self) -> &Path
pub fn get_root_path(&self) -> &Path
Getter for self.root_path.
Sourcepub fn get_doc_file_date(&self) -> Option<SystemTime>
pub fn get_doc_file_date(&self) -> Option<SystemTime>
Getter for self.doc_file_date.
Sourcepub fn mark_as_invalid(self) -> Context<Invalid>
pub fn mark_as_invalid(self) -> Context<Invalid>
Transition to the fault state.
Sourcepub fn from_context_path(context: &Context<S>) -> Context<HasSettings>
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.
Sourcepub fn insert_raw_text_from_existing_content(&mut self, content: &impl Content)
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§impl Context<Invalid>
The start state of all Context objects.
impl Context<Invalid>
The start state of all Context objects.
Sourcepub fn from(path: &Path) -> Result<Context<HasSettings>, FileError>
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>
impl Context<HasSettings>
Sourcepub fn insert_front_matter(
self,
fm: &FrontMatter,
) -> Context<ReadyForFilenameTemplate>
pub fn insert_front_matter( self, fm: &FrontMatter, ) -> Context<ReadyForFilenameTemplate>
Merges fm into self.ct.
Sourcepub fn insert_front_matter_and_raw_text_from_existing_content(
self,
clipboards: &Vec<&impl Content>,
) -> Result<Context<HasExistingContent>, NoteError>
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.\"");Sourcepub fn insert_error_content(
self,
note_erroneous_content: &impl Content,
error_message: &str,
viewer_doc_js: &str,
) -> Context<ReadyForHtmlErrorTemplate>
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_JSfromviewer_doc_jsTMPL_HTML_VAR_DOC_ERRORfromerror_messageTMPL_HTML_VAR_DOC_TEXTfromnote_erroneous_content
Source§impl Context<HasExistingContent>
impl Context<HasExistingContent>
Sourcepub fn insert_front_matter_and_raw_text_from_existing_content(
self,
clipboards: &Vec<&impl Content>,
) -> Result<Context<HasExistingContent>, NoteError>
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>.
Sourcepub fn set_state_ready_for_content_template(
self,
) -> Context<ReadyForContentTemplate>
pub fn set_state_ready_for_content_template( self, ) -> Context<ReadyForContentTemplate>
Mark this as ready for a content template.
Source§impl Context<ReadyForFilenameTemplate>
impl Context<ReadyForFilenameTemplate>
Sourcepub fn assert_precoditions(&self) -> Result<(), NoteError>
pub fn assert_precoditions(&self) -> Result<(), NoteError>
Checks if the front matter variables satisfy preconditions.
self.path is the path to the current document.
Sourcepub fn insert_raw_content_and_css(
self,
content: &impl Content,
viewer_doc_js: &str,
) -> Context<ReadyForHtmlTemplate>
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_JSfromviewer_doc_jsTMPL_VAR_DOCfromcontent.header()andcontent.body()TMPL_HTML_VAR_EXPORTER_DOC_CSSTMPL_HTML_VAR_EXPORTER_HIGHLIGHTING_CSSTMPL_HTML_VAR_EXPORTER_HIGHLIGHTING_CSSTMPL_HTML_VAR_VIEWER_DOC_CSS_PATHTMPL_HTML_VAR_VIEWER_DOC_CSS_PATH_VALUETMPL_HTML_VAR_VIEWER_HIGHLIGHTING_CSS_PATHTMPL_HTML_VAR_VIEWER_HIGHLIGHTING_CSS_PATH_VALUE
Trait Implementations§
Source§impl<S: ContextState> Deref for Context<S>
Auto dereferences for convenient access to tera::Context.
impl<S: ContextState> Deref for Context<S>
Auto dereferences for convenient access to tera::Context.
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>
impl<S> Sync for Context<S>
impl<S> Unpin for Context<S>
impl<S> UnwindSafe for Context<S>where
S: UnwindSafe + ?Sized,
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