feat: add title and description fields to capture grammar
Implement | prefix for titles and // separator for descriptions across the full stack: parser, schema, API, CLI, and web frontend. - Parser: line-aware extraction for |title, |title // desc, // leading desc, body // inline desc. URL-safe (skips :// lines). Modifiers (#tag, @time, ^card) extracted from all segments. - Schema: ALTER TABLE migration adds title, description columns - DB: Entity/EntityUpdate structs, all CRUD queries updated - API: title/description on create/update/response, body validation relaxed (title-only entries valid) - CLI: shows title as scan label when present - Web: parseInput mirrors Go parser, list shows title, detail pane renders title + description with double-click inline editing - Tests: 10 new cases (grammar, entity, API) — 71 total, all pass
This commit is contained in:
+136
-56
@@ -7,11 +7,13 @@ import (
|
||||
)
|
||||
|
||||
type Result struct {
|
||||
Body string
|
||||
Glyph string
|
||||
TimeAnchor *string
|
||||
Tags []string
|
||||
CardSuffix *string
|
||||
Body string
|
||||
Glyph string
|
||||
Title *string
|
||||
Description *string
|
||||
TimeAnchor *string
|
||||
Tags []string
|
||||
CardSuffix *string
|
||||
}
|
||||
|
||||
var validCardTypes = map[string]string{
|
||||
@@ -35,61 +37,139 @@ func Parse(input string) (*Result, error) {
|
||||
Tags: []string{},
|
||||
}
|
||||
|
||||
tokens := strings.Fields(input)
|
||||
if len(tokens) == 0 {
|
||||
return nil, fmt.Errorf("empty input")
|
||||
}
|
||||
remaining := input
|
||||
|
||||
first := tokens[0]
|
||||
switch first {
|
||||
case "-", "▸":
|
||||
r.Glyph = "todo"
|
||||
tokens = tokens[1:]
|
||||
case "*", "◇":
|
||||
r.Glyph = "event"
|
||||
tokens = tokens[1:]
|
||||
}
|
||||
|
||||
var bodyParts []string
|
||||
seen := map[string]bool{}
|
||||
|
||||
for _, tok := range tokens {
|
||||
switch {
|
||||
case strings.HasPrefix(tok, "@") && len(tok) > 1:
|
||||
timeStr := tok[1:]
|
||||
if err := validateTime(timeStr); err != nil {
|
||||
return nil, fmt.Errorf("invalid time %q: %w", timeStr, err)
|
||||
}
|
||||
if r.TimeAnchor != nil {
|
||||
return nil, fmt.Errorf("multiple time anchors")
|
||||
}
|
||||
r.TimeAnchor = &timeStr
|
||||
|
||||
case strings.HasPrefix(tok, "#") && len(tok) > 1:
|
||||
tag := tok[1:]
|
||||
if !seen[tag] {
|
||||
r.Tags = append(r.Tags, tag)
|
||||
seen[tag] = true
|
||||
}
|
||||
|
||||
case strings.HasPrefix(tok, "^") && len(tok) > 1:
|
||||
suffix := tok[1:]
|
||||
cardType, ok := validCardTypes[suffix]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid card type %q", suffix)
|
||||
}
|
||||
if r.CardSuffix != nil {
|
||||
return nil, fmt.Errorf("multiple card suffixes")
|
||||
}
|
||||
r.CardSuffix = &cardType
|
||||
|
||||
default:
|
||||
bodyParts = append(bodyParts, tok)
|
||||
if sp := strings.IndexByte(remaining, ' '); sp >= 0 {
|
||||
switch remaining[:sp] {
|
||||
case "-", "▸":
|
||||
r.Glyph = "todo"
|
||||
remaining = strings.TrimSpace(remaining[sp+1:])
|
||||
case "*", "◇":
|
||||
r.Glyph = "event"
|
||||
remaining = strings.TrimSpace(remaining[sp+1:])
|
||||
}
|
||||
} else {
|
||||
switch remaining {
|
||||
case "-", "▸":
|
||||
r.Glyph = "todo"
|
||||
remaining = ""
|
||||
case "*", "◇":
|
||||
r.Glyph = "event"
|
||||
remaining = ""
|
||||
}
|
||||
}
|
||||
|
||||
r.Body = strings.Join(bodyParts, " ")
|
||||
if r.Body == "" {
|
||||
var titleRaw, descRaw string
|
||||
hasTitle := false
|
||||
|
||||
lines := strings.SplitN(remaining, "\n", 2)
|
||||
firstLine := strings.TrimSpace(lines[0])
|
||||
|
||||
if strings.HasPrefix(firstLine, "|") {
|
||||
hasTitle = true
|
||||
titleContent := firstLine[1:]
|
||||
if idx := strings.Index(titleContent, " // "); idx >= 0 {
|
||||
titleRaw = strings.TrimSpace(titleContent[:idx])
|
||||
descRaw = strings.TrimSpace(titleContent[idx+4:])
|
||||
} else {
|
||||
titleRaw = strings.TrimSpace(titleContent)
|
||||
}
|
||||
if len(lines) > 1 {
|
||||
remaining = lines[1]
|
||||
} else {
|
||||
remaining = ""
|
||||
}
|
||||
} else {
|
||||
allLines := strings.Split(remaining, "\n")
|
||||
var descParts []string
|
||||
startBody := 0
|
||||
for i, line := range allLines {
|
||||
trimmed := strings.TrimSpace(line)
|
||||
if strings.HasPrefix(trimmed, "// ") || trimmed == "//" {
|
||||
descParts = append(descParts, strings.TrimSpace(trimmed[2:]))
|
||||
startBody = i + 1
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(descParts) > 0 {
|
||||
descRaw = strings.Join(descParts, " ")
|
||||
remaining = strings.Join(allLines[startBody:], "\n")
|
||||
} else if !strings.Contains(firstLine, "://") {
|
||||
if idx := strings.Index(firstLine, " // "); idx >= 0 {
|
||||
descRaw = strings.TrimSpace(firstLine[idx+4:])
|
||||
remaining = strings.TrimSpace(firstLine[:idx])
|
||||
if len(lines) > 1 {
|
||||
remaining += "\n" + lines[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
seen := map[string]bool{}
|
||||
extract := func(text string) (string, error) {
|
||||
tokens := strings.Fields(text)
|
||||
var parts []string
|
||||
for _, tok := range tokens {
|
||||
switch {
|
||||
case strings.HasPrefix(tok, "@") && len(tok) > 1:
|
||||
timeStr := tok[1:]
|
||||
if err := validateTime(timeStr); err != nil {
|
||||
return "", fmt.Errorf("invalid time %q: %w", timeStr, err)
|
||||
}
|
||||
if r.TimeAnchor != nil {
|
||||
return "", fmt.Errorf("multiple time anchors")
|
||||
}
|
||||
r.TimeAnchor = &timeStr
|
||||
case strings.HasPrefix(tok, "#") && len(tok) > 1:
|
||||
tag := tok[1:]
|
||||
if !seen[tag] {
|
||||
r.Tags = append(r.Tags, tag)
|
||||
seen[tag] = true
|
||||
}
|
||||
case strings.HasPrefix(tok, "^") && len(tok) > 1:
|
||||
suffix := tok[1:]
|
||||
cardType, ok := validCardTypes[suffix]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("invalid card type %q", suffix)
|
||||
}
|
||||
if r.CardSuffix != nil {
|
||||
return "", fmt.Errorf("multiple card suffixes")
|
||||
}
|
||||
r.CardSuffix = &cardType
|
||||
default:
|
||||
parts = append(parts, tok)
|
||||
}
|
||||
}
|
||||
return strings.Join(parts, " "), nil
|
||||
}
|
||||
|
||||
if hasTitle {
|
||||
clean, err := extract(titleRaw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if clean != "" {
|
||||
r.Title = &clean
|
||||
}
|
||||
}
|
||||
if descRaw != "" {
|
||||
clean, err := extract(descRaw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if clean != "" {
|
||||
r.Description = &clean
|
||||
}
|
||||
}
|
||||
|
||||
clean, err := extract(remaining)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.Body = clean
|
||||
|
||||
if r.Body == "" && r.Title == nil {
|
||||
return nil, fmt.Errorf("empty body after extracting modifiers")
|
||||
}
|
||||
|
||||
|
||||
@@ -13,46 +13,61 @@ func TestParse(t *testing.T) {
|
||||
input string
|
||||
wantBody string
|
||||
wantGlyph string
|
||||
wantTitle *string
|
||||
wantDesc *string
|
||||
wantTime *string
|
||||
wantTags []string
|
||||
wantCard *string
|
||||
wantErrSub string
|
||||
}{
|
||||
// Glyph detection
|
||||
{"plain note", "hello world", "hello world", "note", nil, nil, nil, ""},
|
||||
{"dash todo", "- deploy nginx", "deploy nginx", "todo", nil, nil, nil, ""},
|
||||
{"unicode todo", "▸ deploy nginx", "deploy nginx", "todo", nil, nil, nil, ""},
|
||||
{"star event", "* dentist", "dentist", "event", nil, nil, nil, ""},
|
||||
{"unicode event", "◇ dentist", "dentist", "event", nil, nil, nil, ""},
|
||||
{"plain note", "hello world", "hello world", "note", nil, nil, nil, nil, nil, ""},
|
||||
{"dash todo", "- deploy nginx", "deploy nginx", "todo", nil, nil, nil, nil, nil, ""},
|
||||
{"unicode todo", "▸ deploy nginx", "deploy nginx", "todo", nil, nil, nil, nil, nil, ""},
|
||||
{"star event", "* dentist", "dentist", "event", nil, nil, nil, nil, nil, ""},
|
||||
{"unicode event", "◇ dentist", "dentist", "event", nil, nil, nil, nil, nil, ""},
|
||||
|
||||
// Time anchor
|
||||
{"with time", "meeting @14:00", "meeting", "note", sp("14:00"), nil, nil, ""},
|
||||
{"time at start", "@9:30 standup", "standup", "note", sp("9:30"), nil, nil, ""},
|
||||
{"invalid hours", "meeting @25:00", "", "", nil, nil, nil, "invalid time"},
|
||||
{"invalid minutes", "meeting @14:60", "", "", nil, nil, nil, "invalid time"},
|
||||
{"with time", "meeting @14:00", "meeting", "note", nil, nil, sp("14:00"), nil, nil, ""},
|
||||
{"time at start", "@9:30 standup", "standup", "note", nil, nil, sp("9:30"), nil, nil, ""},
|
||||
{"invalid hours", "meeting @25:00", "", "", nil, nil, nil, nil, nil, "invalid time"},
|
||||
{"invalid minutes", "meeting @14:60", "", "", nil, nil, nil, nil, nil, "invalid time"},
|
||||
|
||||
// Tags
|
||||
{"single tag", "deploy #ops", "deploy", "note", nil, []string{"ops"}, nil, ""},
|
||||
{"multiple tags", "deploy #ops #infra", "deploy", "note", nil, []string{"ops", "infra"}, nil, ""},
|
||||
{"duplicate tags", "deploy #ops #ops", "deploy", "note", nil, []string{"ops"}, nil, ""},
|
||||
{"tag with hyphen", "task #dev-ops", "task", "note", nil, []string{"dev-ops"}, nil, ""},
|
||||
{"single tag", "deploy #ops", "deploy", "note", nil, nil, nil, []string{"ops"}, nil, ""},
|
||||
{"multiple tags", "deploy #ops #infra", "deploy", "note", nil, nil, nil, []string{"ops", "infra"}, nil, ""},
|
||||
{"duplicate tags", "deploy #ops #ops", "deploy", "note", nil, nil, nil, []string{"ops"}, nil, ""},
|
||||
{"tag with hyphen", "task #dev-ops", "task", "note", nil, nil, nil, []string{"dev-ops"}, nil, ""},
|
||||
|
||||
// Card suffix
|
||||
{"caret card", "trick #nginx ^card", "trick", "note", nil, []string{"nginx"}, sp("snippet"), ""},
|
||||
{"caret c", "trick ^c", "trick", "note", nil, nil, sp("snippet"), ""},
|
||||
{"caret template", "deploy ${host} ^template", "deploy ${host}", "note", nil, nil, sp("template"), ""},
|
||||
{"caret snippet explicit", "trick ^snippet", "trick", "note", nil, nil, sp("snippet"), ""},
|
||||
{"invalid card type", "thing ^bogus", "", "", nil, nil, nil, "invalid card type"},
|
||||
{"caret card", "trick #nginx ^card", "trick", "note", nil, nil, nil, []string{"nginx"}, sp("snippet"), ""},
|
||||
{"caret c", "trick ^c", "trick", "note", nil, nil, nil, nil, sp("snippet"), ""},
|
||||
{"caret template", "deploy ${host} ^template", "deploy ${host}", "note", nil, nil, nil, nil, sp("template"), ""},
|
||||
{"caret snippet explicit", "trick ^snippet", "trick", "note", nil, nil, nil, nil, sp("snippet"), ""},
|
||||
{"invalid card type", "thing ^bogus", "", "", nil, nil, nil, nil, nil, "invalid card type"},
|
||||
|
||||
// Combined
|
||||
{"full input", "- deploy nginx to staging @15:00 #ops", "deploy nginx to staging", "todo", sp("15:00"), []string{"ops"}, nil, ""},
|
||||
{"full with card", "figured out the proxy_pass trick #nginx ^card", "figured out the proxy_pass trick", "note", nil, []string{"nginx"}, sp("snippet"), ""},
|
||||
{"full input", "- deploy nginx to staging @15:00 #ops", "deploy nginx to staging", "todo", nil, nil, sp("15:00"), []string{"ops"}, nil, ""},
|
||||
{"full with card", "figured out the proxy_pass trick #nginx ^card", "figured out the proxy_pass trick", "note", nil, nil, nil, []string{"nginx"}, sp("snippet"), ""},
|
||||
|
||||
// Title
|
||||
{"title with body", "|nginx trick\nproxy_pass trailing slash #ops", "proxy_pass trailing slash", "note", sp("nginx trick"), nil, nil, []string{"ops"}, nil, ""},
|
||||
{"no title", "no pipe here #ops", "no pipe here", "note", nil, nil, nil, []string{"ops"}, nil, ""},
|
||||
{"todo with title", "- |deploy staging\nrebuild docker #ops", "rebuild docker", "todo", sp("deploy staging"), nil, nil, []string{"ops"}, nil, ""},
|
||||
{"title only", "|title only", "", "note", sp("title only"), nil, nil, nil, nil, ""},
|
||||
{"title and desc", "|title // description #ops\nbody here", "body here", "note", sp("title"), sp("description"), nil, []string{"ops"}, nil, ""},
|
||||
{"todo title desc", "- |deploy staging // rebuild and push #ops", "", "todo", sp("deploy staging"), sp("rebuild and push"), nil, []string{"ops"}, nil, ""},
|
||||
|
||||
// Description without title
|
||||
{"leading desc", "// leading desc\nbody content", "body content", "note", nil, sp("leading desc"), nil, nil, nil, ""},
|
||||
{"inline desc", "body text // inline desc", "body text", "note", nil, sp("inline desc"), nil, nil, nil, ""},
|
||||
{"url no split", "http://example.com // should not split", "http://example.com // should not split", "note", nil, nil, nil, nil, nil, ""},
|
||||
|
||||
// Edge cases
|
||||
{"empty input", "", "", "", nil, nil, nil, "empty"},
|
||||
{"only glyph", "-", "", "", nil, nil, nil, "empty body"},
|
||||
{"only modifiers", "#ops @14:00", "", "", nil, nil, nil, "empty body"},
|
||||
{"whitespace only", " ", "", "", nil, nil, nil, "empty"},
|
||||
{"empty input", "", "", "", nil, nil, nil, nil, nil, "empty"},
|
||||
{"only glyph", "-", "", "", nil, nil, nil, nil, nil, "empty body"},
|
||||
{"only modifiers", "#ops @14:00", "", "", nil, nil, nil, nil, nil, "empty body"},
|
||||
{"whitespace only", " ", "", "", nil, nil, nil, nil, nil, "empty"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -79,6 +94,12 @@ func TestParse(t *testing.T) {
|
||||
if got.Glyph != tt.wantGlyph {
|
||||
t.Errorf("glyph: got %q, want %q", got.Glyph, tt.wantGlyph)
|
||||
}
|
||||
if !ptrEq(got.Title, tt.wantTitle) {
|
||||
t.Errorf("title: got %v, want %v", strPtr(got.Title), strPtr(tt.wantTitle))
|
||||
}
|
||||
if !ptrEq(got.Description, tt.wantDesc) {
|
||||
t.Errorf("description: got %v, want %v", strPtr(got.Description), strPtr(tt.wantDesc))
|
||||
}
|
||||
if !ptrEq(got.TimeAnchor, tt.wantTime) {
|
||||
t.Errorf("time_anchor: got %v, want %v", strPtr(got.TimeAnchor), strPtr(tt.wantTime))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user