impr: input parsing

This commit is contained in:
2025-07-04 10:59:21 +02:00
parent e1d6b2b08e
commit e80d9deb79
3 changed files with 70 additions and 73 deletions

View File

@@ -2,36 +2,26 @@
BEGIN {
FS=":";
type = "VJOURNAL";
zulu = strftime("%Y%m%dT%H%M%SZ", systime(), 1);
}
desc { desc = desc "\\n" escape($0); next; }
{
if (substr($0, 1, 6) == "::: |>")
{
start = substr(zulu, 1, 8);
getline;
}
if (substr($0, 1, 6) == "::: <|")
{
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;
}
desc { desc = desc "\\n" escape($0); next; }
/^::: \|>/ && !start { gsub("\"", ""); start = substr(zulu, 1, 8); next; }
/^::: <\| / && !due { gsub("\"", ""); due = substr($0, 8); next; }
/^# / && !summary { summary = escape(substr($0, 3)); next; }
/^> / && !categories { categories = escape_but_commas(substr($0, 3)); next; }
!$0 && !el { el = 1; next; }
!el { print "Unrecognized header on line "NR": " $0 > "/dev/stderr"; exit 1; }
{ desc = "D" escape($0); next; }
END {
# Sanitize input
type = due ? "VTODO" : "VJOURNAL"
if (due) {
# Use command line `date` for parsing
cmd = "date -d \"" due "\" +\"%Y%m%d\"";
cmd | getline res
suc = cmd | getline res
close(cmd)
if (suc != 1)
exit 1
due = res ? res : ""
}

View File

@@ -6,39 +6,30 @@ BEGIN {
}
ENDFILE {
if (NR == FNR)
{
# Sanitize input
if (due) {
# Use command line `date` for parsing
cmd = "date -d \"" due "\" +\"%Y%m%d\"";
cmd | getline res
due = res ? res : ""
}
if (NR == FNR && due) {
# Use command line `date` for parsing
cmd = "date -d \"" due "\" +\"%Y%m%d\"";
suc = cmd | getline res
close(cmd)
if (suc != 1)
exit 1
due = res ? res : ""
}
}
NR == FNR && desc { desc = desc "\\n" escape($0); next; }
NR == FNR {
if (substr($0, 1, 6) == "::: <|")
{
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;
}
/^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
NR == FNR && desc { desc = desc "\\n" escape($0); next; }
NR == FNR && /^::: <\| / && !due { gsub("\"",""); due = substr($0, 8); next; }
NR == FNR && /^# / && !summary { summary = escape(substr($0, 3)); next; }
NR == FNR && /^> / && !categories { categories = escape_but_commas(substr($0, 3)); next; }
NR == FNR && !$0 && !el { el = 1; next; }
NR == FNR && !el { print "Unrecognized header on line "NR": " $0 > "/dev/stderr"; exit 1; }
NR == FNR { desc = "D" escape($0); next; }
due && type == "VJOURNAL" { print "Notes and journal entries do not have due dates." > "/dev/stderr"; exit 1; }
/^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 {
seq = seq ? seq + 1 : 1;
print "SEQUENCE:" seq;