Skip to content

Commit 965db09

Browse files
authored
Fix ordering of image converters (#148)
Lua tables (maps) are not order-preserving unless they have numeric keys, which means that arbitrarily, either imagemagick or drawio could be used to convert a '.drawio.svg' file. This change removes the table-based converterFor logic and replaces it with a simpler if-statement based routine.
1 parent d99306b commit 965db09

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

filter/convert-images.lua

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,38 +42,33 @@ end
4242
-- Wrap calls to drawio in xvfb-run. Note that --no-sandbox has to be the last argument.
4343
-- https://github.com/jgraph/drawio-desktop/issues/249
4444
function drawio(source, dest)
45+
print(string.format('converting %s using drawio...', source))
4546
if not runCommandSuppressOutput(string.format("xvfb-run -a drawio -x -f pdf -o %s %s --no-sandbox 2>&1", dest, source)) then
46-
print(string.format('failed to convert %s to %s using drawio, falling back to letting latex try to pick it up', img.src, new_filename))
47+
print(string.format('failed to convert %s to %s using drawio, falling back to letting latex try to pick it up', source, dest))
4748
return false
4849
end
4950
return true
5051
end
5152

5253
function imagemagick(source, dest)
54+
print(string.format('converting %s using imagemagick...', source))
5355
if not runCommandSuppressOutput(string.format("convert -density 300 %s %s 2>&1", source, dest)) then
54-
print(string.format('failed to convert %s to %s using imagemagick, falling back to letting latex try to pick it up', img.src, new_filename))
56+
print(string.format('failed to convert %s to %s using imagemagick, falling back to letting latex try to pick it up', source, dest))
5557
return false
5658
end
5759
return true
5860
end
5961

60-
local converters = {
61-
['.drawio'] = drawio,
62-
['.drawio.svg'] = drawio,
63-
['.jpg'] = imagemagick,
64-
['.png'] = imagemagick,
65-
['.svg'] = imagemagick
66-
}
67-
6862
function string:hassuffix(suffix)
6963
return self:sub(-#suffix) == suffix
7064
end
7165

7266
function converterFor(filename)
73-
for suffix, handler in pairs(converters) do
74-
if filename:hassuffix(suffix) then
75-
return handler
76-
end
67+
if filename:hassuffix('.drawio') or filename:hassuffix('.drawio.svg') then
68+
return drawio
69+
end
70+
if filename:hassuffix('.jpg') or filename:hassuffix('.png') or filename:hassuffix('.svg') then
71+
return imagemagick
7772
end
7873
return nil
7974
end

0 commit comments

Comments
 (0)