//! Operator-selected look for the homepage, the OAuth cards, the account
//! pages, the signup page and /stats. A skin is CSS,
//! layered after a page's own stylesheet, plus at most one optional script
//! that decorates the page after it has loaded. It never changes markup,
//! form behavior or protocol output, and every page works the same if the
//! script never runs. Skins are embedded at build time and `ZDS_WEB_SKIN`
//! picks one at startup. The garden is the heavy one (a WebGL scene and its
//! textures), so it is only embedded by a build with `-Dweb-garden=true`.
//! See skins/README.md.
const std = @import("std");
const build_options = @import("build_options");
/// Whether this build carries the garden skin.
pub const garden_built = build_options.web_garden;
pub const Skin = enum {
/// The default: the existing glass appearance, with nothing layered on.
glass,
/// Cream paper sheets on a green ground.
paper,
/// Paper, with a live WebGL ground drawn under the cards once the page
/// has loaded.
garden,
pub fn parse(value: []const u8) ?Skin {
return std.meta.stringToEnum(Skin, value);
}
};
const Parts = struct { home: []const u8 = "", authorize: []const u8 = "", account: []const u8 = "", signup: []const u8 = "", stats: []const u8 = "", script: []const u8 = "" };
fn parts(comptime skin: Skin) Parts {
const paper_base = @embedFile("skins/paper/base.css") ++ "\n";
return switch (skin) {
.glass => .{},
.paper => .{
.home = paper_base ++ @embedFile("skins/paper/home.css"),
.authorize = paper_base ++ @embedFile("skins/paper/authorize.css"),
.account = paper_base ++ @embedFile("skins/paper/account.css"),
.signup = paper_base ++ @embedFile("skins/paper/signup.css"),
.stats = paper_base ++ @embedFile("skins/paper/home.css") ++ "\n" ++ @embedFile("skins/paper/stats.css"),
},
.garden => if (!garden_built) .{} else .{
.home = paper_base ++ @embedFile("skins/paper/home.css") ++ "\n" ++ @embedFile("skins/garden/garden.css"),
.authorize = paper_base ++ @embedFile("skins/paper/authorize.css") ++ "\n" ++ @embedFile("skins/garden/garden.css"),
.account = paper_base ++ @embedFile("skins/paper/account.css") ++ "\n" ++ @embedFile("skins/garden/garden.css"),
.signup = paper_base ++ @embedFile("skins/paper/signup.css") ++ "\n" ++ @embedFile("skins/garden/garden.css"),
.stats = paper_base ++ @embedFile("skins/paper/home.css") ++ "\n" ++ @embedFile("skins/paper/stats.css") ++ "\n" ++ @embedFile("skins/garden/garden.css"),
.script = squeeze(@embedFile("skins/garden/garden.js")),
},
};
}
/// A script as pages carry it: its whole-line comments and indentation
/// dropped, so the source stays readable without shipping the prose. Only
/// lines that start with `//` go, so code and strings are untouched.
fn squeeze(comptime src: []const u8) []const u8 {
comptime {
@setEvalBranchQuota(10_000_000);
var n: usize = 0;
var lines = std.mem.splitScalar(u8, src, '\n');
while (lines.next()) |line| {
const t = std.mem.trimStart(u8, line, " \t");
if (t.len != 0 and !std.mem.startsWith(u8, t, "//")) n += t.len + 1;
}
var buf: [n]u8 = undefined;
var i: usize = 0;
lines = std.mem.splitScalar(u8, src, '\n');
while (lines.next()) |line| {
const t = std.mem.trimStart(u8, line, " \t");
if (t.len == 0 or std.mem.startsWith(u8, t, "//")) continue;
@memcpy(buf[i..][0..t.len], t);
buf[i + t.len] = '\n';
i += t.len + 1;
}
const out = buf;
return &out;
}
}
/// The reading face for paper and garden (glass keeps its monospace). A
/// preset is embedded at build time; `.file` is an operator's own woff2, read
/// once at startup. Either way it is served from this origin at `font_path`,
/// so pages never fetch a font from anywhere else. `.system` serves nothing
/// and pages fall back to the platform's UI sans.
pub const Font = union(enum) {
nunito,
inter,
fraunces,
system,
file: []const u8,
pub fn parse(value: []const u8) ?Font {
inline for (.{ "nunito", "inter", "fraunces", "system" }) |name| {
if (std.mem.eql(u8, value, name)) return @field(Font, name);
}
return null;
}
/// The woff2 bytes to serve, or null for `.system`.
pub fn bytes(self: Font) ?[]const u8 {
return switch (self) {
.nunito => @embedFile("fonts/nunito.woff2"),
.inter => @embedFile("fonts/inter.woff2"),
.fraunces => @embedFile("fonts/fraunces.woff2"),
.system => null,
.file => |data| data,
};
}
};
pub const font_path = "/web/font.woff2";
/// The one face every skinned page declares when a font is served. Skin CSS
/// names it first in `--paper-sans`, so with no font it is simply skipped.
const font_face = "@font-face{font-family:\"zds sans\";src:url(" ++ font_path ++ ") format(\"woff2\");font-weight:100 900;font-display:swap}";
var selected: Skin = .glass;
var selected_font: Font = .nunito;
pub fn select(skin: Skin) void {
selected = skin;
}
pub fn selectFont(choice: Font) void {
selected_font = choice;
}
pub fn font() Font {
return selected_font;
}
/// Whether the selected skin's pages declare the served face.
pub fn fontServed() bool {
return selected != .glass and selected_font.bytes() != null;
}
pub fn current() Skin {
return selected;
}
/// The selected skin's favicon, or null to keep the default mark.
pub fn favicon() ?[]const u8 {
return switch (selected) {
.glass => null,
.paper, .garden => @embedFile("skins/paper/favicon.svg"),
};
}
/// The selected skin's homepage style element, or "" for glass.
pub fn homeStyle() []const u8 {
return switch (selected) {
inline else => |skin| if (fontServed()) comptime styled(skin, .home, true) else comptime styled(skin, .home, false),
};
}
/// The selected skin's OAuth-card style element, or "" for glass.
pub fn authorizeStyle() []const u8 {
return switch (selected) {
inline else => |skin| if (fontServed()) comptime styled(skin, .authorize, true) else comptime styled(skin, .authorize, false),
};
}
/// The selected skin's signup-page style element, or "" for glass.
pub fn signupStyle() []const u8 {
return switch (selected) {
inline else => |skin| if (fontServed()) comptime styled(skin, .signup, true) else comptime styled(skin, .signup, false),
};
}
/// The selected skin's /stats style element, or "" for glass.
pub fn statsStyle() []const u8 {
return switch (selected) {
inline else => |skin| if (fontServed()) comptime styled(skin, .stats, true) else comptime styled(skin, .stats, false),
};
}
/// The selected skin's script element, or "" when it has none. Pages put it
/// last in the body.
pub fn script() []const u8 {
return switch (selected) {
inline else => |skin| comptime scriptFor(skin),
};
}
/// A skin's account-page style element, for pages built whole at compile
/// time once per skin.
pub fn accountStyleFor(comptime skin: Skin, comptime with_font: bool) []const u8 {
return comptime styled(skin, .account, with_font);
}
/// A page's skin style element, then the font face's when one is served.
fn styled(comptime skin: Skin, comptime which: Part, comptime with_font: bool) []const u8 {
comptime {
const sheet = element("style", part(skin, which));
return if (with_font and skin != .glass) sheet ++ element("style", font_face) else sheet;
}
}
/// A skin's script element, for pages built at compile time.
pub fn scriptFor(comptime skin: Skin) []const u8 {
return comptime element("script", parts(skin).script);
}
/// Every skin's CSS and scripts, for the pages' content security policies.
/// The policy is fixed at build time, so it allows each skin the binary
/// carries and switching skins never trips it.
pub const home_policy_styles = collect(.home) ++ .{font_face};
pub const authorize_policy_styles = collect(.authorize) ++ .{font_face};
pub const account_policy_styles = collect(.account) ++ .{font_face};
pub const signup_policy_styles = collect(.signup) ++ .{font_face};
pub const stats_policy_styles = collect(.stats) ++ .{font_face};
pub const policy_scripts = collect(.script);
fn element(comptime tag: []const u8, comptime body: []const u8) []const u8 {
return if (body.len == 0) "" else "<" ++ tag ++ ">" ++ body ++ "" ++ tag ++ ">";
}
const Part = enum { home, authorize, account, signup, stats, script };
fn part(comptime skin: Skin, comptime which: Part) []const u8 {
const p = parts(skin);
return switch (which) {
.home => p.home,
.authorize => p.authorize,
.account => p.account,
.signup => p.signup,
.stats => p.stats,
.script => p.script,
};
}
fn count(comptime which: Part) usize {
comptime {
var n: usize = 0;
for (std.enums.values(Skin)) |skin| {
if (part(skin, which).len != 0) n += 1;
}
return n;
}
}
fn collect(comptime which: Part) [count(which)][]const u8 {
comptime {
var out: [count(which)][]const u8 = undefined;
var i: usize = 0;
for (std.enums.values(Skin)) |skin| {
const body = part(skin, which);
if (body.len == 0) continue;
out[i] = body;
i += 1;
}
return out;
}
}
test "skins parse by name and default to glass" {
try std.testing.expectEqual(Skin.paper, Skin.parse("paper").?);
try std.testing.expectEqual(Skin.garden, Skin.parse("garden").?);
try std.testing.expectEqual(Skin.glass, Skin.parse("glass").?);
try std.testing.expect(Skin.parse("Paper") == null);
try std.testing.expect(Skin.parse("") == null);
try std.testing.expectEqual(Skin.glass, current());
try std.testing.expectEqualStrings("", homeStyle());
try std.testing.expectEqualStrings("", authorizeStyle());
try std.testing.expectEqualStrings("", script());
}
test "a selected skin layers its own elements, all allowed by the policy" {
defer select(.glass);
select(.paper);
try std.testing.expect(std.mem.startsWith(u8, homeStyle(), ""));
try std.testing.expect(std.mem.indexOf(u8, homeStyle(), "--ground") != null);
try std.testing.expectEqualStrings("", script());
select(.garden);
if (garden_built) {
try std.testing.expect(std.mem.startsWith(u8, script(), "