blog

git clone https://git.ce9e.org/blog.git

commit
f72178b8d6dddbf70d5d64ae60502c45d41444d3
parent
410d036d24f7167e63b1534b0225b0a7c8c5b78e
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-04-04 19:35
automatically set img size

see https://web.dev/articles/optimize-cls

Diffstat

M Makefile 6 +++---
A filters/img-size.lua 17 +++++++++++++++++

2 files changed, 20 insertions, 3 deletions


diff --git a/Makefile b/Makefile

@@ -4,7 +4,7 @@ PAGES_MD := $(shell find _content -name 'index.md' | sed 's/^_content/build/')
    4     4 PAGES_FEED := $(shell find _content -name 'index.md' | sed 's/md$$/html/')
    5     5 STATIC := $(shell find static -type f | sed 's/^/build\//')
    6     6 IMG := $(shell find _content -regex '.*\.\(png\|gif\|jpg\)$$' | sed 's/^_content/build/')
    7    -1 PANDOC_ARGS = -f markdown-smart --wrap=none
   -1     7 PANDOC_ARGS = -f markdown-smart --wrap=none --lua-filter filters/*.lua
    8     8 
    9     9 .PHONY: all
   10    10 all: build/index.html $(PAGES) $(PAGES_MD) build/feed.xml build/feed-archive.xml build/feed.json build/feed-archive.json $(STATIC) build/static/style.css $(IMG)
@@ -13,7 +13,7 @@ all: build/index.html $(PAGES) $(PAGES_MD) build/feed.xml build/feed-archive.xml
   13    13 push: all
   14    14 	rsync --delete -vrlc build/ ce9e:/var/www/blog
   15    15 
   16    -1 build/%.html: build/%.md _templates/base.html
   -1    16 build/%.html: build/%.md _templates/base.html filters/*.lua
   17    17 	pandoc $(PANDOC_ARGS) --template _templates/base.html $< -o $@
   18    18 	@sed -i 's/<tr class="header">/<tr>/g' $@
   19    19 
@@ -25,7 +25,7 @@ build/index.md: _content/posts/*/index.md posts.sh
   25    25 	@mkdir -p $$(dirname $@)
   26    26 	./posts.sh > $@
   27    27 
   28    -1 _content/%.html: _content/%.md
   -1    28 _content/%.html: _content/%.md filters/*.lua
   29    29 	pandoc $(PANDOC_ARGS) $< -o $@
   30    30 	@sed -i 's/<tr class="header">/<tr>/g' $@
   31    31 

diff --git a/filters/img-size.lua b/filters/img-size.lua

@@ -0,0 +1,17 @@
   -1     1 function resolve(path)
   -1     2     if (not(pandoc.path.is_relative(path))) then
   -1     3         return path
   -1     4     end
   -1     5     local input_path = PANDOC_STATE.input_files[1]
   -1     6     local dir_path = pandoc.path.directory(input_path)
   -1     7     return pandoc.path.join({dir_path, path})
   -1     8 end
   -1     9 
   -1    10 function Image(img)
   -1    11     local path = resolve(img.src)
   -1    12     local size = pandoc.pipe('xargs', {'identify', '-format', '%wx%h'}, path)
   -1    13     local width, height = size:match("(%d+)x(%d+)")
   -1    14     img.attributes.width = width
   -1    15     img.attributes.height = height
   -1    16     return img
   -1    17 end