<commit_before>//package template provides Go Templates compiling Stages for Slurp.

package template

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

	"github.com/omeid/slurp"
)

type executable interface {
	Execute(io.Writer, interface{}) error
}

func NewTemplateReadCloser(c *slurp.C, wg sync.WaitGroup, e executable, data interface{}) templateReadCloser {

	buf := new(bytes.Buffer)
	wg.Add(1)
	go func() {
		defer wg.Done()
		err := e.Execute(buf, data)
		if err != nil {
			c.Println(err)
		}
	}()

	return templateReadCloser{buf}
}

type templateReadCloser struct {
	io.Reader
}

func (t templateReadCloser) Close() error {
	return nil
}


// Compiles the input files a html/template using the provided data
// and passes them down the line.
// It creates an increamental collection of templates that allows accessing
// templates from templates (Just pass the "required" templates first.)
func HTML(c *slurp.C, data interface{}) slurp.Stage {
	return func(in <-chan slurp.File, out chan<- slurp.File) {

		templates := html.New("")

		var wg sync.WaitGroup
		defer wg.Wait() //Wait before all templates are executed.

		for f := range in {

			buf := new(bytes.Buffer)
			_, err := buf.ReadFrom(f.Reader)
			f.Close()
			if err != nil {
				c.Println(err)
				break
			}

			s, err := f.Stat()
			if err != nil {
				c.Println("Can't get file name.")
				break
			}

			template, err := templates.New(s.Name()).Parse(buf.String())
			if err != nil {
				c.Println(err)
				break
			}

			f.Reader = NewTemplateReadCloser(c, wg, template, data)

			out <- f
		}
	}
}
<commit_msg>Refactor stages/tempplate: Simple is better.<commit_after>//package template provides Go Templates compiling Stages for Slurp.

package template

import (
	"bytes"
	"html/template"

	"github.com/omeid/slurp"
)

// Compiles the input files a html/template using the provided data
// and passes them down the line.
// It creates an increamental collection of templates that allows accessing
// templates from templates (Just pass the "required" templates first.)
func HTML(c *slurp.C, data interface{}) slurp.Stage {
	return func(in <-chan slurp.File, out chan<- slurp.File) {

		templates := template.New("")

		for f := range in {

			buf := new(bytes.Buffer)
			_, err := buf.ReadFrom(f.Reader)
			f.Close()
			if err != nil {
				c.Println(err)
				break
			}

			s, err := f.Stat()
			if err != nil {
				c.Println("Can't get file name.")
				break
			}

			template, err := templates.New(s.Name()).Parse(buf.String())
			if err != nil {
				c.Println(err)
				break
			}

			buff := new(bytes.Buffer)
			err = template.Execute(buff, data)
			if err != nil {
			  c.Println(err)
			  break
			}

			f.Reader = buff

			out <- f
		}
	}
}

{'fraction_non_alphanumeric': 0.13057790782735917, 'fraction_numerical': 0.000365764447695684, 'mean_word_length': 2.9985380116959064, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '5689793', 'n_tokens_mistral': 1080, 'n_tokens_neox': 923, 'n_words': 278}