<commit_before>package render

import (
	"encoding/json"
	"fmt"
	"html"
	"html/template"
	"io"
	"log"
	"path/filepath"
	"strings"

	"github.com/gobuffalo/tags"

	"github.com/shurcooL/github_flavored_markdown"
)

type templateRenderer struct {
	*Engine
	contentType string
	names       []string
}

func (s templateRenderer) ContentType() string {
	return s.contentType
}

func (s templateRenderer) Render(w io.Writer, data Data) error {
	var body template.HTML
	var err error
	for _, name := range s.names {
		body, err = s.exec(name, data)
		if err != nil {
			return err
		}
		data["yield"] = body
	}
	w.Write([]byte(body))
	return nil
}

func (s templateRenderer) partial(name string, dd Data) (template.HTML, error) {
	d, f := filepath.Split(name)
	name = filepath.Join(d, "_"+f)
	return s.exec(name, dd)
}

func (s templateRenderer) exec(name string, data Data) (template.HTML, error) {
	source, err := s.TemplatesBox.MustBytes(name)
	if err != nil {
		return "", err
	}

	helpers := map[string]interface{}{
		"partial": s.partial,
	}

	helpers = s.addAssetsHelpers(helpers)

	for k, v := range s.Helpers {
		helpers[k] = v
	}

	if strings.ToLower(filepath.Ext(name)) == ".md" && strings.ToLower(s.contentType) != "text/plain" {
		source = github_flavored_markdown.Markdown(source)
		source = []byte(html.UnescapeString(string(source)))
	}

	body, err := s.TemplateEngine(string(source), data, helpers)
	if err != nil {
		return "", err
	}

	return template.HTML(body), nil
}

func (s templateRenderer) addAssetsHelpers(helpers map[string]interface{}) map[string]interface{} {
	helpers["assetPath"] = func(file string) template.HTML {
		return template.HTML(s.assetPath(file))
	}

	helpers["javascriptTag"] = func(file string) template.HTML {
		jsTag := tags.New("script", tags.Options{
			"type": "text/javascript",
			"src":  s.assetPath(file),
		})

		return jsTag.HTML()
	}

	helpers["stylesheetTag"] = func(file string) template.HTML {
		cssTag := tags.New("link", tags.Options{
			"rel":   "stylesheet",
			"media": "screen",
			"href":  s.assetPath(file),
		})

		return cssTag.HTML()
	}

	return helpers
}

func (s templateRenderer) assetPath(file string) string {
	manifest, err := s.AssetsBox.MustString("manifest.json")
	if err != nil {
		log.Println("[INFO] didn't find manifest, using raw path to assets")

		return fmt.Sprintf("assets/%v", file)
	}

	var manifestData map[string]string
	err = json.Unmarshal([]byte(manifest), &manifestData)

	if err != nil {
		log.Println("[Warning] seems your manifest is not correct")
		return ""
	}

	if file == "application.css" {
		file = "main.css"
	}

	if file == "application.js" {
		file = "main.js"
	}
<commit_msg>adds tag options to the jstag and csstag<commit_after>package render

import (
	"encoding/json"
	"fmt"
	"html"
	"html/template"
	"io"
	"log"
	"path/filepath"
	"strings"

	"github.com/gobuffalo/tags"

	"github.com/shurcooL/github_flavored_markdown"
)

type templateRenderer struct {
	*Engine
	contentType string
	names       []string
}

func (s templateRenderer) ContentType() string {
	return s.contentType
}

func (s templateRenderer) Render(w io.Writer, data Data) error {
	var body template.HTML
	var err error
	for _, name := range s.names {
		body, err = s.exec(name, data)
		if err != nil {
			return err
		}
		data["yield"] = body
	}
	w.Write([]byte(body))
	return nil
}

func (s templateRenderer) partial(name string, dd Data) (template.HTML, error) {
	d, f := filepath.Split(name)
	name = filepath.Join(d, "_"+f)
	return s.exec(name, dd)
}

func (s templateRenderer) exec(name string, data Data) (template.HTML, error) {
	source, err := s.TemplatesBox.MustBytes(name)
	if err != nil {
		return "", err
	}

	helpers := map[string]interface{}{
		"partial": s.partial,
	}

	helpers = s.addAssetsHelpers(helpers)

	for k, v := range s.Helpers {
		helpers[k] = v
	}

	if strings.ToLower(filepath.Ext(name)) == ".md" && strings.ToLower(s.contentType) != "text/plain" {
		source = github_flavored_markdown.Markdown(source)
		source = []byte(html.UnescapeString(string(source)))
	}

	body, err := s.TemplateEngine(string(source), data, helpers)
	if err != nil {
		return "", err
	}

	return template.HTML(body), nil
}

func (s templateRenderer) addAssetsHelpers(helpers map[string]interface{}) map[string]interface{} {
	helpers["assetPath"] = func(file string) template.HTML {
		return template.HTML(s.assetPath(file))
	}

	helpers["javascriptTag"] = func(file string, options tags.Options) template.HTML {
		if options == nil {
			options = tags.Options{}
		}

		if options["type"] == nil {
			options["type"] = "text/javascript"
		}

		options["src"] = s.assetPath(file)
		jsTag := tags.New("script", options)

		return jsTag.HTML()
	}

	helpers["stylesheetTag"] = func(file string, options tags.Options) template.HTML {
		if options == nil {
			options = tags.Options{}
		}

		if options["rel"] == nil {
			options["rel"] = "stylesheet"
		}

		if options["media"] == nil {
			options["media"] = "screen"
		}

		options["href"] = s.assetPath(file)
		cssTag := tags.New("link", options)

		return cssTag.HTML()
	}

	return helpers
}

func (s templateRenderer) assetPath(file string) string {
	manifest, err := s.AssetsBox.MustString("manifest.json")
	if err != nil {
		log.Println("[INFO] didn't find manifest, using raw path to assets")

		return fmt.Sprintf("assets/%v", file)
	}

	var manifestData map[string]string
	err = json.Unmarshal([]byte(manifest), &manifestData)

	if err != nil {
		log.Println("[Warning] seems your manifest is not correct")
		return ""
	}

	if file == "application.css" {
		file = "main.css"
	}

	if file == "application.js" {
		file = "main.js"
	}

{'fraction_non_alphanumeric': 0.154675623121796, 'fraction_numerical': 0.0, 'mean_word_length': 4.088129496402877, 'pattern_counts': {'":': 7, '<': 3, '<?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': '1633402', 'n_tokens_mistral': 2112, 'n_tokens_neox': 1992, 'n_words': 476}