summaryrefslogtreecommitdiff
path: root/support/make4ht/extensions/make4ht-ext-latexmk_build.lua
blob: b93f458d622624304619395fc2b2e26f1593c943 (plain)
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
-- use Latexmk in first LaTeX call
-- only in the first call, because we don't need to execute  biber, etc. in the subsequent
-- LaTeX calls, these are only for resolving the cross-references
local M = {}
function M.modify_build(make)
  local used = false
  local first 
  local build_seq = make.build_seq
  -- find first htlatex call in the build sequence
  for pos,v in ipairs(build_seq) do
    if v.name == "htlatex" and not first then
      first = pos
    end
  end
  -- we need to save contents of the .tmp file, to prevent extra executions from latexmk
  -- tex4ht command overwrites content that was set by LaTeX with it's own stuff
  local tmp_file 
  make:add("save_tmp", function(par)
    local f = io.open(mkutils.file_in_builddir(par.input .. ".tmp", par), "r")
    if f then
      tmp_file = f:read("*all")
      f:close()
    end
    return 0
  end)
  make:add("load_tmp", function(par)
    if tmp_file then
      local f = io.open(mkutils.file_in_builddir(par.input .. ".tmp", par), "w")
      if f then
        f:write(tmp_file)
      end
    end
    return 0
  end)
  -- if htlatex was found
  if first then
    -- handle tmp file
    make:load_tmp {}
    make:save_tmp {}
    -- add dummy latexmk call to the build sequence
    make:latexmk {}
    -- replace name, command and type in the first htlatex
    -- call with values from the dummy latexmk call
    local replaced = build_seq[first]
    local latexmk = build_seq[#build_seq]
    replaced.name = latexmk.name
    replaced.command = latexmk.command
    replaced.type = latexmk.type
    -- remove the dummy latexmk
    table.remove(build_seq)
  end
  -- remove htlatex calls from the build sequence, they are unnecessary
  local new_build_seq = {}
  for pos, v in ipairs(build_seq) do
    if v.name ~= "htlatex" and v.name ~= "tex4ht" then
      table.insert(new_build_seq, v)
    elseif v.name == "tex4ht" then
      -- insert save_tmp before tex4ht
      table.insert(new_build_seq, build_seq[#build_seq])
      -- remove save_tmp from the end
      table.remove(build_seq)
      -- and now insert tex4ht
      table.insert(new_build_seq, v)
    end
  end
  make.build_seq = new_build_seq
  return make
end
return M