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

41
main.go
View file

@ -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 -}}
<p>
{{- if ( .URL | is_image) -}}
<a
href="{{.URL | url $ctx}}"
>
{{- if ( .URL | isImage ) -}}
<img
src="{{.URL | url $ctx}}"
{{if .Name}}alt="{{.Name}}" title="{{.Name}}"{{end}}
>
{{- else -}}
<a
href="{{.URL | url $ctx}}"
>{{if .Name}}{{.Name}}{{else}}{{.URL}}{{end}}</a>
{{if .Name}}{{.Name}}{{else}}{{.URL}}{{end}}
{{- end -}}
</a>
</p>
{{- end -}}
@ -361,6 +368,10 @@ a:before {
left: -1.25rem;
}
a:has(img):before {
content: '';
}
pre {
background-color: #eee;
margin: 0 -1rem;