| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package templatesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/ |
| 16 | |
| 17 | import ( |
| 18 | "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/" |
| 19 | "github.com/caddyserver/caddy/v2/caddyconfigscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig`/" |
| 20 | "github.com/caddyserver/caddy/v2/caddyconfig/caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/" |
| 21 | "github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/" |
| 22 | "github.com/caddyserver/caddy/v2/modules/caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/" |
| 23 | ) |
| 24 | |
| 25 | func initscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/init().() { |
| 26 | httpcaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/.RegisterHandlerDirectivescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/RegisterHandlerDirective().("templates", parseCaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/parseCaddyfile().) |
| 27 | } |
| 28 | |
| 29 | // parseCaddyfile sets up the handler from Caddyfile tokens. Syntax: |
| 30 | // |
| 31 | // templates [<matcher>] { |
| 32 | // mime <types...> |
| 33 | // between <open_delim> <close_delim> |
| 34 | // root <path> |
| 35 | // } |
| 36 | func parseCaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/parseCaddyfile().(h httpcaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/.Helperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#) (caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.MiddlewareHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MiddlewareHandler#, error) { |
| 37 | h.Nextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Next().() // consume directive name |
| 38 | t := new(Templatesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/Templates#) |
| 39 | for h.NextBlockscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextBlock().(0) { |
| 40 | switch h.Valscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Val().() { |
| 41 | case "mime": |
| 42 | t.MIMETypesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/Templates#MIMETypes. = h.RemainingArgsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#RemainingArgs().() |
| 43 | if len(t.MIMETypesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/Templates#MIMETypes.) == 0 { |
| 44 | return nil, h.ArgErrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#ArgErr().() |
| 45 | } |
| 46 | case "between": |
| 47 | t.Delimitersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/Templates#Delimiters. = h.RemainingArgsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#RemainingArgs().() |
| 48 | if len(t.Delimitersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/Templates#Delimiters.) != 2 { |
| 49 | return nil, h.ArgErrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#ArgErr().() |
| 50 | } |
| 51 | case "root": |
| 52 | if !h.Argsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Args().(&t.FileRootscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/Templates#FileRoot.) { |
| 53 | return nil, h.ArgErrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#ArgErr().() |
| 54 | } |
| 55 | case "extensions": |
| 56 | if h.NextArgscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextArg().() { |
| 57 | return nil, h.ArgErrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#ArgErr().() |
| 58 | } |
| 59 | if t.ExtensionsRawscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/Templates#ExtensionsRaw. != nil { |
| 60 | return nil, h.Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Err().("extensions already specified") |
| 61 | } |
| 62 | for nesting := h.Nestingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Nesting().(); h.NextBlockscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextBlock().(nesting); { |
| 63 | extensionModuleName := h.Valscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Val().() |
| 64 | modID := "http.handlers.templates.functions." + extensionModuleName |
| 65 | unm, err := caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.UnmarshalModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/UnmarshalModule().(h.Dispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#Dispenser., modID) |
| 66 | if err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | cf, ok := unm.(CustomFunctionsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/CustomFunctions#) |
| 70 | if !ok { |
| 71 | return nil, h.Errfscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Errf().("module %s (%T) does not provide template functions", modID, unm) |
| 72 | } |
| 73 | if t.ExtensionsRawscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/Templates#ExtensionsRaw. == nil { |
| 74 | t.ExtensionsRawscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/Templates#ExtensionsRaw. = make(caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleMap#) |
| 75 | } |
| 76 | t.ExtensionsRawscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/Templates#ExtensionsRaw.[extensionModuleName] = caddyconfigscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig`/.JSONscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig`/JSON().(cf, nil) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | return t, nil |
| 81 | } |