mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-26 22:04:32 +02:00
@@ -67,10 +67,11 @@ var embeddedTemplatesAliases = map[string][]string{
|
||||
}
|
||||
|
||||
var (
|
||||
_ tpl.TemplateManager = (*templateExec)(nil)
|
||||
_ tpl.TemplateHandler = (*templateExec)(nil)
|
||||
_ tpl.TemplateFuncGetter = (*templateExec)(nil)
|
||||
_ tpl.TemplateFinder = (*templateExec)(nil)
|
||||
_ tpl.TemplateManager = (*templateExec)(nil)
|
||||
_ tpl.TemplateHandler = (*templateExec)(nil)
|
||||
_ tpl.TemplateFuncGetter = (*templateExec)(nil)
|
||||
_ tpl.TemplateFinder = (*templateExec)(nil)
|
||||
_ tpl.UnusedTemplatesProvider = (*templateExec)(nil)
|
||||
|
||||
_ tpl.Template = (*templateState)(nil)
|
||||
_ tpl.Info = (*templateState)(nil)
|
||||
@@ -130,6 +131,11 @@ func newTemplateExec(d *deps.Deps) (*templateExec, error) {
|
||||
funcMap[k] = v.Interface()
|
||||
}
|
||||
|
||||
var templateUsageTracker map[string]templateInfo
|
||||
if d.Cfg.GetBool("printUnusedTemplates") {
|
||||
templateUsageTracker = make(map[string]templateInfo)
|
||||
}
|
||||
|
||||
h := &templateHandler{
|
||||
nameBaseTemplateName: make(map[string]string),
|
||||
transformNotFound: make(map[string]*templateState),
|
||||
@@ -146,6 +152,8 @@ func newTemplateExec(d *deps.Deps) (*templateExec, error) {
|
||||
layoutHandler: output.NewLayoutHandler(),
|
||||
layoutsFs: d.BaseFs.Layouts.Fs,
|
||||
layoutTemplateCache: make(map[layoutCacheKey]tpl.Template),
|
||||
|
||||
templateUsageTracker: templateUsageTracker,
|
||||
}
|
||||
|
||||
if err := h.loadEmbedded(); err != nil {
|
||||
@@ -225,13 +233,72 @@ func (t *templateExec) Execute(templ tpl.Template, wr io.Writer, data interface{
|
||||
defer t.Metrics.MeasureSince(templ.Name(), time.Now())
|
||||
}
|
||||
|
||||
if t.templateUsageTracker != nil {
|
||||
if ts, ok := templ.(*templateState); ok {
|
||||
t.templateUsageTrackerMu.Lock()
|
||||
if _, found := t.templateUsageTracker[ts.Name()]; !found {
|
||||
t.templateUsageTracker[ts.Name()] = ts.info
|
||||
}
|
||||
|
||||
if !ts.baseInfo.IsZero() {
|
||||
if _, found := t.templateUsageTracker[ts.baseInfo.name]; !found {
|
||||
t.templateUsageTracker[ts.baseInfo.name] = ts.baseInfo
|
||||
}
|
||||
}
|
||||
t.templateUsageTrackerMu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
execErr := t.executor.Execute(templ, wr, data)
|
||||
if execErr != nil {
|
||||
execErr = t.addFileContext(templ, execErr)
|
||||
}
|
||||
|
||||
return execErr
|
||||
}
|
||||
|
||||
// TODO1
|
||||
func (t *templateExec) UnusedTemplates() []tpl.FileInfo {
|
||||
if t.templateUsageTracker == nil {
|
||||
return nil
|
||||
}
|
||||
var unused []tpl.FileInfo
|
||||
|
||||
for _, ti := range t.needsBaseof {
|
||||
if _, found := t.templateUsageTracker[ti.name]; !found {
|
||||
unused = append(unused, ti)
|
||||
}
|
||||
}
|
||||
|
||||
for _, ti := range t.baseof {
|
||||
if _, found := t.templateUsageTracker[ti.name]; !found {
|
||||
unused = append(unused, ti)
|
||||
}
|
||||
}
|
||||
|
||||
for _, ts := range t.main.templates {
|
||||
ti := ts.info
|
||||
if strings.HasPrefix(ti.name, "_internal/") {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(ti.name, "partials/inline/pagination") {
|
||||
// TODO(bep) we need to fix this. These are internal partials, but
|
||||
// they may also be defined in the project, which currently could
|
||||
// lead to some false negatives.
|
||||
continue
|
||||
}
|
||||
if _, found := t.templateUsageTracker[ti.name]; !found {
|
||||
unused = append(unused, ti)
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(unused, func(i, j int) bool {
|
||||
return unused[i].Name() < unused[j].Name()
|
||||
})
|
||||
|
||||
return unused
|
||||
}
|
||||
|
||||
func (t *templateExec) GetFunc(name string) (reflect.Value, bool) {
|
||||
v, found := t.funcs[name]
|
||||
return v, found
|
||||
@@ -285,6 +352,10 @@ type templateHandler struct {
|
||||
// Note that for shortcodes that same information is embedded in the
|
||||
// shortcodeTemplates type.
|
||||
templateInfo map[string]tpl.Info
|
||||
|
||||
// May be nil.
|
||||
templateUsageTracker map[string]templateInfo
|
||||
templateUsageTrackerMu sync.Mutex
|
||||
}
|
||||
|
||||
// AddTemplate parses and adds a template to the collection.
|
||||
|
Reference in New Issue
Block a user