1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
226
227
228
229
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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! Creates a memory representations of the note by inserting `tp-note`'s
//! environment data in some templates. If the note exists on disk already,
//! the memory representation is established be reading the note-file with
//! its front matter.

extern crate chrono;
extern crate tera;
extern crate time;

use crate::config::ARGS;
use crate::config::CFG;
use crate::config::CLIPBOARD;
use crate::config::NOTE_FILENAME_LEN_MAX;
use crate::content::Content;
use crate::filter::ContextWrapper;
use crate::filter::TERA;
use anyhow::{anyhow, Context, Result};
use serde::{Deserialize, Serialize};
use std::default::Default;
use std::env;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::{Path, PathBuf};
use tera::Tera;

#[derive(Debug, PartialEq)]
/// Represents a note.
pub struct Note {
    /// The front matter of the note.
    front_matter: Option<FrontMatter>,
    /// Captured environment of `tp-note` that
    /// is used to fill in templates.
    context: ContextWrapper,
    /// The full text content of the note, including
    /// its front matter.
    pub content: Content,
}

#[derive(Debug, PartialEq, Serialize, Deserialize, Default)]
/// Represents the front matter of the note.
struct FrontMatter {
    /// The compulsory note's title.
    title: String,
    /// The compulsory note's subtitle.
    subtitle: String,
}

use std::fs;
impl Note {
    /// Constructor that creates a memory representation of an existing note on
    /// disk.
    pub fn from_existing_note(path: &Path) -> Result<Self> {
        let content = Content::new(
            fs::read_to_string(path)
                .with_context(|| format!("failed to read `{}`", path.display()))?
                .as_str(),
        );
        let fm = Self::deserialize_note(&content)?;

        let mut context = Self::capture_environment(&path)?;

        context.insert("title", &fm.title);
        context.insert("subtitle", &fm.subtitle);

        Ok(Self {
            front_matter: Some(fm),
            context,
            content,
        })
    }

    /// Constructor that creates a new note by filling in the template
    /// `template`.
    pub fn new(path: &Path, template: &str) -> Result<Self> {
        // render template

        // there is no front matter yet to capture
        let mut context = Self::capture_environment(&path)?;

        let content = Content::new({
            let mut tera = Tera::default();
            tera.extend(&TERA).unwrap();

            tera.render_str(template, &context)
                .with_context(|| format!("failed to render template:\n`{}`", template))?
                .as_str()
        });

        if ARGS.debug {
            eprintln!(
                "*** Debug: Available substitution variables for context template:\n{:#?}\n",
                *context
            );
            eprintln!("*** Debug: Applying content template:\n{}\n", template);
            eprintln!("*** Debug: Rendered content template:\n{}\n\n", *content);
        };

        // deserialize the rendered result
        let fm = Note::deserialize_note(&content)?;

        context.insert("title", &fm.title);
        context.insert("subtitle", &fm.subtitle);

        // return result
        Ok(Self {
            front_matter: Some(fm),
            context,
            content,
        })
    }

