Skip to content

Commit

Permalink
stricter directive parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcloughlin committed Feb 26, 2023
1 parent 496dc48 commit d9d574f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var subexpIdx = map[string]int{}

func init() {
urlRegexp, _ := xurls.StrictMatchingScheme("https?")
directiveExpr := `(?P<action>\w+):?\s+(?P<url>` + urlRegexp.String() + `)(\s+\((?P<extra>.+)\))?`
directiveExpr := `^\s*(?P<action>\w+):\s+(?P<url>` + urlRegexp.String() + `)(\s+\((?P<extra>.+)\))?`
directiveRegex = regexp.MustCompile(directiveExpr)

for i, name := range directiveRegex.SubexpNames() {
Expand Down
2 changes: 2 additions & 0 deletions processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func TestParseDirective(t *testing.T) {
line := "insert: https://www.rfc-editor.org/rfc/rfc918.txt (13-16)"
dir, err := ParseDirective(line)
require.NoError(t, err)
require.NotNil(t, dir)
assert.Equal(t, "insert", dir.ActionRaw)
assert.Equal(t, "https://www.rfc-editor.org/rfc/rfc918.txt", dir.Citation.URL.String())
assert.Equal(t, "13-16", dir.Citation.Extra)
Expand All @@ -21,6 +22,7 @@ func TestParseDirectiveNoExtra(t *testing.T) {
line := "Action: http://google.org"
dir, err := ParseDirective(line)
require.NoError(t, err)
require.NotNil(t, dir)
assert.Equal(t, "Action", dir.ActionRaw)
assert.Equal(t, "http://google.org", dir.Citation.URL.String())
assert.Equal(t, "", dir.Citation.Extra)
Expand Down

0 comments on commit d9d574f

Please sign in to comment.