Skip to content

Commit

Permalink
feat: locations plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 11, 2025
1 parent 9bca13a commit 4018144
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/eagle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

_ "go.hacdias.com/eagle/plugins/external-links"
_ "go.hacdias.com/eagle/plugins/linkding"
_ "go.hacdias.com/eagle/plugins/locations"
_ "go.hacdias.com/eagle/plugins/mastodon"
_ "go.hacdias.com/eagle/plugins/miniflux"
_ "go.hacdias.com/eagle/plugins/webarchive"
Expand Down
5 changes: 5 additions & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,8 @@ plugins:
# Which fields should be archived. It is either a string with @all or
# an array with the list of fields. @all is equivalent to [bookmark-of].
fields: "@all"

# Optional Locations plugin.
locations:
# Tries to expand the "location" property into a more detailed object.
expand: true
5 changes: 3 additions & 2 deletions core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,9 @@ type ImgProxy struct {
}

type SiteConfig struct {
Taxonomies map[string]string
Pagination struct {
LanguageCode string
Taxonomies map[string]string
Pagination struct {
PagerSize int
}
Params struct {
Expand Down
5 changes: 5 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,8 @@ func NewCore(cfg *Config) (*Core, error) {
func (co *Core) BaseURL() *url.URL {
return cloneURL(co.baseURL)
}

// Language returns the language of the website.
func (co *Core) Language() string {
return co.cfg.Site.LanguageCode
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/writeas/go-strip-markdown/v2 v2.1.1
go.etcd.io/bbolt v1.4.0
go.hacdias.com/indielib v0.4.3
go.hacdias.com/maze v0.4.0
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.33.0
golang.org/x/net v0.34.0
Expand All @@ -35,6 +36,7 @@ require (
)

require (
git.jlel.se/jlelse/go-geouri v0.0.0-20210525190615-a9c1d50f42d6 // indirect
github.com/andybalholm/brotli v1.1.1 // indirect
github.com/andybalholm/cascadia v1.3.3 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
Expand All @@ -58,6 +60,7 @@ require (
github.com/magiconair/properties v1.8.9 // indirect
github.com/mailru/easyjson v0.9.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/paulmach/go.geojson v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
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=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
Expand Down Expand Up @@ -379,6 +381,8 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/paulmach/go.geojson v1.5.0 h1:7mhpMK89SQdHFcEGomT7/LuJhwhEgfmpWYVlVmLEdQw=
github.com/paulmach/go.geojson v1.5.0/go.mod h1:DgdUy2rRVDDVgKqrjMe2vZAHMfhDTrjVKt3LmHIXGbU=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml/v2 v2.0.5/go.mod h1:OMHamSCAODeSsVrwwvcJOaoN0LIUIaFVNZzmWyNfXas=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
Expand Down Expand Up @@ -495,6 +499,10 @@ go.etcd.io/etcd/client/v2 v2.305.4/go.mod h1:Ud+VUwIi9/uQHOMA+4ekToJ12lTxlv0zB/+
go.etcd.io/etcd/client/v3 v3.5.4/go.mod h1:ZaRkVgBZC+L+dLCjTcF1hRXpgZXQPOvnA/Ak/gq3kiY=
go.hacdias.com/indielib v0.4.3 h1:1QT0ZzMk+vMkoe4uZ31DLnWlLklGPgBYry0I+lCl0qM=
go.hacdias.com/indielib v0.4.3/go.mod h1:W7tSM6pCiM2JLdZ8xzSMpPf3GBB2hz+ONvGfvdp6S9o=
go.hacdias.com/maze v0.3.0 h1:8Que0B+k3juO9sGoAfSCRiK3xloD1qqvDPq5NA840UQ=
go.hacdias.com/maze v0.3.0/go.mod h1:jPceWckKjTDyyr4xgQkDEs0I4jYOZljoxFPs/zBHqlU=
go.hacdias.com/maze v0.4.0 h1:KqajgnOgxPFn+sgjlhr89jway7L728Z+O/lGdjnt1lY=
go.hacdias.com/maze v0.4.0/go.mod h1:jPceWckKjTDyyr4xgQkDEs0I4jYOZljoxFPs/zBHqlU=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
Expand Down
85 changes: 85 additions & 0 deletions plugins/locations/locations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package locations

import (
"net/http"
"strings"
"time"

"github.com/karlseguin/typed"
"go.hacdias.com/eagle/core"
"go.hacdias.com/eagle/server"
"go.hacdias.com/maze"
)

var (
_ server.HookPlugin = &Locations{}
)

func init() {
server.RegisterPlugin("locations", NewLocations)
}

type Locations struct {
core *core.Core
maze *maze.Maze
language string
expand bool
}

func NewLocations(co *core.Core, config map[string]interface{}) (server.Plugin, error) {
cfg := typed.New(config)

language := co.Language()
if language == "" {
language = "en"
}

return &Locations{
core: co,
maze: maze.NewMaze(&http.Client{
Timeout: time.Minute,
}),
language: language,
expand: cfg.Bool("expand"),
}, nil
}

func (l *Locations) PreSaveHook(*core.Entry) error {
return nil
}

func (l *Locations) PostSaveHook(e *core.Entry) error {
if !l.expand {
return nil
}

locationStr := typed.New(e.Other).String("location")
if locationStr == "" {
return nil
}

location, err := l.parseLocation(locationStr)
if err != nil {
return err
}

if location == nil {
return nil
}

e.Other["location"] = location
return l.core.SaveEntry(e)
}

func (l *Locations) parseLocation(str string) (*maze.Location, error) {
if strings.HasPrefix(str, "geo:") {
return l.maze.ReverseGeoURI(l.language, str)
} else if strings.HasPrefix(str, "airport:") {
code := strings.TrimPrefix(str, "airport:")
return l.maze.Airport(code)
} else {
return l.maze.Search(l.language, str)
}

// Also add swarm option
}

0 comments on commit 4018144

Please sign in to comment.