impr: input parsing
This commit is contained in:
@@ -2,36 +2,26 @@
|
|||||||
|
|
||||||
BEGIN {
|
BEGIN {
|
||||||
FS=":";
|
FS=":";
|
||||||
type = "VJOURNAL";
|
|
||||||
zulu = strftime("%Y%m%dT%H%M%SZ", systime(), 1);
|
zulu = strftime("%Y%m%dT%H%M%SZ", systime(), 1);
|
||||||
}
|
}
|
||||||
desc { desc = desc "\\n" escape($0); next; }
|
desc { desc = desc "\\n" escape($0); next; }
|
||||||
{
|
/^::: \|>/ && !start { gsub("\"", ""); start = substr(zulu, 1, 8); next; }
|
||||||
if (substr($0, 1, 6) == "::: |>")
|
/^::: <\| / && !due { gsub("\"", ""); due = substr($0, 8); next; }
|
||||||
{
|
/^# / && !summary { summary = escape(substr($0, 3)); next; }
|
||||||
start = substr(zulu, 1, 8);
|
/^> / && !categories { categories = escape_but_commas(substr($0, 3)); next; }
|
||||||
getline;
|
!$0 && !el { el = 1; next; }
|
||||||
}
|
!el { print "Unrecognized header on line "NR": " $0 > "/dev/stderr"; exit 1; }
|
||||||
if (substr($0, 1, 6) == "::: <|")
|
{ desc = "D" escape($0); next; }
|
||||||
{
|
|
||||||
type = "VTODO"
|
|
||||||
due = substr($0, 8);
|
|
||||||
getline;
|
|
||||||
}
|
|
||||||
summary = substr($0, 1, 2) != "# " ? "" : escape(substr($0, 3));
|
|
||||||
getline;
|
|
||||||
categories = substr($0, 1, 1) != ">" ? "" : escape_but_commas(substr($0, 3));
|
|
||||||
getline; # This line should be empty
|
|
||||||
getline; # First line of description
|
|
||||||
desc = "D" escape($0);
|
|
||||||
next;
|
|
||||||
}
|
|
||||||
END {
|
END {
|
||||||
# Sanitize input
|
# Sanitize input
|
||||||
|
type = due ? "VTODO" : "VJOURNAL"
|
||||||
if (due) {
|
if (due) {
|
||||||
# Use command line `date` for parsing
|
# Use command line `date` for parsing
|
||||||
cmd = "date -d \"" due "\" +\"%Y%m%d\"";
|
cmd = "date -d \"" due "\" +\"%Y%m%d\"";
|
||||||
cmd | getline res
|
suc = cmd | getline res
|
||||||
|
close(cmd)
|
||||||
|
if (suc != 1)
|
||||||
|
exit 1
|
||||||
due = res ? res : ""
|
due = res ? res : ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,39 +6,30 @@ BEGIN {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ENDFILE {
|
ENDFILE {
|
||||||
if (NR == FNR)
|
if (NR == FNR && due) {
|
||||||
{
|
|
||||||
# Sanitize input
|
|
||||||
if (due) {
|
|
||||||
# Use command line `date` for parsing
|
# Use command line `date` for parsing
|
||||||
cmd = "date -d \"" due "\" +\"%Y%m%d\"";
|
cmd = "date -d \"" due "\" +\"%Y%m%d\"";
|
||||||
cmd | getline res
|
suc = cmd | getline res
|
||||||
|
close(cmd)
|
||||||
|
if (suc != 1)
|
||||||
|
exit 1
|
||||||
due = res ? res : ""
|
due = res ? res : ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
NR == FNR && desc { desc = desc "\\n" escape($0); next; }
|
NR == FNR && desc { desc = desc "\\n" escape($0); next; }
|
||||||
NR == FNR {
|
NR == FNR && /^::: <\| / && !due { gsub("\"",""); due = substr($0, 8); next; }
|
||||||
if (substr($0, 1, 6) == "::: <|")
|
NR == FNR && /^# / && !summary { summary = escape(substr($0, 3)); next; }
|
||||||
{
|
NR == FNR && /^> / && !categories { categories = escape_but_commas(substr($0, 3)); next; }
|
||||||
due = substr($0, 8);
|
NR == FNR && !$0 && !el { el = 1; next; }
|
||||||
getline;
|
NR == FNR && !el { print "Unrecognized header on line "NR": " $0 > "/dev/stderr"; exit 1; }
|
||||||
}
|
NR == FNR { desc = "D" escape($0); next; }
|
||||||
summary = substr($0, 1, 2) != "# " ? "" : escape(substr($0, 3));
|
due && type == "VJOURNAL" { print "Notes and journal entries do not have due dates." > "/dev/stderr"; exit 1; }
|
||||||
getline;
|
/^BEGIN:(VJOURNAL|VTODO)/ { type = $2; print; next; }
|
||||||
categories = substr($0, 1, 1) != ">" ? "" : escape_but_commas(substr($0, 3));
|
/^X-ALT-DESC/ && type { next; } # drop this alternative description
|
||||||
getline; # This line should be empty
|
/^ / && type { next; } # drop this folded line (the only content with folded lines will be updated)
|
||||||
getline; # First line of description
|
/^(DUE|SUMMARY|CATEGORIES|DESCRIPTION|LAST-MODIFIED)/ && type { next; } # skip for now, we will write updated fields at the end
|
||||||
desc = "D" escape($0);
|
/^SEQUENCE/ && type { seq = $2; next; } # store sequence number and skip
|
||||||
next;
|
|
||||||
}
|
|
||||||
|
|
||||||
/^BEGIN:(VJOURNAL|VTODO)/ { type = $2; print; next }
|
|
||||||
/^X-ALT-DESC/ && type { next } # drop this alternative description
|
|
||||||
/^ / && type { next } # drop this folded line (the only content with folded lines will be updated)
|
|
||||||
/^(DUE|SUMMARY|CATEGORIES|DESCRIPTION|LAST-MODIFIED)/ && type { next } # skip for now, we will write updated fields at the end
|
|
||||||
/^SEQUENCE/ && type { seq = $2; next } # store sequence number and skip
|
|
||||||
/^END:/ && type == $2 {
|
/^END:/ && type == $2 {
|
||||||
seq = seq ? seq + 1 : 1;
|
seq = seq ? seq + 1 : 1;
|
||||||
print "SEQUENCE:" seq;
|
print "SEQUENCE:" seq;
|
||||||
|
|||||||
@@ -41,24 +41,32 @@ __change_priority() {
|
|||||||
__edit() {
|
__edit() {
|
||||||
file="$1"
|
file="$1"
|
||||||
shift
|
shift
|
||||||
filetmp=$(mktemp --suffix='.md')
|
tmpmd=$(mktemp --suffix='.md')
|
||||||
awk "$AWK_EXPORT" "$file" >"$filetmp"
|
awk "$AWK_EXPORT" "$file" >"$tmpmd"
|
||||||
checksum=$(cksum "$filetmp")
|
checksum=$(cksum "$tmpmd")
|
||||||
|
|
||||||
# Open in editor
|
# Open in editor
|
||||||
$EDITOR "$filetmp" >/dev/tty
|
$EDITOR "$tmpmd" >/dev/tty
|
||||||
|
|
||||||
# Update only if changes are detected
|
# Update only if changes are detected
|
||||||
if [ "$checksum" != "$(cksum "$filetmp")" ]; then
|
while [ "$checksum" != "$(cksum "$tmpmd")" ]; do
|
||||||
file_new="$filetmp.ics"
|
tmpfile="$tmpmd.ics"
|
||||||
awk "$AWK_UPDATE" "$filetmp" "$file" >"$file_new"
|
if awk "$AWK_UPDATE" "$tmpmd" "$file" >"$tmpfile"; then
|
||||||
mv "$file_new" "$file"
|
mv "$tmpfile" "$file"
|
||||||
if [ -n "${GIT:-}" ]; then
|
if [ -n "${GIT:-}" ]; then
|
||||||
$GIT add "$file"
|
$GIT add "$file"
|
||||||
$GIT commit -q -m "File modified" -- "$file"
|
$GIT commit -q -m "File modified" -- "$file"
|
||||||
fi
|
fi
|
||||||
|
break
|
||||||
|
else
|
||||||
|
rm -f "$tmpfile"
|
||||||
|
err "Failed to update entry. Press <enter> to continue."
|
||||||
|
read -r tmp
|
||||||
|
# Re-open in editor
|
||||||
|
$EDITOR "$tmpmd" >/dev/tty
|
||||||
fi
|
fi
|
||||||
rm "$filetmp"
|
done
|
||||||
|
rm "$tmpmd"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Delete file
|
# Delete file
|
||||||
@@ -112,14 +120,22 @@ __new() {
|
|||||||
$EDITOR "$tmpmd" >/dev/tty
|
$EDITOR "$tmpmd" >/dev/tty
|
||||||
|
|
||||||
# Update if changes are detected
|
# Update if changes are detected
|
||||||
if [ "$checksum" != "$(cksum "$tmpmd")" ]; then
|
while [ "$checksum" != "$(cksum "$tmpmd")" ]; do
|
||||||
tmpfile="$tmpmd.ics"
|
tmpfile="$tmpmd.ics"
|
||||||
awk -v uid="$uuid" "$AWK_NEW" "$tmpmd" >"$tmpfile"
|
if awk -v uid="$uuid" "$AWK_NEW" "$tmpmd" >"$tmpfile"; then
|
||||||
mv "$tmpfile" "$file"
|
mv "$tmpfile" "$file"
|
||||||
if [ -n "${GIT:-}" ]; then
|
if [ -n "${GIT:-}" ]; then
|
||||||
$GIT add "$file"
|
$GIT add "$file"
|
||||||
$GIT commit -q -m "File added" -- "$file"
|
$GIT commit -q -m "File added" -- "$file"
|
||||||
fi
|
fi
|
||||||
|
break
|
||||||
|
else
|
||||||
|
rm -f "$tmpfile"
|
||||||
|
err "Failed to create new entry. Press <enter> to continue."
|
||||||
|
read -r tmp
|
||||||
|
# Re-open in editor
|
||||||
|
$EDITOR "$tmpmd" >/dev/tty
|
||||||
fi
|
fi
|
||||||
|
done
|
||||||
rm "$tmpmd"
|
rm "$tmpmd"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user