Wrapping base64-encoded files at 76 chars

like it should be!
This commit is contained in:
Dan Buch 2012-12-19 00:06:15 -05:00
parent 72b269650b
commit 45c45bb838
3 changed files with 730 additions and 21 deletions

View File

@ -39,24 +39,24 @@ var WebAssets webAssetStrings
func init() { func init() {
rand.Seed(time.Now().UTC().UnixNano()) rand.Seed(time.Now().UTC().UnixNano())
jqmin, err := fromGzB64(JQUERY_MIN_JS)
if err != nil {
log.Fatal("Failed to decode internal jquery.min.js")
}
WebAssets.JqueryMinJS = jqmin
normCss, err := fromGzB64(NORMALIZE_CSS)
if err != nil {
log.Fatal("Failed to decode internal normalize.css")
}
WebAssets.NormalizeCSS = normCss
idxHt, err := fromGzB64(GAME_OF_LIFE_INDEX_HTML) idxHt, err := fromGzB64(GAME_OF_LIFE_INDEX_HTML)
if err != nil { if err != nil {
log.Fatal("Failed to decode internal index.html") log.Fatal("Failed to decode internal index.html: ", err)
} }
WebAssets.IndexHTML = idxHt WebAssets.IndexHTML = idxHt
normCss, err := fromGzB64(NORMALIZE_CSS)
if err != nil {
log.Fatal("Failed to decode internal normalize.css: ", err)
}
WebAssets.NormalizeCSS = normCss
jqmin, err := fromGzB64(JQUERY_MIN_JS)
if err != nil {
log.Fatal("Failed to decode internal jquery.min.js: ", err)
}
WebAssets.JqueryMinJS = jqmin
} }
func fromGzB64(input string) ([]byte, error) { func fromGzB64(input string) ([]byte, error) {

File diff suppressed because one or more lines are too long

View File

@ -7,16 +7,21 @@ import (
"encoding/base64" "encoding/base64"
"errors" "errors"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"os" "os"
"strings"
"text/template" "text/template"
"time" "time"
) )
const b64Width = 76
var ( var (
webAssetsTmpl = template.Must(template.New("web_assets").Parse("" + webAssetsTmpl = template.Must(template.New("web_assets").Parse("" +
"// WARNING: GENERATED FILE, NERDS! {{.Now}}\n" +
"package conway\n" + "package conway\n" +
"\n" + "\n" +
"const (\n" + "const (\n" +
@ -40,13 +45,77 @@ func die(err error) {
} }
func toB64(input []byte) string { func toB64(input []byte) string {
return base64.StdEncoding.EncodeToString(input) var (
err error
out bytes.Buffer
)
encoded := base64.StdEncoding.EncodeToString(input)
lenEncoded := len(encoded)
outbuf := bufio.NewWriter(&out)
inbuf := strings.NewReader(encoded)
outbuf.WriteByte('\n')
if err != nil {
die(err)
}
nWritten := 0
for {
for n := 0; n < b64Width; n++ {
c, err := inbuf.ReadByte()
if err == io.EOF {
break
}
if err != nil {
die(err)
}
err = outbuf.WriteByte(c)
if err != nil {
die(err)
}
nWritten++
}
_, err := inbuf.ReadByte()
if err == io.EOF {
break
}
if err != nil {
die(err)
}
err = inbuf.UnreadByte()
if err != nil {
die(err)
}
err = outbuf.WriteByte('\n')
if err != nil {
die(err)
}
}
if nWritten != lenEncoded {
die(errors.New(fmt.Sprintf("base64-encoded length = %d, but wrote %d",
lenEncoded, nWritten)))
}
outbuf.Flush()
return string(out.Bytes())
} }
func toGz(input []byte) []byte { func toGz(input []byte) []byte {
var buf bytes.Buffer var buf bytes.Buffer
zwriter := gzip.NewWriter(&buf) zwriter, err := gzip.NewWriterLevel(&buf, gzip.BestCompression)
if err != nil {
die(err)
}
zbuf := bufio.NewWriter(zwriter) zbuf := bufio.NewWriter(zwriter)
@ -94,15 +163,19 @@ func fetchIndexHtml(indexHtmlPath string) []byte {
return body return body
} }
func toB64Gz(input []byte) string {
return string(toB64(toGz(input)))
}
func main() { func main() {
if len(os.Args) < 2 { if len(os.Args) < 2 {
die(errors.New(fmt.Sprintf("Usage: %s <index.html-path>", os.Args[0]))) die(errors.New(fmt.Sprintf("Usage: %s <index.html-path>", os.Args[0])))
} }
assets := &webAssetStrings{ assets := &webAssetStrings{
IndexHTML: toB64(toGz(fetchIndexHtml(os.Args[1]))), IndexHTML: toB64Gz(fetchIndexHtml(os.Args[1])),
NormalizeCSS: toB64(toGz(fetchUrl(normalizeCssUrl))), NormalizeCSS: toB64Gz(fetchUrl(normalizeCssUrl)),
JqueryMinJS: toB64(toGz(fetchUrl(jqueryMinJsUrl))), JqueryMinJS: toB64Gz(fetchUrl(jqueryMinJsUrl)),
Now: time.Now(), Now: time.Now(),
} }