From 586b293bef75233e4607d28b8e0d0db4f8c6b36d Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Wed, 9 Jun 2021 01:19:26 +0200 Subject: [PATCH 1/3] Convert images to images instead of links Image links will result in a `img` tag instead of a `a` tag. This behaviour is disabled by default and can be enabled by adding the following line to to config file: convertimages: true To get access to the config in the convert file, I had to move the config file inside the gmitohtml namespace. This is specially handy later on if the config file contains other settings which are useful for the rest of the codebase. --- config.go | 7 ++++++- main.go | 12 ++++++------ pkg/gmitohtml/convert.go | 33 +++++++++++++++++++++++++++------ 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/config.go b/config.go index ab9104b..aa8ca70 100644 --- a/config.go +++ b/config.go @@ -1,4 +1,4 @@ -package main +package gmitohtml import ( "crypto/tls" @@ -22,12 +22,17 @@ type certConfig struct { type appConfig struct { Bookmarks map[string]string + // Convert image links to images instead of normal links + ConvertImages bool + Certs map[string]*certConfig } var config = &appConfig{ Bookmarks: make(map[string]string), + ConvertImages: false, + Certs: make(map[string]*certConfig), } diff --git a/main.go b/main.go index 1b3685f..191a634 100644 --- a/main.go +++ b/main.go @@ -46,7 +46,7 @@ func main() { // TODO option to include response header in page flag.Parse() - defaultConfig := defaultConfigPath() + defaultConfig := gmitohtml.DefaultConfigPath() if configFile == "" { configFile = defaultConfig } @@ -58,18 +58,18 @@ func main() { } if configExists || configFile != defaultConfig { - err := readconfig(configFile) + err := gmitohtml.ReadConfig(configFile) if err != nil { log.Fatalf("failed to read configuration file at %s: %v\nSee CONFIGURATION.md for information on configuring gmitohtml", configFile, err) } - for u, label := range config.Bookmarks { + for u, label := range gmitohtml.Config.Bookmarks { gmitohtml.AddBookmark(u, label) } } } - for domain, cc := range config.Certs { + for domain, cc := range gmitohtml.Config.Certs { certData, err := ioutil.ReadFile(cc.Cert) if err != nil { log.Fatalf("failed to load client certificate for domain %s: %s", domain, err) @@ -88,9 +88,9 @@ func main() { if daemon != "" { gmitohtml.SetOnBookmarksChanged(func() { - config.Bookmarks = gmitohtml.GetBookmarks() + gmitohtml.Config.Bookmarks = gmitohtml.GetBookmarks() - err := saveConfig(configFile) + err := gmitohtml.SaveConfig(configFile) if err != nil { log.Fatal(err) } diff --git a/pkg/gmitohtml/convert.go b/pkg/gmitohtml/convert.go index 549df8e..59588be 100644 --- a/pkg/gmitohtml/convert.go +++ b/pkg/gmitohtml/convert.go @@ -19,6 +19,8 @@ var daemonAddress string var assetLock sync.Mutex +var imageExtensions = []string{"png", "jpg", "jpeg", "gif", "svg", "webp"} + func rewriteURL(u string, loc *url.URL) string { if daemonAddress != "" { scheme := "gemini" @@ -126,12 +128,31 @@ func Convert(page []byte, u string) []byte { linkLabel = line[splitStart:] } - link := append([]byte(``)...) - link = append(link, html.EscapeString(string(linkLabel))...) - link = append(link, []byte(``)...) - result = append(result, link...) - result = append(result, []byte("
")...) + // If link ends with gif/png/jpg, add a image instead of a link + parts := strings.Split(string(linkURL), ".") + extension := parts[len(parts)-1] + isImage := false + for _, ext := range imageExtensions { + if extension == ext { + isImage = true + } + } + + if isImage && Config.ConvertImages { + img := append([]byte(``)...)
+				img = append(img, html.EscapeString(string(linkLabel))...)
+				img = append(img, []byte(``)...) + result = append(result, img...) + } else { + link := append([]byte(``)...) + link = append(link, html.EscapeString(string(linkLabel))...) + link = append(link, []byte(``)...) + result = append(result, link...) + result = append(result, []byte("
")...) + } + continue } From 0bec3c3eef6340d0d73310b412aeb1d0a11e4a69 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Fri, 9 Jul 2021 22:45:52 +0200 Subject: [PATCH 2/3] Fix wrong package name and clean up tag-building DRY the tag building for links and images. --- config.go | 2 +- pkg/gmitohtml/convert.go | 18 ++++++------------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/config.go b/config.go index aa8ca70..5d26292 100644 --- a/config.go +++ b/config.go @@ -1,4 +1,4 @@ -package gmitohtml +package main import ( "crypto/tls" diff --git a/pkg/gmitohtml/convert.go b/pkg/gmitohtml/convert.go index 59588be..fcaf208 100644 --- a/pkg/gmitohtml/convert.go +++ b/pkg/gmitohtml/convert.go @@ -128,7 +128,6 @@ func Convert(page []byte, u string) []byte { linkLabel = line[splitStart:] } - // If link ends with gif/png/jpg, add a image instead of a link parts := strings.Split(string(linkURL), ".") extension := parts[len(parts)-1] isImage := false @@ -138,19 +137,14 @@ func Convert(page []byte, u string) []byte { } } + uri := html.EscapeString(rewriteURL(string(linkURL), parsedURL)) + title := html.EscapeString(string(linkLabel)) + + // If link ends with gif/png/jpg, add a image instead of a link if isImage && Config.ConvertImages { - img := append([]byte(``)...)
-				img = append(img, html.EscapeString(string(linkLabel))...)
-				img = append(img, []byte(``)...) - result = append(result, img...) + result = append(result, []byte("\""")...) } else { - link := append([]byte(``)...) - link = append(link, html.EscapeString(string(linkLabel))...) - link = append(link, []byte(``)...) - result = append(result, link...) - result = append(result, []byte("
")...) + result = append(result, []byte("" + title + "
")...) } continue From 2fdec61b41e41f6387f10cc089e33745bc111945 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Fri, 9 Jul 2021 23:19:30 +0200 Subject: [PATCH 3/3] Move config to gmitohtml namespace --- config.go => pkg/gmitohtml/config.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) rename config.go => pkg/gmitohtml/config.go (78%) diff --git a/config.go b/pkg/gmitohtml/config.go similarity index 78% rename from config.go rename to pkg/gmitohtml/config.go index 5d26292..b4dace8 100644 --- a/config.go +++ b/pkg/gmitohtml/config.go @@ -1,4 +1,4 @@ -package main +package gmitohtml import ( "crypto/tls" @@ -8,7 +8,6 @@ import ( "os" "path" - "code.rocketnine.space/tslocum/gmitohtml/pkg/gmitohtml" "gopkg.in/yaml.v3" ) @@ -28,7 +27,7 @@ type appConfig struct { Certs map[string]*certConfig } -var config = &appConfig{ +var Config = &appConfig{ Bookmarks: make(map[string]string), ConvertImages: false, @@ -36,7 +35,7 @@ var config = &appConfig{ Certs: make(map[string]*certConfig), } -func defaultConfigPath() string { +func DefaultConfigPath() string { homedir, err := os.UserHomeDir() if err == nil && homedir != "" { return path.Join(homedir, ".config", "gmitohtml", "config.yaml") @@ -44,7 +43,7 @@ func defaultConfigPath() string { return "" } -func readconfig(configPath string) error { +func ReadConfig(configPath string) error { if configPath == "" { return errors.New("file unspecified") } @@ -59,15 +58,15 @@ func readconfig(configPath string) error { if err != nil { return err } - config = newConfig + Config = newConfig return nil } -func saveConfig(configPath string) error { - config.Bookmarks = gmitohtml.GetBookmarks() +func SaveConfig(configPath string) error { + Config.Bookmarks = GetBookmarks() - out, err := yaml.Marshal(config) + out, err := yaml.Marshal(Config) if err != nil { return fmt.Errorf("failed to marshal configuration: %s", err) }