forked from Dherse/codly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodly.typ
357 lines (326 loc) · 11 KB
/
codly.typ
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#let __codly-enabled = state("codly-enabled", false)
#let __codly-offset = state("codly-offset", 0)
#let __codly-range = state("codly-range", none)
#let __codly-languages = state("codly-languages", (:))
#let __codly-display-names = state("codly-display-names", true)
#let __codly-display-icons = state("codly-display-icons", true)
#let __codly-default-color = state("codly-default-color", rgb("#283593"))
#let __codly-radius = state("codly-radius", 0.32em)
#let __codly-padding = state("codly-padding", 0.32em)
#let __codly-fill = state("codly-fill", white)
#let __codly-zebra-color = state("codly-zebra-color", luma(240))
#let __codly-stroke-width = state("codly-stroke-width", none)
#let __codly-stroke-color = state("codly-stroke-color", luma(240))
#let __codly-numbers-format = state("codly-numbers-format", text)
#let __codly-breakable = state("codly-breakable", true)
#let __codly-enable-numbers = state("codly-enable-numbers", true)
// Default language-block style
#let default-language-block(name, icon, color, loc) = {
let radius = __codly-radius.at(loc)
let padding = __codly-padding.at(loc)
let stroke-width = __codly-stroke-width.at(loc)
let color = if color == none { __codly-default-color.at(loc) } else { color }
box(
radius: radius,
fill: color.lighten(60%),
inset: padding,
stroke: stroke-width + color,
outset: 0pt,
icon + name,
)
}
#let __codly-language-block = state("codly-language-block", default-language-block)
/// Lets you set a line number offset.
/// - offset (int): Line offset
#let codly-offset(offset: 0) = {
__codly-offset.update(offset)
}
/// Lets you set a range of line numbers to highlight.
/// - start (int): Starting line (inclusive)
/// - end (int, none): Ending line. Use `none`
#let codly-range(
start: 1,
end: none,
) = {
__codly-range.update((start, end))
}
/// Disables codly.
#let disable-codly() = {
__codly-enabled.update(false)
}
/// Configures codly.
/// - languages (dict): The list of languages, allows setting a display name and an icon,
/// it should be a dict of the form:\
/// `<language-name>: (name: <display-name>, icon: <icon-content>, color: <color>)`\
/// Note that the `icon` field is optional if no icon is needed/wanted.\
/// also allowing for alises of the form where `<language-name2>` is a key in the dictionary:\
/// `<language-name1>: <language-name2>`
/// - display-name (bool): Whether to display the language name.
/// - display-icon (bool): Whether to display the language icon.
/// - default-color (color, gradient, pattern): The default color for a language not in the list.
/// Only used if `display-icon` or `display-name` is `true`.
/// - radius (length): Radius of a code block.
/// - padding (length): Padding of a code block.
/// - fill (color, gradient, pattern): Fill color of lines.
/// If zebra color is enabled, this is just for odd lines.
/// - zebra-color (none, color, gradient, pattern): The zebra color to use or `none` to disable
/// - stroke-width (length): The stroke width to use to surround the code block.
/// Set to `none` to disable strokes.
/// - stroke-color (color, gradient, pattern): The stroke color to use to surround the code block.
/// - enable-numbers (bool): Whether to enable line numbers.
/// - numbers-format (function): Format of the line numbers.
/// This is a function applied to the text of every line number.
/// - language-block (function): A function that takes 3 positional parameters:
/// - name
/// - icon
/// - color
///
/// It returns the content for the language block.
/// - breakable (bool): Whether this code block is breakable.
#let codly(
languages: none,
display-name: none,
display-icon: none,
default-color: none,
radius: none,
padding: none,
fill: none,
zebra-color: false,
stroke-width: false,
stroke-color: none,
enable-numbers: none,
numbers-format: none,
language-block: none,
breakable: none,
) = {
// Enable codly
__codly-enabled.update(true)
if languages != none {
assert(type(languages) == type((:)), message: "codly: `languages` must be a dict")
for (k, v) in languages.pairs().filter(((_, v)) => type(v) == type((:))) {
v.insert("icon", v.at("icon", default: none))
languages.insert(k, v)
}
for (k, v) in languages.pairs().filter(((_, v)) => type(v) == type("")) {
assert(languages.keys().contains(v), message: "codly: Alias key is missing from `languages`")
languages.insert(k, languages.get(v))
}
__codly-languages.update(languages)
}
if display-name != none {
assert(type(display-name) == bool, message: "codly: `display-name` must be a bool")
__codly-display-names.update(display-name)
}
if display-icon != none {
assert(type(display-icon) == bool, message: "codly: `display-icon` must be a bool")
__codly-display-icons.update(display-icon)
}
if default-color != none {
assert(
type(default-color) == color
or type(default-color) == gradient
or type(default-color) == pattern,
message: "codly: `default-color` must be a color or a gradient or a pattern"
)
__codly-default-color.update(default-color)
}
if radius != none {
assert(
type(radius) == type(1pt + 0.32em),
message: "codly: `radius` must be a length"
)
__codly-radius.update(radius)
}
if padding != none {
assert(
type(padding) == type(1pt + 0.32em),
message: "codly: `padding` must be a length"
)
__codly-padding.update(padding)
}
if fill != none {
assert(
type(fill) == color
or type(fill) == gradient
or type(fill) == pattern,
message: "codly: `fill` must be a color or a gradient or a pattern"
)
__codly-fill.update(fill)
}
if zebra-color != false {
assert(
zebra-color == none
or type(zebra-color) == color
or type(zebra-color) == gradient
or type(zebra-color) == pattern,
message: "codly: `zebra-color` must be none, a color, a gradient, or a pattern"
)
__codly-zebra-color.update(zebra-color)
}
if stroke-width != false {
assert(
type(stroke-width) == type(1pt + 0.1em)
or type(stroke-width) == type(none),
message: "codly: `stroke-width` must be a length or none"
)
__codly-stroke-width.update(stroke-width)
}
if stroke-color != none {
assert(
type(stroke-color) == color
or type(stroke-color) == gradient
or type(stroke-color) == pattern,
message: "codly: `stroke-color` must be a color, a gradient, or a pattern"
)
__codly-stroke-color.update(stroke-color)
}
if enable-numbers != none {
assert(
type(enable-numbers) == bool,
message: "codly: `enable-numbers` must be a bool"
)
__codly-enable-numbers.update(enable-numbers)
}
if numbers-format != none {
assert(
type(numbers-format) == function,
message: "codly: `numbers-format` must be a function"
)
__codly-numbers-format.update((_) => numbers-format)
}
if breakable != none {
assert(
type(breakable) == bool,
message: "codly: `breakable` must be a bool"
)
__codly-breakable.update(breakable)
}
if language-block != none {
assert(
type(language-block) == function,
message: "codly: `language-block` must be a function"
)
__codly-language-block.update(language-block)
}
}
/// Initialise codly to run on your document. Add the following to the top of your code:
/// ```typst
/// #show: codly-init.with()
/// ```
#let codly-init(
body,
) = {
show raw.where(block: true): it => locate(loc => {
let range = __codly-range.at(loc)
let in_range(line) = {
if range == none {
true
} else if range.at(1) == none {
line >= range.at(0)
} else {
line >= range.at(0) and line <= range.at(1)
}
}
if __codly-enabled.at(loc) != true {
return it
}
let languages = __codly-languages.at(loc)
let display-names = __codly-display-names.at(loc)
let display-icons = __codly-display-icons.at(loc)
let language-block = __codly-language-block.at(loc)
let default-color = __codly-default-color.at(loc)
let radius = __codly-radius.at(loc)
let offset = __codly-offset.at(loc)
let stroke-width = __codly-stroke-width.at(loc)
let stroke-color = __codly-stroke-color.at(loc)
let zebra-color = __codly-zebra-color.at(loc)
let numbers-format = __codly-numbers-format.at(loc)
let padding = __codly-padding.at(loc)
let breakable = __codly-breakable.at(loc)
let fill = __codly-fill.at(loc)
let enable-numbers = __codly-enable-numbers.at(loc)
let start = if range == none { 1 } else { range.at(0) };
let stroke = if stroke-width == 0pt or stroke-width == none or stroke-color == none {
none
} else {
stroke-width + stroke-color
};
let items = ()
for (i, line) in it.lines.enumerate() {
if not in_range(line.number) {
continue
}
// Always push the formatted line number
if enable-numbers {
items.push(numbers-format(str(line.number + offset)))
}
// The language block (icon + name)
let language-block = if line.number != start or display-names != true and display-icons != true {
items.push(line)
continue
} else if it.lang == none {
items.push(line)
continue
} else if it.lang in languages {
let lang = languages.at(it.lang);
let name = if display-names {
lang.name
} else {
[]
}
let icon = if display-icons {
lang.icon
} else {
[]
}
(language-block)(name, icon, lang.color, loc)
} else if display-names {
(language-block)(it.lang, [], default-color, loc)
}
// Push the line and the language block in a grid for alignment
items.push(style(styles => grid(
columns: (1fr, measure(language-block, styles).width + 2 * padding),
line,
place(right + horizon, language-block),
)))
}
block(
breakable: breakable,
clip: true,
width: 100%,
radius: radius,
stroke: stroke-color + stroke-width,
fill: fill,
if enable-numbers {
table(
columns: (auto, 1fr),
inset: padding * 1.5,
stroke: none,
align: left + horizon,
fill: (x, y) => if zebra-color != none and calc.rem(y, 2) == 0 {
zebra-color
} else {
fill
},
..items,
)
} else {
table(
columns: (1fr,),
inset: padding * 1.5,
stroke: none,
align: left + horizon,
fill: (x, y) => if zebra-color != none and calc.rem(y, 2) == 0 {
zebra-color
} else {
fill
},
..items,
)
}
)
codly-offset()
codly-range(start: 1, end: none)
})
body
}