Added new functions module

This commit is contained in:
Maxim Lebedev 2021-04-09 17:16:46 +05:00
parent c515b1b555
commit 982e009441
No known key found for this signature in database
GPG Key ID: F8978F46FF0FFA4F
5 changed files with 91 additions and 0 deletions

51
functions/README.md Normal file
View File

@ -0,0 +1,51 @@
# Functions
Various useful utilities for Hugo static generated site.
## Install
### Config
[Import module](https://gohugo.io/hugo-modules/use-modules/#use-a-module-for-a-theme) in your site
configuration:
```yaml
imports:
- path: gitlab.com/toby3d/hugo/functions
```
## Usage
### `srcset`
Generates the `srcset` attribute for adaptive image resources. Takes an image resource as an argument and returns an attribute with references to alternate sizes of that resource. If no alternate resources can be generated based on the settings, an empty string will be returned.
#### Config
Add the following parameters to the site configuration. The example below shows the default values:
```yaml
params:
imaging:
min: 50 # Minimum width for generated images
max: 1000 # Maximum width for generated images
count: 20 # Maximum number of generated images
```
#### Usage
```html
{{- $image := resources.GetMatch "sample" -}}
<img src="{{ $image.Permalink }}" {{ partial "functions/srcset" $image | safeHTMLAttr }} loading="lazy" />
```
#### Result
```html
<img src="https://example.com/images/image.jpg" srcset="https://example.com/images/image_hu163a76a9d5551ae508ac1b34fee2c6dc_611848_960x0_resize_box_2.png 960w, https://example.com/images/image_hu163a76a9d5551ae508ac1b34fee2c6dc_611848_480x0_resize_box_2.png 480w, https://example.com/images/image_hu163a76a9d5551ae508ac1b34fee2c6dc_611848_240x0_resize_box_2.png 240w" />
```
### `wbr`
Renders long URLs with hyphenation tags. See «[Better Line Breaks for Long URLs](https://css-tricks.com/better-line-breaks-for-long-urls/)».
#### Usage
```html
{{ partial "functions/wbr" $.Page.Permalink | safeHTML }}
```
#### Result
```html
http:<wbr>//<wbr>www<wbr>.<wbr>example<wbr>.com<wbr>/<wbr>s/<wbr>ref<wbr>=<wbr>sr<wbr>_<wbr>nr<wbr>_<wbr>i<wbr>_o<wbr>
```

8
functions/config.yaml Normal file
View File

@ -0,0 +1,8 @@
---
module:
hugoVersion:
extended: true
min: "0.81.0"
mounts:
- source: layouts/partials
target: layouts/partials/functions

3
functions/go.mod Normal file
View File

@ -0,0 +1,3 @@
module gitlab.com/toby3d/hugo/functions
go 1.16

View File

@ -0,0 +1,20 @@
{{- $params := dict
"min" (default site.Params.imaging.min 200)
"max" (default site.Params.imaging.max 1400)
"count" (default site.Params.imaging.count 20)
-}}
{{- $original := . -}}
{{- $result := slice -}}
{{- $width := $original.Width -}}
{{- range $step := seq $params.count -}}
{{- $width = div $width 2 -}}
{{- if and (ge $width $params.min) (le $width $params.max) -}}
{{- $image := $original.Resize (printf "%dx" $width) -}}
{{- $result = $result | append (printf "%s %dw" (absURL $image.RelPermalink) $image.Width) -}}
{{- end -}}
{{- end -}}
{{- return (cond (ge (len $result) 1) (printf "srcset=\"%s\"" (delimit $result ", ")) "") -}}

View File

@ -0,0 +1,9 @@
{{- $result := slice -}}
{{- range (split . "//") -}}
{{- $str := replaceRE "(\\:)" "$1<wbr>" . -}}
{{- $str = replaceRE "([\\/\\~\\.\\,\\-\\_\\?\\#\\%])" "<wbr>$1" $str -}}
{{- $result = $result | append (replaceRE "([\\=\\&])" "<wbr>$1<wbr>" $str) -}}
{{- end -}}
{{- return (delimit $result "//<wbr>") -}}