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:
tjp 2024-01-09 09:53:15 -07:00
parent 15214b7c98
commit a44a063c1a
4 changed files with 35 additions and 4 deletions

View File

@ -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.

View File

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

View File

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

View File

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