Skip to content

Commit 17ab042

Browse files
committed
Short
1 parent e775fed commit 17ab042

15 files changed

+129
-76
lines changed

common/paths/pathparser.go

+37-18
Original file line numberDiff line numberDiff line change
@@ -240,18 +240,18 @@ func (pp *PathParser) doParse(component, s string, p *Path) (*Path, error) {
240240
if isContent {
241241
switch b {
242242
case "index":
243-
p.bundleType = PathTypeLeaf
243+
p.pathType = PathTypeLeaf
244244
case "_index":
245-
p.bundleType = PathTypeBranch
245+
p.pathType = PathTypeBranch
246246
default:
247-
p.bundleType = PathTypeContentSingle
247+
p.pathType = PathTypeContentSingle
248248
}
249249

250250
if slashCount == 2 && p.IsLeafBundle() {
251251
p.posSectionHigh = 0
252252
}
253253
} else if b == files.NameContentData && files.IsContentDataExt(p.Ext()) {
254-
p.bundleType = PathTypeContentData
254+
p.pathType = PathTypeContentData
255255
}
256256
}
257257

@@ -262,20 +262,34 @@ func (pp *PathParser) doParse(component, s string, p *Path) (*Path, error) {
262262
}
263263
}
264264

265+
if component == files.ComponentFolderLayouts {
266+
pth := p.Path()
267+
if strings.Contains(pth, "/_shortcodes/") {
268+
p.pathType = PathTypeShortcode
269+
} else if strings.Contains(pth, "/_markup/") {
270+
p.pathType = PathTypeMarkup
271+
} else if strings.HasPrefix(pth, "/_partials/") {
272+
p.pathType = PathTypePartial
273+
}
274+
}
275+
265276
return p, nil
266277
}
267278

268279
func ModifyPathBundleTypeResource(p *Path) {
269280
if p.IsContent() {
270-
p.bundleType = PathTypeContentResource
281+
p.pathType = PathTypeContentResource
271282
} else {
272-
p.bundleType = PathTypeFile
283+
p.pathType = PathTypeFile
273284
}
274285
}
275286

287+
//go:generate stringer -type PathType
288+
276289
type PathType int
277290

