diff options
| author | tjpcc <tjp@ctrl-c.club> | 2023-10-10 17:02:49 -0600 |
|---|---|---|
| committer | tjpcc <tjp@ctrl-c.club> | 2023-10-10 18:13:59 -0600 |
| commit | f5478f396d6a15f29dee3915132af99e84716f4a (patch) | |
| tree | dff518257813e2071f6e58875ace0e65b7f00779 | |
| parent | 365b6bd5319cde40b5cf34b73d01e0fe5755d92e (diff) | |
support showyourwork template override directories on git directives
fixes #5
| -rw-r--r-- | example.conf | 4 | ||||
| -rw-r--r-- | gemini.go | 2 | ||||
| -rw-r--r-- | gopher.go | 2 | ||||
| -rw-r--r-- | parse.go | 26 | ||||
| -rw-r--r-- | 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 <name>" 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 } @@ -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) @@ -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) @@ -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) { @@ -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 } |
