#[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
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

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§

Constructor encoding the logic under what circumstances what template should be used.

Examples found in repository?
src/workflow.rs (line 276)
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)
}

Returns the content template string as it is defined in the configuration file. Panics for TemplateKind::SyncFilename and TemplateKind::None.

Examples found in repository?
src/note.rs (line 199)
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(),
        })
    }

Returns the content template variable name as it is used in the configuration file.

Examples found in repository?
src/note.rs (line 188)
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(),
        })
    }

Returns the file template string as it is defined in the configuration file. Panics for TemplateKind::None.

Examples found in repository?
src/note.rs (line 245)
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(())
    }

Returns the content template variable name as it is used in the configuration file.

Examples found in repository?
src/note.rs (line 237)
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§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

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
Compare self to key and return true if they are equal.

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.