From f5478f396d6a15f29dee3915132af99e84716f4a Mon Sep 17 00:00:00 2001 From: tjpcc Date: Tue, 10 Oct 2023 17:02:49 -0600 Subject: support showyourwork template override directories on git directives fixes #5 --- example.conf | 4 +++- gemini.go | 2 +- gopher.go | 2 +- parse.go | 26 ++++++++++++++++++++++++++ types.go | 2 ++ 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/example.conf b/example.conf index 18f58f7..0a8f3e0 100644 --- a/example.conf +++ b/example.conf @@ -108,5 +108,7 @@ gemini 0.0.0.0:1965 { # "static", "cgi", and "git" directives support an "auth " clause which requires that an authentication to pass. cgi ~/public_gemini/cgi-bin/private at /~/cgi-bin/private auth private_gemini - git ~/code at /~/code + # The "templates" modifier can be used on "git" directives and provide a directory with template files in it. + # These can be used to customize the presentation of git repositories. + git ~/code at /~/code with templates /var/syw_templates } diff --git a/gemini.go b/gemini.go index 87af01f..975b62b 100644 --- a/gemini.go +++ b/gemini.go @@ -152,7 +152,7 @@ func addGeminiStaticRoute(router *sr.Router, route RouteDirective) { func addGeminiGitRoute(router *sr.Router, route RouteDirective) { buildAndAddRoute(router, route, func(route RouteDirective) sr.Handler { handler := sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { - subrouter := syw.GeminiRouter(route.FsPath, nil) + subrouter := syw.GeminiRouter(route.FsPath, route.Modifiers.Templates) reqclone := cloneRequest(request) reqclone.Path = strings.TrimPrefix(reqclone.Path, route.URLPath) diff --git a/gopher.go b/gopher.go index d5cf3c0..dcd2085 100644 --- a/gopher.go +++ b/gopher.go @@ -109,7 +109,7 @@ func addGopherCGIRoute(router *sr.Router, route RouteDirective) { func addGopherGitRoute(router *sr.Router, route RouteDirective) { buildAndAddRoute(router, route, func(route RouteDirective) sr.Handler { return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { - subrouter := syw.GopherRouter(route.FsPath, nil) + subrouter := syw.GopherRouter(route.FsPath, route.Modifiers.Templates) reqclone := cloneRequest(request) reqclone.Path = strings.TrimPrefix(reqclone.Path, route.URLPath) diff --git a/parse.go b/parse.go index d087e95..15c5877 100644 --- a/parse.go +++ b/parse.go @@ -8,8 +8,10 @@ import ( "io" "net" "os/user" + "path/filepath" "strconv" "strings" + "text/template" "github.com/go-kit/log/level" "tildegit.org/tjp/sliderule/gemini" @@ -448,12 +450,36 @@ func parseModifiers(text string) (Modifiers, string, error) { mod.titanName = text[0:idx] text = text[idx+1:] } + case "templates": + if sep != " " { + return mod, "", errors.New("invalid 'templates' clause") + } + text = strings.TrimLeft(text, " \t") + idx = strings.IndexAny(text, " \t,") + var err error + if idx == 0 { + return mod, "", errors.New("invalid 'templates' clause") + } else if idx < 0 { + mod.Templates, err = loadTemplates(text) + text = "" + } else { + mod.Templates, err = loadTemplates(text[0:idx]) + text = text[idx+1:] + } + + if err != nil { + return mod, "", err + } default: return mod, text, nil } } } +func loadTemplates(dirpath string) (*template.Template, error) { + return template.ParseGlob(filepath.Join(dirpath, "*")) +} + func parseAuth(text string) (string, string, error) { spl := strings.SplitN(text, " ", 2) switch len(spl) { diff --git a/types.go b/types.go index 3dd3935..9523d9c 100644 --- a/types.go +++ b/types.go @@ -5,6 +5,7 @@ import ( "crypto/tls" "net" "os/user" + "text/template" "github.com/go-kit/log/level" "tildegit.org/tjp/sliderule" @@ -18,6 +19,7 @@ type Modifiers struct { ExtendedGophermap bool AutoAtom bool Titan *Auth + Templates *template.Template titanName string } -- cgit v1.2.3