From efd9d2c70c7c2b4bad1417243056be1b70aad4e3 Mon Sep 17 00:00:00 2001 From: unexplrd Date: Sat, 24 Jan 2026 15:03:23 +0200 Subject: [PATCH] add img to utils.typ --- template/coursework.typ | 2 +- template/lab.typ | 2 +- template/utils.typ | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 template/utils.typ diff --git a/template/coursework.typ b/template/coursework.typ index bf678e6..6a445f2 100644 --- a/template/coursework.typ +++ b/template/coursework.typ @@ -1,7 +1,7 @@ #import "@local/nure:0.1.0": * #import style: spacing -// #import "utils.typ": * +#import "utils.typ": img #let authors = ( ( diff --git a/template/lab.typ b/template/lab.typ index 53f05e9..e45825d 100644 --- a/template/lab.typ +++ b/template/lab.typ @@ -1,5 +1,5 @@ #import "@local/nure:0.1.0": * -// #import "utils.typ": * +#import "utils.typ": img #import style: spacing diff --git a/template/utils.typ b/template/utils.typ new file mode 100644 index 0000000..4041d2a --- /dev/null +++ b/template/utils.typ @@ -0,0 +1,36 @@ +/// captioned image with label derived from path: +/// - "image.png" = @image +/// - "img/image.png" = @image +/// - "img/foo/image.png" = @foo_image +/// - "img/foo/foo_image.png" = @foo_image +/// the caption will be modified based on a conditional positional value: +/// - `none`: no change +/// - some value: "`caption` (за даними `value`)" +/// - no value: "`caption` (рисунок виконано самостійно)" +/// additional named arguments will be passed to original `image` function +#let img(path, caption, ..sink) = { + let parts = path.split(".").first().split("/") + + let label_string = if ( + parts.len() <= 2 or parts.at(-1).starts-with(parts.at(-2)) + ) { + // ("image",), (_, "image") and (.., "img", "img_image") + parts.last() + } else { + // (.., "img", "image") = "img_image" + parts.at(-2) + "_" + parts.at(-1) + }.replace(" ", "_") + + let caption = if sink.pos().len() == 0 { + caption + } else if sink.pos().first() == none { + caption + " (рисунок виконано самостійно)" + } else { + [#caption (за даними #sink.pos().first())] + } + + [#figure( + image(path, ..sink.named()), + caption: caption, + ) #label(label_string)] +}