Skip to content

Commit

Permalink
refactor: use generic .Process for card processing and allow bundled …
Browse files Browse the repository at this point in the history
…images (#538)

* Change processing method to more generic hugo .Process and allow resources from local .Page.Resources as well as global resources

* Add comments

* Fix double curly brackets error

Co-authored-by: Xin <[email protected]>

* Maintain backward compatibility by using defaulting $process to $methd $options

#538 (comment)

* Use printf instead of delimit

Co-authored-by: Xin <[email protected]>

---------

Co-authored-by: Xin <[email protected]>
  • Loading branch information
jzerfowski and imfing authored Jan 5, 2025
1 parent 323f4c4 commit a27f6ee
Showing 1 changed file with 11 additions and 23 deletions.
34 changes: 11 additions & 23 deletions layouts/shortcodes/card.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,18 @@
{{/* Image processing options */}}
{{- $method := .Get "method" | default "Resize" | humanize -}}
{{- $options := .Get "options" | default "800x webp q80" -}}
{{- $process := .Get "process" | default (printf "%s %s" $method $options) -}}

{{- if and $image (not (urls.Parse $image).Scheme) -}}
{{/* Process images in assets */}}
{{- with resources.Get $image -}}
{{- $processed := "" -}}
{{- if eq $method "Resize" -}}
{{- $processed = (.Resize $options) -}}
{{- else if eq $method "Fit" -}}
{{- $processed = (.Fit $options) -}}
{{- else if eq $method "Fill" -}}
{{- $processed = (.Fill $options) -}}
{{- else if eq $method "Crop" -}}
{{- $processed = (.Crop $options) -}}
{{- else -}}
{{- errorf "Invalid image processing command: Must be one of Crop, Fit, Fill or Resize." -}}
{{- end -}}
{{- $width = $processed.Width -}}
{{- $height = $processed.Height -}}
{{- $image = $processed.RelPermalink -}}
{{- else -}}
{{/* Otherwise, use relative link of the image */}}
{{- if hasPrefix $image "/" -}}
{{- $image = relURL (strings.TrimPrefix "/" $image) -}}
{{- end -}}
{{- with or (.Page.Resources.Get $image) (resources.Get $image) -}}
{{/* Retrieve the $image resource from local or global resources */}}
{{- $processed := .Process $process -}}
{{- $width = $processed.Width -}}
{{- $height = $processed.Height -}}
{{- $image = $processed.RelPermalink -}}
{{ else }}
{{/* Otherwise, use relative link of the image */}}
{{- if hasPrefix $image "/" -}}
{{- $image = relURL (strings.TrimPrefix "/" $image) -}}
{{- end -}}
{{- end -}}

Expand Down

0 comments on commit a27f6ee

Please sign in to comment.