pub fn create_new_note_or_synchronize_filename<T, F>(
path: &Path,
clipboard: &T,
stdin: &T,
tk_filter: F,
args_export: Option<&Path>
) -> Result<PathBuf, NoteError>where
T: Content,
F: Fn(TemplateKind) -> TemplateKind,Expand description
Create a new note by inserting Tp-Note’s environment in a template.
If the note to be created exists already, append a so called copy_counter
to the filename and try to save it again. In case this does not succeed either,
increment the copy_counter until a free filename is found.
The returned path points to the (new) note file on disk.
Depending on the context, Tp-Note chooses one TemplateKind to operate
(c.f. tpnote_lib::template::TemplateKind::from()).
The tk-filter allows to overwrite this choice, e.g. you may set
TemplateKind::None under certain circumstances. This way the caller
can inject command line parameters like --no-filename-sync.
Returns the note’s new or existing filename in <Note>.rendered_filename.
Example with TemplateKind::FromClipboard
use tpnote_lib::content::Content;
use tpnote_lib::content::ContentString;
use tpnote_lib::workflow::create_new_note_or_synchronize_filename;
use std::env::temp_dir;
use std::path::PathBuf;
use std::fs;
// Prepare test.
let notedir = temp_dir();
let clipboard = ContentString::from("my clipboard\n".to_string());
let stdin = ContentString::from("my stdin\n".to_string());
// This is the condition to choose: `TemplateKind::FromClipboard`:
assert!(clipboard.header().is_empty() && stdin.header().is_empty());
assert!(!clipboard.body().is_empty() || !stdin.body().is_empty());
let template_kind_filer = |tk|tk;
// Start test.
// You can plug in your own type (must impl. `Content`).
let n = create_new_note_or_synchronize_filename::<ContentString, _>(
¬edir, &clipboard, &stdin, template_kind_filer, None).unwrap();
// Check result.
assert!(n.as_os_str().to_str().unwrap()
.contains("my stdin-my clipboard--Note"));
assert!(n.is_file());
let raw_note = fs::read_to_string(n).unwrap();
#[cfg(not(target_family = "windows"))]
assert!(raw_note.starts_with(
"\u{feff}---\ntitle: \"my stdin\\nmy clipboard\\n\""));
#[cfg(target_family = "windows")]
assert!(raw_note.starts_with(
"\u{feff}---\r\ntitle: \"my stdin"));