Skip to content

Commit

Permalink
switch to rimage
Browse files Browse the repository at this point in the history
  • Loading branch information
gleicher committed Aug 24, 2024
1 parent 7b5f2df commit 318e806
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions content/tutorials/sample-tutorial/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ draft = true

This is the sample tutorial

{{< rimage src="students-treemap.png" caption="This is a caption" attr="by Mike" attrlink="/" >}}
{{< rimage src="students-treemap.png" width="200" caption="Size 200" attr="by Mike" attrlink="/" >}}

{{< debugurl >}}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions layouts/shortcodes/rimage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{{/* this is meant to take the place of resource-image
it is not a drop-in for resrouce image, because it uses WIDTH not SIZE
we need to use WIDTH since we need it for the caption

it takes the place of figure (in that it allows for a caption), but allows for the resource finding and re-sizing of resource-image

this whole thing was caused by the need to have the caption sized to the width of the image (since the captions look stupid otherwise)

the argugements are:
src - the reource name
width - target width (default 300) - height is auto
alt - alt text (default is the resource name)
class - class for the figure (default is "")
caption - caption for the image (default is "")
attr - attribution for the image (default is "")
attrlink - link for the attribution (default is "")

*/}}
{{- $page := . }}
{{- $name := .Get "src" }}
{{- if $name }}{{- else }}{{errorf "resource-image needs a src argument" }}{{- end}}
{{- $width := .Get "width" | default "300" }}
{{- $size := (printf "%vx%v" $width 5000) }}
{{- $alt := .Get "alt" | default $name }}
{{- $class := .Get "class" | default "" }}
{{- $local := .Page.Resources.GetMatch $name }}
{{- $global := resources.GetMatch $name}}
{{- $img := $local | default $global -}}
{{- $caption := .Get "caption" | default "" }}
{{- $attr := .Get "attr" | default "" }}
{{- $attrlink := .Get "attrlink" | default "" }}
{{- if (.Get "size") }}{{ errorf "rimage does not allow size - use width"}}{{end -}}
{{- with $img }}
{{ $img := .Fit $size }}
<figure class="{{ $class }}" style="width: {{ $width }}px;">
<a href="{{ .RelPermalink }}">
<img src="{{ $img.RelPermalink }}" alt="{{ $alt }}" />
</a>
{{- if or $caption $attr -}}<figcaption><p>
{{- $caption | markdownify -}}
{{- with $attrlink }}
<a href="{{ . }}">
{{- end -}}
{{- $attr | markdownify -}}
{{- if $attrlink }}</a>{{ end }}</p></figcaption>
{{- end }}
</figure>
{{- else}}
{{ errorf "resource-image couldn't open '%s':" $name -}}
{{- end}}

0 comments on commit 318e806

Please sign in to comment.