Add resources.Copy

Implemented by most Resource objects, but not Page (for now).

Fixes #9313
This commit is contained in:
Bjørn Erik Pedersen
2022-05-24 09:34:36 +02:00
parent 6f7fbe03b1
commit cd0112a05a
8 changed files with 169 additions and 13 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
// Copyright 2022 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.
@@ -120,8 +120,19 @@ func (t transformerNotAvailable) Key() internal.ResourceTransformationKey {
return t.key
}
// resourceCopier is for internal use.
type resourceCopier interface {
cloneTo(targetPath string) resource.Resource
}
// Copy copies r to the targetPath given.
func Copy(r resource.Resource, targetPath string) resource.Resource {
return r.(resourceCopier).cloneTo(targetPath)
}
type baseResourceResource interface {
resource.Cloner
resourceCopier
resource.ContentProvider
resource.Resource
resource.Identifier
@@ -225,6 +236,20 @@ func (l *genericResource) Clone() resource.Resource {
return l.clone()
}
func (l *genericResource) cloneTo(targetPath string) resource.Resource {
c := l.clone()
targetPath = helpers.ToSlashTrimLeading(targetPath)
dir, file := path.Split(targetPath)
c.resourcePathDescriptor = &resourcePathDescriptor{
relTargetDirFile: dirFile{dir: dir, file: file},
}
return c
}
func (l *genericResource) Content() (any, error) {
if err := l.initContent(); err != nil {
return nil, err