Improved figure and img shortcodes that handle images in both /static and /assets. New size attribute activates image resizing.
This commit is contained in:
parent
e31293d523
commit
0848b49f3f
2 changed files with 94 additions and 28 deletions
|
|
@ -1,50 +1,66 @@
|
|||
{{ $alt := .Get "alt" -}}
|
||||
{{ $attr := .Get "attr" -}}
|
||||
{{ $attrlink := .Get "attrlink" -}}
|
||||
{{ $caption := .Get "caption" -}}
|
||||
{{ $class := .Get "class" -}}
|
||||
{{ $height := .Get "height" -}}
|
||||
{{ $link := .Get "link" -}}
|
||||
{{ $size := .Get "size" -}}
|
||||
{{ $src := .Get "src" -}}
|
||||
{{ $title := .Get "title" -}}
|
||||
{{ $width := .Get "width" -}}
|
||||
{{ $maxwidth := .Site.Params.imageMaxWidth -}}
|
||||
{{ $realwidth := 0 -}}
|
||||
{{ $realheight := 0 -}}
|
||||
{{ $width := 0 -}}
|
||||
{{ $height := 0 -}}
|
||||
{{ $height := 0 -}}
|
||||
{{ if (fileExists (add "/static" (.Get "src"))) -}}
|
||||
{{ with (imageConfig (add "/static" (.Get "src"))) -}}
|
||||
{{ if (fileExists (add "/assets" $src)) -}}
|
||||
{{ $original := resources.Get $src -}}
|
||||
{{ if $size -}}
|
||||
{{ $image := $original.Resize $size -}}
|
||||
{{ $src = $image.RelPermalink -}}
|
||||
{{ $width = $image.Width -}}
|
||||
{{ $height = $image.Height -}}
|
||||
{{ else -}}
|
||||
{{ $src = $original.RelPermalink -}}
|
||||
{{ $realwidth = $original.Width -}}
|
||||
{{ $realheight = $original.Height -}}
|
||||
{{ end -}}
|
||||
{{ else if (fileExists (add "/static" $src)) -}}
|
||||
{{ with (imageConfig (add "/static" $src)) -}}
|
||||
{{ $realwidth = .Width -}}
|
||||
{{ $realheight = .Height -}}
|
||||
{{ $width = .Width -}}
|
||||
{{ $height = .Height -}}
|
||||
{{ end -}}
|
||||
{{ end -}}
|
||||
{{ if and (.Get "width") (.Get "height") -}}
|
||||
{{ $width = (int (.Get "width")) -}}
|
||||
{{ $height = (int (.Get "height")) -}}
|
||||
{{ else if (.Get "width") -}}
|
||||
{{ $width = (int (.Get "width")) -}}
|
||||
{{ if and $width $height -}}
|
||||
{{ $width = int $width -}}
|
||||
{{ $height = int $height -}}
|
||||
{{ else if $width -}}
|
||||
{{ $width = int $width -}}
|
||||
{{ $height = $realheight -}}
|
||||
{{ if and ($realwidth) (lt ($width) ($realwidth)) -}}
|
||||
{{ $height = (mul ($realheight) (div (add ($width) 0.0) ($realwidth))) -}}
|
||||
{{ $height = (math.Round $height) -}}
|
||||
{{ end -}}
|
||||
{{ else if (.Get "height") -}}
|
||||
{{ $height = (int (.Get "height")) -}}
|
||||
{{ else if $height -}}
|
||||
{{ $width = $realwidth -}}
|
||||
{{ $height = int $height -}}
|
||||
{{ if and ($realheight) (lt ($height) ($realheight)) -}}
|
||||
{{ $width = (mul ($realwidth) (div (add ($height) 0.0) ($realheight))) -}}
|
||||
{{ $width = (math.Round $width) -}}
|
||||
{{ end -}}
|
||||
{{ else if and ($maxwidth) (lt ($maxwidth) ($width)) -}}
|
||||
{{ else if and ($maxwidth) (lt ($maxwidth) ($realwidth)) -}}
|
||||
{{ $width = ($maxwidth) -}}
|
||||
{{ $height = (mul ($realheight) (div (add ($width) 0.0) ($realwidth))) -}}
|
||||
{{ $height = (math.Round $height) -}}
|
||||
{{ end -}}
|
||||
<figure class="image {{ .Get "class" | default "" }}">
|
||||
{{ with .Get "link" }}<a href="{{ . }}">{{ end -}}
|
||||
<img src="{{ .Get "src" }}" {{ if or (.Get "alt") (.Get "caption") }}alt="{{ with .Get "alt" }}{{ . }}{{ else }}{{ .Get "caption" }}{{ end }}"{{ end }}{{ with $width }} width="{{ . }}"{{ end }}{{ with $height }} height="{{ . }}"{{ end }}>
|
||||
{{- if .Get "link" }}</a>{{ end }}
|
||||
{{ if or (or (.Get "title") (.Get "caption" | markdownify)) (.Get "attr") -}}
|
||||
<figcaption>{{ if isset .Params "title" }}
|
||||
<h4>{{ .Get "title" }}</h4>{{ end }}
|
||||
{{ if or (.Get "caption") (.Get "attr") -}}
|
||||
<p>
|
||||
{{- .Get "caption" -}}
|
||||
{{ with .Get "attrlink" }}<a href="{{ . }}">{{ end }}{{ .Get "attr" | markdownify }}{{ if .Get "attrlink" }}</a>{{ end -}}
|
||||
</p>
|
||||
{{ end -}}
|
||||
<figure class="image{{ with $class }} {{ . }}{{ end }}">
|
||||
{{ with $link }}<a href="{{ . }}">{{ end -}}
|
||||
<img src="{{ $src }}"{{ with $alt }} alt="{{ . }}"{{ end }}{{ with $width }} width="{{ . }}"{{ end }}{{ with $height }} height="{{ . }}"{{ end }}>
|
||||
{{- if $link }}</a>{{ end }}
|
||||
{{ if or $title $caption $attr -}}
|
||||
<figcaption>
|
||||
{{ with $title }}<h4>{{ . }}</h4>{{ end -}}
|
||||
{{- $caption | markdownify }}{{ if and $caption $attr }} {{ end -}}
|
||||
{{ with $attrlink }}<a href="{{ . }}">{{ end }}{{ $attr | markdownify }}{{ if $attrlink }}</a>{{ end -}}
|
||||
</figcaption>
|
||||
{{ end -}}
|
||||
</figure>
|
||||
|
|
|
|||
50
layouts/shortcodes/img.html
Normal file
50
layouts/shortcodes/img.html
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
{{ $alt := .Get "alt" -}}
|
||||
{{ $class := .Get "class" -}}
|
||||
{{ $height := .Get "height" -}}
|
||||
{{ $size := .Get "size" -}}
|
||||
{{ $src := .Get "src" -}}
|
||||
{{ $width := .Get "width" -}}
|
||||
{{ $maxwidth := .Site.Params.imageMaxWidth -}}
|
||||
{{ $realwidth := 0 -}}
|
||||
{{ $realheight := 0 -}}
|
||||
{{ if (fileExists (add "/assets" $src)) -}}
|
||||
{{ $original := resources.Get $src -}}
|
||||
{{ if $size -}}
|
||||
{{ $image := $original.Resize $size -}}
|
||||
{{ $src = $image.RelPermalink -}}
|
||||
{{ $width = $image.Width -}}
|
||||
{{ $height = $image.Height -}}
|
||||
{{ else -}}
|
||||
{{ $src = $original.RelPermalink -}}
|
||||
{{ $realwidth = $original.Width -}}
|
||||
{{ $realheight = $original.Height -}}
|
||||
{{ end -}}
|
||||
{{ else if (fileExists (add "/static" $src)) -}}
|
||||
{{ with (imageConfig (add "/static" $src)) -}}
|
||||
{{ $realwidth = .Width -}}
|
||||
{{ $realheight = .Height -}}
|
||||
{{ end -}}
|
||||
{{ end -}}
|
||||
{{ if and $width $height -}}
|
||||
{{ $width = int $width -}}
|
||||
{{ $height = int $height -}}
|
||||
{{ else if $width -}}
|
||||
{{ $width = int $width -}}
|
||||
{{ $height = $realheight -}}
|
||||
{{ if and ($realwidth) (lt ($width) ($realwidth)) -}}
|
||||
{{ $height = (mul ($realheight) (div (add ($width) 0.0) ($realwidth))) -}}
|
||||
{{ $height = (math.Round $height) -}}
|
||||
{{ end -}}
|
||||
{{ else if $height -}}
|
||||
{{ $width = $realwidth -}}
|
||||
{{ $height = int $height -}}
|
||||
{{ if and ($realheight) (lt ($height) ($realheight)) -}}
|
||||
{{ $width = (mul ($realwidth) (div (add ($height) 0.0) ($realheight))) -}}
|
||||
{{ $width = (math.Round $width) -}}
|
||||
{{ end -}}
|
||||
{{ else if and ($maxwidth) (lt ($maxwidth) ($realwidth)) -}}
|
||||
{{ $width = ($maxwidth) -}}
|
||||
{{ $height = (mul ($realheight) (div (add ($width) 0.0) ($realwidth))) -}}
|
||||
{{ $height = (math.Round $height) -}}
|
||||
{{ end -}}
|
||||
<img class="image{{ with $class }} {{ . }}{{ end }}" src="{{ $src }}"{{ with $alt }} alt="{{ . }}"{{ end }}{{ with $width }} width="{{ . }}"{{ end }}{{ with $height }} height="{{ . }}"{{ end }}>
|
||||
Loading…
Reference in a new issue