Created
May 23, 2022 23:56
-
-
Save pleasantone/640ebe3c6b44851812804f9270895a04 to your computer and use it in GitHub Desktop.
tacky render-image.html with help from hkskup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{- $src := .Page.Resources.GetMatch (printf "*%s*" .Destination) }} | |
{{- $altText := .PlainText }} | |
{{- $imgTitle := replaceRE "{.*}$" "" .Title }} | |
{{- /* Parse attributes */ -}} | |
{{- $attrs := split ( trim ( index (findRE `{.*}` .Title ) 0 ) "{}" ) " " -}} | |
{{- $attributes := (dict) -}} | |
{{ range $attrs -}} | |
{{- if index (findRE `(\#)(.+)` .) 0 -}} | |
{{- /* IDs */ -}} | |
{{- $attributes = merge $attributes ( dict "id" (slice ( trim . "#" )) ) -}} | |
{{- else if index (findRE `(\.)(.+)` .) 0 -}} | |
{{- /* Classes */ -}} | |
{{- $class := slice (trim . "." ) -}} | |
{{- $current := index $attributes "class" | default (slice) -}} | |
{{- $attributes = merge $attributes ( dict "class" (append $current $class) ) -}} | |
{{- else if index (findRE `(\w+)=(['"]?\w+["']?)` . ) 0 -}} | |
{{- /* Attributes */ -}} | |
{{- $attr := replaceRE `(\w+)=(['"]?\w+["']?)` `$1` . -}} | |
{{- $val := split ( replaceRE `(\w+)=(['"]?(\w+)["']?)` `$3` .) " " -}} | |
{{- $current := index $attributes $attr | default (slice) -}} | |
{{- $attributes = merge $attributes ( dict $attr (append $current $val)) -}} | |
{{- else if index (findRE `(\w+)` . ) 0 -}} | |
{{/* Booleans */}} | |
{{- $attributes = merge $attributes (dict . true) -}} | |
{{- end -}} | |
{{- end -}} | |
<figure class="figure d-block text-center"> | |
{{- range $attr, $val := $attributes -}} | |
{{- if eq $val true -}} | |
{{- (print " " $attr) | safeHTMLAttr -}} | |
{{- else -}} | |
{{- (printf " %s=%q" $attr (delimit $val ` ` )) | safeHTML -}} | |
{{- end -}} | |
{{- end -}} | |
{{- if $src -}} | |
{{- $sizes := dict "tiny" 500 "small" 800 "medium" 1200 "large" 1500 }} | |
{{- $scratch := newScratch }} | |
{{- range $name, $width := $sizes }} | |
{{- $scratch.Set $name ($src.Resize (printf "%dx" $width)) }} | |
{{- end -}} | |
<img class="figure-img img-fluid" | |
src="{{ $src.Permalink }}" | |
sizes="(min-width: 35em) 1200px, 100vw" | |
srcset=" | |
{{- range $name, $width := $sizes }} | |
{{- if ge $src.Width $width }} | |
{{- with $scratch.Get $name }}{{ .RelPermalink }} {{ $width }}w, {{ end }} | |
{{- end }} | |
{{- end }}" | |
{{- else -}} | |
<img class="figure-img img-fluid" | |
src="{{ .Destination | safeURL }}" | |
{{- end -}} | |
{{- with $imgTitle }} title="{{ . }}" {{ end -}} | |
{{- with .PlainText }} alt="{{ . }}" {{ end -}} | |
/> | |
{{- with $imgTitle }}<figcaption class="figure-caption text-right">{{ . | safeHTML }}</figcaption>{{ end }} | |
</figure> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment