Skip to content

Commit

Permalink
add size for gif
Browse files Browse the repository at this point in the history
  • Loading branch information
22mahmoud committed Aug 26, 2023
1 parent f007db4 commit 71bf34b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
48 changes: 26 additions & 22 deletions filters/img_filter.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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+)$", "")
Expand Down Expand Up @@ -28,10 +27,6 @@ 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")
Expand All @@ -40,12 +35,27 @@ local function get_thumb_path(file)
return get_file_name(thumb) .. ".webp"
end

function Image(img)
img.attributes.loading = "lazy"
local set_image_size = function(img, path)
local handle = io.popen('identify -format "%w %h\n" ' .. path .. " | head -n1")
local result = handle:read("*a")
handle:close()

if is_gif(img.src) then
return img
local width, height = result:match("(%d+) (%d+)")

if width then
img.attributes.width = width
end

if height then
img.attributes.height = height
end
end

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

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

local absolute_path = remove_src_prefix(get_file_absolute_path(img.src))
local thumb = get_thumb_path(absolute_path)
Expand All @@ -54,24 +64,18 @@ function Image(img)
local output_file = dist .. thumb
local output_path = dist .. remove_file_name(thumb)

if is_gif(img.src) then
set_image_size(img, input_file)
img.src = absolute_path
return img
end

os.execute("mkdir -p " .. output_path)
os.execute("cwebp -resize 640 0 -q 80 " .. input_file .. " -o " .. output_file)

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
set_image_size(img, output_file)

-- Wrap the image in an anchor tag
local link = pandoc.Link(img, absolute_path)
Expand Down
3 changes: 2 additions & 1 deletion templates/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@
}

figure {
max-width: 90%;
max-width: 98%;
margin: 0 auto;
}

figcaption,
Expand Down

0 comments on commit 71bf34b

Please sign in to comment.