-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-list.lua
87 lines (81 loc) · 2.68 KB
/
image-list.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
--
--------------------------------------------------------------------------------
-- File: image-list.lua
--
-- Usage: pandoc --lua-filter=image-list.lua
--
-- Description:
-- convert SVG or PDF/EPS to EMF for inclusion into Word or RTF
-- convert PDF/EPS to SVG for inclusion into HTML and similar
-- needs: pdf2svg, inkscape and ImageMagick convert for EMF export
-- prints: list of graphics,
-- e.g. for packing them separately for a publisher
--
-- Author: Bernhard Fisseni (teoric), <[email protected]>
-- Version: 0.5
-- Created: 2019-03-17
-- Last Changed: 2023-05-24, 08:03:54 (+02:00)
--------------------------------------------------------------------------------
--
-- local inspect = require('inspect')
local text = require 'text'
local list = require 'pandoc.List'
Loc_utils = require(debug.getinfo(1, "S").source:sub(2):match(
"(.*[\\/])") .. "utils")
-- convert SVG to PDF
local function convert_to_svg(im)
local img_orig = im.src
if Loc_utils.endswith(text.lower(im.src), ".pdf")
or Loc_utils.endswith(text.lower(im.src), ".eps") then
im.src = string.gsub(im.src, "%.[^.]+$", ".svg")
-- if not loc_utils.file_exists(img_svg) then
pandoc.pipe("pdf2svg", {img_orig, img.src}, "")
-- end
end
return im
end
-- convert to EMF for inclusion into Word
local function convert_to_emf(im)
local img_orig = im.src
im = convert_to_svg(im)
if Loc_utils.endswith(text.lower(im.src), ".svg") then
im.src = string.gsub(im.src, "%.[^.]+$", ".emf")
pandoc.pipe("inkscape", {img_orig, "--export-emf", im.src}, "")
elseif (
not Loc_utils.endswith(text.lower(im.src), ".emf") and
not (
Loc_utils.endswith(text.lower(im.src), ".png")
or
Loc_utils.endswith(text.lower(im.src), ".jpg")
or
Loc_utils.endswith(text.lower(im.src), ".jpeg")
)) then
-- let's try our best
im.src = string.gsub(im.src, "%.[^.]+$", ".emf")
pandoc.pipe("convert", {img_orig, im.src}, "")
end
return im
end
local image_no = 0
return {
{
Image = function (im)
image_no = image_no + 1
local image_orig = im.src
if FORMAT:find("html") or FORMAT:find("epub") then
if Loc_utils.endswith(text.lower(im.src), ".pdf") or
Loc_utils.endswith(text.lower(im.src), ".eps") then
im = convert_to_svg(im)
end
elseif FORMAT == "docx" or FORMAT == "rtf" then
im = convert_to_emf(im)
end
if image_orig == im.src then
io.stderr:write(string.format("%-3d\t%s\n", image_no, im.src))
else
io.stderr:write(string.format("%-3d\t%s\t%s\n", image_no, im.src, image_orig))
end
return im
end
}
}