1
0
mirror of https://github.com/GitJournal/GitJournal.git synced 2025-08-06 15:21:21 +08:00

Add basic txt file support

With this 'txt' files are now read and written. However, they don't have
any metadata so they don't show up correctly in the sort order and
currently the Markdown Editor is still available for them.

Related to 
This commit is contained in:
Vishesh Handa
2020-06-02 00:15:18 +02:00
parent f2d37f2647
commit 42d6386096
4 changed files with 116 additions and 17 deletions

@ -3,6 +3,7 @@ import 'dart:io';
import 'package:gitjournal/core/md_yaml_doc_loader.dart';
import 'package:gitjournal/core/note_notifier.dart';
import 'package:gitjournal/core/notes_folder_fs.dart';
import 'package:gitjournal/error_reporting.dart';
import 'package:gitjournal/settings.dart';
import 'package:gitjournal/utils/markdown.dart';
import 'package:gitjournal/utils/logger.dart';
@ -22,6 +23,7 @@ enum NoteLoadState {
Loading,
Loaded,
NotExists,
Error,
}
enum NoteType {
@ -30,6 +32,11 @@ enum NoteType {
Journal,
}
enum NoteFileFormat {
Markdown,
Txt,
}
class Note with NotesNotifier {
NotesFolderFS parent;
String _filePath;
@ -41,6 +48,8 @@ class Note with NotesNotifier {
NoteType _type = NoteType.Unknown;
Set<String> _tags = {};
NoteFileFormat _fileFormat;
MdYamlDoc _data = MdYamlDoc();
NoteSerializer noteSerializer = NoteSerializer();
@ -65,8 +74,17 @@ class Note with NotesNotifier {
String get filePath {
if (_filePath == null) {
_filePath = p.join(parent.folderPath, _buildFileName());
if (!_filePath.toLowerCase().endsWith('.md')) {
_filePath += '.md';
switch (_fileFormat) {
case NoteFileFormat.Markdown:
if (!_filePath.toLowerCase().endsWith('.md')) {
_filePath += '.md';
}
break;
case NoteFileFormat.Txt:
if (!_filePath.toLowerCase().endsWith('.txt')) {
_filePath += '.txt';
}
break;
}
}
@ -154,6 +172,9 @@ class Note with NotesNotifier {
}
bool get canHaveMetadata {
if (_fileFormat == NoteFileFormat.Txt) {
return false;
}
return Settings.instance.yamlHeaderEnabled;
}
@ -199,20 +220,49 @@ class Note with NotesNotifier {
if (this.fileLastModified == fileLastModified) {
return _loadState;
}
} on FileSystemException catch (e) {
if (e.osError.errorCode == 2 /* File Not Found */) {
this.fileLastModified = fileLastModified;
} catch (e, stackTrace) {
if (e is FileSystemException &&
e.osError.errorCode == 2 /* File Not Found */) {
_loadState = NoteLoadState.NotExists;
_notifyModified();
return _loadState;
}
logExceptionWarning(e, stackTrace);
_loadState = NoteLoadState.Error;
_notifyModified();
return _loadState;
}
Log.d("Note modified: $_filePath");
}
try {
data = await _mdYamlDocLoader.loadDoc(_filePath);
} on MdYamlDocNotFoundException catch (_) {
_loadState = NoteLoadState.NotExists;
var fpLowerCase = _filePath.toLowerCase();
var isMarkdown = fpLowerCase.endsWith('.md');
var isTxt = fpLowerCase.endsWith('.txt');
if (isMarkdown) {
try {
data = await _mdYamlDocLoader.loadDoc(_filePath);
_fileFormat = NoteFileFormat.Markdown;
} on MdYamlDocNotFoundException catch (_) {
_loadState = NoteLoadState.NotExists;
_notifyModified();
return _loadState;
}
} else if (isTxt) {
try {
body = await File(_filePath).readAsString();
_fileFormat = NoteFileFormat.Txt;
} catch (e, stackTrace) {
logExceptionWarning(e, stackTrace);
_loadState = NoteLoadState.Error;
_notifyModified();
return _loadState;
}
} else {
_loadState = NoteLoadState.Error;
_notifyModified();
return _loadState;
}
@ -246,8 +296,18 @@ class Note with NotesNotifier {
void rename(String newName) {
// Do not let the user rename it to a non-markdown file
if (!newName.toLowerCase().endsWith('.md')) {
newName += '.md';
switch (_fileFormat) {
case NoteFileFormat.Markdown:
if (!newName.toLowerCase().endsWith('.md')) {
newName += '.md';
}
break;
case NoteFileFormat.Txt:
if (!newName.toLowerCase().endsWith('.txt')) {
newName += '.txt';
}
break;
}
var oldFilePath = filePath;
@ -414,6 +474,10 @@ class Note with NotesNotifier {
List<Link> links() {
return _links;
}
NoteFileFormat get fileFormat {
return _fileFormat;
}
}
String buildTitleFileName(String parentDir, String title) {

@ -214,10 +214,13 @@ class NotesFolderFS with NotesFolderNotifier implements NotesFolder {
});
continue;
}
if (!note.filePath.toLowerCase().endsWith('.md')) {
var noteFilePath = note.filePath.toLowerCase();
var isMarkdownFile = noteFilePath.endsWith('.md');
var isTxtFile = noteFilePath.endsWith('.txt');
if (!isMarkdownFile && !isTxtFile) {
Log.v("Ignoring file", props: {
"path": fsEntity.path,
"reason": "Doesn't end with a .md",
"reason": "Doesn't end with .md or .txt",
});
continue;
}

@ -8,5 +8,6 @@ Future<void> main() async {
};
final filename = 'lib/.env.dart';
File(filename).writeAsString('final environment = ${json.encode(config)};');
await File(filename)
.writeAsString('final environment = ${json.encode(config)};');
}

@ -26,7 +26,7 @@ modified: 2017-02-15T22:41:19+01:00
Hello""";
var notePath = p.join(tempDir.path, "note.md");
File(notePath).writeAsString(content);
await File(notePath).writeAsString(content);
var parentFolder = NotesFolderFS(null, tempDir.path);
var note = Note(parentFolder, notePath);
@ -56,7 +56,7 @@ mod: 2017-02-15T22:41:19+01:00
Hello""";
var notePath = p.join(tempDir.path, "note.md");
File(notePath).writeAsString(content);
await File(notePath).writeAsString(content);
var parentFolder = NotesFolderFS(null, tempDir.path);
var note = Note(parentFolder, notePath);
@ -86,7 +86,7 @@ tags: [A, B]
Hello""";
var notePath = p.join(tempDir.path, "note5.md");
File(notePath).writeAsString(content);
await File(notePath).writeAsString(content);
var parentFolder = NotesFolderFS(null, tempDir.path);
var note = Note(parentFolder, notePath);
@ -124,7 +124,7 @@ title: Foo
""";
var notePath = p.join(tempDir.path, "note6.md");
File(notePath).writeAsString(content);
await File(notePath).writeAsString(content);
var parentFolder = NotesFolderFS(null, tempDir.path);
var note = Note(parentFolder, notePath);
@ -139,5 +139,36 @@ title: Foo
expect(links.length, 2);
});
test('Should detect file format', () async {
var content = """---
title: Foo
---
Gee
""";
var notePath = p.join(tempDir.path, "note16.md");
await File(notePath).writeAsString(content);
var parentFolder = NotesFolderFS(null, tempDir.path);
var note = Note(parentFolder, notePath);
await note.load();
expect(note.fileFormat, NoteFileFormat.Markdown);
//
// Txt files
//
var txtNotePath = p.join(tempDir.path, "note16.txt");
await File(txtNotePath).writeAsString(content);
var txtNote = Note(parentFolder, txtNotePath);
await txtNote.load();
expect(txtNote.fileFormat, NoteFileFormat.Txt);
expect(txtNote.canHaveMetadata, false);
expect(txtNote.title.isEmpty, true);
});
});
}