mirror of
https://code.rocketnine.space/tslocum/gmitohtml.git
synced 2024-05-12 16:53:22 +02:00
Merge pull request 'Convert images to images instead of links' (#5) from f/gmitohtml:image-output into master
Reviewed-on: https://code.rocketnine.space/tslocum/gmitohtml/pulls/5
This commit is contained in:
commit
8e8eef57e2
12
main.go
12
main.go
|
@ -46,7 +46,7 @@ func main() {
|
||||||
// TODO option to include response header in page
|
// TODO option to include response header in page
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
defaultConfig := defaultConfigPath()
|
defaultConfig := gmitohtml.DefaultConfigPath()
|
||||||
if configFile == "" {
|
if configFile == "" {
|
||||||
configFile = defaultConfig
|
configFile = defaultConfig
|
||||||
}
|
}
|
||||||
|
@ -58,18 +58,18 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if configExists || configFile != defaultConfig {
|
if configExists || configFile != defaultConfig {
|
||||||
err := readconfig(configFile)
|
err := gmitohtml.ReadConfig(configFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to read configuration file at %s: %v\nSee CONFIGURATION.md for information on configuring gmitohtml", configFile, err)
|
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)
|
gmitohtml.AddBookmark(u, label)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for domain, cc := range config.Certs {
|
for domain, cc := range gmitohtml.Config.Certs {
|
||||||
certData, err := ioutil.ReadFile(cc.Cert)
|
certData, err := ioutil.ReadFile(cc.Cert)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to load client certificate for domain %s: %s", domain, err)
|
log.Fatalf("failed to load client certificate for domain %s: %s", domain, err)
|
||||||
|
@ -88,9 +88,9 @@ func main() {
|
||||||
|
|
||||||
if daemon != "" {
|
if daemon != "" {
|
||||||
gmitohtml.SetOnBookmarksChanged(func() {
|
gmitohtml.SetOnBookmarksChanged(func() {
|
||||||
config.Bookmarks = gmitohtml.GetBookmarks()
|
gmitohtml.Config.Bookmarks = gmitohtml.GetBookmarks()
|
||||||
|
|
||||||
err := saveConfig(configFile)
|
err := gmitohtml.SaveConfig(configFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package gmitohtml
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
@ -8,7 +8,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
"code.rocketnine.space/tslocum/gmitohtml/pkg/gmitohtml"
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -22,16 +21,21 @@ type certConfig struct {
|
||||||
type appConfig struct {
|
type appConfig struct {
|
||||||
Bookmarks map[string]string
|
Bookmarks map[string]string
|
||||||
|
|
||||||
|
// Convert image links to images instead of normal links
|
||||||
|
ConvertImages bool
|
||||||
|
|
||||||
Certs map[string]*certConfig
|
Certs map[string]*certConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
var config = &appConfig{
|
var Config = &appConfig{
|
||||||
Bookmarks: make(map[string]string),
|
Bookmarks: make(map[string]string),
|
||||||
|
|
||||||
|
ConvertImages: false,
|
||||||
|
|
||||||
Certs: make(map[string]*certConfig),
|
Certs: make(map[string]*certConfig),
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultConfigPath() string {
|
func DefaultConfigPath() string {
|
||||||
homedir, err := os.UserHomeDir()
|
homedir, err := os.UserHomeDir()
|
||||||
if err == nil && homedir != "" {
|
if err == nil && homedir != "" {
|
||||||
return path.Join(homedir, ".config", "gmitohtml", "config.yaml")
|
return path.Join(homedir, ".config", "gmitohtml", "config.yaml")
|
||||||
|
@ -39,7 +43,7 @@ func defaultConfigPath() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func readconfig(configPath string) error {
|
func ReadConfig(configPath string) error {
|
||||||
if configPath == "" {
|
if configPath == "" {
|
||||||
return errors.New("file unspecified")
|
return errors.New("file unspecified")
|
||||||
}
|
}
|
||||||
|
@ -54,15 +58,15 @@ func readconfig(configPath string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
config = newConfig
|
Config = newConfig
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveConfig(configPath string) error {
|
func SaveConfig(configPath string) error {
|
||||||
config.Bookmarks = gmitohtml.GetBookmarks()
|
Config.Bookmarks = GetBookmarks()
|
||||||
|
|
||||||
out, err := yaml.Marshal(config)
|
out, err := yaml.Marshal(Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to marshal configuration: %s", err)
|
return fmt.Errorf("failed to marshal configuration: %s", err)
|
||||||
}
|
}
|
|
@ -19,6 +19,8 @@ var daemonAddress string
|
||||||
|
|
||||||
var assetLock sync.Mutex
|
var assetLock sync.Mutex
|
||||||
|
|
||||||
|
var imageExtensions = []string{"png", "jpg", "jpeg", "gif", "svg", "webp"}
|
||||||
|
|
||||||
func rewriteURL(u string, loc *url.URL) string {
|
func rewriteURL(u string, loc *url.URL) string {
|
||||||
if daemonAddress != "" {
|
if daemonAddress != "" {
|
||||||
scheme := "gemini"
|
scheme := "gemini"
|
||||||
|
@ -126,12 +128,25 @@ func Convert(page []byte, u string) []byte {
|
||||||
linkLabel = line[splitStart:]
|
linkLabel = line[splitStart:]
|
||||||
}
|
}
|
||||||
|
|
||||||
link := append([]byte(`<a href="`), html.EscapeString(rewriteURL(string(linkURL), parsedURL))...)
|
parts := strings.Split(string(linkURL), ".")
|
||||||
link = append(link, []byte(`">`)...)
|
extension := parts[len(parts)-1]
|
||||||
link = append(link, html.EscapeString(string(linkLabel))...)
|
isImage := false
|
||||||
link = append(link, []byte(`</a>`)...)
|
for _, ext := range imageExtensions {
|
||||||
result = append(result, link...)
|
if extension == ext {
|
||||||
result = append(result, []byte("<br>")...)
|
isImage = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
result = append(result, []byte("<img src=\"" + uri + "\" alt=\"" + title + "\">")...)
|
||||||
|
} else {
|
||||||
|
result = append(result, []byte("<a href=\"" + uri + "\">" + title + "</a><br>")...)
|
||||||
|
}
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue