Skip to content

Commit 9440085

Browse files
authored
kml_export: use more utility functions
kml_export: use more utility functions as suggested by @wpferguson
1 parent 5c24077 commit 9440085

File tree

1 file changed

+25
-171
lines changed

1 file changed

+25
-171
lines changed

contrib/kml_export.lua

Lines changed: 25 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ WARNING
2828
This script is only tested with Linux
2929
3030
USAGE
31-
* require script "official/yield" from your main Lua file in the first line
3231
* require this script from your main Lua file
3332
* when choosing file format, pick JPEG or PNG as Google Earth doesn't support other formats
3433
@@ -37,13 +36,14 @@ USAGE
3736
local dt = require "darktable"
3837
local du = require "lib/dtutils"
3938
local df = require "lib/dtutils.file"
39+
local ds = require "lib/dtutils.string"
4040
local dsys = require "lib/dtutils.system"
41-
require "official/yield"
41+
4242
local gettext = dt.gettext
4343

4444
local PS = dt.configuration.running_os == "windows" and "\\" or "/"
4545

46-
du.check_min_api_version("3.0.0", kml_export)
46+
du.check_min_api_version("5.0.0", kml_export)
4747

4848
-- Tell gettext where to find the .mo file translating messages for a particular domain
4949
gettext.bindtextdomain("kml_export",dt.configuration.config_dir.."/lua/locale/")
@@ -52,168 +52,10 @@ local function _(msgid)
5252
return gettext.dgettext("kml_export", msgid)
5353
end
5454

