summaryrefslogtreecommitdiffstats
path: root/identity
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-11-27 13:42:36 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-12-18 11:44:40 +0100
commite625088ef5a970388ad50e464e87db56b358dac4 (patch)
treef7b26dec1f3695411558d05ca7d0995817a42250 /identity
parent67f3aa72cf9aaf3d6e447fa6bc12de704d46adf7 (diff)
Add render template hooks for links and images
This commit also * revises the change detection for templates used by content files in server mode. * Adds a Page.RenderString method Fixes #6545 Fixes #4663 Closes #6043
Diffstat (limited to 'identity')
-rw-r--r--identity/identity.go131
-rw-r--r--identity/identity_test.go42
2 files changed, 173 insertions, 0 deletions
diff --git a/identity/identity.go b/identity/identity.go
new file mode 100644
index 000000000..d06710efe
--- /dev/null
+++ b/identity/identity.go
@@ -0,0 +1,131 @@
+package identity
+
+import (
+ "path/filepath"
+ "strings"
+ "sync"
+)
+
+// NewIdentityManager creates a new Manager starting at id.
+func NewManager(id Provider) Manager {
+ return &identityManager{
+ Provider: id,
+ ids: Identities{id.GetIdentity(): id},
+ }
+}
+
+// NewPathIdentity creates a new Identity with the two identifiers
+// type and path.
+func NewPathIdentity(typ, pat string) PathIdentity {
+ pat = strings.ToLower(strings.TrimPrefix(filepath.ToSlash(pat), "/"))
+ return PathIdentity{Type: typ, Path: pat}
+}
+
+// Identities stores identity providers.
+type Identities map[Identity]Provider
+
+func (ids Identities) search(id Identity) Provider {
+ if v, found := ids[id]; found {
+ return v
+ }
+ for _, v := range ids {
+ switch t := v.(type) {
+ case IdentitiesProvider:
+ if nested := t.GetIdentities().search(id); nested != nil {
+ return nested
+ }
+ }
+ }
+ return nil
+}
+
+// IdentitiesProvider provides all Identities.
+type IdentitiesProvider interface {
+ GetIdentities() Identities
+}
+
+// Identity represents an thing that can provide an identify. This can be
+// any Go type, but the Identity returned by GetIdentify must be hashable.
+type Identity interface {
+ Provider
+ Name() string
+}
+
+// Manager manages identities, and is itself a Provider of Identity.
+type Manager interface {
+ IdentitiesProvider
+ Provider
+ Add(ids ...Provider)
+ Search(id Identity) Provider
+ Reset()
+}
+
+// A PathIdentity is a common identity identified by a type and a path, e.g. "layouts" and "_default/single.html".
+type PathIdentity struct {
+ Type string
+ Path string
+}
+
+// GetIdentity returns itself.
+func (id PathIdentity) GetIdentity() Identity {
+ return id
+}
+
+// Name returns the Path.
+func (id PathIdentity) Name() string {
+ return id.Path
+}
+
+// A KeyValueIdentity a general purpose identity.
+type KeyValueIdentity struct {
+ Key string
+ Value string
+}
+
+// GetIdentity returns itself.
+func (id KeyValueIdentity) GetIdentity() Identity {
+ return id
+}
+
+// Name returns the Key.
+func (id KeyValueIdentity) Name() string {
+ return id.Key
+}
+
+// Provider provides the hashable Identity.
+type Provider interface {
+ GetIdentity() Identity
+}
+
+type identityManager struct {
+ sync.Mutex
+ Provider
+ ids Identities
+}
+
+func (im *identityManager) Add(ids ...Provider) {
+ im.Lock()
+ for _, id := range ids {
+ im.ids[id.GetIdentity()] = id
+ }
+ im.Unlock()
+}
+
+func (im *identityManager) Reset() {
+ im.Lock()
+ id := im.GetIdentity()
+ im.ids = Identities{id.GetIdentity(): id}
+ im.Unlock()
+}
+
+func (im *identityManager) GetIdentities() Identities {
+ im.Lock()
+ defer im.Unlock()
+ return im.ids
+}
+
+func (im *identityManager) Search(id Identity) Provider {
+ im.Lock()
+ defer im.Unlock()
+ return im.ids.search(id.GetIdentity())
+}
diff --git a/identity/identity_test.go b/identity/identity_test.go
new file mode 100644
index 000000000..adebcad91
--- /dev/null
+++ b/identity/identity_test.go
@@ -0,0 +1,42 @@
+// Copyright 2019 The Hugo Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package identity
+
+import (
+ "testing"
+
+ qt "github.com/frankban/quicktest"
+)
+
+func TestIdentityManager(t *testing.T) {
+ c := qt.New(t)
+
+ id1 := testIdentity{name: "id1"}
+ im := NewManager(id1)
+
+ c.Assert(im.Search(id1).GetIdentity(), qt.Equals, id1)
+ c.Assert(im.Search(testIdentity{name: "notfound"}), qt.Equals, nil)
+}
+
+type testIdentity struct {
+ name string
+}
+
+func (id testIdentity) GetIdentity() Identity {
+ return id
+}
+
+func (id testIdentity) Name() string {
+ return id.name
+}