rastrillo / fiesta Public

Clone
git clone https://amadan.net/rastrillo/fiesta

Plain git — no account needed to clone.

Download

Download this file

1package gallery
2
3import (
4 "strings"
5
6 "golang.org/x/text/language"
7 "golang.org/x/text/message"
8 "golang.org/x/text/number"
9
10 "amadan.net/rastrillo/fiesta/internal/build"
11)
12
13// What a file weighs. The Getting started page gives each shipped file
14// two figures, the bytes as written and gzipped, computed at render
15// time from the release build's bytes. Neither is typed into prose or
16// pinned in a test: compressor output can move between Go versions, so
17// a golden figure would churn on a toolchain bump while proving
18// nothing. Rastrillo's third figure, gzipped without comments, has no
19// place here: a release file is already stripped, so the line's third
20// part is always dropped.
21
22// weightSep is the separator the weight line's parts are cut at. The
23// stripped figure is the last part after it in every language, so a
24// file with no stripped figure loses that part rather than repeating
25// the gzipped figure under a different name.
26const weightSep = " · "
27
28// weight is one file's figures, in bytes.
29type weight struct {
30 Raw, Gzip int
31}
32
33// weigh measures one file: its length, and its length gzipped at level
34// 6, the level a server or edge that compresses on the fly typically
35// uses, through the release build's own compressor so the two agree.
36func weigh(body []byte) weight {
37 return weight{Raw: len(body), Gzip: build.Gzipped(body)}
38}
39
40// kib formats a byte count in KiB (1024; lowercase "kb" is kilobits)
41// in the page's own number format: one decimal below 10 KiB, where
42// rounding to a whole number would turn a 634-byte theme into "1" or
43// "0", and whole numbers above, where a decimal is noise. The digits
44// and separators are the locale's own, as x/text writes them.
45func kib(locale string, n int) string {
46 tag, err := language.Parse(locale)
47 if err != nil {
48 tag = language.English
49 }
50 p := message.NewPrinter(tag)
51 v := float64(n) / 1024
52 if n < 10*1024 {
53 return p.Sprint(number.Decimal(v, number.MinFractionDigits(1), number.MaxFractionDigits(1)))
54 }
55 return p.Sprint(number.Decimal(v, number.MaxFractionDigits(0)))
56}
57
58// weightIn is one file's line in one locale: the weight line with its
59// last part, the stripped figure, cut off, since no release file has
60// one, and the two figures written in.
61func (s *site) weightIn(locale string, w weight) string {
62 line := s.say(locale, "gallery.started.weight")
63 if i := strings.LastIndex(line, weightSep); i >= 0 {
64 line = line[:i]
65 }
66 return interpolate(line, []string{"raw", kib(locale, w.Raw), "gzip", kib(locale, w.Gzip)})
67}
68