Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
22mahmoud committed Sep 18, 2023
1 parent 71bf34b commit c4b3ba3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dist
.tmp
.luarc.json

# to get nvim lsp works
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ sitemap := $(bin)/sitemap

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

dev:
find src filters templates -type f | entr make install

html: $(html_files)

dist/rss.xml: $(md_files) $(rss)
Expand All @@ -26,6 +29,7 @@ dist/%.html: src/%.md templates/* $(MD_TO_HTML)

static:
cd $(source) && find . -type f ! -name "*.md" -print0 | cpio -pdvm0 ../$(output)
cp -r public/* $(output)

clean:
@rm -vrf $(output)
Expand Down
40 changes: 38 additions & 2 deletions filters/img_filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ local is_gif = function(file)
local ext = get_file_extension(file)
return ext == ".gif"
end

local function get_thumb_path(file)
-- add thumbs/ to the path
local thumb = string.gsub(file, "([^/]*%.%w+)$", "thumbs/%1")
Expand All @@ -51,10 +52,46 @@ local set_image_size = function(img, path)
end
end

local function file_exists(name)
local f = io.open(name, "r")
if f ~= nil then
io.close(f)
return true
else
return false
end
end

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

local function get_the_img(input_file, output_file, output_path)
-- 1. check if the output file existes on dist folder, if so, return
if file_exists(output_file) then
return
end

-- 2. check if the output file existes on .tmp/images folder, if so, copy it to dist folder and return

-- replace / and . with _
local tmp_file = string.gsub(output_file, "/", "_")
tmp_file = string.gsub(tmp_file, "%.", "_")
tmp_file = ".tmp/images/" .. tmp_file

if file_exists(tmp_file) then
os.execute("mkdir -p " .. output_path)
os.execute("cp " .. tmp_file .. " " .. output_file)
return
end

-- 3. if not, generate the output file and copy it to dist folder and .tmp/images folder
os.execute("mkdir -p " .. output_path)
os.execute("cwebp -resize 640 0 -q 80 " .. input_file .. " -o " .. output_file)
os.execute("cp " .. output_file .. " " .. tmp_file)
end

function Image(img)
os.execute("mkdir -p .tmp/images")
img.attributes.loading = "lazy"

local absolute_path = remove_src_prefix(get_file_absolute_path(img.src))
Expand All @@ -70,8 +107,7 @@ function Image(img)
return img
end

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

img.src = thumb

Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit c4b3ba3

Please sign in to comment.