Update image inlining

This commit is contained in:
m0xEE 2025-01-17 13:35:08 -05:00 committed by Erick Ruiz de Chavez
parent 96aba4fdc1
commit 34a61406f0
2 changed files with 27 additions and 21 deletions

7
go.mod
View file

@ -1,13 +1,8 @@
module git.sr.ht/~sircmpwn/kineto module git.sr.ht/~sircmpwn/kineto
go 1.21 go 1.16
require ( require (
git.sr.ht/~adnano/go-gemini v0.1.17 git.sr.ht/~adnano/go-gemini v0.1.17
git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3 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
)

41
main.go
View file

@ -10,7 +10,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"slices" "path/filepath"
"strings" "strings"
"time" "time"
"unicode" "unicode"
@ -19,6 +19,9 @@ import (
"git.sr.ht/~sircmpwn/getopt" "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. var gemtextPage = template.Must(template.
New("gemtext"). New("gemtext").
Funcs(template.FuncMap{ Funcs(template.FuncMap{
@ -100,20 +103,22 @@ var gemtextPage = template.Must(template.
return nil return nil
} }
}, },
"is_image": func(s string) bool { "isImage": func(s string) bool {
u, err := url.Parse(s) u, err := url.Parse(s)
if err != nil { if err != nil {
return false return false
} }
ext := strings.ToLower(filepath.Ext(u.Path))
parts := strings.Split(u.Path, ".") log.Printf("Testing if %s is a known extension", ext)
extension := parts[len(parts)-1] for _, l := range imgexts {
log.Printf("Testing if %s == %s", ext, l)
// Source: https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types if ext == l {
knownExtensions := []string{"apng", "png", "avif", "gif", "jpg", "jpeg", "jfif", "pjpeg", "pjp", "png", "svg", "webp", "bmp", "ico", "cur", "tif", "tiff"} log.Printf("It is a known image!")
return true
return slices.Contains(knownExtensions, extension) }
}
log.Printf("Not an image")
return false
}, },
"url": func(ctx *GemtextContext, s string) template.URL { "url": func(ctx *GemtextContext, s string) template.URL {
u, err := url.Parse(s) u, err := url.Parse(s)
@ -205,16 +210,18 @@ var gemtextPage = template.Must(template.
{{- with . | link }} {{- with . | link }}
{{- $isList = false -}} {{- $isList = false -}}
<p> <p>
{{- if ( .URL | is_image) -}} <a
href="{{.URL | url $ctx}}"
>
{{- if ( .URL | isImage ) -}}
<img <img
src="{{.URL | url $ctx}}" src="{{.URL | url $ctx}}"
{{if .Name}}alt="{{.Name}}" title="{{.Name}}"{{end}} {{if .Name}}alt="{{.Name}}" title="{{.Name}}"{{end}}
> >
{{- else -}} {{- else -}}
<a {{if .Name}}{{.Name}}{{else}}{{.URL}}{{end}}
href="{{.URL | url $ctx}}"
>{{if .Name}}{{.Name}}{{else}}{{.URL}}{{end}}</a>
{{- end -}} {{- end -}}
</a>
</p> </p>
{{- end -}} {{- end -}}
@ -361,6 +368,10 @@ a:before {
left: -1.25rem; left: -1.25rem;
} }
a:has(img):before {
content: '';
}
pre { pre {
background-color: #eee; background-color: #eee;
margin: 0 -1rem; margin: 0 -1rem;