Skip to content

Commit

Permalink
refactor image optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
22mahmoud committed Aug 26, 2023
1 parent c5b0a9c commit f007db4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 33 deletions.
8 changes: 2 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ thumb := $(bin)/thumb
rss := $(bin)/rss
sitemap := $(bin)/sitemap

install: html static image dist/sitemap.xml dist/rss.xml
install: html static dist/sitemap.xml dist/rss.xml

html: $(html_files)

Expand All @@ -27,11 +27,7 @@ dist/%.html: src/%.md templates/* $(MD_TO_HTML)
static:
cd $(source) && find . -type f ! -name "*.md" -print0 | cpio -pdvm0 ../$(output)

image:
@$(thumb)

clean:
@rm -vrf $(output)


.PHONY: install html static image clean
.PHONY: install html static clean
17 changes: 0 additions & 17 deletions bin/thumb

This file was deleted.

75 changes: 65 additions & 10 deletions filters/img_filter.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,80 @@
local path = require("pandoc.path")
local utils = require("pandoc.utils")

local function get_file_absolute_path(file)
local current_file = string.gsub(PANDOC_STATE.input_files[1], "([^/]*%.%w+)$", "")
return "/" .. path.join({ current_file, file })
end

local remove_file_name = function(file)
return string.gsub(file, "([^/]*%.%w+)$", "")
end

local remove_src_prefix = function(file)
return string.gsub(file, "src/", "")
end

local function get_file_extension(file)
local _, ext = path.split_extension(file)
return ext
end

local get_file_name = function(file)
local name, _ = path.split_extension(file)
return name
end

local is_gif = function(file)
local ext = get_file_extension(file)
return ext == ".gif"
end

local src = "src"
local dist = "dist"

local function get_thumb_path(file)
-- add thumbs/ to the path
local thumb = string.gsub(file, "([^/]*%.%w+)$", "thumbs/%1")

-- change extension to .webp
return get_file_name(thumb) .. ".webp"
end

function Image(img)
local src = img.src
img.attributes.loading = "lazy"

local _, ext = path.split_extension(src)
if ext == ".gif" then
if is_gif(img.src) then
return img
end

-- Add the 'thumbs/' prefix to the image filename
img.src = string.gsub(img.src, "([^/]*%.%w+)$", "thumbs/%1")
local absolute_path = remove_src_prefix(get_file_absolute_path(img.src))
local thumb = get_thumb_path(absolute_path)

local input_file = src .. absolute_path
local output_file = dist .. thumb
local output_path = dist .. remove_file_name(thumb)

-- Split the image filename and file extension
local filename = path.split_extension(img.src)
os.execute("mkdir -p " .. output_path)
os.execute("cwebp -resize 640 0 -q 80 " .. input_file .. " -o " .. output_file)

-- Set the file extension to '.webp'
img.src = filename .. ".webp"
img.src = thumb

-- get width and height for thumb image using identify command
local handle = io.popen('identify -format "%w %h" ' .. output_file)
local result = handle:read("*a")
handle:close()
local width, height = result:match("(%d+) (%d+)")

if width then
img.attributes.width = width
end

if height then
img.attributes.height = height
end

-- Wrap the image in an anchor tag
local link = pandoc.Link(img, src)
local link = pandoc.Link(img, absolute_path)

-- Return the modified image
return link
Expand Down

0 comments on commit f007db4

Please sign in to comment.