-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMasafAutomation.lua
More file actions
2063 lines (1790 loc) · 54.7 KB
/
MasafAutomation.lua
File metadata and controls
2063 lines (1790 loc) · 54.7 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
include("karaskel.lua")
local tr = aegisub.gettext
local utf8 = require "utf8"
local re = require "aegisub.re"
--local inspect = require "inspect"
add_background = tr "Masaf/Add Backgrounds"
remove_background_lines = tr "Masaf/Remove all Background lines"
------------ Corrections -------------
rtl_correction_script_name = tr "Masaf/Correction/Rtl Correction - All lines"
rtl_correction_selected_line = tr "Masaf/Correction/Rtl Correction - Selected lines"
rtl_correction_without_normalize = tr "Masaf/Correction/Rtl Correction without normalize"
add_rle = tr "Masaf/Correction/Add RLE - Selected lines"
undo_rtl_correction = tr "Masaf/Correction/Undo Rtl Correction - Selected lines"
convert_numbers_to_english = tr "Masaf/Correction/Numbers to English"
convert_numbers_to_arabic = tr "Masaf/Correction/Numbers to Arabic"
convert_numbers_to_persian = tr "Masaf/Correction/Numbers to Persian"
selected_numbers_to_english = tr "Masaf/Correction/Selected Numbers to English"
selected_numbers_to_arabic = tr "Masaf/Correction/Selected Numbers to Arabic"
selected_numbers_to_persian = tr "Masaf/Correction/Selected Numbers to Persian"
------------ Timing -------------
shift_start_line_forward = tr "Masaf/Timing/Shift start line forward"
shift_start_line_backward = tr "Masaf/Timing/Shift start line backward"
shift_end_line_forward = tr "Masaf/Timing/Shift end line forward"
shift_end_line_backward = tr "Masaf/Timing/Shift end line backward"
make_next_line_continuous = tr "Masaf/Timing/Make next line continuous"
make_same_time = tr "Masaf/Timing/Make Same time"
make_same_start_time = tr "Masaf/Timing/Make Same Start time"
make_same_end_time = tr "Masaf/Timing/Make Same End time"
------------ Text Movements -------------
move_last_text_part = tr "Masaf/Text Movement/Move last text part"
move_first_part_of_next = tr "Masaf/Text Movement/Move first part of next"
move_last_word = tr "Masaf/Text Movement/Move last word"
move_first_word_of_next = tr "Masaf/Text Movement/Move first word of next"
shift_line_break = tr "Masaf/Text Movement/Shift Linebreak"
shift_line_break_back = tr "Masaf/Text Movement/Shift Linebreak Back"
split_script_name = tr "Masaf/Split line"
split_at_index_script_name = tr "Masaf/Split line at Index"
break_semi_long_lines = tr "Masaf/Break Semi Long lines"
break_selected_line = tr "Masaf/Break Selected line"
show_rtl_editor_script_name = tr "Masaf/Show Rtl Editor"
remove_line_break_script_name = tr "Masaf/Remove line Breaks"
remove_position_tags = tr "Masaf/Remove Position tags"
select_playing_line = tr "Masaf/Select playing line"
generate_srt_like_text = tr "Masaf/Generate SRT like text"
------------ Special Tags ------------
fix_line_position = tr "Masaf/Special Tags/Fix line Position"
set_line_as_no_background = tr "Masaf/Special Tags/Set line as No Background"
set_line_as_dont_correct_rtl = tr "Masaf/Special Tags/Set line as Don't Correct RTL"
set_line_as_dont_remove = tr "Masaf/Special Tags/Set line as Don't Remove"
------------ Miscs ------------
unify_background_lines_script_name = tr "Masaf/Misc/Unify Background lines"
add_code_to_selected_lines_script_name = tr "Masaf/Misc/Add Code to Selected lines"
import_text_to_selected_lines = tr "Masaf/Misc/Import text to selected Lines"
display_sum_of_times = tr "Masaf/Misc/Display sum of times"
go_to_line = tr "Masaf/Misc/Go to line"
script_description = tr "Some Aegisub automation scripts specially designed for Right-To-Left language subtitles"
script_author = "Majid Shamkhani"
script_version = "1.26.0"
-- <<<<<<<<<<<<<<<<<<<<<<<<< Main Methods >>>>>>>>>>>>>>>>>>>>>>>>>
BgPatternRegex =
[[\{\\p1.*?\}m (\d+(\.\d+)?) (\d+(\.\d+)?) l (\d+(\.\d+)?) (\d+(\.\d+)?) l (\d+(\.\d+)?) (\d+(\.\d+)?) l (\d+(\.\d+)?) (\d+(\.\d+)?) l (\d+(\.\d+)?) (\d+(\.\d+)?)]]
PosPattern = "{\\pos%(.-%)}"
BgPosPattern = "\\pos%(.-%)"
SplitChars = {"||", "\\N", "%.", ",", "،", ";", "%?", "؟", "!", ":", "؛", "۔"}
WeakChars = "~!@#\\$%\\^&\\*\\-\\+=;\\|×÷٪\\?؟\\\\"
PunctuationMarks = [[%.,،%?؟:؛!;۔]]
PunctuationMarksRegex = "[\\.,،\\?؟:؛!;۔]+"
StartingBracketChars = [[%({%[<«“]]
EndingsBracketChars = [[%)}%]>»”]]
CodePattern = "({.-})"
LastPunctuationMark = "([\\.,\\?!؟:،۔؛]\\s*$)"
LreChar = utf8.char(0x202A)
RleChar = utf8.char(0x202B)
PdfChar = utf8.char(0x202C)
FixedPosTag = "\\fixedpos"
NoBgTag = "\\nobg" -- No Background
DcrtlTag = "\\dcrtl" -- Dont Correct RTL
Drl = "\\drl" -- Dont Remove Line
-- ------------------------- AddBackground ---------------------
function AddBackground(subs)
if not videoLoaded() then
return
end
local meta, styles = karaskel.collect_head(subs)
-- start processing lines
local i, n = 0, #subs
n = subs.n
local periorEndTime = ""
local groupBackgroundIndex = -1
local groupCount = 0
local bgShape, doExit = getBackgroundLine(subs, styles)
-- Missing background shape
-- Adding new shape line and exit
if doExit then
return
end
-- Comment background line
bgShape.comment = true
subs[bgShape.i] = bgShape
local positionTag = getPositionTag(bgShape.text)
local secondForContinuousBackground, dialogOk =
getNumberFromUser("\r\n Enter maximum second to make background continious: \r\n", 1)
if not dialogOk then
return
end
local lastLineStyle = nil
while i < n do
i = i + 1
aegisub.progress.task("Processing line " .. i .. "/" .. n)
aegisub.progress.set(i / n * 100)
local l = subs[i]
if l.class == "dialogue" and l.effect == "" and not l.comment and shouldAddBackground(l) then
-- remove already added background line
if bgShape ~= nil and i ~= bgShape.i and isBackgroundLine(l) then
if canRemoveBackground(l) then
subs.delete(i)
i = i - 1
n = n - 1
end
goto continue
end
-- Set text style align to 5 once.
if lastLineStyle == nil or lastLineStyle ~= l.name then
lastLineStyle = changeStyleAlignToFive(subs, styles, l)
end
if not string.find(l.text, FixedPosTag) then
l.text = addPositionTag(l.text, positionTag)
end
subs[i] = l
local startTimeEqualsPeriorEndTime = isStartTimeEqualsPeriorEndTime(l, periorEndTime, secondForContinuousBackground)
if not startTimeEqualsPeriorEndTime then
l.i = i
l.comment = false
local bgLine = generateBackground(l, bgShape)
if groupBackgroundIndex ~= -1 and groupCount > 0 then
setLastGroupBackgroundEndTime(subs, groupBackgroundIndex, periorEndTime)
groupCount = 0
end
subs.insert(i, bgLine)
groupBackgroundIndex = i
i = i + 1
n = n + 1
else
groupCount = groupCount + 1
end
periorEndTime = l.end_time
::continue::
end
end
if groupCount > 0 then
setLastGroupBackgroundEndTime(subs, groupBackgroundIndex, periorEndTime)
end
aegisub.set_undo_point(add_background)
end
------------------------------ Split Line -----------------------------
function Split(subs, selected)
if #selected > 1 then
return
end
local index = selected[1]
local textParts = {}
local line = subs[index]
local text = line.text
local line2 = table.copy(line)
-- Finding manual splittnig symbol -> ||
s, e = utf8.find(text, SplitChars[1])
if s then
line.text = utf8.sub(text, 1, s - 1)
line2.text = utf8.sub(text, e + 1, utf8.len(text))
line.text = rtlCorrectNonCodeText(trim(line.text))
line2.text = rtlCorrectNonCodeText(trim(line2.text))
changeLineTimeAfterSplit(text, line, line2)
subs[index] = line
subs.insert(index + 1, line2)
goto continue
end
textParts = getSubtitleTextParts(text)
s, e, idx = getFirstChar(text, textParts)
if idx > 0 then
-- Remove split char from end of text
if idx <= 2 then
line.text = utf8.sub(text, 1, s - 1)
line2.text = utf8.sub(text, e + 1, utf8.len(text))
else
line.text = utf8.sub(text, 1, e)
line2.text = utf8.sub(text, e + 1, utf8.len(text))
end
changeLineTimeAfterSplit(text, line, line2)
line.text = rtlCorrectNonCodeText(trim(line.text))
line2.text = rtlCorrectNonCodeText(trim(line2.text))
subs[index] = line
subs.insert(index + 1, line2)
end
::continue::
aegisub.set_undo_point(split_script_name)
return selected
end
-- -------------------------SplitAtIndex ---------------------
function SplitAtIndex(subs, selected)
if #selected > 1 then
return
end
local line = subs[selected[1]]
text = line.text
line2 = table.copy(line)
local idx, dialogOk =
getNumberFromUser("\r\n Enter index of character that you want to split line on that character: \r\n", 2)
if not dialogOk then
return
end
local s, e, idx = getCharAtIndex(text, idx)
if s then
-- Remove split char from end of text
if idx <= 2 then
line.text = utf8.sub(text, 1, s - 1)
line2.text = utf8.sub(text, e + 1, utf8.len(text))
else
line.text = utf8.sub(text, 1, e)
line2.text = utf8.sub(text, e + 1, utf8.len(text))
end
changeLineTimeAfterSplit(text, line, line2)
line.text = rtlCorrectNonCodeText(trim(line.text))
line2.text = rtlCorrectNonCodeText(trim(line2.text))
subs[selected[1]] = line
subs.insert(selected[1] + 1, line2)
end
aegisub.set_undo_point(split_at_index_script_name)
return selected
end
function BreakSemiLongLines(subs)
if not videoLoaded() then
return
end
local i, n = 0, #subs
local meta, styles = karaskel.collect_head(subs)
local videoWidth = getVideoWidth()
n = subs.n
while i < n do
i = i + 1
local l = subs[i]
if l.class == "dialogue" and l.effect == "" and not l.comment and notBreakedText(l.text) and not isBackgroundLine(l) then
local textWidth = getTextWidth(l, styles)
local breakToleranse = (videoWidth / 5) * 3
if textWidth >= breakToleranse then
l.text = autoBreakLine(l.text)
subs[i] = l
end
end
end
aegisub.set_undo_point(break_semi_long_lines)
end
function BreakSelectedLine(subs, selected)
if #selected > 1 then
return
end
local l = subs[selected[1]]
local meta, styles = karaskel.collect_head(subs)
if l.class == "dialogue" and l.effect == "" and not l.comment and notBreakedText(l.text) and not isBackgroundLine(l) then
local textWidth = getTextWidth(l, styles)
l.text = autoBreakLine(l.text)
subs[selected[1]] = l
end
aegisub.set_undo_point(break_selected_line)
end
--------------------------- RtlCorrection ---------------------
function RtlCorrection(subs)
-- start processing lines
local i, n = 0
n = subs.n
while i < n do
i = i + 1
local l = subs[i]
if l.class == "dialogue" and l.effect == "" and not l.comment and canCorrectRtl(l.text) then
if not isBackgroundLine(l) then
local parts = getSubtitleTextParts(l.text)
local text = ""
for k = 1, #parts do
local t = parts[k]
t = rtlCorrectNonCodeText(t)
text = text .. t
end
l.text = text
subs[i] = l
end
end
end
aegisub.set_undo_point(rtl_correction_script_name)
end
------------------------- Rtl Corrector Selected Line -----------------------
function RtlCorrectorSelectedLine(subs, selected)
for i = 1, #selected, 1 do
local line = subs[selected[i]]
-- start processing lines
if (not isBackgroundLine(line)) then
line.text = rtlCorrectIfAllowed(line.text)
subs[selected[i]] = line
end
end
aegisub.set_undo_point(rtl_correction_selected_line)
end
------------------------- Rtl Corrector without Normalize -----------------------
function RtlCorrectorWithoutNormalize(subs, selected)
for i = 1, #selected, 1 do
local line = subs[selected[i]]
-- start processing lines
if (not isBackgroundLine(line)) then
line.text = rtlCorrectTextWithCode(line.text, true)
subs[selected[i]] = line
end
end
aegisub.set_undo_point(rtl_correction_without_normalize)
end
---------------------- Add RLE -----------------------
function AddRle(subs, selected)
for i = 1, #selected, 1 do
local line = subs[selected[i]]
-- start processing lines
if (not isBackgroundLine(line)) then
local text, code = removeRtlChars(line.text), ""
local textParts = getSubtitleTextParts(text)
code, text = getCodeAndPlainTextPart(text, textParts)
text = addRleToBeginigOfLine(text)
line.text = code .. text
subs[selected[i]] = line
end
end
aegisub.set_undo_point(add_rle)
end
------------------------------ Undo Rtl Correction ----------------------------
function UndoRtlCorrection(subs, selected)
for i = 1, #selected, 1 do
local line = subs[selected[i]]
line.text = removeRtlChars(line.text)
subs[selected[i]] = line
end
aegisub.set_undo_point(undo_rtl_correction)
end
------------------------------ Show Rtl Editor ---------------------------------
function ShowRtlEditor(subs, selected)
if #selected > 1 then
return
end
local line = subs[selected[1]]
local textParts = getSubtitleTextParts(line.text)
local codeText, plainText = getCodeAndPlainTextPart(line.text, textParts)
local sourceText = utf8.gsub(plainText, "\\N", "\n")
local result, newText = openEditor(sourceText)
if not result then
return
end
-- Replace line break with \N
newText = utf8.gsub(newText, "\n", "\\N")
if canCorrectRtl(codeText) then
newText = rtlCorrectNonCodeText(newText)
end
line.text = codeText .. newText
subs[selected[1]] = line
aegisub.set_undo_point(show_rtl_editor_script_name)
end
--------------------------- Unify Background lines ------------------------------
function UnifyBackgroundLines(subs, selected)
local firstLine, firstLineIdx = getFirstSubtitleLine(subs)
if not isBackgroundLine(firstLine) then
return
end
-- start processing lines
local i, n = 0
n = subs.n
local lastBackgroundIdx = firstLineIdx
while i < n do
i = i + 1
local l = subs[i]
-- Prevent moving first line (Background shape)
if l.class == "dialogue" and l.effect == "" and i ~= firstLineIdx then
if isBackgroundLine(l) then
lastBackgroundIdx = lastBackgroundIdx + 1
subs.insert(lastBackgroundIdx, l)
i = i + 1
subs.delete(i)
end
end
end
aegisub.set_undo_point(unify_background_lines_script_name)
end
--------------------------- Add Code To Selected Lines ------------------------------
function AddCodeToSelectedLines(subs, selected)
local code = getTextFromUser()
if code == nil then
return
end
for i = 1, #selected, 1 do
local line = subs[selected[i]]
line.text = code .. line.text
subs[selected[i]] = line
end
aegisub.set_undo_point(add_code_to_selected_lines_script_name)
end
--------------------------- Remove line Breaks ------------------------------
function RemoveLineBreaks(subs, selected)
for i = 1, #selected, 1 do
local line = subs[selected[i]]
line.text = utf8.gsub(line.text, "\\N", " ")
line.text = removeDoubleSpace(line.text)
subs[selected[i]] = line
end
aegisub.set_undo_point(remove_line_break_script_name)
end
---------------------- Import Text to selected lines -------------------------
function ImportTextToSelectedLines(subs, selected)
if #selected == 0 then
return
end
local result, text = openEditor("")
if not result then
return
end
local texts = re.split(text, "\n")
for i = 1, #selected, 1 do
if i > table.getn(texts) then
return
end
local line = subs[selected[i]]
line.text = texts[i]
subs[selected[i]] = line
end
aegisub.set_undo_point(import_text_to_selected_lines)
end
---------------------- Select playing line -------------------------
function SelectPlayingLine(subs, selected)
local vframe = aegisub.project_properties().video_position
local fr2ms = aegisub.ms_from_frame
local j = #selected
if j < 1 or j == #subs then
j = 1
end
for i = j, #subs do
local line = subs[i]
if line.class == "dialogue" and line.start_time >= fr2ms(vframe) then
selected = {i - 1}
return selected
end
end
if j > 1 then
for i = 1, j do
local line = subs[i]
if line.class == "dialogue" and line.start_time >= fr2ms(vframe) then
selected = {i - 1}
return selected
end
end
end
end
---------------------- Make next line continuous -------------------------
function MakeNextLineContinuous(subs, selected)
if #selected == 0 or #selected > 1 then
return
end
local index = selected[1]
if index == subs.n then
return
end
local line = subs[index]
local nextLine = subs[index + 1]
nextLine.start_time = line.end_time
if nextLine.end_time == 0 then
nextLine.end_time = line.end_time + (utf8.len(nextLine.text) * 100)
end
subs[index + 1] = nextLine
selected = {index + 1}
aegisub.set_undo_point(make_next_line_continuous)
return selected
end
function MakeSameTime(subs, selected)
if #selected < 2 then
return
end
local firstLine = subs[selected[1]]
for i = 2, #selected, 1 do
local l = subs[selected[i]]
l.start_time = firstLine.start_time
l.end_time = firstLine.end_time
subs[selected[i]] = l
end
aegisub.set_undo_point(make_same_time)
end
function MakeSameStartTime(subs, selected)
if #selected < 2 then
return
end
local firstLine = subs[selected[1]]
for i = 2, #selected, 1 do
local l = subs[selected[i]]
l.start_time = firstLine.start_time
subs[selected[i]] = l
end
aegisub.set_undo_point(make_same_start_time)
end
function MakeSameEndTime(subs, selected)
if #selected < 2 then
return
end
local firstLine = subs[selected[1]]
for i = 2, #selected, 1 do
local l = subs[selected[i]]
l.end_time = firstLine.end_time
subs[selected[i]] = l
end
aegisub.set_undo_point(make_same_end_time)
end
---------------------- Start/End line shifter -------------------------
function ShiftStartLineForward(subs, selected)
if #selected == 0 or #selected > 1 then
return
end
local index = selected[1]
local line = subs[index]
line.start_time = line.start_time + 100
subs[index] = line
aegisub.set_undo_point(shift_start_line_forward)
end
function ShiftStartLineBackward(subs, selected)
if #selected == 0 or #selected > 1 then
return
end
local index = selected[1]
local line = subs[index]
line.start_time = line.start_time - 100
subs[index] = line
aegisub.set_undo_point(shift_start_line_backward)
end
function ShiftEndLineForward(subs, selected)
if #selected == 0 or #selected > 1 then
return
end
local index = selected[1]
local line = subs[index]
line.end_time = line.end_time + 100
subs[index] = line
aegisub.set_undo_point(shift_end_line_forward)
end
function ShiftEndLineBackward(subs, selected)
if #selected == 0 or #selected > 1 then
return
end
local index = selected[1]
local line = subs[index]
line.end_time = line.end_time - 100
subs[index] = line
aegisub.set_undo_point(shift_end_line_backward)
end
---------------------- Move part of lines -------------------------
function MoveLastTextPart(subs, selected)
if #selected == 0 or #selected > 1 then
return
end
local index = selected[1]
if index == subs.n then
return
end
local line = subs[index]
local nextLine = subs[index + 1]
local oldLine = table.copy(line)
local text, code = removeRtlChars(line.text), ""
local textParts = getSubtitleTextParts(text)
code, text = getCodeAndPlainTextPart(text, textParts)
local parts = getTextSplitCharsParts(text)
if #parts == 0 then
return
end
local textParts = getTextPartsBySplitCharIndexes(parts, text)
text = ""
for i = 1, #textParts - 1, 1 do
text = text .. textParts[i] .. " "
end
local nextText, nextCode = removeRtlChars(trim(nextLine.text)), ""
local nextTextParts = getSubtitleTextParts(nextText)
nextCode, nextText = getCodeAndPlainTextPart(nextText, nextTextParts)
nextText = textParts[#textParts] .. " " .. nextText
line.text = code .. rtlCorrectIfAllowed(text)
nextLine.text = nextCode .. rtlCorrectIfAllowed(nextText)
changeLineTimeAfterMove(oldLine, line, nextLine)
subs[index] = line
subs[index + 1] = nextLine
aegisub.set_undo_point(move_last_text_part)
end
function MoveFirstPartOfNext(subs, selected)
if #selected == 0 or #selected > 1 then
return
end
local index = selected[1]
if index == subs.n then
return
end
local line = subs[index]
local nextLine = subs[index + 1]
local nextText, nextCode = ""
nextText = removeRtlChars(nextLine.text)
local nextTextParts = getSubtitleTextParts(nextText)
nextCode, nextText = getCodeAndPlainTextPart(nextText, nextTextParts)
local oldLine = table.copy(line)
local parts = getTextSplitCharsParts(nextText)
if #parts == 0 then
return
end
local textParts = getTextPartsBySplitCharIndexes(parts, nextText)
nextText = ""
for i = 2, #textParts, 1 do
nextText = nextText .. textParts[i] .. " "
end
nextText = nextCode .. nextText
nextLine.text = rtlCorrectIfAllowed(nextText)
local text = removeRtlChars(line.text)
text = text .. " " .. trim(textParts[1])
line.text = rtlCorrectIfAllowed(text)
changeLineTimeAfterMove(oldLine, line, nextLine)
subs[index] = line
subs[index + 1] = nextLine
aegisub.set_undo_point(move_first_part_of_next)
end
---------------------- Move words -------------------------
function MoveLastWord(subs, selected)
if #selected == 0 or #selected > 1 then
return
end
local index = selected[1]
if index == subs.n then
return
end
local line = subs[index]
local nextLine = subs[index + 1]
local text = trim(removeRtlChars(line.text))
local oldLine = table.copy(line)
local lastWord = getLastWord(text)
if lastWord == nil then
return
end
local textLen = utf8.len(text)
line.text = utf8.sub(text, 1, textLen - utf8.len(lastWord) - 1)
local nextText, nextCode = removeRtlChars(nextLine.text), ""
local textParts = getSubtitleTextParts(nextText)
nextCode, nextText = getCodeAndPlainTextPart(nextText, textParts)
nextText = nextCode .. lastWord .. " " .. nextText
line.text = rtlCorrectIfAllowed(trim(line.text))
nextLine.text = rtlCorrectIfAllowed(trim(nextText))
changeLineTimeAfterMove(oldLine, line, nextLine)
subs[index] = line
subs[index + 1] = nextLine
aegisub.set_undo_point(move_last_word)
end
function MoveFirstWordOfNext(subs, selected)
if #selected == 0 or #selected > 1 then
return
end
local index = selected[1]
if index == subs.n then
return
end
local line = subs[index]
local nextLine = subs[index + 1]
local text, code = removeRtlChars(trim(nextLine.text)), ""
local textParts = getSubtitleTextParts(text)
code, text = getCodeAndPlainTextPart(text, textParts)
local oldLine = table.copy(line)
local firstWord = getFirstWord(text)
if firstWord == nil then
return
end
line.text = line.text .. " " .. firstWord
line.text = rtlCorrectIfAllowed(line.text)
text = code .. utf8.sub(text, utf8.len(firstWord) + 2, utf8.len(text))
nextLine.text = rtlCorrectIfAllowed(text)
changeLineTimeAfterMove(oldLine, line, nextLine)
subs[index] = line
subs[index + 1] = nextLine
aegisub.set_undo_point(move_first_word_of_next)
end
---------------------- Remove Position Tags -------------------------
function RemovePositionTags(subs)
for i = 1, #subs do
local l = subs[i]
if l.class == "dialogue" and l.effect == "" and (not l.comment) and (not isBackgroundLine(l)) then
l.text = removePosTag(l.text)
subs[i] = l
end
end
aegisub.set_undo_point(remove_position_tags)
end
---------------------- Display sum of times -------------------------
function DisplaySumOfTimes(subs)
local sum = 0
for i = 1, #subs do
local l = subs[i]
if l.class == "dialogue" and l.effect == "" and (not l.comment) and (not isBackgroundLine(l)) then
sum = sum + (l.end_time - l.start_time)
end
end
local minutes = math.ceil(sum / 1000 / 60)
local msg = "Total minutes = " .. tostring(minutes)
msg = msg .. "\nTotal time = " .. secondsToClock(sum / 1000)
showMessage(msg)
end
---------------------- Generate SRT Like Text -------------------------
function GenerateSrtLikeText(subs)
local sum = 0
local srtText = ""
local lineNumber = 0
for i = 1, #subs do
local l = subs[i]
if l.class == "dialogue" and l.effect == "" then
lineNumber = lineNumber + 1
if (not l.comment) and (not isBackgroundLine(l)) then
srtText = srtText .. lineNumber .. "\n"
srtText = srtText .. secondsToClock(l.start_time / 1000) .. " --> " .. secondsToClock(l.end_time / 1000) .. "\n"
srtText = srtText .. replaceLineBreak(cleanTags(l.text)) .. "\n"
srtText = srtText .. "\n"
end
end
end
openEditor(srtText)
end
---------------------- Remove Background Lines -------------------------
function RemoveBackgroundLines(subs)
local i, n = 0, #subs
n = subs.n
while i < n do
i = i + 1
local l = subs[i]
if l.class == "dialogue" and l.effect == "" and not l.comment and isBackgroundLine(l) then
subs.delete(i)
i = i - 1
n = n - 1
end
end
aegisub.set_undo_point(remove_background_lines)
end
---------------------- Number Converters -------------------------
function ConvertNumbersToEnglish(subs)
local i, n = 0
n = subs.n
while i < n do
i = i + 1
local l = subs[i]
if l.class == "dialogue" and l.effect == "" and not l.comment then
if not isBackgroundLine(l) then
local parts = getSubtitleTextParts(l.text)
local text = ""
for k = 1, #parts do
local t = parts[k]
t = applyNumbersToEnglish(t)
text = text .. t
end
l.text = text
subs[i] = l
end
end
end
aegisub.set_undo_point(convert_numbers_to_english)
end
function SelectedNumbersToEnglish(subs, selected)
for i = 1, #selected, 1 do
local l = subs[selected[i]]
if l.class == "dialogue" and l.effect == "" and not l.comment then
if not isBackgroundLine(l) then
local parts = getSubtitleTextParts(l.text)
local text = ""
for k = 1, #parts do
local t = parts[k]
t = applyNumbersToEnglish(t)
text = text .. t
end
l.text = text
subs[selected[i]] = l
end
end
end
aegisub.set_undo_point(selected_numbers_to_english)
end
function ConvertNumbersToArabic(subs)
local i, n = 0
n = subs.n
while i < n do
i = i + 1
local l = subs[i]
if l.class == "dialogue" and l.effect == "" and not l.comment then
if not isBackgroundLine(l) then
local parts = getSubtitleTextParts(l.text)
local text = ""
for k = 1, #parts do
local t = parts[k]
t = applyNumbersToArabic(t)
text = text .. t
end
l.text = text
subs[i] = l
end
end
end
aegisub.set_undo_point(convert_numbers_to_arabic)
end
function SelectedNumbersToArabic(subs, selected)
for i = 1, #selected, 1 do
local l = subs[selected[i]]
if l.class == "dialogue" and l.effect == "" and not l.comment then
if not isBackgroundLine(l) then
local parts = getSubtitleTextParts(l.text)
local text = ""
for k = 1, #parts do
local t = parts[k]
t = applyNumbersToArabic(t)
text = text .. t
end
l.text = text
subs[selected[i]] = l
end
end
end
aegisub.set_undo_point(selected_numbers_to_arabic)
end
function ConvertNumbersToPersian(subs)
local i, n = 0
n = subs.n
while i < n do
i = i + 1
local l = subs[i]
if l.class == "dialogue" and l.effect == "" and not l.comment then
if not isBackgroundLine(l) then
local parts = getSubtitleTextParts(l.text)
local text = ""
for k = 1, #parts do
local t = parts[k]
t = applyNumbersToPersian(t)
text = text .. t
end
l.text = text
subs[i] = l
end
end
end
aegisub.set_undo_point(convert_numbers_to_arabic)
end
function SelectedNumbersToPersian(subs, selected)
for i = 1, #selected, 1 do
local l = subs[selected[i]]
if l.class == "dialogue" and l.effect == "" and not l.comment then
if not isBackgroundLine(l) then
local parts = getSubtitleTextParts(l.text)
local text = ""
for k = 1, #parts do
local t = parts[k]
t = applyNumbersToPersian(t)
text = text .. t
end