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.
This commit is contained in:
Aaron Fischer 2021-06-09 01:19:26 +02:00
parent e18a99b437
commit 586b293bef
3 changed files with 39 additions and 13 deletions

View File

@ -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),
}

12
main.go
View File

@ -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)
}

View File

@ -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(`<a href="`), html.EscapeString(rewriteURL(string(linkURL), parsedURL))...)
link = append(link, []byte(`">`)...)
link = append(link, html.EscapeString(string(linkLabel))...)
link = append(link, []byte(`</a>`)...)
result = append(result, link...)
result = append(result, []byte("<br>")...)
// 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 src="`), html.EscapeString(rewriteURL(string(linkURL), parsedURL))...)
img = append(img, []byte(`" alt="`)...)
img = append(img, html.EscapeString(string(linkLabel))...)
img = append(img, []byte(`"/>`)...)
result = append(result, img...)
} else {
link := append([]byte(`<a href="`), html.EscapeString(rewriteURL(string(linkURL), parsedURL))...)
link = append(link, []byte(`">`)...)
link = append(link, html.EscapeString(string(linkLabel))...)
link = append(link, []byte(`</a>`)...)
result = append(result, link...)
result = append(result, []byte("<br>")...)
}
continue
}