diff --git a/go.mod b/go.mod index e09410a..d88d702 100644 --- a/go.mod +++ b/go.mod @@ -1,13 +1,8 @@ module git.sr.ht/~sircmpwn/kineto -go 1.21 +go 1.16 require ( git.sr.ht/~adnano/go-gemini v0.1.17 git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3 ) - -require ( - golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect - golang.org/x/text v0.3.3 // indirect -) diff --git a/main.go b/main.go index 7aeaafd..5e4688b 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ import ( "net/http" "net/url" "os" - "slices" + "path/filepath" "strings" "time" "unicode" @@ -19,6 +19,9 @@ import ( "git.sr.ht/~sircmpwn/getopt" ) +// Source: https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types +var imgexts = []string{".apng", ".png", ".avif", ".gif", ".jpg", ".jpeg", ".jfif", ".pjpeg", ".pjp", ".png", ".svg", ".webp", ".bmp", ".ico", ".cur", ".tif", ".tiff"} + var gemtextPage = template.Must(template. New("gemtext"). Funcs(template.FuncMap{ @@ -100,20 +103,22 @@ var gemtextPage = template.Must(template. return nil } }, - "is_image": func(s string) bool { + "isImage": func(s string) bool { u, err := url.Parse(s) - if err != nil { return false } - - parts := strings.Split(u.Path, ".") - extension := parts[len(parts)-1] - - // Source: https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types - knownExtensions := []string{"apng", "png", "avif", "gif", "jpg", "jpeg", "jfif", "pjpeg", "pjp", "png", "svg", "webp", "bmp", "ico", "cur", "tif", "tiff"} - - return slices.Contains(knownExtensions, extension) + ext := strings.ToLower(filepath.Ext(u.Path)) + log.Printf("Testing if %s is a known extension", ext) + for _, l := range imgexts { + log.Printf("Testing if %s == %s", ext, l) + if ext == l { + log.Printf("It is a known image!") + return true + } + } + log.Printf("Not an image") + return false }, "url": func(ctx *GemtextContext, s string) template.URL { u, err := url.Parse(s) @@ -205,16 +210,18 @@ var gemtextPage = template.Must(template. {{- with . | link }} {{- $isList = false -}}

- {{- if ( .URL | is_image) -}} + + {{- if ( .URL | isImage ) -}} {{- else -}} - {{if .Name}}{{.Name}}{{else}}{{.URL}}{{end}} + {{if .Name}}{{.Name}}{{else}}{{.URL}}{{end}} {{- end -}} +

{{- end -}} @@ -361,6 +368,10 @@ a:before { left: -1.25rem; } +a:has(img):before { + content: ''; +} + pre { background-color: #eee; margin: 0 -1rem;