278291
const (
292+
279293
// A generic resource, e.g. a JSON file.
280294
PathTypeFile PathType = iota
281295

@@ -296,6 +310,11 @@ const (
296310

297311
// Content data file, _content.gotmpl.
298312
PathTypeContentData
313+
314+
// Layout types.
315+
PathTypeMarkup
316+
PathTypeShortcode
317+
PathTypePartial
299318
)
300319

301320
type Path struct {
@@ -306,8 +325,8 @@ type Path struct {
306325
posContainerHigh int
307326
posSectionHigh int
308327

309-
component string
310-
bundleType PathType
328+
component string
329+
pathType PathType
311330

312331
identifiers []types.LowHigh[string]
313332

@@ -345,7 +364,7 @@ func (p *Path) reset() {
345364
p.posContainerHigh = -1
346365
p.posSectionHigh = -1
347366
p.component = ""
348-
p.bundleType = 0
367+
p.pathType = 0
349368
p.identifiers = p.identifiers[:0]
350369
p.posIdentifierLanguage = -1
351370
p.posIdentifierOutputFormat = -1
@@ -416,13 +435,13 @@ func (p *Path) Section() string {
416435
// IsContent returns true if the path is a content file (e.g. mypost.md).
417436
// Note that this will also return true for content files in a bundle.
418437
func (p *Path) IsContent() bool {
419-
return p.BundleType() >= PathTypeContentResource
438+
return p.Type() >= PathTypeContentResource && p.Type() <= PathTypeContentData
420439
}
421440

422441
// isContentPage returns true if the path is a content file (e.g. mypost.md),
423442
// but nof if inside a leaf bundle.
424443
func (p *Path) isContentPage() bool {
425-
return p.BundleType() >= PathTypeContentSingle
444+
return p.Type() >= PathTypeContentSingle && p.Type() <= PathTypeContentData
426445
}
427446

428447
// Name returns the last element of path.
@@ -650,28 +669,28 @@ func (p *Path) IdentifiersUnknown() []string {
650669
return ids
651670
}
652671

653-
func (p *Path) BundleType() PathType {
654-
return p.bundleType
672+
func (p *Path) Type() PathType {
673+
return p.pathType
655674
}
656675

657676
func (p *Path) IsBundle() bool {
658-
return p.bundleType >= PathTypeLeaf
677+
return p.pathType >= PathTypeLeaf && p.pathType <= PathTypeContentData
659678
}
660679

661680
func (p *Path) IsBranchBundle() bool {
662-
return p.bundleType == PathTypeBranch
681+
return p.pathType == PathTypeBranch
663682
}
664683

665684
func (p *Path) IsLeafBundle() bool {
666-
return p.bundleType == PathTypeLeaf
685+
return p.pathType == PathTypeLeaf
667686
}
668687

669688
func (p *Path) IsContentData() bool {
670-
return p.bundleType == PathTypeContentData
689+
return p.pathType == PathTypeContentData
671690
}
672691

673692
func (p Path) ForBundleType(t PathType) *Path {
674-
p.bundleType = t
693+
p.pathType = t
675694
return &p
676695
}
677696

common/paths/pathparser_test.go

+53-11
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func TestParse(t *testing.T) {
140140

141141
// Reclassify it as a content resource.
142142
ModifyPathBundleTypeResource(p)
143-
c.Assert(p.BundleType(), qt.Equals, PathTypeContentResource)
143+
c.Assert(p.Type(), qt.Equals, PathTypeContentResource)
144144
c.Assert(p.IsContent(), qt.IsTrue)
145145
c.Assert(p.Name(), qt.Equals, "b.md")
146146
c.Assert(p.Base(), qt.Equals, "/a/b.md")
@@ -386,41 +386,41 @@ func TestParseLayouts(t *testing.T) {
386386
}{
387387
{
388388
"Basic",
389-
"/_default/list.html",
389+
"/list.html",
390390
func(c *qt.C, p *Path) {
391-
c.Assert(p.Base(), qt.Equals, "/_default/list.html")
391+
c.Assert(p.Base(), qt.Equals, "/list.html")
392392
},
393393
},
394394
{
395395
"Lang",
396-
"/_default/list.no.html",
396+
"/list.no.html",
397397
func(c *qt.C, p *Path) {
398-
c.Assert(p.Base(), qt.Equals, "/_default/list.html")
398+
c.Assert(p.Base(), qt.Equals, "/list.html")
399399
c.Assert(p.Lang(), qt.Equals, "no")
400400
},
401401
},
402402
{
403403
"Lang and output format",
404-
"/_default/list.no.amp.not.html",
404+
"/list.no.amp.not.html",
405405
func(c *qt.C, p *Path) {
406406
c.Assert(p.Identifiers(), qt.DeepEquals, []string{"html", "not", "amp", "no"})
407407
c.Assert(p.OutputFormat(), qt.Equals, "amp")
408408
c.Assert(p.Ext(), qt.Equals, "html")
409409
c.Assert(p.Lang(), qt.Equals, "no")
410-
c.Assert(p.Base(), qt.Equals, "/_default/list.html")
410+
c.Assert(p.Base(), qt.Equals, "/list.html")
411411
},
412412
},
413413
{
414414
"Term",
415-
"/_default/term.html",
415+
"/term.html",
416416
func(c *qt.C, p *Path) {
417417
c.Assert(p.Identifiers(), qt.DeepEquals, []string{"html", "term"})
418-
c.Assert(p.PathNoIdentifier(), qt.Equals, "/_default")
419-
c.Assert(p.PathBeforeLangAndOutputFormatAndExt(), qt.Equals, "/_default/term")
418+
c.Assert(p.PathNoIdentifier(), qt.Equals, "/")
419+
c.Assert(p.PathBeforeLangAndOutputFormatAndExt(), qt.Equals, "/term")
420420
c.Assert(p.Lang(), qt.Equals, "")
421421
c.Assert(p.Kind(), qt.Equals, "term")
422422
c.Assert(p.OutputFormat(), qt.Equals, "html")
423-
c.Assert(p.Base(), qt.Equals, "/_default.html") // TODO1
423+
c.Assert(p.Base(), qt.Equals, "/.html") // TODO1
424424
},
425425
},
426426
{
@@ -447,6 +447,48 @@ func TestParseLayouts(t *testing.T) {
447447
c.Assert(p.NameNoIdentifier(), qt.Equals, "baseof")
448448
},
449449
},
450+
{
451+
"Markup",
452+
"/_markup/render-link.html",
453+
func(c *qt.C, p *Path) {
454+
c.Assert(p.Type(), qt.Equals, PathTypeMarkup)
455+
},
456+
},
457+
{
458+
"Markup nested",
459+
"/foo/_markup/render-link.html",
460+
func(c *qt.C, p *Path) {
461+
c.Assert(p.Type(), qt.Equals, PathTypeMarkup)
462+
},
463+
},
464+
{
465+
"Shortcode",
466+
"/_shortcodes/myshortcode.html",
467+
func(c *qt.C, p *Path) {
468+
c.Assert(p.Type(), qt.Equals, PathTypeShortcode)
469+
},
470+
},
471+
{
472+
"Shortcode nested",
473+
"/foo/_shortcodes/myshortcode.html",
474+
func(c *qt.C, p *Path) {
475+
c.Assert(p.Type(), qt.Equals, PathTypeShortcode)
476+
},
477+
},
478+
{
479+
"Shortcode nested sub",
480+
"/foo/_shortcodes/foo/myshortcode.html",
481+
func(c *qt.C, p *Path) {
482+
c.Assert(p.Type(), qt.Equals, PathTypeShortcode)
483+
},
484+
},
485+
{
486+
"Partials",
487+
"/_partials/foo.bar",
488+
func(c *qt.C, p *Path) {
489+
c.Assert(p.Type(), qt.Equals, PathTypePartial)
490+
},
491+
},
450492
}
451493

452494
for _, test := range tests {

common/paths/pathtype_string.go

+7-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hugolib/content_map.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func (m *pageMap) AddFi(fi hugofs.FileMetaInfo, buildConfig *BuildCfg) (pageCoun
264264
meta := fi.Meta()
265265
pi := meta.PathInfo
266266

267-
switch pi.BundleType() {
267+
switch pi.Type() {
268268
case paths.PathTypeFile, paths.PathTypeContentResource:
269269
m.s.Log.Trace(logg.StringFunc(
270270
func() string {

hugolib/page.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -476,15 +476,17 @@ func (ps *pageState) initCommonProviders(pp pagePaths) error {
476476
return nil
477477
}
478478

479-
func (p *pageState) getBaseAndTemplateDescriptor() (string, tplimpl.TemplateDescriptor) {
479+
func (po *pageOutput) getBaseAndTemplateDescriptor() (string, tplimpl.TemplateDescriptor) {
480+
p := po.p
481+
f := po.f
480482
base := p.PathInfo().BaseReTyped(p.m.pageConfig.Type)
481483
return base, tplimpl.TemplateDescriptor{
482484
Kind: p.Kind(),
483485
Lang: p.Language().Lang,
484486
Layout: p.Layout(),
485-
OutputFormat: p.outputFormat().Name,
486-
MediaType: p.outputFormat().MediaType.Type,
487-
IsPlainText: p.outputFormat().IsPlainText,
487+
OutputFormat: f.Name,
488+
MediaType: f.MediaType.Type,
489+
IsPlainText: f.IsPlainText,
488490
}
489491
}
490492

hugolib/page__content.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -684,10 +684,9 @@ func (c *cachedContentScope) contentToC(ctx context.Context) (contentTableOfCont
684684
if err := cp.initRenderHooks(); err != nil {
685685
return nil, err
686686
}
687-
f := cp.po.f
688687
po := cp.po
689688
p := po.p
690-
ct.contentPlaceholders, err = c.shortcodeState.prepareShortcodesForPage(ctx, p, f, false)
689+
ct.contentPlaceholders, err = c.shortcodeState.prepareShortcodesForPage(ctx, po, false)
691690
if err != nil {
692691
return nil, err
693692
}
@@ -978,7 +977,7 @@ func (c *cachedContentScope) RenderString(ctx context.Context, args ...any) (tem
978977
return "", err
979978
}
980979

981-
placeholders, err := s.prepareShortcodesForPage(ctx, pco.po.p, pco.po.f, true)
980+
placeholders, err := s.prepareShortcodesForPage(ctx, pco.po, true)
982981
if err != nil {
983982
return "", err
984983
}

hugolib/page__meta.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (p *pageMeta) Aliases() []string {
109109
}
110110

111111
func (p *pageMeta) BundleType() string {
112-
switch p.pathInfo.BundleType() {
112+
switch p.pathInfo.Type() {
113113
case paths.PathTypeLeaf:
114114
return "leaf"
115115
case paths.PathTypeBranch:

hugolib/pages_capture.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func (c *pagesCollector) Collect() (collectErr error) {
195195
return id.p.Dir() == fim.Meta().PathInfo.Dir()
196196
}
197197

198-
if fim.Meta().PathInfo.IsLeafBundle() && id.p.BundleType() == paths.PathTypeContentSingle {
198+
if fim.Meta().PathInfo.IsLeafBundle() && id.p.Type() == paths.PathTypeContentSingle {
199199
return id.p.Dir() == fim.Meta().PathInfo.Dir()
200200
}
201201

hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ ADD_MORE_PLACEHOLDER
9898

9999
func TestPagesFromGoTmplMisc(t *testing.T) {
100100
t.Parallel()
101-
b := hugolib.Test(t, filesPagesFromDataTempleBasic)
101+
b := hugolib.Test(t, filesPagesFromDataTempleBasic, hugolib.TestOptWarn())
102+
b.AssertLogContains("! WARN")
102103
b.AssertPublishDir(`
103104
docs/p1/mytext.txt
104105
docs/p1/sub/mytex2.tx

0 commit comments

Comments
 (0)