diff --git a/README.gmi b/README.gmi index 6511bc2..4a7eebe 100644 --- a/README.gmi +++ b/README.gmi @@ -119,10 +119,11 @@ On any other nagivation, this context is cleared and next/previous actions won't The config file is located at XDG_DATA_HOME/x-1/config.toml. This is usually under .local/share in your home directory. -It contains the following configuration options: - +It has a "[main]" section with the following configuration options: * quiet (bool): disables automatic printing of pages upon navigation. default "false" * vim_keys (bool): whether to activate vim keybindings for the readline prompt. default "true" * default_scheme (string): the URL scheme to use in the "go" command when none is provided. default "gemini" * soft_wrap (int): the number of columns to wrap, or -1 to not add soft wrapping. default 72 * download_folder (string): path at which to store files saved by the "save" command. default $HOME + +Then a "[handlers]" section contains mappings of mimetypes to shell commands which should be used to handle them. diff --git a/actions.go b/actions.go index 28713ee..448ac06 100644 --- a/actions.go +++ b/actions.go @@ -8,6 +8,7 @@ import ( "net/url" "os" "os/exec" + "path" "path/filepath" "strconv" "strings" @@ -176,7 +177,7 @@ func Reload(state *BrowserState, conf *Config) error { return err } - return print(state) + return HandleResource(state, conf) } func externalMessage() ([]byte, error) { @@ -470,6 +471,23 @@ func Print(state *BrowserState) error { return print(state) } +func HandleResource(state *BrowserState, conf *Config) error { + if state.Modal != nil { + return Print(state) + } + + if handler, ok := conf.Handlers[state.DocType]; ok { + return Pipe(state, handler) + } + + switch state.DocType { + case "text/gemini", "text/x-gophermap", "text/plain": + return print(state) + } + + return Save(state, path.Base(state.Url.Path), conf) +} + func Outline(state *BrowserState, conf *Config) error { if state.Body == nil { return ErrMustBeOnAPage diff --git a/files.go b/files.go index 5ddf53b..052c4fb 100644 --- a/files.go +++ b/files.go @@ -26,6 +26,8 @@ type ConfigMain struct { type Config struct { ConfigMain `toml:"main"` + + Handlers map[string]string `toml:"handlers"` } func getConfig() (*Config, error) { @@ -41,7 +43,7 @@ func getConfig() (*Config, error) { } c := Config{ - ConfigMain{ + ConfigMain: ConfigMain{ VimKeys: true, DefaultScheme: "gemini", SoftWrap: 100, @@ -49,6 +51,7 @@ func getConfig() (*Config, error) { Quiet: false, Pager: "auto", }, + Handlers: map[string]string{}, } if _, err := toml.DecodeFile(path, &c); err != nil { return nil, err diff --git a/handlers.go b/handlers.go index d918fd0..64ea5c0 100644 --- a/handlers.go +++ b/handlers.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "mime" + "net/http" "net/url" "strings" @@ -57,6 +58,14 @@ func docType(u *url.URL, response *sliderule.Response) string { } } + if u.Scheme == "http" || u.Scheme == "https" { + resp := response.Meta.(*http.Response) + mtype, _, err := mime.ParseMediaType(resp.Header.Get("Content-Type")) + if err == nil { + return mtype + } + } + return "text/plain" }