Add option to add "location" to posts and display link to OSM

This commit is contained in:
Jan-Lukas Else 2021-05-25 22:17:38 +02:00
parent bf9dc3f11a
commit cf316c4d18
7 changed files with 59 additions and 3 deletions

View File

@ -179,6 +179,7 @@ type configMicropub struct {
AudioParam string `mapstructure:"audioParam"` AudioParam string `mapstructure:"audioParam"`
PhotoParam string `mapstructure:"photoParam"` PhotoParam string `mapstructure:"photoParam"`
PhotoDescriptionParam string `mapstructure:"photoDescriptionParam"` PhotoDescriptionParam string `mapstructure:"photoDescriptionParam"`
LocationParam string `mapstructure:"locationParam"`
MediaStorage *configMicropubMedia `mapstructure:"mediaStorage"` MediaStorage *configMicropubMedia `mapstructure:"mediaStorage"`
} }
@ -251,6 +252,7 @@ func initConfig() error {
viper.SetDefault("micropub.audioParam", "audio") viper.SetDefault("micropub.audioParam", "audio")
viper.SetDefault("micropub.photoParam", "images") viper.SetDefault("micropub.photoParam", "images")
viper.SetDefault("micropub.photoDescriptionParam", "imagealts") viper.SetDefault("micropub.photoDescriptionParam", "imagealts")
viper.SetDefault("micropub.locationParam", "location")
viper.SetDefault("activityPub.keyPath", "data/private.pem") viper.SetDefault("activityPub.keyPath", "data/private.pem")
viper.SetDefault("activityPub.tagsTaxonomies", []string{"tags"}) viper.SetDefault("activityPub.tagsTaxonomies", []string{"tags"})
viper.SetDefault("webmention.disableSending", false) viper.SetDefault("webmention.disableSending", false)

1
go.mod
View File

@ -6,6 +6,7 @@ go 1.16
replace github.com/yuin/goldmark-emoji => github.com/jlelse/goldmark-emoji v1.0.2-0.20210524171342-a1615b7be38a replace github.com/yuin/goldmark-emoji => github.com/jlelse/goldmark-emoji v1.0.2-0.20210524171342-a1615b7be38a
require ( require (
git.jlel.se/jlelse/go-geouri v0.0.0-20210525190615-a9c1d50f42d6
git.jlel.se/jlelse/goldmark-mark v0.0.0-20210522162520-9788c89266a4 git.jlel.se/jlelse/goldmark-mark v0.0.0-20210522162520-9788c89266a4
git.jlel.se/jlelse/template-strings v0.0.0-20210523080439-57437f8d23ee git.jlel.se/jlelse/template-strings v0.0.0-20210523080439-57437f8d23ee
github.com/PuerkitoBio/goquery v1.6.1 github.com/PuerkitoBio/goquery v1.6.1

2
go.sum
View File

@ -12,6 +12,8 @@ cloud.google.com/go/firestore v1.1.0/go.mod h1:ulACoGHTpvq5r8rxGJ4ddJZBZqakUQqCl
cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I= cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I=
cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw= cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
git.jlel.se/jlelse/go-geouri v0.0.0-20210525190615-a9c1d50f42d6 h1:d7k1NKd9fr+Eq7EtUrqUly+HDqDzpx9T9v8Gl2jJvpo=
git.jlel.se/jlelse/go-geouri v0.0.0-20210525190615-a9c1d50f42d6/go.mod h1:eKL81ZHiGWZ4cdv9MI0ADmPiG9p0C+ajkSldNTr2ftQ=
git.jlel.se/jlelse/goldmark-mark v0.0.0-20210522162520-9788c89266a4 h1:p3c/vCY6MUADNXWCYI/hsZ8ZEvN7LtvwHzK2B8Y6BSk= git.jlel.se/jlelse/goldmark-mark v0.0.0-20210522162520-9788c89266a4 h1:p3c/vCY6MUADNXWCYI/hsZ8ZEvN7LtvwHzK2B8Y6BSk=
git.jlel.se/jlelse/goldmark-mark v0.0.0-20210522162520-9788c89266a4/go.mod h1:ZFhxwbX+afhgbzh5rpkSJUp6vIduNPtIGDrsWpIcHTE= git.jlel.se/jlelse/goldmark-mark v0.0.0-20210522162520-9788c89266a4/go.mod h1:ZFhxwbX+afhgbzh5rpkSJUp6vIduNPtIGDrsWpIcHTE=
git.jlel.se/jlelse/template-strings v0.0.0-20210523080439-57437f8d23ee h1:zun+B6UygGTYm+OBxK9eoPUHSd3M7AebIxXJyFyZzR4= git.jlel.se/jlelse/template-strings v0.0.0-20210523080439-57437f8d23ee h1:zun+B6UygGTYm+OBxK9eoPUHSd3M7AebIxXJyFyZzR4=

View File

@ -198,56 +198,81 @@ func convertMPValueMapToPost(values map[string][]string) (*post, error) {
if h, ok := values["h"]; ok && (len(h) != 1 || h[0] != "entry") { if h, ok := values["h"]; ok && (len(h) != 1 || h[0] != "entry") {
return nil, errors.New("only entry type is supported so far") return nil, errors.New("only entry type is supported so far")
} }
delete(values, "h")
entry := &post{ entry := &post{
Parameters: map[string][]string{}, Parameters: map[string][]string{},
} }
if content, ok := values["content"]; ok { if content, ok := values["content"]; ok {
entry.Content = content[0] entry.Content = content[0]
delete(values, "content")
} }
if published, ok := values["published"]; ok { if published, ok := values["published"]; ok {
entry.Published = published[0] entry.Published = published[0]
delete(values, "published")
} }
if updated, ok := values["updated"]; ok { if updated, ok := values["updated"]; ok {
entry.Updated = updated[0] entry.Updated = updated[0]
delete(values, "updated")
} }
if status, ok := values["post-status"]; ok { if status, ok := values["post-status"]; ok {
entry.Status = postStatus(status[0]) entry.Status = postStatus(status[0])
delete(values, "post-status")
}
if slug, ok := values["mp-slug"]; ok {
entry.Slug = slug[0]
delete(values, "mp-slug")
} }
// Parameter // Parameter
if name, ok := values["name"]; ok { if name, ok := values["name"]; ok {
entry.Parameters["title"] = name entry.Parameters["title"] = name
delete(values, "name")
} }
if category, ok := values["category"]; ok { if category, ok := values["category"]; ok {
entry.Parameters[appConfig.Micropub.CategoryParam] = category entry.Parameters[appConfig.Micropub.CategoryParam] = category
delete(values, "category")
} else if categories, ok := values["category[]"]; ok { } else if categories, ok := values["category[]"]; ok {
entry.Parameters[appConfig.Micropub.CategoryParam] = categories entry.Parameters[appConfig.Micropub.CategoryParam] = categories
delete(values, "category[]")
} }
if inReplyTo, ok := values["in-reply-to"]; ok { if inReplyTo, ok := values["in-reply-to"]; ok {
entry.Parameters[appConfig.Micropub.ReplyParam] = inReplyTo entry.Parameters[appConfig.Micropub.ReplyParam] = inReplyTo
delete(values, "in-reply-to")
} }
if likeOf, ok := values["like-of"]; ok { if likeOf, ok := values["like-of"]; ok {
entry.Parameters[appConfig.Micropub.LikeParam] = likeOf entry.Parameters[appConfig.Micropub.LikeParam] = likeOf
delete(values, "like-of")
} }
if bookmarkOf, ok := values["bookmark-of"]; ok { if bookmarkOf, ok := values["bookmark-of"]; ok {
entry.Parameters[appConfig.Micropub.BookmarkParam] = bookmarkOf entry.Parameters[appConfig.Micropub.BookmarkParam] = bookmarkOf
delete(values, "bookmark-of")
} }
if audio, ok := values["audio"]; ok { if audio, ok := values["audio"]; ok {
entry.Parameters[appConfig.Micropub.AudioParam] = audio entry.Parameters[appConfig.Micropub.AudioParam] = audio
delete(values, "audio")
} else if audio, ok := values["audio[]"]; ok { } else if audio, ok := values["audio[]"]; ok {
entry.Parameters[appConfig.Micropub.AudioParam] = audio entry.Parameters[appConfig.Micropub.AudioParam] = audio
delete(values, "audio[]")
} }
if photo, ok := values["photo"]; ok { if photo, ok := values["photo"]; ok {
entry.Parameters[appConfig.Micropub.PhotoParam] = photo entry.Parameters[appConfig.Micropub.PhotoParam] = photo
delete(values, "photo")
} else if photos, ok := values["photo[]"]; ok { } else if photos, ok := values["photo[]"]; ok {
entry.Parameters[appConfig.Micropub.PhotoParam] = photos entry.Parameters[appConfig.Micropub.PhotoParam] = photos
delete(values, "photo[]")
} }
if photoAlt, ok := values["mp-photo-alt"]; ok { if photoAlt, ok := values["mp-photo-alt"]; ok {
entry.Parameters[appConfig.Micropub.PhotoDescriptionParam] = photoAlt entry.Parameters[appConfig.Micropub.PhotoDescriptionParam] = photoAlt
delete(values, "mp-photo-alt")
} else if photoAlts, ok := values["mp-photo-alt[]"]; ok { } else if photoAlts, ok := values["mp-photo-alt[]"]; ok {
entry.Parameters[appConfig.Micropub.PhotoDescriptionParam] = photoAlts entry.Parameters[appConfig.Micropub.PhotoDescriptionParam] = photoAlts
delete(values, "mp-photo-alt[]")
} }
if slug, ok := values["mp-slug"]; ok { if location, ok := values["location"]; ok {
entry.Slug = slug[0] entry.Parameters[appConfig.Micropub.LocationParam] = location
delete(values, "location")
}
for n, p := range values {
entry.Parameters[n] = append(entry.Parameters[n], p...)
} }
err := entry.computeExtraPostParameters() err := entry.computeExtraPostParameters()
if err != nil { if err != nil {

View File

@ -15,6 +15,7 @@ import (
"strings" "strings"
"time" "time"
gogeouri "git.jlel.se/jlelse/go-geouri"
"github.com/araddon/dateparse" "github.com/araddon/dateparse"
servertiming "github.com/mitchellh/go-server-timing" servertiming "github.com/mitchellh/go-server-timing"
) )
@ -170,6 +171,16 @@ func initRendering() error {
"urlToString": func(u url.URL) string { "urlToString": func(u url.URL) string {
return u.String() return u.String()
}, },
"geouri": func(u string) *gogeouri.Geo {
g, _ := gogeouri.Parse(u)
return g
},
"geourip": func(g *gogeouri.Geo, parameter string) (s string) {
if gp := g.Parameters[parameter]; len(gp) > 0 {
return gp[0]
}
return
},
} }
baseTemplate, err := template.New("base").Funcs(templateFunctions).ParseFiles(path.Join(templatesDir, templateBase+templatesExt)) baseTemplate, err := template.New("base").Funcs(templateFunctions).ParseFiles(path.Join(templatesDir, templateBase+templatesExt))

View File

@ -1,5 +1,5 @@
{{ define "title" }} {{ define "title" }}
<title>{{ with p .Data "title" }}{{ . }} - {{end}}{{ .Blog.Title }}</title> <title>{{ with title .Data }}{{ . }} - {{end}}{{ .Blog.Title }}</title>
{{ include "postheadmeta" . }} {{ include "postheadmeta" . }}
{{ with shorturl .Data }}<link rel="shortlink" href="{{ . }}">{{ end }} {{ with shorturl .Data }}<link rel="shortlink" href="{{ . }}">{{ end }}
{{ end }} {{ end }}

View File

@ -1,6 +1,21 @@
{{ define "postmeta" }} {{ define "postmeta" }}
<div class="p"> <div class="p">
{{ include "summaryandpostmeta" . }} {{ include "summaryandpostmeta" . }}
{{ $loc := ( p .Data "location" ) }}
{{ if $loc }}
{{ with geouri $loc }}
<div>📍 <a class="p-location h-geo" href="https://www.openstreetmap.org/?mlat={{ .Latitude }}&mlon={{ .Longitude }}" target="_blank" rel="nofollow noopener noreferrer">
{{ $geoname := ( geourip . "name" ) }}
{{ if $geoname }}
<span class="p-name">{{ $geoname }}</span>
<data class="p-longitude" value="{{ .Longitude }}" />
<data class="p-latitude" value="{{ .Latitude }}" />
{{ else }}
<span class="p-latitude">{{ .Latitude }}</span>, <span class="p-longitude">{{ .Longitude }}</span>
{{ end }}
</a></div>
{{ end }}
{{ end }}
{{ $translations := (translations .Data) }} {{ $translations := (translations .Data) }}
{{ if gt (len $translations) 0 }} {{ if gt (len $translations) 0 }}
<div>{{ string .Blog.Lang "translations" }}: {{ $delimiter := "" }}{{ range $i, $t := $translations }}{{ $delimiter }}<a href="{{ $t.Path }}" translate="no">{{ title $t }}</a>{{ $delimiter = ", " }}{{ end }}</div> <div>{{ string .Blog.Lang "translations" }}: {{ $delimiter := "" }}{{ range $i, $t := $translations }}{{ $delimiter }}<a href="{{ $t.Path }}" translate="no">{{ title $t }}</a>{{ $delimiter = ", " }}{{ end }}</div>