msgviewer: open http links from messages
Parse http links from a message and display them as completions in the :open-link command. Add the following binds to the [view] section in your binds.conf: <C-l> = :open-link <space> Parsing can be disabled in aerc.conf by setting parse-http-links to false in the viewer section. Thanks to Moritz for the help with the regular expression. Signed-off-by: Koni Marti <koni.marti@gmail.com> Reviewed-by: Moritz Poldrack <git@moritz.sh> Acked-by: Robin Jarry <robin@jarry.cc>
This commit is contained in:
parent
4753cfd3e3
commit
e1d8bc4d17
9 changed files with 192 additions and 2 deletions
lib/parse
44
lib/parse/hyperlinks.go
Normal file
44
lib/parse/hyperlinks.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package parse
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"io"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var submatch = `(https?:\/\/[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,10}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*))`
|
||||
var httpRe = regexp.MustCompile("\"" + submatch + "\"" + "|" + "\\(" + submatch + "\\)" + "|" + "<" + submatch + ">" + "|" + submatch)
|
||||
|
||||
// HttpLinks searches a reader for a http link and returns a copy of the
|
||||
// reader and a slice with links.
|
||||
func HttpLinks(r io.Reader) (io.Reader, []string) {
|
||||
var buf bytes.Buffer
|
||||
tr := io.TeeReader(r, &buf)
|
||||
|
||||
scanner := bufio.NewScanner(tr)
|
||||
linkMap := make(map[string]struct{})
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if !strings.Contains(line, "http") {
|
||||
continue
|
||||
}
|
||||
for _, word := range strings.Fields(line) {
|
||||
if links := httpRe.FindStringSubmatch(word); len(links) > 0 {
|
||||
for _, l := range links[1:] {
|
||||
if l != "" {
|
||||
linkMap[strings.TrimSpace(l)] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
results := []string{}
|
||||
for link, _ := range linkMap {
|
||||
results = append(results, link)
|
||||
}
|
||||
|
||||
return &buf, results
|
||||
}
|
99
lib/parse/hyperlinks_test.go
Normal file
99
lib/parse/hyperlinks_test.go
Normal file
|
@ -0,0 +1,99 @@
|
|||
package parse_test
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.sr.ht/~rjarry/aerc/lib/parse"
|
||||
)
|
||||
|
||||
func TestHyperlinks(t *testing.T) {
|
||||
tests := []struct {
|
||||
text string
|
||||
links []string
|
||||
}{
|
||||
{
|
||||
text: "http://aerc-mail.org",
|
||||
links: []string{"http://aerc-mail.org"},
|
||||
},
|
||||
{
|
||||
text: "https://aerc-mail.org",
|
||||
links: []string{"https://aerc-mail.org"},
|
||||
},
|
||||
{
|
||||
text: "text https://aerc-mail.org more text",
|
||||
links: []string{"https://aerc-mail.org"},
|
||||
},
|
||||
{
|
||||
text: "text (https://aerc-mail.org) more text",
|
||||
links: []string{"https://aerc-mail.org"},
|
||||
},
|
||||
{
|
||||
text: "text \"https://aerc-mail.org\" more text",
|
||||
links: []string{"https://aerc-mail.org"},
|
||||
},
|
||||
{
|
||||
text: "text <https://aerc-mail.org> more text",
|
||||
links: []string{"https://aerc-mail.org"},
|
||||
},
|
||||
{
|
||||
text: "<a href=\"https://aerc-mail.org\">",
|
||||
links: []string{"https://aerc-mail.org"},
|
||||
},
|
||||
{
|
||||
text: "text https://aerc-mail.org more text https://aerc-mail.org more text",
|
||||
links: []string{"https://aerc-mail.org"},
|
||||
},
|
||||
{
|
||||
text: "text https://aerc-mail.org more text http://git.sr.ht/~rjarry/aerc more text",
|
||||
links: []string{"https://aerc-mail.org", "http://git.sr.ht/~rjarry/aerc"},
|
||||
},
|
||||
{
|
||||
text: "text http://www.ietf.org/rfc/rfc2396.txt more text",
|
||||
links: []string{"http://www.ietf.org/rfc/rfc2396.txt"},
|
||||
},
|
||||
{
|
||||
text: "text <http://example.com:8042/over/there?name=ferret#nose> more text",
|
||||
links: []string{"http://example.com:8042/over/there?name=ferret#nose"},
|
||||
},
|
||||
{
|
||||
text: "text http://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm more text",
|
||||
links: []string{"http://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm"},
|
||||
},
|
||||
{
|
||||
text: "text https://www.ics.uci.edu/pub/ietf/uri/#Related more text",
|
||||
links: []string{"https://www.ics.uci.edu/pub/ietf/uri/#Related"},
|
||||
},
|
||||
{
|
||||
text: "text https://www.example.com/index.php?id_sezione=360&sid=3a5ebc944f41daa6f849f730f1 more text",
|
||||
links: []string{"https://www.example.com/index.php?id_sezione=360&sid=3a5ebc944f41daa6f849f730f1"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
||||
// make sure reader is exact copy of input reader
|
||||
reader, links := parse.HttpLinks(strings.NewReader(test.text))
|
||||
if data, err := ioutil.ReadAll(reader); err != nil {
|
||||
t.Errorf("could not read text: %v", err)
|
||||
} else if string(data) != test.text {
|
||||
t.Errorf("did not copy input reader correctly")
|
||||
}
|
||||
|
||||
// check correct parsed links
|
||||
if len(links) != len(test.links) {
|
||||
t.Errorf("different number of links: got %d but expected %d", len(links), len(test.links))
|
||||
}
|
||||
linkMap := make(map[string]struct{})
|
||||
for _, got := range links {
|
||||
linkMap[got] = struct{}{}
|
||||
}
|
||||
for _, expected := range test.links {
|
||||
if _, ok := linkMap[expected]; !ok {
|
||||
t.Errorf("link not parsed: %s", expected)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue