From 982e009441e07c024817f81e96d744c5e7899d78 Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Fri, 9 Apr 2021 17:16:46 +0500 Subject: [PATCH] :sparkles: Added new functions module --- functions/README.md | 51 ++++++++++++++++++++++++++ functions/config.yaml | 8 ++++ functions/go.mod | 3 ++ functions/layouts/partials/srcset.html | 20 ++++++++++ functions/layouts/partials/wbr.html | 9 +++++ 5 files changed, 91 insertions(+) create mode 100644 functions/README.md create mode 100644 functions/config.yaml create mode 100644 functions/go.mod create mode 100644 functions/layouts/partials/srcset.html create mode 100644 functions/layouts/partials/wbr.html diff --git a/functions/README.md b/functions/README.md new file mode 100644 index 0000000..51b91bc --- /dev/null +++ b/functions/README.md @@ -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" -}} + +``` + +#### Result +```html + +``` + +### `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://www.example.com/s/ref=sr_nr_i_o +``` \ No newline at end of file diff --git a/functions/config.yaml b/functions/config.yaml new file mode 100644 index 0000000..b459794 --- /dev/null +++ b/functions/config.yaml @@ -0,0 +1,8 @@ +--- +module: + hugoVersion: + extended: true + min: "0.81.0" + mounts: + - source: layouts/partials + target: layouts/partials/functions \ No newline at end of file diff --git a/functions/go.mod b/functions/go.mod new file mode 100644 index 0000000..1a9a635 --- /dev/null +++ b/functions/go.mod @@ -0,0 +1,3 @@ +module gitlab.com/toby3d/hugo/functions + +go 1.16 diff --git a/functions/layouts/partials/srcset.html b/functions/layouts/partials/srcset.html new file mode 100644 index 0000000..5026c78 --- /dev/null +++ b/functions/layouts/partials/srcset.html @@ -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 ", ")) "") -}} diff --git a/functions/layouts/partials/wbr.html b/functions/layouts/partials/wbr.html new file mode 100644 index 0000000..efe8186 --- /dev/null +++ b/functions/layouts/partials/wbr.html @@ -0,0 +1,9 @@ +{{- $result := slice -}} + +{{- range (split . "//") -}} + {{- $str := replaceRE "(\\:)" "$1" . -}} + {{- $str = replaceRE "([\\/\\~\\.\\,\\-\\_\\?\\#\\%])" "$1" $str -}} + {{- $result = $result | append (replaceRE "([\\=\\&])" "$1" $str) -}} +{{- end -}} + +{{- return (delimit $result "//") -}}