pub fn create_new_note_or_synchronize_filename<T, F>(
path: &Path,
clipboard: &T,
stdin: &T,
tk_filter: F,
html_export: &Option<(PathBuf, LocalLinkKind)>,
force_lang: Option<String>
) -> 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.
If html_export = Some((dir, local_link_kind)), the function acts like
like described above, but in addition it renders
the note’s content into HTML and saves the .html file in the
directory dir. This optional HTML rendition is performed just before
returning and does not affect any above described operation.
force_lang disables the automatic language detection and uses force_lang
instead; or, if - use the environment variable TPNOTE_LANG or, - if not
defined - use the user’s default language as reported from the operating
system.
Returns the note’s new or existing filename. Repeated calls, will reload the environment variables, but not the configuration file. This function is stateless.
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_filter = |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_filter,
&None, 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: |\n my stdin\n my clipboard"));
#[cfg(target_family = "windows")]
assert!(raw_note.starts_with(
"\u{feff}---\r\ntitle: |"));