Created urls template funcs namespace

This commit is contained in:
Maxim Lebedev 2023-11-09 07:50:08 +06:00
parent d677254e32
commit 70853bc665
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
1 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,42 @@
package urls
import (
"context"
"errors"
"fmt"
"net/url"
"path"
"golang.org/x/text/language"
"source.toby3d.me/toby3d/home/internal/site"
)
type Namespace struct {
siter site.UseCase
}
var ErrAbsURL error = errors.New("unsupported input type for AbsURL")
func New(siter site.UseCase) *Namespace {
return &Namespace{
siter: siter,
}
}
func (ns *Namespace) AbsURL(p string) (string, error) {
site, err := ns.siter.Do(context.Background(), language.Und)
if err != nil {
return "", fmt.Errorf("cannot fetch site root for AbsURL processing: %w", err)
}
return site.BaseURL.JoinPath(p).String(), nil
}
func (ns *Namespace) RelURL(p string) string {
return path.Clean("/" + p)
}
func (ns *Namespace) Parse(rawUrl string) (*url.URL, error) {
return url.Parse(rawUrl)
}