mirror of
https://github.com/gohugoio/hugo.git
synced 2025-05-11 21:29:08 +03:00
parent
1074e01152
commit
d1c394442b
4 changed files with 47 additions and 15 deletions
|
@ -60,12 +60,13 @@ type descriptorHandler struct {
|
||||||
|
|
||||||
// Note that this in this setup is usually a descriptor constructed from a page,
|
// Note that this in this setup is usually a descriptor constructed from a page,
|
||||||
// so we want to find the best match for that page.
|
// so we want to find the best match for that page.
|
||||||
func (s descriptorHandler) compareDescriptors(category Category, this, other TemplateDescriptor) weight {
|
func (s descriptorHandler) compareDescriptors(category Category, isEmbedded bool, this, other TemplateDescriptor) weight {
|
||||||
if this.LayoutMustMatch && this.Layout != other.Layout {
|
if this.LayoutMustMatch && this.Layout != other.Layout {
|
||||||
return weightNoMatch
|
return weightNoMatch
|
||||||
}
|
}
|
||||||
|
|
||||||
w := this.doCompare(category, other)
|
w := this.doCompare(category, isEmbedded, other)
|
||||||
|
|
||||||
if w.w1 <= 0 {
|
if w.w1 <= 0 {
|
||||||
if category == CategoryMarkup && (this.Variant1 == other.Variant1) && (this.Variant2 == other.Variant2 || this.Variant2 != "" && other.Variant2 == "") {
|
if category == CategoryMarkup && (this.Variant1 == other.Variant1) && (this.Variant2 == other.Variant2 || this.Variant2 != "" && other.Variant2 == "") {
|
||||||
// See issue 13242.
|
// See issue 13242.
|
||||||
|
@ -82,7 +83,7 @@ func (s descriptorHandler) compareDescriptors(category Category, this, other Tem
|
||||||
}
|
}
|
||||||
|
|
||||||
//lint:ignore ST1006 this vs other makes it easier to reason about.
|
//lint:ignore ST1006 this vs other makes it easier to reason about.
|
||||||
func (this TemplateDescriptor) doCompare(category Category, other TemplateDescriptor) weight {
|
func (this TemplateDescriptor) doCompare(category Category, isEmbedded bool, other TemplateDescriptor) weight {
|
||||||
w := weightNoMatch
|
w := weightNoMatch
|
||||||
|
|
||||||
// HTML in plain text is OK, but not the other way around.
|
// HTML in plain text is OK, but not the other way around.
|
||||||
|
@ -135,10 +136,16 @@ func (this TemplateDescriptor) doCompare(category Category, other TemplateDescri
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if isEmbedded {
|
||||||
|
if other.Variant2 != "" && other.Variant2 != this.Variant2 {
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
} else {
|
||||||
// If both are set and different, no match.
|
// If both are set and different, no match.
|
||||||
if other.Variant2 != "" && this.Variant2 != "" && other.Variant2 != this.Variant2 {
|
if other.Variant2 != "" && this.Variant2 != "" && other.Variant2 != this.Variant2 {
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
weightKind = 3 // page, home, section, taxonomy, term (and only those)
|
weightKind = 3 // page, home, section, taxonomy, term (and only those)
|
||||||
|
|
|
@ -20,14 +20,14 @@ func TestTemplateDescriptorCompare(t *testing.T) {
|
||||||
|
|
||||||
less := func(category Category, this, other1, other2 TemplateDescriptor) {
|
less := func(category Category, this, other1, other2 TemplateDescriptor) {
|
||||||
c.Helper()
|
c.Helper()
|
||||||
result1 := dh.compareDescriptors(category, this, other1)
|
result1 := dh.compareDescriptors(category, false, this, other1)
|
||||||
result2 := dh.compareDescriptors(category, this, other2)
|
result2 := dh.compareDescriptors(category, false, this, other2)
|
||||||
c.Assert(result1.w1 < result2.w1, qt.IsTrue, qt.Commentf("%d < %d", result1, result2))
|
c.Assert(result1.w1 < result2.w1, qt.IsTrue, qt.Commentf("%d < %d", result1, result2))
|
||||||
}
|
}
|
||||||
|
|
||||||
check := func(category Category, this, other TemplateDescriptor, less bool) {
|
check := func(category Category, this, other TemplateDescriptor, less bool) {
|
||||||
c.Helper()
|
c.Helper()
|
||||||
result := dh.compareDescriptors(category, this, other)
|
result := dh.compareDescriptors(category, false, this, other)
|
||||||
if less {
|
if less {
|
||||||
c.Assert(result.w1 < 0, qt.IsTrue, qt.Commentf("%d", result))
|
c.Assert(result.w1 < 0, qt.IsTrue, qt.Commentf("%d", result))
|
||||||
} else {
|
} else {
|
||||||
|
@ -98,7 +98,7 @@ func BenchmarkCompareDescriptors(b *testing.B) {
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
for _, pair := range pairs {
|
for _, pair := range pairs {
|
||||||
_ = dh.compareDescriptors(CategoryLayout, pair.d1, pair.d2)
|
_ = dh.compareDescriptors(CategoryLayout, false, pair.d1, pair.d2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -311,7 +311,7 @@ func (ti *TemplInfo) findBestMatchBaseof(s *TemplateStore, d1 TemplateDescriptor
|
||||||
distance := slashCountK1 - slashCountK2
|
distance := slashCountK1 - slashCountK2
|
||||||
|
|
||||||
for d2, vv := range v {
|
for d2, vv := range v {
|
||||||
weight := s.dh.compareDescriptors(CategoryBaseof, d1, d2)
|
weight := s.dh.compareDescriptors(CategoryBaseof, false, d1, d2)
|
||||||
weight.distance = distance
|
weight.distance = distance
|
||||||
if best.isBetter(weight, vv.Template) {
|
if best.isBetter(weight, vv.Template) {
|
||||||
best.updateValues(weight, k2, d2, vv.Template)
|
best.updateValues(weight, k2, d2, vv.Template)
|
||||||
|
@ -447,7 +447,7 @@ func (s *TemplateStore) FindAllBaseTemplateCandidates(overlayKey string, desc Te
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if vv.D.isKindInLayout(desc.Layout) && s.dh.compareDescriptors(CategoryBaseof, descBaseof, vv.D).w1 > 0 {
|
if vv.D.isKindInLayout(desc.Layout) && s.dh.compareDescriptors(CategoryBaseof, false, descBaseof, vv.D).w1 > 0 {
|
||||||
result = append(result, keyTemplateInfo{Key: k, Info: vv})
|
result = append(result, keyTemplateInfo{Key: k, Info: vv})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -584,7 +584,7 @@ func (s *TemplateStore) LookupShortcode(q TemplateQuery) *TemplInfo {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
weight := s.dh.compareDescriptors(q.Category, q.Desc, k)
|
weight := s.dh.compareDescriptors(q.Category, vv.subCategory == SubCategoryEmbedded, q.Desc, k)
|
||||||
weight.distance = distance
|
weight.distance = distance
|
||||||
if best.isBetter(weight, vv) {
|
if best.isBetter(weight, vv) {
|
||||||
best.updateValues(weight, k2, k, vv)
|
best.updateValues(weight, k2, k, vv)
|
||||||
|
@ -737,7 +737,7 @@ func (s *TemplateStore) findBestMatchGet(key string, category Category, consider
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
weight := s.dh.compareDescriptors(category, desc, k.d)
|
weight := s.dh.compareDescriptors(category, vv.subCategory == SubCategoryEmbedded, desc, k.d)
|
||||||
if best.isBetter(weight, vv) {
|
if best.isBetter(weight, vv) {
|
||||||
best.updateValues(weight, key, k.d, vv)
|
best.updateValues(weight, key, k.d, vv)
|
||||||
}
|
}
|
||||||
|
@ -758,7 +758,7 @@ func (s *TemplateStore) findBestMatchWalkPath(q TemplateQuery, k1 string, slashC
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
weight := s.dh.compareDescriptors(q.Category, q.Desc, k.d)
|
weight := s.dh.compareDescriptors(q.Category, vv.subCategory == SubCategoryEmbedded, q.Desc, k.d)
|
||||||
|
|
||||||
weight.distance = distance
|
weight.distance = distance
|
||||||
isBetter := best.isBetter(weight, vv)
|
isBetter := best.isBetter(weight, vv)
|
||||||
|
|
|
@ -826,6 +826,31 @@ func TestPartialHTML(t *testing.T) {
|
||||||
b.AssertFileContent("public/index.html", "<link rel=\"stylesheet\" href=\"/css/style.css\">")
|
b.AssertFileContent("public/index.html", "<link rel=\"stylesheet\" href=\"/css/style.css\">")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Issue #13593.
|
||||||
|
func TestNoGoat(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
files := `
|
||||||
|
-- hugo.toml --
|
||||||
|
-- content/_index.md --
|
||||||
|
---
|
||||||
|
title: "Home"
|
||||||
|
---
|
||||||
|
|
||||||
|
§§§
|
||||||
|
printf "Hello, world!"
|
||||||
|
§§§
|
||||||
|
|
||||||
|
-- layouts/all.html --
|
||||||
|
{{ .Content }}
|
||||||
|
|
||||||
|
`
|
||||||
|
|
||||||
|
b := hugolib.Test(t, files)
|
||||||
|
|
||||||
|
b.AssertFileContent("public/index.html", "Hello, world!")
|
||||||
|
}
|
||||||
|
|
||||||
// Issue #13515
|
// Issue #13515
|
||||||
func TestPrintPathWarningOnDotRemoval(t *testing.T) {
|
func TestPrintPathWarningOnDotRemoval(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue