| 1 | // Package quick herkent quick actions op een eigen regel. |
| 2 | // Een regel in een codeblok blijft gewone tekst. |
| 3 | package quick |
| 4 | |
| 5 | import "strings" |
| 6 | |
| 7 | // Command is één opdracht. Names zijn labels, gebruikers of een mijlpaal, zonder ~ @ of %. |
| 8 | type Command struct { |
| 9 | Kind string |
| 10 | Names []string |
| 11 | Due string |
| 12 | } |
| 13 | |
| 14 | // Piece is één regel. Cmd is nil als de regel tekst blijft. |
| 15 | type Piece struct { |
| 16 | Text string |
| 17 | Cmd *Command |
| 18 | } |
| 19 | |
| 20 | // Scan splitst de tekst in regels. Opdrachten in een hek ``` blijven tekst. |
| 21 | func Scan(body string) []Piece { |
| 22 | body = strings.ReplaceAll(body, "\r\n", "\n") |
| 23 | lines := strings.Split(body, "\n") |
| 24 | out := make([]Piece, 0, len(lines)) |
| 25 | fence := false |
| 26 | for _, line := range lines { |
| 27 | trim := strings.TrimSpace(line) |
| 28 | if strings.HasPrefix(trim, "```") { |
| 29 | fence = !fence |
| 30 | out = append(out, Piece{Text: line}) |
| 31 | continue |
| 32 | } |
| 33 | if fence { |
| 34 | out = append(out, Piece{Text: line}) |
| 35 | continue |
| 36 | } |
| 37 | if cmd, ok := parse(trim); ok { |
| 38 | c := cmd |
| 39 | out = append(out, Piece{Text: line, Cmd: &c}) |
| 40 | continue |
| 41 | } |
| 42 | out = append(out, Piece{Text: line}) |
| 43 | } |
| 44 | return out |
| 45 | } |
| 46 | |
| 47 | func parse(trim string) (Command, bool) { |
| 48 | switch { |
| 49 | case trim == "/confidential": |
| 50 | return Command{Kind: "confidential"}, true |
| 51 | case strings.HasPrefix(trim, "/due "): |
| 52 | due := strings.TrimSpace(strings.TrimPrefix(trim, "/due ")) |
| 53 | if len(due) != 10 || due[4] != '-' || due[7] != '-' || !digits(due[0:4]) || !digits(due[5:7]) || !digits(due[8:10]) { |
| 54 | return Command{}, false |
| 55 | } |
| 56 | return Command{Kind: "due", Due: due}, true |
| 57 | case strings.HasPrefix(trim, "/label "): |
| 58 | names := sigils(strings.TrimSpace(strings.TrimPrefix(trim, "/label ")), '~') |
| 59 | if len(names) == 0 { |
| 60 | return Command{}, false |
| 61 | } |
| 62 | return Command{Kind: "label", Names: names}, true |
| 63 | case strings.HasPrefix(trim, "/unlabel "): |
| 64 | names := sigils(strings.TrimSpace(strings.TrimPrefix(trim, "/unlabel ")), '~') |
| 65 | if len(names) == 0 { |
| 66 | return Command{}, false |
| 67 | } |
| 68 | return Command{Kind: "unlabel", Names: names}, true |
| 69 | case strings.HasPrefix(trim, "/assign "): |
| 70 | names := sigils(strings.TrimSpace(strings.TrimPrefix(trim, "/assign ")), '@') |
| 71 | if len(names) == 0 { |
| 72 | return Command{}, false |
| 73 | } |
| 74 | return Command{Kind: "assign", Names: names}, true |
| 75 | case strings.HasPrefix(trim, "/unassign "): |
| 76 | names := sigils(strings.TrimSpace(strings.TrimPrefix(trim, "/unassign ")), '@') |
| 77 | if len(names) == 0 { |
| 78 | return Command{}, false |
| 79 | } |
| 80 | return Command{Kind: "unassign", Names: names}, true |
| 81 | case strings.HasPrefix(trim, "/milestone "): |
| 82 | names := sigils(strings.TrimSpace(strings.TrimPrefix(trim, "/milestone ")), '%') |
| 83 | if len(names) != 1 { |
| 84 | return Command{}, false |
| 85 | } |
| 86 | return Command{Kind: "milestone", Names: names}, true |
| 87 | default: |
| 88 | return Command{}, false |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func digits(s string) bool { |
| 93 | if s == "" { |
| 94 | return false |
| 95 | } |
| 96 | for _, r := range s { |
| 97 | if r < '0' || r > '9' { |
| 98 | return false |
| 99 | } |
| 100 | } |
| 101 | return true |
| 102 | } |
| 103 | |
| 104 | func sigils(s string, mark byte) []string { |
| 105 | var out []string |
| 106 | i := 0 |
| 107 | for i < len(s) { |
| 108 | if s[i] == ' ' || s[i] == '\t' { |
| 109 | i++ |
| 110 | continue |
| 111 | } |
| 112 | if s[i] != mark { |
| 113 | return nil |
| 114 | } |
| 115 | i++ |
| 116 | if i >= len(s) { |
| 117 | return nil |
| 118 | } |
| 119 | if s[i] == '"' || s[i] == '\'' { |
| 120 | q := s[i] |
| 121 | i++ |
| 122 | j := strings.IndexByte(s[i:], q) |
| 123 | if j < 0 { |
| 124 | return nil |
| 125 | } |
| 126 | name := strings.TrimSpace(s[i : i+j]) |
| 127 | if name == "" { |
| 128 | return nil |
| 129 | } |
| 130 | out = append(out, name) |
| 131 | i += j + 1 |
| 132 | continue |
| 133 | } |
| 134 | j := i |
| 135 | for j < len(s) && s[j] != ' ' && s[j] != '\t' { |
| 136 | j++ |
| 137 | } |
| 138 | if j == i { |
| 139 | return nil |
| 140 | } |
| 141 | out = append(out, s[i:j]) |
| 142 | i = j |
| 143 | } |
| 144 | return out |
| 145 | } |