Skip to content

Commit

Permalink
support drawio.svg
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisfenner committed Mar 3, 2024
1 parent 951beec commit 5a009cb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 37 deletions.
24 changes: 17 additions & 7 deletions filter/convert-images.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,34 +58,44 @@ function imagemagick(source, dest)
end

local converters = {
['.drawio'] = drawio,
['.drawio.svg'] = drawio,
['.jpg'] = imagemagick,
['.png'] = imagemagick,
['.svg'] = imagemagick,
['.drawio'] = drawio
['.svg'] = imagemagick
}

function string:hassuffix(suffix)
return self:sub(-#suffix) == suffix
end

function converterFor(filename)
for suffix, handler in pairs(converters) do
if filename:hassuffix(suffix) then
return handler
end
end
return nil
end

function Image (img)
-- Try to convert anything that is not a pdf, jpg, or png.
-- This allows us to support file types that latex doesn't (e.g., SVG),
-- as well as speed up the latex render iterations.
local file_ext = img.src:match("^.+(%..+)$")
if file_ext and converters[file_ext] then
local converter = converterFor(img.src)
if converter then
local new_filename = img.src .. '.' .. getFileHash(img.src) .. '.convert.pdf'
if fileExists(new_filename) then
print(string.format(" not converting %s (already up-to-date as %s)", img.src, new_filename))
img.src = new_filename
elseif converters[file_ext](img.src, new_filename) then
elseif converter(img.src, new_filename) then
print(string.format(" converted %s to %s", img.src, new_filename))
-- Delete stale copies of this file. This makes it easier to cache only the latest converted pdfs
deleteFilesExcept(img.src .. ".*.convert.pdf", new_filename)
img.src = new_filename
end
elseif file_ext ~= ".pdf" then
print(string.format(" not converting %s (extension %s)", img.src, file_ext))
else
print(string.format(" not converting %s", img.src))
end
return img
end
4 changes: 2 additions & 2 deletions guide.tcg
Original file line number Diff line number Diff line change
Expand Up @@ -650,10 +650,10 @@ diagrams. There is a Visual Studio Code extension for it.
Check in and add draw.io diagrams to your document like in @fig:sample-drawio:

```md
![Sample DrawIO Diagram](sample.drawio){#fig:sample-drawio width=50%}
![Sample DrawIO Diagram](sample.drawio.svg){#fig:sample-drawio width=50%}
```

![Sample DrawIO Diagram](sample.drawio){#fig:sample-drawio width=50%}
![Sample DrawIO Diagram](sample.drawio.svg){#fig:sample-drawio width=50%}

# Tables {#sec:tables}

Expand Down
28 changes: 0 additions & 28 deletions sample.drawio

This file was deleted.

1 change: 1 addition & 0 deletions sample.drawio.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5a009cb

Please sign in to comment.