11-- Convert a variety of image formats to PDF
22
3+
4+ -- Wrap calls to drawio in xvfb-run. Note that --no-sandbox has to be the last argument.
5+ -- https://github.com/jgraph/drawio-desktop/issues/249
6+ function drawio (source , dest )
7+ if not os.execute (string.format (" xvfb-run -a drawio -x -f pdf -o %s %s --no-sandbox" , dest , source )) then
8+ 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 ))
9+ return false
10+ end
11+ return true
12+ end
13+
14+ function imagemagick (source , dest )
15+ if not os.execute (string.format (" convert -density 300 %s %s" , source , dest )) then
16+ 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 ))
17+ return false
18+ end
19+ return true
20+ end
21+
22+ local converters = {
23+ [' .jpg' ] = imagemagick ,
24+ [' .png' ] = imagemagick ,
25+ [' .svg' ] = imagemagick ,
26+ [' .drawio' ] = drawio
27+ }
28+
329function string :hassuffix (suffix )
430 return self :sub (-# suffix ) == suffix
531end
@@ -9,15 +35,15 @@ if FORMAT:match 'latex' then
935 -- Try to convert anything that is not a pdf, jpg, or png.
1036 -- This allows us to support file types that latex doesn't (e.g., SVG),
1137 -- as well as speed up the latex render iterations.
12- if not img .src or img .src :hassuffix (' pdf' ) or img .src :hassuffix (' jpg' ) or img .src :hassuffix (' png' ) then
13- return img
14- end
15- local new_filename = pandoc .sha1 (img .src ) .. ' .temp.pdf'
16- if not os.execute (string.format (" convert -density 300 %s %s" , img .src , new_filename )) then
17- print (string.format (' failed to convert %s to %s, falling back to letting latex try to pick it up' , img .src , new_filename ))
18- return img
38+ local file_ext = img .src :match (" ^.+(%..+)$" )
39+ if file_ext and converters [file_ext ] then
40+ local new_filename = pandoc .sha1 (img .src ) .. ' .temp.pdf'
41+ if converters [file_ext ](img .src , new_filename ) then
42+ img .src = new_filename
43+ end
44+ else
45+ print (string.format (" not converting %s (extension %s)" , img .src , file_ext ))
1946 end
20- img .src = new_filename
2147 return img
2248 end
2349end
0 commit comments