-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathstring.lua
More file actions
1352 lines (1166 loc) · 52.1 KB
/
string.lua
File metadata and controls
1352 lines (1166 loc) · 52.1 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local dtutils_string = {}
local dt = require "darktable"
local du = require "lib/dtutils"
local log = require "lib/dtutils.log"
local gettext = dt.gettext.gettext
local DEFAULT_LOG_LEVEL = log.error
local function _(msg)
return gettext(msg)
end
dtutils_string.log_level = DEFAULT_LOG_LEVEL
dtutils_string.libdoc = {
Name = [[dtutils.string]],
Synopsis = [[a library of string utilities for use in darktable lua scripts]],
Usage = [[local ds = require "lib/dtutils.string"]],
Description = [[This library contains string manipulation routines to aid in building
darktable lua scripts.]],
Return_Value = [[du - library - the darktable lua string library]],
Limitations = [[]],
Example = [[]],
See_Also = [[]],
Reference = [[]],
License = [[This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.]],
Copyright = [[Copyright (c) 2016 Bill Ferguson <wpferguson@gmail.com>]],
functions = {}
}
dtutils_string.libdoc.functions["strip_accents"] = {
Name = [[strip_accents]],
Synopsis = [[strip accents from characters]],
Usage = [[local ds = require "lib/dtutils.string"
local result = ds.strip_accents(str)
str - string - the string with characters that need accents removed]],
Description = [[strip_accents removes accents from accented characters returning the
unaccented character.]],
Return_Value = [[result - string - the string containing unaccented characters]],
Limitations = [[]],
Example = [[]],
See_Also = [[]],
Reference = [[Copied from https://forums.coronalabs.com/topic/43048-remove-special-characters-from-string/]],
License = [[]],
Copyright = [[]],
}
function dtutils_string.strip_accents( str )
local old_log_level = log.log_level()
log.log_level(dtutils_string.log_level)
local tableAccents = {}
tableAccents["à"] = "a"
tableAccents["á"] = "a"
tableAccents["â"] = "a"
tableAccents["ã"] = "a"
tableAccents["ä"] = "a"
tableAccents["ç"] = "c"
tableAccents["è"] = "e"
tableAccents["é"] = "e"
tableAccents["ê"] = "e"
tableAccents["ë"] = "e"
tableAccents["ì"] = "i"
tableAccents["í"] = "i"
tableAccents["î"] = "i"
tableAccents["ï"] = "i"
tableAccents["ñ"] = "n"
tableAccents["ò"] = "o"
tableAccents["ó"] = "o"
tableAccents["ô"] = "o"
tableAccents["õ"] = "o"
tableAccents["ö"] = "o"
tableAccents["ù"] = "u"
tableAccents["ú"] = "u"
tableAccents["û"] = "u"
tableAccents["ü"] = "u"
tableAccents["ý"] = "y"
tableAccents["ÿ"] = "y"
tableAccents["À"] = "A"
tableAccents["Á"] = "A"
tableAccents["Â"] = "A"
tableAccents["Ã"] = "A"
tableAccents["Ä"] = "A"
tableAccents["Ç"] = "C"
tableAccents["È"] = "E"
tableAccents["É"] = "E"
tableAccents["Ê"] = "E"
tableAccents["Ë"] = "E"
tableAccents["Ì"] = "I"
tableAccents["Í"] = "I"
tableAccents["Î"] = "I"
tableAccents["Ï"] = "I"
tableAccents["Ñ"] = "N"
tableAccents["Ò"] = "O"
tableAccents["Ó"] = "O"
tableAccents["Ô"] = "O"
tableAccents["Õ"] = "O"
tableAccents["Ö"] = "O"
tableAccents["Ù"] = "U"
tableAccents["Ú"] = "U"
tableAccents["Û"] = "U"
tableAccents["Ü"] = "U"
tableAccents["Ý"] = "Y"
local normalizedString = ""
for strChar in string.gmatch(str, "([%z\1-\127\194-\244][\128-\191]*)") do
if tableAccents[strChar] ~= nil then
normalizedString = normalizedString..tableAccents[strChar]
else
normalizedString = normalizedString..strChar
end
end
log.log_level(old_log_level)
return normalizedString
end
dtutils_string.libdoc.functions["escape_xml_characters"] = {
Name = [[escape_xml_characters]],
Synopsis = [[escape characters for xml documents]],
Usage = [[local ds = require "lib/dtutils.string"
local result = ds.escape_xml_characters(str)
str - string - the string that needs escaped]],
Description = [[escape_xml_characters provides the escape sequences for
"&", '"', "'", "<", and ">" with the corresponding "&",
""", "'", "<", and ">".]],
Return_Value = [[result - string - the string containing escapes for the xml characters]],
Limitations = [[]],
Example = [[]],
See_Also = [[]],
Reference = [[https://stackoverflow.com/questions/1091945/what-characters-do-i-need-to-escape-in-xml-documents]],
License = [[]],
Copyright = [[]],
}
-- Keep & first, otherwise it will double escape other characters
function dtutils_string.escape_xml_characters( str )
local old_log_level = log.log_level()
log.log_level(dtutils_string.log_level)
str = string.gsub(str,"&", "&")
str = string.gsub(str,"\"", """)
str = string.gsub(str,"'", "'")
str = string.gsub(str,"<", "<")
str = string.gsub(str,">", ">")
log.log_level(old_log_level)
return str
end
dtutils_string.libdoc.functions["urlencode"] = {
Name = [[urlencode]],
Synopsis = [[encode a string in a websage manner]],
Usage = [[local ds = require "lib/dtutils.string"
local result = ds.urlencode(str)
str - string - the string that needs to be made websafe]],
Description = [[urlencode converts a string into a websafe version suitable for
use in a web browser.]],
Return_Value = [[result - string - a websafe string]],
Limitations = [[]],
Example = [[]],
See_Also = [[]],
Reference = [[https://forums.coronalabs.com/topic/43048-remove-special-characters-from-string/]],
License = [[]],
Copyright = [[]],
}
function dtutils_string.urlencode(str)
local old_log_level = log.log_level()
log.log_level(dtutils_string.log_level)
if (str) then
str = string.gsub (str, "\n", "\r\n")
str = string.gsub (str, "([^%w ])", function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
log.log_level(old_log_level)
return str
end
dtutils_string.libdoc.functions["is_not_sanitized"] = {
Name = [[is_not_sanitized]],
Synopsis = [[Check if a string has been sanitized]],
Usage = [[local ds = require "lib/dtutils.string"
local result = ds.is_not_sanitized(str)
str - string - the string that needs to be made safe]],
Description = [[is_not_sanitized checks a string to see if it
has been made safe use passing as an argument in a system command.]],
Return_Value = [[result - boolean - true if the string is not sanitized otherwise false]],
Limitations = [[]],
Example = [[]],
See_Also = [[]],
Reference = [[]],
License = [[]],
Copyright = [[]],
}
local function _is_not_sanitized_posix(str)
local old_log_level = log.log_level()
log.log_level(dtutils_string.log_level)
-- A sanitized string must be quoted.
if not string.match(str, "^'.*'$") then
log.log_level(old_log_level)
return true
-- A quoted string containing no quote characters within is sanitized.
elseif string.match(str, "^'[^']*'$") then
log.log_level(old_log_level)
return false
end
-- Any quote characters within a sanitized string must be properly
-- escaped.
local quotesStripped = string.sub(str, 2, -2)
local escapedQuotesRemoved = string.gsub(quotesStripped, "'\\''", "")
if string.find(escapedQuotesRemoved, "'") then
log.log_level(old_log_level)
return true
else
log.log_level(old_log_level)
return false
end
end
local function _is_not_sanitized_windows(str)
local old_log_level = log.log_level()
log.log_level(dtutils_string.log_level)
if not string.match(str, "^\".*\"$") then
log.log_level(old_log_level)
return true
else
log.log_level(old_log_level)
return false
end
end
function dtutils_string.is_not_sanitized(str)
local old_log_level = log.log_level()
log.log_level(dtutils_string.log_level)
if dt.configuration.running_os == "windows" then
log.log_level(old_log_level)
return _is_not_sanitized_windows(str)
else
log.log_level(old_log_level)
return _is_not_sanitized_posix(str)
end
end
dtutils_string.libdoc.functions["sanitize"] = {
Name = [[sanitize]],
Synopsis = [[surround a string in quotes making it safe to pass as an argument]],
Usage = [[local ds = require "lib/dtutils.string"
local result = ds.sanitize(str)
str - string - the string that needs to be made safe]],
Description = [[sanitize converts a string into a version suitable for
use passing as an argument in a system command.]],
Return_Value = [[result - string - a websafe string]],
Limitations = [[]],
Example = [[]],
See_Also = [[]],
Reference = [[]],
License = [[]],
Copyright = [[]],
}
local function _sanitize_posix(str)
local old_log_level = log.log_level()
log.log_level(dtutils_string.log_level)
if _is_not_sanitized_posix(str) then
log.log_level(old_log_level)
return "'" .. string.gsub(str, "'", "'\\''") .. "'"
else
log.log_level(old_log_level)
return str
end
end
local function _sanitize_windows(str)
local old_log_level = log.log_level()
log.log_level(dtutils_string.log_level)
if _is_not_sanitized_windows(str) then
log.log_level(old_log_level)
return "\"" .. string.gsub(str, "\"", "\"^\"\"") .. "\""
else
log.log_level(old_log_level)
return str
end
end
local function _should_be_sanitized(str)
local old_log_level = log.log_level()
local result = false
local UNSAFE_POSIX_FILENAME_CHARS <const> = "[^%w/._%-]+"
local UNSAFE_WIN_FILENAME_CHARS <const> = "[^%w\\._%-:]+"
local pattern = UNSAFE_POSIX_FILENAME_CHARS
if dt.configuration.running_os == "windows" then
pattern = UNSAFE_WIN_FILENAME_CHARS
end
log.log_level(dtutils_string.log_level)
if string.match(str, pattern) then
result = true
end
log.log_level(old_log_level)
return result
end
function dtutils_string.sanitize(str)
local old_log_level = log.log_level()
local sanitized_str = nil
log.log_level(dtutils_string.log_level)
if _should_be_sanitized(str) then
if dt.configuration.running_os == "windows" then
sanitized_str = _sanitize_windows(str)
else
sanitized_str = _sanitize_posix(str)
end
else
sanitized_str = str
end
log.log_level(old_log_level)
return sanitized_str
end
dtutils_string.libdoc.functions["sanitize_lua"] = {
Name = [[sanitize_lua]],
Synopsis = [[escape lua 'magic' characters from a pattern string]],
Usage = [[local ds = require "lib/dtutils.string"
local result = ds.sanitize_lua(str)
str - string - the string that needs to be made safe]],
Description = [[sanitize_lua escapes lua 'magic' characters so that
a string may be used in lua string/patten matching.]],
Return_Value = [[result - string - a lua pattern safe string]],
Limitations = [[]],
Example = [[]],
See_Also = [[]],
Reference = [[]],
License = [[]],
Copyright = [[]],
}
function dtutils_string.sanitize_lua(str)
local old_log_level = log.log_level()
log.log_level(dtutils_string.log_level)
str = string.gsub(str, "%%", "%%%%")
str = string.gsub(str, "%-", "%%-")
str = string.gsub(str, "%(", "%%(")
str = string.gsub(str, "%)", "%%)")
str = string.gsub(str, "%[", "%%[")
str = string.gsub(str, "%]", "%%]")
str = string.gsub(str, "+", "%%+")
log.log_level(old_log_level)
return str
end
dtutils_string.libdoc.functions["split_filepath"] = {
Name = [[split_filepath]],
Synopsis = [[split a filepath into parts]],
Usage = [[local ds = require "lib/dtutils.string"
local result = ds.split_filepath(filepath)
filepath - string - path and filename]],
Description = [[split_filepath splits a filepath into the path, filename, basename and filetype and puts
that in a table]],
Return_Value = [[result - table - a table containing the path, filename, basename, and filetype]],
Limitations = [[]],
Example = [[]],
See_Also = [[]],
Reference = [[]],
License = [[]],
Copyright = [[]],
}
function dtutils_string.split_filepath(str)
local old_log_level = log.log_level()
log.log_level(dtutils_string.log_level)
-- strip out single quotes from quoted pathnames
str = string.gsub(str, "'", "")
str = string.gsub(str, '"', '')
local result = {}
-- Thank you Tobias Jakobs for the awesome regular expression, which I tweaked a little
result["path"], result["filename"], result["basename"], result["filetype"] = string.match(str, "(.-)(([^\\/]-)%.?([^%.\\/]*))$")
if result["basename"] == "" and result["filetype"]:len() > 1 then
result["basename"] = result["filetype"]
result["filetype"] = ""
end
log.log_level(old_log_level)
return result
end
dtutils_string.libdoc.functions["get_path"] = {
Name = [[get_path]],
Synopsis = [[get the path from a file path]],
Usage = [[local ds = require "lib/dtutils.string"
local result = ds.get_path(filepath)
filepath - string - path and filename]],
Description = [[get_path strips the filename and filetype from a path and returns the path]],
Return_Value = [[result - string - the path]],
Limitations = [[]],
Example = [[]],
See_Also = [[]],
Reference = [[]],
License = [[]],
Copyright = [[]],
}
function dtutils_string.get_path(str)
local old_log_level = log.log_level()
log.log_level(dtutils_string.log_level)
local parts = dtutils_string.split_filepath(str)
log.log_level(old_log_level)
return parts["path"]
end
dtutils_string.libdoc.functions["get_filename"] = {
Name = [[get_filename]],
Synopsis = [[get the filename and extension from a file path]],
Usage = [[local ds = require "lib/dtutils.string"
local result = ds.get_filename(filepath)
filepath - string - path and filename]],
Description = [[get_filename strips the path from a filepath and returns the filename]],
Return_Value = [[result - string - the file name and type]],
Limitations = [[]],
Example = [[]],
See_Also = [[]],
Reference = [[]],
License = [[]],
Copyright = [[]],
}
function dtutils_string.get_filename(str)
local old_log_level = log.log_level()
log.log_level(dtutils_string.log_level)
local parts = dtutils_string.split_filepath(str)
log.log_level(old_log_level)
return parts["filename"]
end
dtutils_string.libdoc.functions["get_basename"] = {
Name = [[get_basename]],
Synopsis = [[get the filename without the path or extension]],
Usage = [[local ds = require "lib/dtutils.string"
local result = ds.get_basename(filepath)
filepath - string - path and filename]],
Description = [[get_basename returns the name of the file without the path or filetype
]],
Return_Value = [[result - string - the basename of the file]],
Limitations = [[]],
Example = [[]],
See_Also = [[]],
Reference = [[]],
License = [[]],
Copyright = [[]],
}
function dtutils_string.get_basename(str)
local old_log_level = log.log_level()
log.log_level(dtutils_string.log_level)
local parts = dtutils_string.split_filepath(str)
log.log_level(old_log_level)
return parts["basename"]
end
dtutils_string.libdoc.functions["get_filetype"] = {
Name = [[get_filetype]],
Synopsis = [[get the filetype from a filename]],
Usage = [[local ds = require "lib/dtutils.string"
local result = ds.get_filetype(filepath)
filepath - string - path and filename]],
Description = [[get_filetype returns the filetype from the supplied filepath]],
Return_Value = [[result - string - the filetype]],
Limitations = [[]],
Example = [[]],
See_Also = [[]],
Reference = [[]],
License = [[]],
Copyright = [[]],
}
function dtutils_string.get_filetype(str)
local old_log_level = log.log_level()
log.log_level(dtutils_string.log_level)
local parts = dtutils_string.split_filepath(str)
log.log_level(old_log_level)
return parts["filetype"]
end
dtutils_string.libdoc.functions["build_substitute_list"] = {
Name = [[build_substitute_list]],
Synopsis = [[build a list of variable substitutions]],
Usage = [[local ds = require "lib/dtutils.string"
ds.build_substitute_list(image, sequence, variable_string, [username], [pic_folder], [home], [desktop])
image - dt_lua_image_t - the image being processed
sequence - integer - the sequence number of the image being processed (exported)
variable_string - string - the substitution variable string
[username] - string - optional - user name. Will be determined if not supplied
[pic_folder] - string - optional - pictures folder name. Will be determined if not supplied
[home] - string - optional - home directory. Will be determined if not supplied
[desktop] - string - optional - desktop directory. Will be determined if not supplied]],
Description = [[build_substitute_list populates variables with values from the arguments
and determined from the system and darktable.]],
Return_Value = [[]],
Limitations = [[If the value for a variable can not be determined, or if it is not supported,
then an empty string is used for the value.]],
Example = [[]],
See_Also = [[https://docs.darktable.org/usermanual/4.6/en/special-topics/variables/]],
Reference = [[]],
License = [[]],
Copyright = [[]],
}
local substitutes = {}
local category_substitutes = {}
-- - - - - - - - - - - - - - - - - - - - - - - -
-- C O N S T A N T S
-- - - - - - - - - - - - - - - - - - - - - - - -
local PLACEHOLDERS = {"ROLL.NAME",
"FILE.FOLDER",
"FILE.NAME",
"FILE.EXTENSION",
"ID",
"IMAGE.ID",
"IMAGE.ID.NEXT",
"VERSION",
"VERSION.IF.MULTI",
"VERSION.NAME",
"DARKTABLE.VERSION",
"Xmp.darktable.version",
"DARKTABLE.NAME", -- Not Implemented
"SEQUENCE",
"WIDTH.SENSOR",
"HEIGHT.SENSOR",
"WIDTH.RAW",
"HEIGHT.RAW",
"WIDTH.CROP",
"HEIGHT.CROP",
"WIDTH.EXPORT",
"HEIGHT.EXPORT",
"WIDTH.MAX", -- Not Implemented
"HEIGHT.MAX", -- Not Implemented
"YEAR",
"YEAR.SHORT",
"MONTH",
"MONTH.LONG",
"MONTH.SHORT",
"DAY",
"HOUR",
"HOUR.AMPM", -- Not Implemented
"MINUTE",
"SECOND",
"MSEC",
"EXIF.YEAR",
"EXIF.YEAR.SHORT",
"EXIF.MONTH",
"EXIF.MONTH.LONG",
"EXIF.MONTH.SHORT",
"EXIF.DAY",
"EXIF.HOUR",
"EXIF.HOUR.AMPM", -- Not Implemented
"EXIF.MINUTE",
"EXIF.SECOND",
"EXIF.MSEC",
"EXIF.DATE.REGIONAL", -- Not Implemented
"EXIF.TIME.REGIONAL", -- Not Implemented
"EXIF.ISO",
"EXIF.EXPOSURE",
"EXIF.EXPOSURE.BIAS",
"EXIF.EXPOSURE.PROGRAM", -- Not Implemented
"EXIF.APERTURE",
"EXIF.CROP.FACTOR",
"EXIF.FOCAL.LENGTH",
"EXIF.FOCAL.LENGTH.EQUIV", -- Not Implemented
"EXIF.FOCUS.DISTANCE",
"EXIF.MAKER",
"EXIF.MODEL",
"EXIF.WHTIEBALANCE", -- Not Implemented
"EXIF.METERING", -- Not Implemented
"EXIF.LENS",
"EXIF.FLASH.ICON", -- Not Implemented
"EXIF.FLASH", -- Not Implemented
"GPS.LONGITUDE", -- Not Implemented
"GPS.LATITUDE", -- Not Implemented
"GPS.ELEVATION", -- Not Implemented
"GPS.LOCATION.ICON", -- Not Implemented
"LONGITUDE",
"LATITUDE",
"ELEVATION",
"GPS.LOCATION", -- Not Implemented
"STARS",
"RATING.ICONS", -- Not Implemented
"LABELS",
"LABELS.ICONS", -- Not Implemented
"TITLE",
"Xmp.dc.title",
"DESCRIPTION",
"Xmp.dc.description",
"CREATOR",
"Xmp.dc.creator",
"PUBLISHER",
"Xmp.dc.publisher",
"RIGHTS",
"Xmp.dc.rights",
"TAGS", -- Not Implemented
"SIDECAR.TXT", -- Not Implemented
"FOLDER.PICTURES",
"FOLDER.HOME",
"FOLDER.DESKTOP",
"OPENCL.ACTIVATED", -- Not Implemented
"USERNAME",
"NL", -- Not Implemented
"JOBCODE" -- Not Implemented
}
local PS = dt.configuration.running_os == "windows" and "\\" or "/"
local USER = os.getenv("USERNAME")
local HOME = dt.configuration.running_os == "windows" and os.getenv("HOMEPATH") or os.getenv("HOME")
local PICTURES = HOME .. PS .. (dt.configuration.running_os == "windows" and "My Pictures" or "Pictures")
local DESKTOP = HOME .. PS .. "Desktop"
local function get_colorlabels(image)
local old_log_level = log.log_level()
log.log_level(dtutils_string.log_level)
local colorlabels = {}
if image.red then table.insert(colorlabels, "red") end
if image.yellow then table.insert(colorlabels, "yellow") end
if image.green then table.insert(colorlabels, "green") end
if image.blue then table.insert(colorlabels, "blue") end
if image.purple then table.insert(colorlabels, "purple") end
local labels = #colorlabels == 1 and colorlabels[1] or du.join(colorlabels, "_")
log.log_level(old_log_level)
return labels
end
-- handle the ROLL.NAME[n] case
local function build_rollname_substitution_list(image, variable_string)
local old_log_level = log.log_level()
log.log_level(dtutils_string.log_level)
for match in string.gmatch(variable_string, "%$%(.-%)?%)") do -- grab each complete variable
log.msg(log.info, "match is " .. match)
local var = string.match(match, "%$%((.-%)?)%)") -- strip of the leading $( and trailing )
log.msg(log.info, "var is " .. var)
local element = nil
if string.match(var, "ROLL.NAME%[") then
element = string.match(var, "%[(%d)%]")
local path = du.split(image.film.path, dt.configuration.running_os ~= "windows" and "/" or "\\")
element = tonumber(element) + 1 -- add one because c arrays are 0 based and lua are 1 based
if element > #path then
log.msg(log.warn, "ROLL.NAME element requested was more than number of elements in film roll")
substitutes[var] = ""
else
substitutes[var] = path[(#path + 1) - element]
end
end
end
end
-- find the $CATEGORYn and $CATEGORY[n,m] requests and add them to the substitute list
local function build_category_substitution_list(image, variable_string)
local old_log_level = log.log_level()
log.log_level(dtutils_string.log_level)
for match in string.gmatch(variable_string, "%$%(.-%)?%)") do -- grab each complete variable
log.msg(log.info, "match is " .. match)
local var = string.match(match, "%$%((.-%)?)%)") -- strip of the leading $( and trailing )
log.msg(log.info, "var is " .. var)
if string.match(var, "CATEGORY%d") or string.match(var, "CATEGORY%[") then
local element
local tag
if string.match(var, "CATEGORY%d") then
element, tag = string.match(var, "CATEGORY(%d)%((.-)%)") -- get the element number and the tag to match
else
element, tag = string.match(var, "%[(%d),(.-)%]") -- new syntax
end
element = element + 1 -- add one to element since lua arrays are 1 based
log.msg(log.debug, "element is " .. element .. " and tag is " .. tag)
local tags = image:get_tags()
log.msg(log.debug, "got " .. #tags .. " from image " .. image.filename)
for _, image_tag in ipairs(tags) do
log.msg(log.debug, "checking tag " .. image_tag.name)
if string.match(image_tag.name, tag) then
fields = du.split(image_tag.name, "|")
if element <= #fields then
substitutes[var] = fields[element]
else
substitutes[var] = ""
log.msg(log.warn, "requested field for tag " .. tag .. " doesn't exist")
end
log.msg(log.info, "set substitute for " .. var .. " to " .. fields[element])
end
end
end
end
log.log_level(old_log_level)
end
-- convert image.exif_datetime_taken to system time
local function exiftime2systime(exiftime)
local yr,mo,dy,h,m,s = string.match(exiftime, "(%d-):(%d-):(%d-) (%d-):(%d-):(%d+)")
return(os.time{year=yr, month=mo, day=dy, hour=h, min=m, sec=s})
end
-- build the argument substitution list from each image
function dtutils_string.build_substitute_list(image, sequence, variable_string, username, pic_folder, home, desktop)
local old_log_level = log.log_level()
log.log_level(dtutils_string.log_level)
-- is time millisecond aware? Implemented in API 9.1.0
local is_api_9_1 = true
if dt.configuration.api_version_string < "9.1.0" then
is_api_9_1 = false
end
local is_api_9_4 = dt.configuration.api_version_string >= "9.4.0" and true or false
local datetime = os.date("*t")
local long_month = os.date("%B")
local short_month = os.date("%b")
local user_name = username or USER
local pictures_folder = pic_folder or PICTURES
local home_folder = home or HOME
local desktop_folder = desktop or DESKTOP
local labels = get_colorlabels(image)
local datetime_taken = ""
local use_millisecs = false
if dt.preferences.read("darktable", "lighttable/ui/milliseconds", "bool") and is_api_9_1 then
use_millisecs = true
end
if image.exif_datetime_taken and image.exif_datetime_taken ~= "" then
datetime_taken = image.exif_datetime_taken
else
if use_millisecs then
datetime_taken = "0000:00:00 00:00:00.0"
else
datetime_taken = "0000:00:00 00:00:00"
end
end
local eyear, emon, eday, ehour, emin, esec, emsec
if use_millisecs then
eyear, emon, eday, ehour, emin, esec, emsec =
string.match(datetime_taken, "(%d+):(%d+):(%d+) (%d+):(%d+):(%d+)%.(%d+)$")
else
emsec = "0"
eyear, emon, eday, ehour, emin, esec =
string.match(datetime_taken, "(%d+):(%d+):(%d+) (%d+):(%d+):(%d+)$")
end
local version_multi = #image:get_group_members() > 1 and image.duplicate_index or ""
local replacements = {dtutils_string.get_basename(image.film.path),-- ROLL.NAME
image.path, -- FILE.FOLDER
dtutils_string.get_basename(image.filename),-- FILE.NAME
dtutils_string.get_filetype(image.filename),-- FILE.EXTENSION
image.id, -- ID
image.id, -- IMAGE.ID
dt.database[#dt.database].id + 1, -- IMAGE.ID.NEXT
image.duplicate_index, -- VERSION
version_multi, -- VERSION.IF_MULTI
image.version_name and image.version_name or "", -- VERSION.NAME
dt.configuration.version, -- DARKTABLE.VERSION
dt.configuration.version, -- Xmp.darktable.version
"", -- DARKTABLE.NAME
string.format("%04d", sequence), -- SEQUENCE
image.width, -- WIDTH.SENSOR
image.height, -- HEIGHT.SENSOR
is_api_9_1 and image.p_width or "", -- WIDTH.RAW
is_api_9_1 and image.p_height or "", -- HEIGHT.RAW
is_api_9_1 and image.final_width or "", -- WIDTH.CROP
is_api_9_1 and image.final_height or "", -- HEIGHT.CROP
is_api_9_1 and image.final_width or "", -- WIDTH.EXPORT
is_api_9_1 and image.final_height or "", -- HEIGHT.EXPORT
"", -- WIDTH.MAX -- from export module
"", -- HEIGHT.MAX -- from export module
string.format("%4d", datetime.year), -- YEAR
string.sub(datetime.year, 3), -- YEAR.SHORT
string.format("%02d", datetime.month), -- MONTH
long_month, -- MONTH.LONG
short_month, -- MONTH.SHORT
string.format("%02d", datetime.day), -- DAY
string.format("%02d", datetime.hour), -- HOUR
"", -- HOUR.AMPM
string.format("%02d", datetime.min), -- MINUTE
string.format("%02d", datetime.sec), -- SECOND
0, -- MSEC
eyear, -- EXIF.YEAR
string.sub(eyear, 3), -- EXIF.YEAR.SHORT
emon, -- EXIF.MONTH
os.date("%B", exiftime2systime(datetime_taken)), -- EXIF.MONTH.LONG
os.date("%b", exiftime2systime(datetime_taken)), -- EXIF.MONTH.SHORT
eday, -- EXIF.DAY
ehour, -- EXIF.HOUR
"", -- EXIF.HOUR.AMPM
emin, -- EXIF.MINUTE
esec, -- EXIF.SECOND
emsec, -- EXIF.MSEC
"", -- EXIF.DATE.REGIONAL - wont be implemented
"", -- EXIF.TIME.REGIONAL - wont be implemented
string.format("%d", image.exif_iso), -- EXIF.ISO
string.format("%.0f", 1./image.exif_exposure), -- EXIF.EXPOSURE
image.exif_exposure_bias, -- EXIF.EXPOSURE.BIAS
"", -- EXIF.EXPOSURE.PROGRAM
string.format("%.01f", image.exif_aperture), -- EXIF.APERTURE
string.format("%.01f", image.exif_crop),-- EXIF.CROP_FACTOR
string.format("%.0f", image.exif_focal_length), -- EXIF.FOCAL.LENGTH
string.format("%.0f", image.exif_focal_length * image.exif_crop), -- EXIF.FOCAL.LENGTH.EQUIV
image.exif_focus_distance, -- EXIF.FOCUS.DISTANCE
image.exif_maker, -- EXIF.MAKER
image.exif_model, -- EXIF.MODEL
is_api_9_4 and image.exif_whitebalance or "", -- EXIF.WHITEBALANCE
is_api_9_4 and image.exif_metering_mode or "", -- EXIF.METERING
image.exif_lens, -- LENS
"", -- EXIF.FLASH.ICON
is_api_9_4 and image.exif_flash or "", -- EXIF.FLASH
"", -- GPS.LONGITUDE
"", -- GPS.LATITUDE
"", -- GPS.ELEVATION
"", -- GPS.LOCATION.ICON
image.longitude or "", -- LONGITUDE
image.latitude or "", -- LATITUDE
image.elevation or "", -- ELEVATION
"", -- GPS.LOCATION - wont be implemented
image.rating, -- STARS
"", -- RATING.ICONS - wont be implemented
labels, -- LABELS
"", -- LABELS.ICONS - wont be implemented
image.title and image.title or "", -- TITLE
image.title and image.title or "", -- Xmp.dc.title
image.description and image.description or "", -- DESCRIPTION
image.description and image.description or "", -- Xmp.dc.description
image.creator and image.creator or "", -- CREATOR
image.creator and image.creator or "", -- Xmp.dc.creator
image.publisher and image.publisher or "", -- PUBLISHER
image.publisher and image.publisher or "", -- Xmp.dc.publisher
image.rights and image.rights or "", -- RIGHTS
image.rights and image.rights or "", -- Xmp.dc.rights
"", -- TAGS - wont be implemented
"", -- SIDECAR.TXT - wont be implemented
pictures_folder, -- FOLDER.PICTURES
home_folder, -- FOLDER.HOME
desktop_folder, -- FOLDER.DESKTOP
"", -- OPENCL.ACTIVATED - wont be implemented
user_name, -- USERNAME
"", -- NL - wont be implemented
"" -- JOBCODE - wont be implemented
}
-- populate the substitution list
for i = 1, #PLACEHOLDERS, 1 do
substitutes[PLACEHOLDERS[i]] = replacements[i]
log.msg(log.info, "setting " .. PLACEHOLDERS[i] .. " to " .. tostring(replacements[i]))
end
-- do category substitutions separately
build_category_substitution_list(image, variable_string)
build_rollname_substitution_list(image, variable_string)
log.log_level(old_log_level)
end
dtutils_string.libdoc.functions["get_substitution_tooltip"] = {
Name = [[get_substitution_tooltip]],
Synopsis = [[get a tooltip that lists the substitution variables]],
Usage = [[local ds = require "lib/dtutils.string"
ds.get_substitution_tooltip()
Description = [[get_substitution_tooltip lists the variables with brief explanations]],
Return_Value = [[string - the tooltip]],
Limitations = [[]],
Example = [[]],
See_Also = [[https://docs.darktable.org/usermanual/4.6/en/special-topics/variables/]],
Reference = [[]],
License = [[]],
Copyright = [[]],
}
function dtutils_string.get_substitution_tooltip()
return table.concat({
_("$(ROLL.NAME) - roll of the input image"),
_("$(FILE.FOLDER) - folder containing the input image"),
_("$(FILE.NAME) - basename of the input image"),
_("$(FILE.EXTENSION) - extension of the input image"),
_("$(ID) - image ID"),
_("$(IMAGE.ID) - image ID"),
_("$(IMAGE.ID.NEXT) - next image ID to be assigned on import"),
_("$(VERSION) - duplicate version"),
_("$(VERSION.IF_MULTI) - same as $(VERSION) but null string if only one version exists"),
_("$(VERSION.NAME) - version name from metadata"),
_("$(DARKTABLE.VERSION) - current darktable version"),
_("$(Xmp.darktable.version) - current darktable version"),
-- _("$(DARKTABLE.NAME) - darktable name"), -- not implemented
_("$(SEQUENCE[n,m]) - sequence number, n: number of digits, m: start number"),
_("$(WIDTH.SENSOR) - image sensor width"),
_("$(HEIGHT.SENSOR) - image sensor height"),
_("$(WIDTH.RAW) - RAW image width"),
_("$(HEIGHT.RAW) - RAW image height"),
_("$(WIDTH.CROP) - image width after crop"),
_("$(HEIGHT.CROP) - image height after crop"),
_("$(WIDTH.EXPORT) - exported image width"),
_("$(HEIGHT.EXPORT) - exported image height"),
-- _("$(WIDTH.MAX) - maximum image export width"), -- not implemented
-- _("$(HEIGHT.MAX) - maximum image export height"), -- not implemented
_("$(YEAR) - year"),
_("$(YEAR.SHORT) - year without century"),
_("$(MONTH) - month"),
_("$(MONTH.LONG) - full month name according to the current locale"),
_("$(MONTH.SHORT) - abbreviated month name according to the current locale"),
_("$(DAY) - day"),
_("$(HOUR) - hour"),
-- _("$(HOUR.AMPM) - hour, 12-hour clock"), -- not implemented
_("$(MINUTE) - minute"),
_("$(SECOND) - second"),
_("$(MSEC) - millisecond"),
_("$(EXIF.YEAR) - EXIF year"),
_("$(EXIF.YEAR.SHORT) - EXIF year without century"),
_("$(EXIF.MONTH) - EXIF month"),
_("$(EXIF.MONTH.LONG) - full EXIF month name according to the current locale"),
_("$(EXIF.MONTH.SHORT) - abbreviated EXIF month name according to the current locale"),
_("$(EXIF.DAY) - EXIF day"),
_("$(EXIF.HOUR) - EXIF hour"),
-- _("$(EXIF.HOUR.AMPM) - EXIF hour, 12-hour clock") .. "\n" .. -- not implemented
_("$(EXIF.MINUTE) - EXIF minute"),
_("$(EXIF.SECOND) - EXIF second"),
_("$(EXIF.MSEC) - EXIF millisecond"),
-- _("$(EXIF.DATE.REGIONAL) - localized EXIF date"), -- not implemented
-- _("$(EXIF.TIME.REGIONAL) - localized EXIF time"), -- not implemented
_("$(EXIF.ISO) - ISO value"),
_("$(EXIF.EXPOSURE) - EXIF exposure"),
_("$(EXIF.EXPOSURE.BIAS) - EXIF exposure bias"),
-- _("$(EXIF.EXPOSURE.PROGRAM) - EXIF exposure program"), -- not implemented
_("$(EXIF.APERTURE) - EXIF aperture"),
_("$(EXIF.CROP_FACTOR) - EXIF crop factor"),
_("$(EXIF.FOCAL.LENGTH) - EXIF focal length"),
_("$(EXIF.FOCAL.LENGTH.EQUIV) - EXIF 35 mm equivalent focal length"),
_("$(EXIF.FOCUS.DISTANCE) - EXIF focal distance"),
_("$(EXIF.MAKER) - camera maker") ..
_("$(EXIF.MODEL) - camera model") ..
_("$(EXIF.WHITEBALANCE) - EXIF selected white balance") .. -- not implemented
_("$(EXIF.METERING) - EXIF exposure metering mode") .. -- not implemented
_("$(EXIF.LENS) - lens") ..
-- _("$(EXIF.FLASH.ICON) - icon indicating whether flash was used") .. -- not implemented
_("$(EXIF.FLASH) - was flash used (yes/no/--)") .. -- not implemented
-- _("$(GPS.LONGITUDE) - longitude"),-- not implemented
-- _("$(GPS.LATITUDE) - latitude"),-- not implemented