    /// Capture `tp_note`'s environment and stores it as variables in a
    /// `context` collection. The variables are needed later to populate
    /// a context-template and a filename-template.
    /// The `path` parameter must be a canonicalized fully qualified file name.
    fn capture_environment(path: &Path) -> Result<ContextWrapper> {
        let mut context = ContextWrapper::new();

        let sort_tag: String = path
            .file_stem()
            .unwrap_or_default()
            .to_str()
            .unwrap_or_default()
            .chars()
            .take_while(|&c| c.is_numeric() || c == '-' || c == '_')
            .collect::<String>();
        context.insert("sort_tag", &sort_tag);

        // `fqpn` is a directory as fully qualified path, ending
        // by a separator.
        let fqpn = if path.is_dir() {
            path
        } else {
            path.parent().unwrap_or_else(|| Path::new("./"))
        };
        context.insert("path", &fqpn.to_str().unwrap_or_default());

        // Strip off the sort tag if there is any
        let dirname = fqpn
            .file_name()
            .unwrap_or_default()
            .to_str()
            .unwrap_or_default()
            .trim_start_matches(|c: char| c.is_numeric() || c == '-' || c == '_');
        context.insert("dirname", &dirname);

        // Strip off the sort tag if there is any.
        let file_stem = if path.is_dir() {
            ""
        } else {
            path.file_stem()
                .unwrap_or_default()
                .to_str()
                .unwrap_or_default()
                .trim_start_matches(|c: char| c.is_numeric() || c == '-' || c == '_')
        };
        context.insert("file_stem", &file_stem);

        // Register input from clipboard.
        context.insert("clipboard", &CLIPBOARD.content);
        context.insert("clipboard_truncated", &CLIPBOARD.content_truncated);
        context.insert("clipboard_heading", &CLIPBOARD.content_heading);
        context.insert("clipboard_linkname", &CLIPBOARD.linkname);
        context.insert("clipboard_linkurl", &CLIPBOARD.linkurl);

        // Extension of the path given on command-line.
        context.insert(
            "extension",
            &path
                .extension()
                .unwrap_or_default()
                .to_str()
                .unwrap_or_default(),
        );
        context.insert("note_extension", CFG.note_extension.as_str());

        // search for UNIX, Windows and MacOS user-names
        let author = env::var("LOGNAME").unwrap_or_else(|_| {
            env::var("USERNAME").unwrap_or_else(|_| env::var("USER").unwrap_or_default())
        });
        context.insert("username", &author);

        context.fqpn = fqpn.to_path_buf();

        Ok(context)
    }

    /// Applies a Tera-template to the notes context in order to generate a
    /// sanitized filename that is in sync with the note's meta data stored in
    /// its front matter.
    pub fn render_filename(&self, template: &str) -> Result<PathBuf> {
        if ARGS.debug {
            eprintln!(
                "*** Debug: Available substitution variables for filename template:\n{:#?}\n",
                *self.context
            );
            eprintln!("*** Debug: Applying filename template:\n{}\n\n", template);
        };

        // render template
        let mut fqfn = self.context.fqpn.to_owned();
        fqfn.push({
            let mut tera = Tera::default();
            tera.extend(&TERA).unwrap();

            tera.render_str(template, &self.context)
                .map(|filename| {
                    if ARGS.debug {
                        eprintln!("*** Debug: Rendered filename template:\n{:?}\n\n", filename);
                    };
                    filename
                })
                .with_context(|| format!("failed to render template:\n`{}`", template))?
                .trim()
        });

        Ok(Self::shorten_filename(Path::new(&fqfn)))
    }

    /// Shortens the stem of a filename so that
    /// `file_stem.len()+file_extension.len() <= NOTE_FILENAME_LEN_MAX`
    fn shorten_filename(fqfn: &Path) -> PathBuf {
        let mut parent = if let Some(p) = fqfn.parent() {
            p.to_path_buf()
        } else {
            PathBuf::new()
        };
        // Determine length of file-extension.
        let mut note_extension_len = 0;
        let mut note_extension = "";
        if let Some(ext) = &fqfn.extension() {
            if let Some(ext) = ext.to_str() {
                note_extension_len = ext.len();
                note_extension = ext;
            }
        };

        // Limit length of file-stem.
        let mut note_stem_short = String::new();
        if let Some(note_stem) = &fqfn.file_stem() {
            if let Some(note_stem) = note_stem.to_str() {
                // Limit the size of `fqfn`
                // `+1` reserves one byte for `.` before the extension.
                for i in (0..NOTE_FILENAME_LEN_MAX-(note_extension_len+1)).rev() {
                    if let Some(s) = note_stem.get(..=i) {
                        note_stem_short = s.to_string();
                        break;
                    }
                }
            }
        };

        // Assemble.
        let mut note_filename = note_stem_short;
        note_filename.push('.');
        note_filename.push_str(note_extension);

        // Add to parent.
        parent.push(Path::new(&note_filename).to_path_buf());
        parent
    }

