package gallery import ( "strings" "golang.org/x/text/language" "golang.org/x/text/message" "golang.org/x/text/number" "amadan.net/rastrillo/fiesta/internal/build" ) // What a file weighs. The Getting started page gives each shipped file // two figures, the bytes as written and gzipped, computed at render // time from the release build's bytes. Neither is typed into prose or // pinned in a test: compressor output can move between Go versions, so // a golden figure would churn on a toolchain bump while proving // nothing. Rastrillo's third figure, gzipped without comments, has no // place here: a release file is already stripped, so the line's third // part is always dropped. // weightSep is the separator the weight line's parts are cut at. The // stripped figure is the last part after it in every language, so a // file with no stripped figure loses that part rather than repeating // the gzipped figure under a different name. const weightSep = " ยท " // weight is one file's figures, in bytes. type weight struct { Raw, Gzip int } // weigh measures one file: its length, and its length gzipped at level // 6, the level a server or edge that compresses on the fly typically // uses, through the release build's own compressor so the two agree. func weigh(body []byte) weight { return weight{Raw: len(body), Gzip: build.Gzipped(body)} } // kib formats a byte count in KiB (1024; lowercase "kb" is kilobits) // in the page's own number format: one decimal below 10 KiB, where // rounding to a whole number would turn a 634-byte theme into "1" or // "0", and whole numbers above, where a decimal is noise. The digits // and separators are the locale's own, as x/text writes them. func kib(locale string, n int) string { tag, err := language.Parse(locale) if err != nil { tag = language.English } p := message.NewPrinter(tag) v := float64(n) / 1024 if n < 10*1024 { return p.Sprint(number.Decimal(v, number.MinFractionDigits(1), number.MaxFractionDigits(1))) } return p.Sprint(number.Decimal(v, number.MaxFractionDigits(0))) } // weightIn is one file's line in one locale: the weight line with its // last part, the stripped figure, cut off, since no release file has // one, and the two figures written in. func (s *site) weightIn(locale string, w weight) string { line := s.say(locale, "gallery.started.weight") if i := strings.LastIndex(line, weightSep); i >= 0 { line = line[:i] } return interpolate(line, []string{"raw", kib(locale, w.Raw), "gzip", kib(locale, w.Gzip)}) }