github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/hugolib/page__ref.go (about) 1 // Copyright 2019 The Hugo Authors. All rights reserved. 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 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package hugolib 15 16 import ( 17 "fmt" 18 19 "github.com/gohugoio/hugo/common/text" 20 21 "github.com/mitchellh/mapstructure" 22 "github.com/pkg/errors" 23 ) 24 25 func newPageRef(p *pageState) pageRef { 26 return pageRef{p: p} 27 } 28 29 type pageRef struct { 30 p *pageState 31 } 32 33 func (p pageRef) Ref(argsm map[string]interface{}) (string, error) { 34 return p.ref(argsm, p.p) 35 } 36 37 func (p pageRef) RefFrom(argsm map[string]interface{}, source interface{}) (string, error) { 38 return p.ref(argsm, source) 39 } 40 41 func (p pageRef) RelRef(argsm map[string]interface{}) (string, error) { 42 return p.relRef(argsm, p.p) 43 } 44 45 func (p pageRef) RelRefFrom(argsm map[string]interface{}, source interface{}) (string, error) { 46 return p.relRef(argsm, source) 47 } 48 49 func (p pageRef) decodeRefArgs(args map[string]interface{}) (refArgs, *Site, error) { 50 var ra refArgs 51 err := mapstructure.WeakDecode(args, &ra) 52 if err != nil { 53 return ra, nil, nil 54 } 55 56 s := p.p.s 57 58 if ra.Lang != "" && ra.Lang != p.p.s.Language().Lang { 59 // Find correct site 60 found := false 61 for _, ss := range p.p.s.h.Sites { 62 if ss.Lang() == ra.Lang { 63 found = true 64 s = ss 65 } 66 } 67 68 if !found { 69 p.p.s.siteRefLinker.logNotFound(ra.Path, fmt.Sprintf("no site found with lang %q", ra.Lang), nil, text.Position{}) 70 return ra, nil, nil 71 } 72 } 73 74 return ra, s, nil 75 } 76 77 func (p pageRef) ref(argsm map[string]interface{}, source interface{}) (string, error) { 78 args, s, err := p.decodeRefArgs(argsm) 79 if err != nil { 80 return "", errors.Wrap(err, "invalid arguments to Ref") 81 } 82 83 if s == nil { 84 return p.p.s.siteRefLinker.notFoundURL, nil 85 } 86 87 if args.Path == "" { 88 return "", nil 89 } 90 91 return s.refLink(args.Path, source, false, args.OutputFormat) 92 } 93 94 func (p pageRef) relRef(argsm map[string]interface{}, source interface{}) (string, error) { 95 args, s, err := p.decodeRefArgs(argsm) 96 if err != nil { 97 return "", errors.Wrap(err, "invalid arguments to Ref") 98 } 99 100 if s == nil { 101 return p.p.s.siteRefLinker.notFoundURL, nil 102 } 103 104 if args.Path == "" { 105 return "", nil 106 } 107 108 return s.refLink(args.Path, source, true, args.OutputFormat) 109 } 110 111 type refArgs struct { 112 Path string 113 Lang string 114 OutputFormat string 115 }