forked from Vanille-N/website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcss.typ
More file actions
104 lines (94 loc) · 2.63 KB
/
css.typ
File metadata and controls
104 lines (94 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#import "html.typ"
// TODO: document and test more thoroughly
#let aux-to-css = (
pt: (abs) => str(abs) + "pt",
em: (em) => str(em) + "em"
)
// TODO: find more types that are useful
// - fr
#let type-to-css = (
string: (elt) => elt,
integer: (elt) => str(elt),
float: (elt) => str(elt),
"auto": (_) => "auto",
length: (len) => {
let abs = len.abs.pt()
let em = len.em
if em == 0 {
(aux-to-css.pt)(abs)
} else if abs == 0 {
(aux-to-css.em)(em)
} else {
"calc(" + (aux-to-css.em)(em) + " + " + (aux-to-css.pt)(abs) + ")"
}
},
ratio: (pct) => repr(pct),
)
#let into-css-str(elt) = {
let has-trivial-impl = type-to-css.at(str(type(elt)), default: none)
if has-trivial-impl != none {
has-trivial-impl(elt)
} else if type(elt) == relative {
let len = elt.length
let abs = len.abs.pt()
let em = len.em
let rat = elt.ratio
let (sign-abs, abs) = if abs < 0 {
(" - ", -abs)
} else {
(" + ", abs)
}
let (sign-em, em) = if em < 0 {
(" - ", -em)
} else {
(" + ", em)
}
"calc(" + (type-to-css.ratio)(rat) + sign-em + (aux-to-css.em)(em) + sign-abs + (aux-to-css.pt)(abs) + ")"
} else if type(elt) == color {
elt.to-hex()
} else if type(elt) == stroke {
let thickness = elt.thickness
if thickness == auto { thickness = 1pt }
let paint = elt.paint
if paint == auto { paint = black }
into-css-str((thickness, "solid", paint))
} else if type(elt) == array {
elt.map(into-css-str).join(" ")
} else {
panic("Unsupported type " + str(type(elt)))
}
}
// Generate raw CSS not bound to a class.
// This can be put in a `style=...` parameter.
// The input should have the form of a dictionary of (key, value) pairs
// so that every `key: str(value)` is a valid CSS setting.
#let raw-style(params) = {
let ans = params.pairs().map(args => {
let (key, val) = args
key + ": " + into-css-str(val) + ";"
}).join(" ")
if ans == none { "" } else { ans }
}
// Bind a style to a class.
#let raw-elem(target, params) = {
target + " { " + raw-style(params) + " }"
}
// Interpret a dictionary as multiple instances of `raw-elem`.
#let raw-elems(dict) = {
dict.pairs().map(args => {
let (key, val) = args
raw-elem(key, val)
}).join("\n")
}
// Include the raw CSS for `raw-elem` in a <style> tag.
#let elem(target, params) = {
html.style(raw-elem(target, params))
}
// Include the raw CSS for `raw-elems` in a <style> element.
#let elems(dict) = {
html.style(raw-elems(dict))
}
// Include a separately written CSS file into the document.
#let include-file(path) = {
html.link(rel: "stylesheet", href: path)
}