<commit_before>package render

import (
	"bytes"
	"html/template"
	"io"
	"io/ioutil"

	"github.com/pkg/errors"
)

type htmlRenderer struct {
	*Engine
	names []string
}

func (s htmlRenderer) ContentType() string {
	return "text/html"
}

func (s htmlRenderer) Render(w io.Writer, data interface{}) error {
	s.moot.Lock()
	defer s.moot.Unlock()

	names := s.names
	if s.HTMLLayout != "" {
		names = append(names, s.HTMLLayout)
	}

	if len(names) > 1 {
		lname := names[1]
		layout := s.templates.Lookup(lname)
		if layout == nil {
			b, err := ioutil.ReadFile(lname)
			if err != nil {
				return err
			}
			layout, err = s.templates.New(names[0]).Parse(string(b))
			if err != nil {
				return err
			}
		}
		layout = layout.Funcs(template.FuncMap{
			"yield": s.yield(names[0], data),
		})
		return layout.Execute(w, data)
	}

	return s.executeTemplate(names[0], w, data)
}

func (s htmlRenderer) executeTemplate(name string, w io.Writer, data interface{}) error {
	b, err := ioutil.ReadFile(name)
	if err != nil {
		return err
	}
	t, err := s.templates.New(name).Parse(string(b))
	if err != nil {
		return err
	}
	s.templates = t
	return t.Execute(w, data)
}

func HTML(names ...string) Renderer {
	e := New(&Options{})
	return e.HTML(names...)
}

func (e *Engine) HTML(names ...string) Renderer {
	hr := htmlRenderer{
		Engine: e,
		names:  names,
	}
	return hr
}

func (s htmlRenderer) yield(name string, data interface{}) func() template.HTML {
	return func() template.HTML {
		bb := &bytes.Buffer{}
		err := s.executeTemplate(name, bb, data)
		if err != nil {<commit_msg>hopefully this fixes go 'tip'<commit_after>package render

import (
	"bytes"
	"html/template"
	"io"
	"io/ioutil"

	"github.com/pkg/errors"
)

type htmlRenderer struct {
	*Engine
	names []string
}

func (s htmlRenderer) ContentType() string {
	return "text/html"
}

func (s htmlRenderer) Render(w io.Writer, data interface{}) error {
	s.moot.Lock()
	defer s.moot.Unlock()

	names := s.names
	if s.HTMLLayout != "" {
		names = append(names, s.HTMLLayout)
	}

	for _, n := range names {
		t := s.templates.Lookup(n)
		if t == nil {
			b, err := ioutil.ReadFile(n)
			if err != nil {
				return err
			}
			s.templates, err = s.templates.New(n).Parse(string(b))
			if err != nil {
				return err
			}
		}
	}

	if len(names) > 1 {
		lname := names[1]
		layout := s.templates.Lookup(lname)
		layout = layout.Funcs(template.FuncMap{
			"yield": s.yield(names[0], data),
		})
		return layout.Execute(w, data)
	}

	return s.executeTemplate(names[0], w, data)
}

func (s htmlRenderer) executeTemplate(name string, w io.Writer, data interface{}) error {
	return s.templates.ExecuteTemplate(w, name, data)
}

func HTML(names ...string) Renderer {
	e := New(&Options{})
	return e.HTML(names...)
}

func (e *Engine) HTML(names ...string) Renderer {
	hr := htmlRenderer{
		Engine: e,
		names:  names,
	}
	return hr
}

func (s htmlRenderer) yield(name string, data interface{}) func() template.HTML {
	return func() template.HTML {
		bb := &bytes.Buffer{}
		err := s.executeTemplate(name, bb, data)
		if err != nil {
{'fraction_non_alphanumeric': 0.159896002599935, 'fraction_numerical': 0.0029249268768280793, 'mean_word_length': 3.454413892908828, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1493751', 'n_tokens_mistral': 1222, 'n_tokens_neox': 1143, 'n_words': 295}