gmitohtml-upstream/main.go
Aaron Fischer 586b293bef 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.
2021-06-09 01:19:26 +02:00

124 lines
2.8 KiB
Go

package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"os/exec"
"runtime"
"code.rocketnine.space/tslocum/gmitohtml/pkg/gmitohtml"
)
func openBrowser(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
log.Fatal(err)
}
}
func main() {
var (
view bool
allowFile bool
daemon string
hostname string
configFile string
)
flag.BoolVar(&view, "view", false, "open web browser")
flag.BoolVar(&allowFile, "allow-file", false, "allow local file access via file://")
flag.StringVar(&daemon, "daemon", "", "start daemon on specified address")
flag.StringVar(&hostname, "hostname", "", "server hostname (e.g. rocketnine.space) (defaults to daemon address)")
flag.StringVar(&configFile, "config", "", "path to configuration file")
// TODO option to include response header in page
flag.Parse()
defaultConfig := gmitohtml.DefaultConfigPath()
if configFile == "" {
configFile = defaultConfig
}
if configFile != "" {
var configExists bool
if _, err := os.Stat(defaultConfig); !os.IsNotExist(err) {
configExists = true
}
if configExists || configFile != defaultConfig {
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 gmitohtml.Config.Bookmarks {
gmitohtml.AddBookmark(u, label)
}
}
}
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)
}
keyData, err := ioutil.ReadFile(cc.Key)
if err != nil {
log.Fatalf("failed to load client certificate for domain %s: %s", domain, err)
}
err = gmitohtml.SetClientCertificate(domain, certData, keyData)
if err != nil {
log.Fatalf("failed to load client certificate for domain %s", domain)
}
}
if daemon != "" {
gmitohtml.SetOnBookmarksChanged(func() {
gmitohtml.Config.Bookmarks = gmitohtml.GetBookmarks()
err := gmitohtml.SaveConfig(configFile)
if err != nil {
log.Fatal(err)
}
})
err := gmitohtml.StartDaemon(daemon, hostname, allowFile)
if err != nil {
log.Fatal(err)
}
if view {
openBrowser("http://" + daemon)
}
select {}
}
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
data = gmitohtml.Convert(data, "")
if view {
openBrowser(string(append([]byte("data:text/html,"), []byte(url.PathEscape(string(data)))...)))
return
}
fmt.Print(gmitohtml.Convert(data, ""))
}