Enum tpnote_lib::template::TemplateKind
source · #[non_exhaustive]
pub enum TemplateKind {
New,
FromClipboardYaml,
FromClipboard,
FromTextFile,
AnnotateFile,
SyncFilename,
None,
}Expand description
Each workflow is related to one TemplateKind, which relates to one
content template and one filename template.
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
New
Templates used when Tp-Note is invoked with a directory path.
FromClipboardYaml
Templates used when the clipboard contains a text with a YAML header.
FromClipboard
Templates used when the clipboard contains a text without header.
FromTextFile
Templates used when Tp-Note is invoked with a path pointing to a text file that does not contain a YAML header.
AnnotateFile
Templates used when Tp-Note is invoked with a path pointing to a non text file.
SyncFilename
Templates used when Tp-Note is invoked with a path pointing to a Tp-Note
text file with a valid YAML header (with a title: field).
None
No templates are used, but the file is still parsed in order to render it
later to HTML (c.f. <Note>.render_content_to_html() and
<Note>.export_html()).
Implementations§
source§impl TemplateKind
impl TemplateKind
sourcepub fn from<T: Content>(
path: &Path,
clipboard: &T,
stdin: &T
) -> (Self, Option<T>)
pub fn from<T: Content>(
path: &Path,
clipboard: &T,
stdin: &T
) -> (Self, Option<T>)
Constructor encoding the logic under what circumstances what template should be used.
Examples found in repository?
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
pub fn create_new_note_or_synchronize_filename<T, F>(
path: &Path,
clipboard: &T,
stdin: &T,
tk_filter: F,
html_export: &Option<(PathBuf, LocalLinkKind)>,
) -> Result<PathBuf, NoteError>
where
T: Content,
F: Fn(TemplateKind) -> TemplateKind,
{
// First, generate a new note (if it does not exist), then parse its front_matter
// and finally rename the file, if it is not in sync with its front matter.
// Collect input data for templates.
let mut context = Context::from(path);
context.insert_content(TMPL_VAR_CLIPBOARD, TMPL_VAR_CLIPBOARD_HEADER, clipboard)?;
context.insert_content(TMPL_VAR_STDIN, TMPL_VAR_STDIN_HEADER, stdin)?;
// `template_king` will tell us what to do.
let (template_kind, content) = TemplateKind::from::<T>(path, clipboard, stdin);
let template_kind = tk_filter(template_kind);
let n = match template_kind {
TemplateKind::New
| TemplateKind::FromClipboardYaml
| TemplateKind::FromClipboard
| TemplateKind::AnnotateFile => {
// CREATE A NEW NOTE WITH `TMPL_NEW_CONTENT` TEMPLATE
let mut n = Note::from_content_template(context, template_kind)?;
n.render_filename(template_kind)?;
// Check if the filename is not taken already
n.set_next_unused_rendered_filename()?;
n.save()?;
n
}
TemplateKind::FromTextFile => {
let mut n = Note::from_text_file(context, content.unwrap(), template_kind)?;
// Render filename.
n.render_filename(template_kind)?;
// Save new note.
let context_path = n.context.path.clone();
n.set_next_unused_rendered_filename_or(&context_path)?;
n.save_and_delete_from(&context_path)?;
n
}
TemplateKind::SyncFilename => synchronize(context, content.unwrap())?,
TemplateKind::None => Note::from_text_file(context, content.unwrap(), template_kind)?,
};
// Export HTML rendition, if wanted.
if let Some((dir, local_link_kind)) = html_export {
n.export_html(
&LIB_CFG.read().unwrap().tmpl_html.exporter,
dir,
*local_link_kind,
)?;
}
// If no new filename was rendered, return the old one.
let mut n = n;
if n.rendered_filename == PathBuf::new() {
n.rendered_filename = n.context.path.clone();
}
Ok(n.rendered_filename)
}sourcepub fn get_content_template(&self) -> String
pub fn get_content_template(&self) -> String
Returns the content template string as it is defined in the configuration file.
Panics for TemplateKind::SyncFilename and TemplateKind::None.
Examples found in repository?
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
pub fn from_content_template(
mut context: Context,
template_kind: TemplateKind,
) -> Result<Note<T>, NoteError> {
log::trace!(
"Available substitution variables for content template:\n{:#?}",
*context
);
log::trace!(
"Applying content template: {:?}",
template_kind.get_content_template_name()
);
// render template
let content: T = T::from({
let mut tera = Tera::default();
tera.extend(&TERA)?;
// Panics, if the content template does not exist (see contract).
// Returns an error, when the rendition goes wrong.
tera.render_str(&template_kind.get_content_template(), &context)
.map_err(|e| {
note_error_tera_template!(
e,
template_kind.get_content_template_name().to_string()
)
})?
});
log::debug!(
"Rendered content template:\n---\n{}\n---\n{}",
content.header(),
content.body().trim()
);
// deserialize the rendered template
let fm = FrontMatter::try_from_content(&content)?;
context.insert_front_matter(&fm);
// Return new note.
Ok(Note {
context,
content,
rendered_filename: PathBuf::new(),
})
}sourcepub fn get_content_template_name(&self) -> &str
pub fn get_content_template_name(&self) -> &str
Returns the content template variable name as it is used in the configuration file.
Examples found in repository?
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
pub fn from_content_template(
mut context: Context,
template_kind: TemplateKind,
) -> Result<Note<T>, NoteError> {
log::trace!(
"Available substitution variables for content template:\n{:#?}",
*context
);
log::trace!(
"Applying content template: {:?}",
template_kind.get_content_template_name()
);
// render template
let content: T = T::from({
let mut tera = Tera::default();
tera.extend(&TERA)?;
// Panics, if the content template does not exist (see contract).
// Returns an error, when the rendition goes wrong.
tera.render_str(&template_kind.get_content_template(), &context)
.map_err(|e| {
note_error_tera_template!(
e,
template_kind.get_content_template_name().to_string()
)
})?
});
log::debug!(
"Rendered content template:\n---\n{}\n---\n{}",
content.header(),
content.body().trim()
);
// deserialize the rendered template
let fm = FrontMatter::try_from_content(&content)?;
context.insert_front_matter(&fm);
// Return new note.
Ok(Note {
context,
content,
rendered_filename: PathBuf::new(),
})
}sourcepub fn get_filename_template(&self) -> String
pub fn get_filename_template(&self) -> String
Returns the file template string as it is defined in the configuration file.
Panics for TemplateKind::None.
Examples found in repository?
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
pub fn render_filename(&mut self, template_kind: TemplateKind) -> Result<(), NoteError> {
log::trace!(
"Available substitution variables for the filename template:\n{:#?}",
*self.context
);
log::trace!(
"Applying the filename template: {:?}",
template_kind.get_filename_template_name()
);
// render template
let mut file_path = self.context.dir_path.to_owned();
let mut tera = Tera::default();
tera.extend(&TERA)?;
match tera.render_str(&template_kind.get_filename_template(), &self.context) {
Ok(filename) => {
log::debug!("Rendered filename template:\n{:?}", filename.trim());
file_path.push(filename.trim());
}
Err(e) => {
return Err(note_error_tera_template!(
e,
template_kind.get_filename_template_name().to_string()
));
}
}
file_path.shorten_filename();
self.rendered_filename = file_path;
Ok(())
}sourcepub fn get_filename_template_name(&self) -> &str
pub fn get_filename_template_name(&self) -> &str
Returns the content template variable name as it is used in the configuration file.
Examples found in repository?
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
pub fn render_filename(&mut self, template_kind: TemplateKind) -> Result<(), NoteError> {
log::trace!(
"Available substitution variables for the filename template:\n{:#?}",
*self.context
);
log::trace!(
"Applying the filename template: {:?}",
template_kind.get_filename_template_name()
);
// render template
let mut file_path = self.context.dir_path.to_owned();
let mut tera = Tera::default();
tera.extend(&TERA)?;
match tera.render_str(&template_kind.get_filename_template(), &self.context) {
Ok(filename) => {
log::debug!("Rendered filename template:\n{:?}", filename.trim());
file_path.push(filename.trim());
}
Err(e) => {
return Err(note_error_tera_template!(
e,
template_kind.get_filename_template_name().to_string()
));
}
}
file_path.shorten_filename();
self.rendered_filename = file_path;
Ok(())
}Trait Implementations§
source§impl Clone for TemplateKind
impl Clone for TemplateKind
source§fn clone(&self) -> TemplateKind
fn clone(&self) -> TemplateKind
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl Debug for TemplateKind
impl Debug for TemplateKind
source§impl Default for TemplateKind
impl Default for TemplateKind
source§fn default() -> TemplateKind
fn default() -> TemplateKind
source§impl PartialEq<TemplateKind> for TemplateKind
impl PartialEq<TemplateKind> for TemplateKind
source§fn eq(&self, other: &TemplateKind) -> bool
fn eq(&self, other: &TemplateKind) -> bool
self and other values to be equal, and is used
by ==.impl Copy for TemplateKind
impl Eq for TemplateKind
impl StructuralEq for TemplateKind
impl StructuralPartialEq for TemplateKind
Auto Trait Implementations§
impl RefUnwindSafe for TemplateKind
impl Send for TemplateKind
impl Sync for TemplateKind
impl Unpin for TemplateKind
impl UnwindSafe for TemplateKind
Blanket Implementations§
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.