55-
-- Sort a table
56-
local function spairs(_table, order) -- Code copied from http://stackoverflow.com/questions/15706270/sort-a-table-in-lua
57-
-- collect the keys
58-
local keys = {}
59-
for _key in pairs(_table) do keys[#keys + 1] = _key end
60-
61-
-- if order function given, sort by it by passing the table and keys a, b,
62-
-- otherwise just sort the keys
63-
if order then
64-
table.sort(keys, function(a,b) return order(_table, a, b) end)
65-
else
66-
table.sort(keys)
67-
end
68-
69-
-- return the iterator function
70-
local i = 0
71-
return function()
72-
i = i + 1
73-
if keys[i] then
74-
return keys[i], _table[keys[i]]
75-
end
76-
end
77-
end
78-
7955
local function show_status(storage, image, format, filename, number, total, high_quality, extra_data)
8056
dt.print(string.format(_("Export Image %i/%i"), number, total))
8157
end
8258

83-
-- Strip accents from a string
84-
-- Copied from https://forums.coronalabs.com/topic/43048-remove-special-characters-from-string/
85-
function string.stripAccents( str )
86-
local tableAccents = {}
87-
-- A
88-
tableAccents["à"] = "a"
89-
tableAccents["À"] = "A"
90-
tableAccents["á"] = "a"
91-
tableAccents["Á"] = "A"
92-
tableAccents["â"] = "a"
93-
tableAccents["Â"] = "A"
94-
tableAccents["ã"] = "a"
95-
tableAccents["Ã"] = "A"
96-
tableAccents["ä"] = "a"
97-
tableAccents["Ä"] = "A"
98-
-- B
99-
-- C
100-
tableAccents["ç"] = "c"
101-
tableAccents["Ç"] = "C"
102-
tableAccents["č"] = "c"
103-
tableAccents["Č"] = "C"
104-
-- D
105-
tableAccents["ď"] = "d"
106-
tableAccents["Ď"] = "d"
107-
-- E
108-
tableAccents["è"] = "e"
109-
tableAccents["È"] = "E"
110-
tableAccents["é"] = "e"
111-
tableAccents["É"] = "E"
112-
tableAccents["ê"] = "e"
113-
tableAccents["Ê"] = "E"
114-
tableAccents["ë"] = "e"
115-
tableAccents["Ë"] = "E"
116-
tableAccents["ě"] = "e"
117-
tableAccents["Ě"] = "E"
118-
-- F
119-
-- G
120-
-- H
121-
-- I
122-
tableAccents["ì"] = "i"
123-
tableAccents["Ì"] = "I"
124-
tableAccents["í"] = "i"
125-
tableAccents["Í"] = "I"
126-
tableAccents["î"] = "i"
127-
tableAccents["Î"] = "I"
128-
tableAccents["ï"] = "i"
129-
tableAccents["Ï"] = "I"
130-
-- J
131-
-- K
132-
-- L
133-
tableAccents["ĺ"] = "l"
134-
tableAccents["Ĺ"] = "L"
135-
tableAccents["ľ"] = "l"
136-
tableAccents["Ľ"] = "L"
137-
-- M
138-
-- N
139-
tableAccents["ñ"] = "n"
140-
tableAccents["Ñ"] = "N"
141-
tableAccents["ň"] = "n"
142-
tableAccents["Ň"] = "N"
143-
-- O
144-
tableAccents["ò"] = "o"
145-
tableAccents["Ò"] = "O"
146-
tableAccents["ó"] = "o"
147-
tableAccents["Ó"] = "O"
148-
tableAccents["ô"] = "o"
149-
tableAccents["Ô"] = "O"
150-
tableAccents["õ"] = "o"
151-
tableAccents["Õ"] = "O"
152-
tableAccents["ö"] = "o"
153-
tableAccents["Ö"] = "O"
154-
-- P
155-
-- Q
156-
-- R
157-
tableAccents["ŕ"] = "r"
158-
tableAccents["Ŕ"] = "R"
159-
tableAccents["ř"] = "r"
160-
tableAccents["Ř"] = "R"
161-
-- S
162-
tableAccents["š"] = "s"
163-
tableAccents["Š"] = "S"
164-
-- T
165-
tableAccents["ť"] = "t"
166-
tableAccents["Ť"] = "T"
167-
-- U
168-
tableAccents["ù"] = "u"
169-
tableAccents["Ù"] = "U"
170-
tableAccents["ú"] = "u"
171-
tableAccents["Ú"] = "U"
172-
tableAccents["û"] = "u"
173-
tableAccents["Û"] = "U"
174-
tableAccents["ü"] = "u"
175-
tableAccents["Ü"] = "U"
176-
tableAccents["ů"] = "u"
177-
tableAccents["Ů"] = "U"
178-
-- V
179-
-- W
180-
-- X
181-
-- Y
182-
tableAccents["ý"] = "y"
183-
tableAccents["Ý"] = "Y"
184-
tableAccents["ÿ"] = "y"
185-
tableAccents["Ÿ"] = "Y"
186-
-- Z
187-
tableAccents["ž"] = "z"
188-
tableAccents["Ž"] = "Z"
189-
190-
local normalizedString = ""
191-
192-
for strChar in string.gmatch(str, "([%z\1-\127\194-\244][\128-\191]*)") do
193-
if tableAccents[strChar] ~= nil then
194-
normalizedString = normalizedString..tableAccents[strChar]
195-
else
196-
normalizedString = normalizedString..strChar
197-
end
198-
end
199-
200-
return normalizedString
201-
end
202-
203-
-- Escape XML characters
204-
-- Keep & first, otherwise it will double escape other characters
205-
-- https://stackoverflow.com/questions/1091945/what-characters-do-i-need-to-escape-in-xml-documents
206-
function string.escapeXmlCharacters( str )
207-
208-
str = string.gsub(str,"&", "&")
209-
str = string.gsub(str,"\"", """)
210-
str = string.gsub(str,"'", "'")
211-
str = string.gsub(str,"<", "&lt;")
212-
str = string.gsub(str,">", "&gt;")
213-
214-
return str
215-
end
216-
21759
-- Add duplicate index to filename
21860
-- image.filename does not have index, exported_image has index
21961
function addDuplicateIndex( index, filename )
@@ -241,7 +83,7 @@ local function create_kml_file(storage, image_table, extra_data)
24183
return
24284
end
24385
end
244-
dt.print_error("Will try to export KML file now")
86+
dt.print_log("Will try to export KML file now")
24587

24688
local imageFoldername
24789
if ( dt.preferences.read("kml_export","CreateKMZ","bool") == true
@@ -276,8 +118,8 @@ local function create_kml_file(storage, image_table, extra_data)
276118
-- will be scaled so its largest dimension is 120 pixels. The '+profile "*"' removes any ICM, EXIF, IPTC, or other
277119
-- profiles that might be present in the input and aren't needed in the thumbnail.
278120

279-
local convertToThumbCommand = magickPath .. " -size 96x96 "..exported_image.." -resize 92x92 -mattecolor \"#FFFFFF\" -frame 2x2 +profile \"*\" "..exportDirectory..PS..imageFoldername.."thumb_"..filename..".jpg"
280-
dt.control.execute(convertToThumbCommand)
121+
local convertToThumbCommand = ds.sanitize(magickPath) .. " -size 96x96 "..exported_image.." -resize 92x92 -mattecolor \"#FFFFFF\" -frame 2x2 +profile \"*\" "..exportDirectory..PS..imageFoldername.."thumb_"..filename..".jpg"
122+
dsys.external_command(convertToThumbCommand)
281123
else
282124
-- Remove exported image if it has no GPS data
283125
os.remove(exported_image)
@@ -288,7 +130,18 @@ local function create_kml_file(storage, image_table, extra_data)
288130

289131
-- Strip accents from the filename, because GoogleEarth can't open them
290132
-- https://github.com/darktable-org/lua-scripts/issues/54
291-
filmName = string.stripAccents(filmName)
133+
filmName = ds.strip_accents(filmName)
134+
135+
-- Remove chars we don't like to have in filenames
136+
filmName = string.gsub(filmName, [[\]], "")
137+
filmName = string.gsub(filmName, [[/]], "")
138+
filmName = string.gsub(filmName, [[:]], "")
139+
filmName = string.gsub(filmName, [["]], "")
140+
filmName = string.gsub(filmName, "<", "")
141+
filmName = string.gsub(filmName, ">", "")
142+
filmName = string.gsub(filmName, "|", "")
143+
filmName = string.gsub(filmName, "*", "")
144+
filmName = string.gsub(filmName, "?", "")
292145
end
293146

294147
exportKMLFilename = filmName..".kml"
@@ -318,12 +171,12 @@ local function create_kml_file(storage, image_table, extra_data)
318171

319172
local image_title, image_description
320173
if (image.title and image.title ~= "") then
321-
image_title = string.escapeXmlCharacters(image.title)
174+
image_title = ds.escape_xml_characters(image.title)
322175
else
323176
image_title = filename..extension
324177
end
325178
-- Characters should not be escaped in CDATA, but we are using HTML fragment, so we must escape them
326-
image_description = string.escapeXmlCharacters(image.description)
179+
image_description = ds.escape_xml_characters(image.description)
327180

328181
kml_file = kml_file.." <name>"..image_title.."</name>\n"
329182
kml_file = kml_file.." <description>"..image_description.."</description>\n"
@@ -368,7 +221,7 @@ local function create_kml_file(storage, image_table, extra_data)
368221
kml_file = kml_file.." <LineString>\n"
369222
kml_file = kml_file.." <coordinates>\n"
370223

371-
for image,exported_image in spairs(image_table, function(t,a,b) return b.exif_datetime_taken > a.exif_datetime_taken end) do
224+
for image,exported_image in du.spairs(image_table, function(t,a,b) return b.exif_datetime_taken > a.exif_datetime_taken end) do
372225
if ((image.longitude and image.latitude) and
373226
(image.longitude ~= 0 and image.latitude ~= 90) -- Sometimes the north-pole but most likely just wrong data
374227
) then
@@ -426,10 +279,11 @@ local function create_kml_file(storage, image_table, extra_data)
426279
if ( dt.preferences.read("kml_export","OpenKmlFile","bool") == true ) then
427280
local path
428281

429-
if ( dt.preferences.read("kml_export","CreateKMZ","bool") == true ) then
430-
path = exportDirectory..PS.."\""..exportKMZFilename.."\""
282+
if ( dt.preferences.read("kml_export","CreateKMZ","bool") == true
283+
and dt.configuration.running_os == "linux") then
284+
path = exportDirectory..PS..exportKMZFilename
431285
else
432-
path = exportDirectory..PS.."\""..exportKMLFilename.."\""
286+
path = exportDirectory..PS..exportKMLFilename
433287
end
434288

435289
dsys.launch_default_app(path)

0 commit comments

Comments
 (0)