| 1 |
-- SPDX-License-Identifier: LPPL-1.3c+ |
| 2 |
|
| 3 |
require "lfs" |
| 4 |
|
| 5 |
-- @param mode directly passed to PlantUML. Recommended: png, svg, pdf (requires Apache Batik to convert svg to pdf) |
| 6 |
function convertPlantUmlToTikz(jobname, mode) |
| 7 |
local plantUmlSourceFilename = jobname .. "-plantuml.txt" |
| 8 |
local plantUmlTargetFilename = jobname .. "-plantuml." .. mode |
| 9 |
|
| 10 |
if not (lfs.attributes(plantUmlSourceFilename)) then |
| 11 |
texio.write_nl("Source " .. plantUmlSourceFilename .. " does not exist.") |
| 12 |
return |
| 13 |
end |
| 14 |
|
| 15 |
local plantUmlJar = os.getenv("PLANTUML_JAR") |
| 16 |
if not plantUmlJar then |
| 17 |
texio.write_nl("Environment variable PLANTUML_JAR not set.") |
| 18 |
return |
| 19 |
end |
| 20 |
|
| 21 |
-- check if plantUmlSourceFilename is the same as sourceCacheFilename, if yes, skip executing PlantUML |
| 22 |
local sourceCacheFilename = plantUmlSourceFilename .. "." .. mode .. ".cache" |
| 23 |
local sourceCacheHandle = io.open(sourceCacheFilename, "r") |
| 24 |
if sourceCacheHandle then |
| 25 |
texio.write_nl("Cache \"" .. sourceCacheFilename .. "\" exists.") |
| 26 |
local sourceCacheContent = sourceCacheHandle:read("*a") |
| 27 |
io.close(sourceCacheHandle) |
| 28 |
|
| 29 |
local sourceHandle = io.open(plantUmlSourceFilename, "r") |
| 30 |
if not sourceHandle then |
| 31 |
texio.write_nl("Error: Could not open source file for reading.") |
| 32 |
end |
| 33 |
local sourceContent = sourceHandle:read("*a") |
| 34 |
io.close(sourceHandle) |
| 35 |
|
| 36 |
if sourceContent == sourceCacheContent then |
| 37 |
texio.write_nl("Source \"" .. plantUmlSourceFilename .. "\" is unchanged, skipping PlantUML execution.") |
| 38 |
return |
| 39 |
else |
| 40 |
texio.write_nl("Source \"" .. plantUmlSourceFilename .. "\" has changed. ") |
| 41 |
end |
| 42 |
end |
| 43 |
-- delete generated file to ensure they are really recreated |
| 44 |
os.remove(plantUmlTargetFilename) |
| 45 |
|
| 46 |
texio.write("Executing PlantUML... ") |
| 47 |
local cmd = "java -Djava.awt.headless=true -jar " .. plantUmlJar .. " -charset UTF-8 -pipe -t" |
| 48 |
if (mode == "latex") then |
| 49 |
cmd = cmd .. "latex:nopreamble" |
| 50 |
-- plantuml has changed output format in https://github.com/plantuml/plantuml/pull/1237 |
| 51 |
plantUmlTargetFilename = jobname .. "-plantuml.tex" |
| 52 |
else |
| 53 |
cmd = cmd .. mode |
| 54 |
end |
| 55 |
|
| 56 |
|
| 57 |
cmd = cmd .. [[ < "]] .. plantUmlSourceFilename .. [[" > "]] .. plantUmlTargetFilename .. [["]] |
| 58 |
texio.write_nl(cmd) |
| 59 |
local handle,error = io.popen(cmd) |
| 60 |
if not handle then |
| 61 |
texio.write_nl("Error during execution of PlantUML.") |
| 62 |
texio.write_nl(error) |
| 63 |
return |
| 64 |
end |
| 65 |
io.close(handle) |
| 66 |
|
| 67 |
if not (lfs.attributes(plantUmlTargetFilename)) then |
| 68 |
texio.write_nl("PlantUML did not generate anything.") |
| 69 |
handle = io.open(plantUmlTargetFilename, "w") |
| 70 |
handle:write("Error during latex code generation") |
| 71 |
io.close(handle) |
| 72 |
return |
| 73 |
else |
| 74 |
-- cache plantUmlSourceFilename for next run |
| 75 |
texio.write_nl("Caching source file \"" .. plantUmlSourceFilename .. "\" to \"" .. sourceCacheFilename .. "\".") |
| 76 |
local sourceHandle = io.open(plantUmlSourceFilename, "r") |
| 77 |
if sourceHandle then |
| 78 |
local cacheHandle = io.open(sourceCacheFilename, "w") |
| 79 |
if cacheHandle then |
| 80 |
cacheHandle:write(sourceHandle:read("*a")) |
| 81 |
io.close(cacheHandle) |
| 82 |
else |
| 83 |
texio.write_nl("Error: Could not open cache file for writing.") |
| 84 |
end |
| 85 |
io.close(sourceHandle) |
| 86 |
else |
| 87 |
texio.write_nl("Error: Could not open source file for reading.") |
| 88 |
end |
| 89 |
end |
| 90 |
end |