@@ -28,7 +28,6 @@ WARNING
28
28
This script is only tested with Linux
29
29
30
30
USAGE
31
- * require script "official/yield" from your main Lua file in the first line
32
31
* require this script from your main Lua file
33
32
* when choosing file format, pick JPEG or PNG as Google Earth doesn't support other formats
34
33
@@ -37,13 +36,14 @@ USAGE
37
36
local dt = require " darktable"
38
37
local du = require " lib/dtutils"
39
38
local df = require " lib/dtutils.file"
39
+ local ds = require " lib/dtutils.string"
40
40
local dsys = require " lib/dtutils.system"
41
- require " official/yield "
41
+
42
42
local gettext = dt .gettext
43
43
44
44
local PS = dt .configuration .running_os == " windows" and " \\ " or " /"
45
45
46
- du .check_min_api_version (" 3 .0.0" , kml_export )
46
+ du .check_min_api_version (" 5 .0.0" , kml_export )
47
47
48
48
-- Tell gettext where to find the .mo file translating messages for a particular domain
49
49
gettext .bindtextdomain (" kml_export" ,dt .configuration .config_dir .. " /lua/locale/" )
@@ -52,168 +52,10 @@ local function _(msgid)
52
52
return gettext .dgettext (" kml_export" , msgid )
53
53
end
54
54
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
-
79
55
local function show_status (storage , image , format , filename , number , total , high_quality , extra_data )
80
56
dt .print (string.format (_ (" Export Image %i/%i" ), number , total ))
81
57
end
82
58
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 ," <" , " <" )
212
- str = string.gsub (str ," >" , " >" )
213
-
214
- return str
215
- end
216
-
217
59
-- Add duplicate index to filename
218
60
-- image.filename does not have index, exported_image has index
219
61
function addDuplicateIndex ( index , filename )
@@ -241,7 +83,7 @@ local function create_kml_file(storage, image_table, extra_data)
241
83
return
242
84
end
243
85
end
244
- dt .print_error (" Will try to export KML file now" )
86
+ dt .print_log (" Will try to export KML file now" )
245
87
246
88
local imageFoldername
247
89
if ( dt .preferences .read (" kml_export" ," CreateKMZ" ," bool" ) == true
@@ -276,8 +118,8 @@ local function create_kml_file(storage, image_table, extra_data)
276
118
-- will be scaled so its largest dimension is 120 pixels. The '+profile "*"' removes any ICM, EXIF, IPTC, or other
277
119
-- profiles that might be present in the input and aren't needed in the thumbnail.
278
120
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 )
281
123
else
282
124
-- Remove exported image if it has no GPS data
283
125
os.remove (exported_image )
@@ -288,7 +130,18 @@ local function create_kml_file(storage, image_table, extra_data)
288
130
289
131
-- Strip accents from the filename, because GoogleEarth can't open them
290
132
-- 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 , " ?" , " " )
292
145
end
293
146
294
147
exportKMLFilename = filmName .. " .kml"
@@ -318,12 +171,12 @@ local function create_kml_file(storage, image_table, extra_data)
318
171
319
172
local image_title , image_description
320
173
if (image .title and image .title ~= " " ) then
321
- image_title = string . escapeXmlCharacters (image .title )
174
+ image_title = ds . escape_xml_characters (image .title )
322
175
else
323
176
image_title = filename .. extension
324
177
end
325
178
-- 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 )
327
180
328
181
kml_file = kml_file .. " <name>" .. image_title .. " </name>\n "
329
182
kml_file = kml_file .. " <description>" .. image_description .. " </description>\n "
@@ -368,7 +221,7 @@ local function create_kml_file(storage, image_table, extra_data)
368
221
kml_file = kml_file .. " <LineString>\n "
369
222
kml_file = kml_file .. " <coordinates>\n "
370
223
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
372
225
if ((image .longitude and image .latitude ) and
373
226
(image .longitude ~= 0 and image .latitude ~= 90 ) -- Sometimes the north-pole but most likely just wrong data
374
227
) then
@@ -426,10 +279,11 @@ local function create_kml_file(storage, image_table, extra_data)
426
279
if ( dt .preferences .read (" kml_export" ," OpenKmlFile" ," bool" ) == true ) then
427
280
local path
428
281
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
431
285
else
432
- path = exportDirectory .. PS .. " \" " .. exportKMLFilename .. " \" "
286
+ path = exportDirectory .. PS .. exportKMLFilename
433
287
end
434
288
435
289
dsys .launch_default_app (path )
0 commit comments