    /// Helper function deserializing the front-matter of an `.md`-file.
    fn deserialize_note(content: &str) -> Result<FrontMatter> {
        // anyhow Error type

        let fm_start = content
            .find("---")
            .context("no YAML front matter start line '---' found")?
            + 3;

        let fm_end = content[fm_start..]
            .find("---\n")
            .unwrap_or_else(|| content[fm_start..].find("...\n").unwrap_or(0))
            + fm_start;

        if fm_start >= fm_end {
            return Err(anyhow!(
                "no YAML front matter end line `---` or `...` found"
            ));
        }

        let fm: FrontMatter = serde_yaml::from_str(&content[fm_start..fm_end])?;
        Ok(fm)
    }

    /// Writes the note to disk with `new_fqfn`-filename.
    pub fn write_to_disk(&self, new_fqfn: &Path) -> Result<PathBuf, anyhow::Error> {
        // Write new note on disk.
        let outfile = OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&new_fqfn);
        match outfile {
            Ok(mut outfile) => {
                println!("Creating file: {:?}", new_fqfn);
                write!(outfile, "{}", &self.content.to_osstring())
                    .with_context(|| format!("can not write new file {:?}", new_fqfn))?
            }
            Err(e) => {
                if Path::new(&new_fqfn).exists() {
                    println!("can not create new file, file exists: {}", e);
                    println!("Instead, try to read existing: {:?}", new_fqfn);
                } else {
                    return Err(anyhow!(format!(
                        "can not write file: {:?}\n{}",
                        new_fqfn, e
                    )));
                }
            }
        }

        Ok(new_fqfn.to_owned())
    }
}

#[cfg(test)]
mod tests {
    use super::FrontMatter;
    use super::Note;

    #[test]
    fn test_shorten_filename() {
        use std::ffi::OsString;
        use std::path::Path;

        // Test short filename.
        let input = Path::new("long directory name/abc.ext");
        let output = Note::shorten_filename(input);
        assert_eq!(OsString::from("long directory name/abc.ext"), output);

        // Test long filename.
        let input = Path::new("long directory name/long filename.ext");
        let output = Note::shorten_filename(input);
        assert_eq!(OsString::from("long directory name/long f.ext"), output);
    }

    #[test]
    fn test_deserialize() {
        let input = "--- # document start
        title: The book
        subtitle: you always wanted
        author: Is's me
        ...\ncontent\nmore content";

        let expected_front_matter = FrontMatter {
            title: "The book".to_string(),
            subtitle: "you always wanted".to_string(),
        };

        assert_eq!(
            expected_front_matter,
            Note::deserialize_note(&input).unwrap()
        );

        // Front matter can also end with '---'

        let input = "--- # document start
        title: \"The book\"
        subtitle: you always wanted
        author: It's me
        ---\ncontent\nmore content";

        let expected_front_matter = FrontMatter {
            title: "The book".to_string(),
            subtitle: "you always wanted".to_string(),
        };

        assert_eq!(
            expected_front_matter,
            Note::deserialize_note(&input).unwrap()
        );

        // Missing start '---'

        let input = "-- # document start
        title: The book
        subtitle: you always wanted
        author: Is's me
        ...\ncontent\nmore content";

        assert!(Note::deserialize_note(&input).is_err());

        // Missing end '...'

        let input = "--- # document start
        title: The book
        subtitle: you always wanted
        author: It's me
        ..\ncontent\nmore content";

        assert!(Note::deserialize_note(&input).is_err());

        // Missing title

        let input = "--- # document start
        titlxxx: The book
        subtitle: you always wanted
        author: It's me
        ...\ncontent\nmore content";

        assert!(Note::deserialize_note(&input).is_err());

        // Missing subtitle

        let input = "--- # document start
        title: The book
        subtitlxxx: you always wanted
        author: It's me
        ...\ncontent\nmore content";

        assert!(Note::deserialize_note(&input).is_err());
    }
}