<commit_before>package hijinks

import (
	"fmt"
	"html/template"
	"log"
	"net/http"
	"path/filepath"
	"regexp"
	"strings"
)

var (
	groupName *regexp.Regexp
)

func init() {
	groupName = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9-_]*$`)
}

type hjResponseWriter struct {
	http.ResponseWriter
	template   *Template
	data       interface{}
	dataCalled bool
	partial    bool
}

func (w *hjResponseWriter) Data(d interface{}) {
	w.data = d
	w.dataCalled = true
}

func (w *hjResponseWriter) Delegate(n string, r *http.Request) (interface{}, bool) {
	var (
		templ *Template
		ok    bool
	)
	templ = w.template
	names := strings.Split(strings.TrimSpace(n), " > ")
	for _, name := range names {
		if templ, ok = templ.Children[name]; !ok {
			panic(fmt.Sprintf("Hijinks Delegate call did not match any template children '%s'", n))
		}
	}
	dw := hjResponseWriter{
		ResponseWriter: w.ResponseWriter,
		template:       templ,
	}
	return dw.loadData(r)
}

func (w *hjResponseWriter) loadData(r *http.Request) (interface{}, bool) {
	if w.template.Handler != nil {
		w.template.Handler(w, r)

		if w.dataCalled {
			return w.data, true
		}
	}
	return nil, false
}

func (w *hjResponseWriter) executeTemplate(data interface{}) {
	files := aggregateTemplateFiles(w.template)

	funcMap := template.FuncMap{
		"hijinksGroup": func(name string, prepend bool) template.HTML {
			prep := ""
			if prepend {
				prep = " prepend"
			}
			if ok := groupName.MatchString(name); !ok {
				log.Fatalf("Invalid hijinks group name: '%s'", name)
			}
			return template.HTML(fmt.Sprintf("<!-- hijinks-group: %s%s -->", name, prep))
		},
	}

	t, err := template.New("__init__").Funcs(funcMap).ParseFiles(files...)
	if err != nil {
		log.Fatal("Error parsing files: ", err)
	}

	if w.partial {
		// w.partial is set to true if "application/x.hijinks-html-partial+xml" is set as the value of
		// the `Accept` request header. To fulfill the content negotiation we must now indicate to
		// the client that the body does indeed contain a hijinks partial as requested.
		w.Header().Set("Content-Type", HijinksContentType)
	}
	// Since we are modulating the representation based upon a header value, it is
	// necessary to inform the caches. See https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.6
	w.Header().Set("Vary", "Accept")

	if err := t.ExecuteTemplate(w, filepath.Base(files[0]), data); err != nil {
		log.Fatal(err)
	}
}

func aggregateTemplateFiles(t *Template) []string {
	// collects a list include this template and all of its descendants
	var tpls []string

	if t.File != "" {
		tpls = append(tpls, t.File)
	}
	// TODO: consider how this list of templates should be ordered,
	//       because this isn't right
	for _, tpl := range t.Children {<commit_msg>fix tests<commit_after>package hijinks

import (
	"fmt"
	"html/template"
	"log"
	"net/http"
	"path/filepath"
	"regexp"
	"strings"
)

var (
	groupName *regexp.Regexp
)

func init() {
	groupName = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9-_]*$`)
}

type hjResponseWriter struct {
	http.ResponseWriter
	template   *Template
	data       interface{}
	dataCalled bool
	partial    bool
}

func (w *hjResponseWriter) Data(d interface{}) {
	w.data = d
	w.dataCalled = true
}

func (w *hjResponseWriter) Delegate(n string, r *http.Request) (interface{}, bool) {
	var (
		templ *Template
		ok    bool
	)
	templ = w.template
	names := strings.Split(strings.TrimSpace(n), " > ")
	for _, name := range names {
		if templ, ok = templ.Children[name]; !ok {
			panic(fmt.Sprintf("Hijinks Delegate call did not match any template children '%s'", n))
		}
	}
	dw := hjResponseWriter{
		ResponseWriter: w.ResponseWriter,
		template:       templ,
	}
	return dw.loadData(r)
}

func (w *hjResponseWriter) loadData(r *http.Request) (interface{}, bool) {
	if w.template.Handler != nil {
		w.template.Handler(w, r)

		if w.dataCalled {
			return w.data, true
		}
	}
	return nil, false
}

func (w *hjResponseWriter) executeTemplate(data interface{}) {
	files := aggregateTemplateFiles(w.template)

	funcMap := template.FuncMap{
		"hijinksGroup": func(name string, prepend bool) template.HTML {
			prep := ""
			if prepend {
				prep = " prepend"
			}
			if ok := groupName.MatchString(name); !ok {
				log.Fatalf("Invalid hijinks group name: '%s'", name)
			}
			return template.HTML(fmt.Sprintf("<!-- hijinks-group: %s%s -->", name, prep))
		},
	}

	t, err := template.New("__init__").Funcs(funcMap).ParseFiles(files...)
	if err != nil {
		log.Fatal("Error parsing files: ", err)
	}

	if w.partial {
		// w.partial is set to true if "application/x.hijinks-html-partial+xml" is set as the value of
		// the `Accept` request header. To fulfill the content negotiation we must now indicate to
		// the client that the body does indeed contain a hijinks partial as requested.
		w.Header().Set("Content-Type", ContentType)
	}
	// Since we are modulating the representation based upon a header value, it is
	// necessary to inform the caches. See https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.6
	w.Header().Set("Vary", "Accept")

	if err := t.ExecuteTemplate(w, filepath.Base(files[0]), data); err != nil {
		log.Fatal(err)
	}
}

func aggregateTemplateFiles(t *Template) []string {
	// collects a list include this template and all of its descendants
	var tpls []string

	if t.File != "" {
		tpls = append(tpls, t.File)
	}
	// TODO: consider how this list of templates should be ordered,
	//       because this isn't right
	for _, tpl := range t.Children {
{'fraction_non_alphanumeric': 0.1414767547857794, 'fraction_numerical': 0.006198723792160438, 'mean_word_length': 3.5792988313856426, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '4752501', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1912, 'n_words': 531}