mimetype handling
upon general navigation: 1. check for any configured handlers for the mimetype 2. normal printing for gophermap, gemtext, and text/plain 3. save the file to downloads folder
This commit is contained in:
parent
15214b7c98
commit
a44a063c1a
|
@ -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.
|
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"
|
* 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"
|
* 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"
|
* 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
|
* 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
|
* 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.
|
||||||
|
|
20
actions.go
20
actions.go
|
@ -8,6 +8,7 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -176,7 +177,7 @@ func Reload(state *BrowserState, conf *Config) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return print(state)
|
return HandleResource(state, conf)
|
||||||
}
|
}
|
||||||
|
|
||||||
func externalMessage() ([]byte, error) {
|
func externalMessage() ([]byte, error) {
|
||||||
|
@ -470,6 +471,23 @@ func Print(state *BrowserState) error {
|
||||||
return print(state)
|
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 {
|
func Outline(state *BrowserState, conf *Config) error {
|
||||||
if state.Body == nil {
|
if state.Body == nil {
|
||||||
return ErrMustBeOnAPage
|
return ErrMustBeOnAPage
|
||||||
|
|
5
files.go
5
files.go
|
@ -26,6 +26,8 @@ type ConfigMain struct {
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
ConfigMain `toml:"main"`
|
ConfigMain `toml:"main"`
|
||||||
|
|
||||||
|
Handlers map[string]string `toml:"handlers"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func getConfig() (*Config, error) {
|
func getConfig() (*Config, error) {
|
||||||
|
@ -41,7 +43,7 @@ func getConfig() (*Config, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
c := Config{
|
c := Config{
|
||||||
ConfigMain{
|
ConfigMain: ConfigMain{
|
||||||
VimKeys: true,
|
VimKeys: true,
|
||||||
DefaultScheme: "gemini",
|
DefaultScheme: "gemini",
|
||||||
SoftWrap: 100,
|
SoftWrap: 100,
|
||||||
|
@ -49,6 +51,7 @@ func getConfig() (*Config, error) {
|
||||||
Quiet: false,
|
Quiet: false,
|
||||||
Pager: "auto",
|
Pager: "auto",
|
||||||
},
|
},
|
||||||
|
Handlers: map[string]string{},
|
||||||
}
|
}
|
||||||
if _, err := toml.DecodeFile(path, &c); err != nil {
|
if _, err := toml.DecodeFile(path, &c); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"mime"
|
"mime"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"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"
|
return "text/plain"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue