Allow to import notes without headers

This commit is contained in:
Konstantin Nazarov 2021-05-29 19:34:18 +00:00
parent dfe6ff0c69
commit 2cd09b260d
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
2 changed files with 72 additions and 3 deletions

View file

@ -121,6 +121,7 @@ get_headers() {
FILTER="\ FILTER="\
{ \ { \
if (\$0~/^$/) {exit} \ if (\$0~/^$/) {exit} \
if (\$0!~/^[^ ]*: .*$/) {exit} \
print \$0 \ print \$0 \
} \ } \
" "
@ -132,8 +133,14 @@ get_body() {
FILTER="\ FILTER="\
{ \ { \
if (body==1) {print \$0}\ if (body==1) {\
print \$0;
}\
if (\$0~/^$/) {body=1} \ if (\$0~/^$/) {body=1} \
if (\$0!~/^[^ ]*: .*$/ && body != 1) {\
body=1;
print \$0;
} \
} \ } \
" "
awk "$FILTER" "$BODY_FILE" awk "$FILTER" "$BODY_FILE"
@ -323,13 +330,21 @@ new_entry() {
fi fi
if [ -f "$INP" ]; then if [ -f "$INP" ]; then
cp "$INP" "$DIR/note.md" {
get_headers "$INP"
echo ""
get_body "$INP"
} >> "$DIR/note.md"
elif [ -d "$INP" ]; then elif [ -d "$INP" ]; then
if [ ! -f "$INP/note.md" ]; then if [ ! -f "$INP/note.md" ]; then
die "File doesn't exist: $INP/note.md" die "File doesn't exist: $INP/note.md"
fi fi
cp -n "$INP"/* "$DIR/" || true cp -n "$INP"/* "$DIR/" || true
cat "$INP/note.md" >> "$DIR/note.md" {
get_headers "$INP/note.md"
echo ""
get_body "$INP/note.md"
} >> "$DIR/note.md"
elif [ -t 0 ]; then elif [ -t 0 ]; then
"$EDITOR" "$ENTRY_FILE" "$EDITOR" "$ENTRY_FILE"
else else

54
test.sh
View file

@ -292,6 +292,57 @@ pack_multipart_binary() {
assert 'echo "$OUTPUT"' "$EXPECTED" assert 'echo "$OUTPUT"' "$EXPECTED"
} }
existing_headers() {
mkdir "$TMP/inpdir"
NOTE_ID="6e50650a-88d1-49a3-92eb-0ec329e6f6f8"
DATE="2021-05-29T18:47:34Z"
cat > "$TMP/inpdir/note.md" <<- EOF
X-Note-Id: $NOTE_ID
X-Date: $DATE
X-Custom: value
Subject: This is a subject
# This is a body
EOF
"$BASE_DIR/notes.sh" -n "$TMP/inpdir"
OUTPUT="$(cat "$(pwd)/notes/cur"/*)"
assert 'echo "$OUTPUT" | grep Subject' "Subject: This is a subject"
assert 'echo "$OUTPUT" | grep X-Note-Id' "X-Note-Id: $NOTE_ID"
assert 'echo "$OUTPUT" | grep X-Date' "X-Date: $DATE"
assert 'echo "$OUTPUT" | grep X-Custom' "X-Custom: value"
}
no_headers() {
cat > "$TMP/input.md" <<- EOF
This is a body
EOF
"$BASE_DIR/notes.sh" -n "$TMP/input.md"
OUTPUT="$(cat "$(pwd)/notes/cur"/*)"
assert 'echo "$OUTPUT" | grep Subject' "Subject: "
assert 'echo "$OUTPUT" | grep "This is a body"' "This is a body"
}
no_headers_dir() {
mkdir "$TMP/inpdir"
cat > "$TMP/inpdir/note.md" <<- EOF
This is a body
EOF
"$BASE_DIR/notes.sh" -n "$TMP/inpdir"
OUTPUT="$(cat "$(pwd)/notes/cur"/*)"
assert 'echo "$OUTPUT" | grep Subject' "Subject: "
assert 'echo "$OUTPUT" | grep "This is a body"' "This is a body"
}
testcase new_note_from_stdin testcase new_note_from_stdin
testcase new_note_from_file testcase new_note_from_file
testcase new_note_from_dir testcase new_note_from_dir
@ -301,6 +352,9 @@ testcase edit_note
testcase resume_editing testcase resume_editing
testcase pack_multipart testcase pack_multipart
testcase pack_multipart_binary testcase pack_multipart_binary
testcase existing_headers
testcase no_headers
testcase no_headers_dir
if [[ "$RESULT" == "0" ]]; then if [[ "$RESULT" == "0" ]]; then
echo "All tests passed." echo "All tests passed."