-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
103 lines (93 loc) · 2.39 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Parses and verifies user-sent post data.
package parser
import (
"bytes"
"database/sql"
"meguca/common"
"meguca/db"
"meguca/templates"
"meguca/util"
"strconv"
"strings"
b "github.com/cutechan/blackfriday"
)
// Check thread subject string.
func ParseSubject(s string) (string, error) {
if s == "" {
return s, common.ErrNoSubject
}
if len(s) > common.MaxLenSubject {
return s, common.ErrSubjectTooLong
}
return strings.TrimSpace(s), nil
}
type parseRenderer struct {
links common.Links
commands common.Commands
*b.Html
}
func (r *parseRenderer) PostLink(out *bytes.Buffer, text []byte) {
link, err := parsePostLink(text)
if err != nil {
return
}
r.links = append(r.links, link)
}
// Extract post links from a text fragment, verify and retrieve their
// parenthood.
func parsePostLink(text []byte) (link [2]uint64, err error) {
idStr := string(text[2:])
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
return
}
op, err := db.GetPostOP(id)
switch err {
case nil:
link = [2]uint64{id, op}
case sql.ErrNoRows: // Points to invalid post. Ignore.
err = nil
}
return
}
func (r *parseRenderer) Command(out *bytes.Buffer, text []byte, c, q string) {
// Allow only single command per post for now.
if len(r.commands) > 0 {
return
}
switch c {
case "roll":
m := templates.RollQueryRe.FindStringSubmatch(q)
if m != nil {
a, _ := strconv.Atoi(m[1])
b, _ := strconv.Atoi(m[2])
v := util.PseudoRandInt(a, b)
cmd := common.Command{Type: common.Roll, Roll: v}
r.commands = append(r.commands, cmd)
}
case "flip":
m := templates.FlipQueryRe.FindStringSubmatch(q)
if m != nil {
a, _ := strconv.Atoi(m[1])
b := util.PseudoRandInt(1, 100)
v := a >= b
cmd := common.Command{Type: common.Flip, Flip: v}
r.commands = append(r.commands, cmd)
}
}
}
// Extract special elements from the post body which need some
// additional processing.
//
// Run the full formatting process which is kinda superfluous (we don't
// need resulting markup) but it shouldn't be too expensive. That would
// guarantee that the parsing is correct (e.g. in case of code blocks).
func ParseBody(body []byte) (common.Links, common.Commands, error) {
renderer := &parseRenderer{
links: nil,
commands: nil,
Html: b.HtmlRenderer(templates.HtmlFlags, "", "").(*b.Html),
}
b.Markdown(body, renderer, templates.Extensions)
return renderer.links, renderer.commands, nil
}