<commit_before>// Wrapper for kasia.go templates designed for easy writing web applications.
package kview

import (
	"fmt"
	"github.com/ziutek/kasia.go"
	"io"
	"log"
	"path"
	"reflect"
	"strings"
)

var (
	// You can modify this, if you store templates in a different directory.
	TemplatesDir = "templates"

	// You can modify this, if want a different error handling.
	ErrorHandler = func(name string, err error) {
		log.Printf("%%View '%s' error. %s\n", name, err.Error())
	}
)

type View interface {
	Copy() View
	Strict(bool)
	Div(string, View)
	Exec(io.Writer, ...interface{})
	Render(...interface{}) *kasia.NestedTemplate
}

// View definition
type KView struct {
	name    string
	tpl     *kasia.Template
	globals map[string]interface{}
}

// Returns a pointer to a page
func New(name string, globals ...map[string]interface{}) *KView {
	var (
		pg  KView
		err error
	)
	pg.name = name
	pg.tpl, err = kasia.ParseFile(path.Join(TemplatesDir, name))
	if err != nil {
		ErrorHandler(name, err)
	}
	pg.globals = make(map[string]interface{})
	// First some default globals
	for k, v := range Globals {
		pg.globals[k] = v
	}
	// globals may default
	for _, g := range globals {
		for k, v := range g {
			pg.globals[k] = v
		}
	}
	return &pg
}

// Returns a pointer to a copy of the page
func (pg *KView) Copy() View {
	new_pg := *pg
	// Make a copy of globals
	new_pg.globals = make(map[string]interface{})
	for k, v := range pg.globals {
		new_pg.globals[k] = v
	}
	return &new_pg
}

// Set strig render flag
func (pg *KView) Strict(strict bool) {
	pg.tpl.Strict = strict
}

// Add subview
func (pg *KView) Div(name string, view View) {
	pg.globals[name] = view
}

func prepend(slice []interface{}, pre ...interface{}) (ret []interface{}) {
	ret = make([]interface{}, len(slice)+len(pre))
	copy(ret, pre)
	copy(ret[len(pre):], slice)
	return
}

// Render view to wr with data
func (pg *KView) Exec(wr io.Writer, ctx ...interface{}) {
	// Add globals to the bottom of the context stack
	ctx = prepend(ctx, pg.globals)
	err := pg.tpl.Run(wr, ctx...)
	if err != nil {
		ErrorHandler(pg.name, err)
	}
}

// Use this method in template text to render page inside other page.
func (pg *KView) Render(ctx ...interface{}) *kasia.NestedTemplate {
	if len(ctx) > 0 {
		// Check if render was called with full template context as first arg<commit_msg>Some typos fixed<commit_after>// Wrapper for kasia.go templates designed for easy writing web applications.
package kview

import (
	"fmt"
	"github.com/ziutek/kasia.go"
	"io"
	"log"
	"path"
	"reflect"
	"strings"
)

var (
	// You can modify this, if you store templates in a different directory.
	TemplatesDir = "templates"

	// You can modify this, if you want a different error handling.
	ErrorHandler = func(name string, err error) {
		log.Printf("%%View '%s' error. %s\n", name, err.Error())
	}
)

type View interface {
	Copy() View
	Strict(bool)
	Div(string, View)
	Exec(io.Writer, ...interface{})
	Render(...interface{}) *kasia.NestedTemplate
}

// View definition
type KView struct {
	name    string
	tpl     *kasia.Template
	globals map[string]interface{}
}

// Returns a pointer to the page
func New(name string, globals ...map[string]interface{}) *KView {
	var (
		pg  KView
		err error
	)
	pg.name = name
	pg.tpl, err = kasia.ParseFile(path.Join(TemplatesDir, name))
	if err != nil {
		ErrorHandler(name, err)
	}
	pg.globals = make(map[string]interface{})
	// First some default globals
	for k, v := range Globals {
		pg.globals[k] = v
	}
	// globals may default
	for _, g := range globals {
		for k, v := range g {
			pg.globals[k] = v
		}
	}
	return &pg
}

// Returns a copy of the page
func (pg *KView) Copy() View {
	new_pg := *pg
	// Make a copy of globals
	new_pg.globals = make(map[string]interface{})
	for k, v := range pg.globals {
		new_pg.globals[k] = v
	}
	return &new_pg
}

// Set the strict render flag
func (pg *KView) Strict(strict bool) {
	pg.tpl.Strict = strict
}

// Add subview
func (pg *KView) Div(name string, view View) {
	pg.globals[name] = view
}

func prepend(slice []interface{}, pre ...interface{}) (ret []interface{}) {
	ret = make([]interface{}, len(slice)+len(pre))
	copy(ret, pre)
	copy(ret[len(pre):], slice)
	return
}

// Render view to wr with data
func (pg *KView) Exec(wr io.Writer, ctx ...interface{}) {
	// Add globals to the bottom of the context stack
	ctx = prepend(ctx, pg.globals)
	err := pg.tpl.Run(wr, ctx...)
	if err != nil {
		ErrorHandler(pg.name, err)
	}
}

// Use this method in template text to render page inside other page.
func (pg *KView) Render(ctx ...interface{}) *kasia.NestedTemplate {
	if len(ctx) > 0 {
		// Check if render was called with full template context as first arg
{'fraction_non_alphanumeric': 0.14453208271157536, 'fraction_numerical': 0.00042634832658281814, 'mean_word_length': 3.4431818181818183, 'pattern_counts': {'":': 0, '<': 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': '6751657', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1693, 'n_words': 544}