File: update-styles.lua

package info (click to toggle)
ipe 7.2.30-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,844 kB
  • sloc: cpp: 42,635; makefile: 664; sh: 44; xml: 17; ansic: 14; python: 7
file content (86 lines) | stat: -rw-r--r-- 2,666 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/env ipescript 
-- -*- lua -*-
----------------------------------------------------------------------
-- Update stylesheets in Ipe documents
----------------------------------------------------------------------
--
-- Running this script as "ipescript update-styles <figures>" will
-- update the stylesheets in the Ipe figures given.
--
-- To update a stylesheet, the script will look for a file with the
-- same name as the stylesheet plus the extension ".isy".  It searches
-- for this file in the current directory, plus all the directories
-- given in the IPESTYLES environment variable (with the same default
-- settings as in Ipe).
--
----------------------------------------------------------------------

local ipestyles = os.getenv("IPESTYLES")
local home = os.getenv("HOME")

if ipestyles then
  styleDirs = { "." }
  for w in string.gmatch(ipestyles, "[^:;]+") do
    if w == "_" then w = config.system_styles end
    styleDirs[#styleDirs + 1] = w
  end
else
  styleDirs = { ".", config.system_styles }
  if config.platform ~= "win" then
    table.insert(styleDirs, 2, home .. "/.ipe/styles")
    if config.platform == "apple" then
      table.insert(styleDirs, 3, home .. "/Library/Ipe/Styles")
    end
  end
end

function findStyle(w)
  for _, d in ipairs(styleDirs) do
    local s = d .. "/" .. w
    if ipe.fileExists(s) then return s end
  end
end

--------------------------------------------------------------------

if #argv == 0 then
  io.stderr:write("Usage: ipescript update-styles <figures>\n")
  return
end

local fignames = argv

for _,figname in ipairs(fignames) do
  io.stderr:write("Updating styles in figure '" .. figname .. "'\n")
  local doc = assert(ipe.Document(figname))
  for index=1,doc:sheets():count() do
    local sheet = doc:sheets():sheet(index)
    local name = sheet:name()
    if not name then
      io.stderr:write(" - unnamed stylesheet\n")
    elseif name == "standard" then
      io.stderr:write(" - standard stylesheet\n")
    else
      io.stderr:write(" - stylesheet '" .. name .. "'\n")
      local s = findStyle(name .. ".isy")
      if s then
	io.stderr:write("     updating from '" .. s .."'\n")
	local nsheet = assert(ipe.Sheet(s))
	doc:sheets():insert(index, nsheet)
	doc:sheets():remove(index + 1) -- remove old sheet
      end
    end
  end
  if figname:sub(-4) ~= ".ipe" and figname:sub(-4) ~= ".xml" then
    assert(doc:runLatex())
  end
  -- make a backup of original figure
  local f = assert(io.open(figname, "rb"))
  local data = f:read("*all")
  f:close()
  f = assert(io.open(figname .. ".bak", "wb"))
  f:write(data)
  f:close()
  -- now write updated figure back
  doc:save(figname)
end