<commit_before>package httpc

import (
	"encoding/json"
	"errors"
	"fmt"
	"mime"
	"net/http"
	"strings"
)

// Viewable represents a view. To provide an expressive API, this
// type is an alias for interface{} that is named for documentation.
type Viewable interface{}

// Render writes the view in the requested format, if available.
func Render(w http.ResponseWriter, req *http.Request, view Viewable, code int) error {
	accept := req.Header.Get("Accept")
	if accept == "" {
		return RenderJSON(w, view, code)
	}
	for _, h := range strings.Split(accept, ",") {
		media, _, err := mime.ParseMediaType(h)
		if err != nil {
			return err
		}
		switch media {
		case "text/html", "text/*":
			if renderer == nil {
				continue
			}
			return RenderHTML(w, view, code)
		case "application/json", "application/*", "*/*":
			return RenderJSON(w, view, code)
		case "text/plain":
			return RenderPlain(w, view, code)
		}
	}
	return Abort(w, http.StatusNotAcceptable)
}

// Renderer represents the ability to render HTML templates.
type Renderer interface {
	Render(view interface{}) ([]byte, error)
}

// renderer is used to render HTML templates.
var renderer Renderer

// SetRenderer sets the Renderer used to render HTML templates.
// This function is not thread safe and is intended to be called
// once during application initialization.
func SetRenderer(r Renderer) {
	renderer = r
}

// RenderHTML writes the view as templated HTML.
func RenderHTML(w http.ResponseWriter, view Viewable, code int) error {
	if renderer == nil {
		return errors.New("httpc: renderer must be set")
	}
	b, err := renderer.Render(view)
	if err != nil {
		return err
	}
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	w.WriteHeader(code)
	_, err = w.Write(b)
	return err
}

// RenderJSON writes the view as marshalled JSON.
func RenderJSON(w http.ResponseWriter, view Viewable, code int) error {
	b, err := json.Marshal(view)
	if err != nil {
		return err
	}
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	w.WriteHeader(code)<commit_msg>Interface upgrades for RenderHTML.<commit_after>package httpc

import (
	"encoding/json"
	"fmt"
	"mime"
	"net/http"
	"strings"
)

// Viewable represents a view. To provide an expressive API, this
// type is an alias for interface{} that is named for documentation.
type Viewable interface{}

// Renderable represents the ability to render HTML templates.
type Renderable interface {
	Render(view interface{}) ([]byte, error)
}

// Render writes the view in the requested format, if available.
func Render(w http.ResponseWriter, req *http.Request, view Viewable, code int) error {
	accept := req.Header.Get("Accept")
	if accept == "" {
		return RenderJSON(w, view, code)
	}
	for _, h := range strings.Split(accept, ",") {
		media, _, err := mime.ParseMediaType(h)
		if err != nil {
			return err
		}
		switch media {
		case "text/html", "text/*":
			v, ok := view.(Renderable)
			if !ok {
				continue
			}
			return RenderHTML(w, v, code)
		case "application/json", "application/*", "*/*":
			return RenderJSON(w, view, code)
		case "text/plain":
			return RenderPlain(w, view, code)
		}
	}
	return Abort(w, http.StatusNotAcceptable)
}

// RenderHTML writes the view as templated HTML.
func RenderHTML(w http.ResponseWriter, view Renderable, code int) error {
	b, err := view.Render(view)
	if err != nil {
		return err
	}
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	w.WriteHeader(code)
	_, err = w.Write(b)
	return err
}

// RenderJSON writes the view as marshalled JSON.
func RenderJSON(w http.ResponseWriter, view Viewable, code int) error {
	b, err := json.Marshal(view)
	if err != nil {
		return err
	}
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	w.WriteHeader(code)
{'fraction_non_alphanumeric': 0.13088587390263368, 'fraction_numerical': 0.0010641127959563714, 'mean_word_length': 3.839124839124839, 'pattern_counts': {'":': 6, '<': 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': '4344581', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1243, 'n_words': 402}