<commit_before>/*
Package sprockets integrates the power of sprockets ( http://getsprockets.org ) with your go program.

To use in development:
 * Assets go in '/assets'
 * Install the 'rack' and 'sprockets' rubygems
 * run `rackup src/github.com/99designs/goodies/sprockets/config.ru`
 * In your app, call 'NewSprocketsServer("http://localhost:8012", "assets") to get a ViewHelper'
 * Use the ViewHelper to generate links to your assets.
*/
package sprockets

import (
	"html/template"
)

type AssetPipeline interface {
	GetAssetUrl(name string) (template.HTMLAttr, error)
	GetAssetContents(name string) ([]byte, error)
}

type ViewHelper struct {
	AssetPipeline
}

func (vh *ViewHelper) StylesheetLinkTag(name string) (template.HTML, error) {
	url, err := vh.asset_url(name)
	return template.HTML(`<link href="` + template.HTMLEscaper(url) + `" rel="stylesheet" type="text/css">`), err
}

func (vh *ViewHelper) InlineStylesheet(name string) (template.HTML, error) {
	content, err := vh.AssetPipeline.GetAssetContents(name)
	return template.HTML(`<style>` + string(content) + `</style>`), err
}

func (vh *ViewHelper) JavascriptTag(name string) (template.HTML, error) {
	url, err := vh.asset_url(name)
	return template.HTML(`<script src="` + template.HTMLEscaper(url) + `" type="text/javascript"></script>`), err
}

func (vh *ViewHelper) asset_url(name string) (template.HTMLAttr, error) {
	url, err := vh.AssetPipeline.GetAssetUrl(name)
	return template.HTMLAttr(url), err
}
<commit_msg>Support serving assets directly from the asset pipeline<commit_after>/*
Package sprockets integrates the power of sprockets ( http://getsprockets.org ) with your go program.

To use in development:
 * Assets go in '/assets'
 * Install the 'rack' and 'sprockets' rubygems
 * run `rackup src/github.com/99designs/goodies/sprockets/config.ru`
 * In your app, call 'NewSprocketsServer("http://localhost:8012", "assets") to get a ViewHelper'
 * Use the ViewHelper to generate links to your assets.
*/
package sprockets

import (
	"html/template"
	"net/http"
)

type AssetPipeline interface {
	GetAssetUrl(name string) (template.HTMLAttr, error)
	GetAssetContents(name string) ([]byte, error)
}

type ViewHelper struct {
	AssetPipeline
}

func (vh ViewHelper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	c, err := vh.GetAssetContents(r.URL.Path[1:])
	if err != nil {
		http.Error(w, err.Error(), 404)
	} else {
		w.Write(c)
	}
}

func (vh *ViewHelper) StylesheetLinkTag(name string) (template.HTML, error) {
	url, err := vh.asset_url(name)
	return template.HTML(`<link href="` + template.HTMLEscaper(url) + `" rel="stylesheet" type="text/css">`), err
}

func (vh *ViewHelper) InlineStylesheet(name string) (template.HTML, error) {
	content, err := vh.AssetPipeline.GetAssetContents(name)
	return template.HTML(`<style>` + string(content) + `</style>`), err
}

func (vh *ViewHelper) JavascriptTag(name string) (template.HTML, error) {
	url, err := vh.asset_url(name)
	return template.HTML(`<script src="` + template.HTMLEscaper(url) + `" type="text/javascript"></script>`), err
}

func (vh *ViewHelper) asset_url(name string) (template.HTMLAttr, error) {
	url, err := vh.AssetPipeline.GetAssetUrl(name)
	return template.HTMLAttr(url), err
}

{'fraction_non_alphanumeric': 0.14511138613861385, 'fraction_numerical': 0.0049504950495049506, 'mean_word_length': 5.3641732283464565, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '2605907', 'n_tokens_mistral': 1129, 'n_tokens_neox': 1095, 'n_words': 284}