private static string CleanForFS(string str, string uniq) { var fn = ANFilenameHelper.StripStringForFilename(str, '_'); if (string.IsNullOrWhiteSpace(fn)) { fn = ANFilenameHelper.StripStringForFilename(uniq); } return(fn); }
private static List <AugmentedNote> GetNotes(NoteRepository repo) { var result = new List <AugmentedNote>(); var existing = new HashSet <string>(); foreach (var note in repo.Notes.OrderBy(p => p.CreationDate)) { var fn = ANFilenameHelper.ConvertStringForFilename(note.Title); if (string.IsNullOrWhiteSpace(fn)) { fn = ANFilenameHelper.StripStringForFilename(note.UniqueName); } var ext = ".txt"; if (note.HasTagCaseInsensitive(AppSettings.TAG_MARKDOWN)) { ext = ".md"; } var path = Path.Combine(note.Path.Enumerate().Select(c => ANFilenameHelper.StripStringForFilename(c)).ToArray()); var oldfn = fn; int idx = 0; string rpath = ""; for (;;) { fn = (idx == 0) ? oldfn : $"{oldfn}_{idx:000}"; rpath = string.IsNullOrWhiteSpace(path) ? (fn + ext) : Path.Combine(path, fn + ext); if (existing.Add(rpath)) { break; } idx++; } string txt = note.Text; if (fn != note.Title && !string.IsNullOrWhiteSpace(note.Title)) { txt = note.Title + "\n\n" + note.Text; } result.Add(new AugmentedNote() { Note = note, RelativePath = rpath, Content = txt }); } return(result); }
private void ExportNote() { if (SelectedNote == null) { return; } var selection = AllSelectedNotes.ToList(); if (selection.Count > 1) { var dialog = new VistaFolderBrowserDialog(); if (!(dialog.ShowDialog() ?? false)) { return; } try { var directory = dialog.SelectedPath; foreach (var note in selection) { var filenameRaw = ANFilenameHelper.StripStringForFilename(note.Title); var filename = filenameRaw; var ext = SelectedNote.HasTagCaseInsensitive(AppSettings.TAG_MARKDOWN) ? ".md" : ".txt"; int i = 1; while (File.Exists(Path.Combine(directory, filename + ext))) { i++; filename = $"{filenameRaw} ({i})"; } File.WriteAllText(Path.Combine(directory, filename + ext), note.Text, Encoding.UTF8); } } catch (Exception e) { App.Logger.Error("Main", "Could not write to file", e); ExceptionDialog.Show(Owner, "Could not write to file", e, string.Empty); } } else { var sfd = new SaveFileDialog(); if (SelectedNote.HasTagCaseInsensitive(AppSettings.TAG_MARKDOWN)) { sfd.Filter = "Markdown files (*.md)|*.md"; sfd.FileName = SelectedNote.Title + ".md"; } else { sfd.Filter = "Text files (*.txt)|*.txt"; sfd.FileName = SelectedNote.Title + ".txt"; } if (sfd.ShowDialog() != true) { return; } try { File.WriteAllText(sfd.FileName, SelectedNote.Text, Encoding.UTF8); } catch (Exception e) { App.Logger.Error("Main", "Could not write to file", e); ExceptionDialog.Show(Owner, "Could not write to file", e, string.Empty); } } }