aboutsummaryrefslogtreecommitdiffstats
path: root/lib/parse/hyperlinks.go
diff options
context:
space:
mode:
authorKoni Marti <koni.marti@gmail.com>2022-06-14 21:10:48 +0200
committerRobin Jarry <robin@jarry.cc>2022-06-14 22:12:48 +0200
commite1d8bc4d17cb7bd79e19fdf866d1da2fedda4de5 (patch)
treeececff85e82e42a2b95b27b03e53c79768ad0709 /lib/parse/hyperlinks.go
parent4753cfd3e33f985ec22c5600a8d7e68b72175607 (diff)
downloadaerc-e1d8bc4d17cb7bd79e19fdf866d1da2fedda4de5.tar.gz
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>
Diffstat (limited to 'lib/parse/hyperlinks.go')
-rw-r--r--lib/parse/hyperlinks.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/parse/hyperlinks.go b/lib/parse/hyperlinks.go
new file mode 100644
index 00000000..7a005383
--- /dev/null
+++ b/lib/parse/hyperlinks.go
@@ -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
+}