1
1
export script_name = " Remove Tags"
2
2
export script_description = " Dynamically remove tags based on selection"
3
- export script_version = " 1.0.1 "
3
+ export script_version = " 1.0.2 "
4
4
export script_author = " PhosCity"
5
5
export script_namespace = " phos.RemoveTags"
6
6
7
+
7
8
DependencyControl = require " l0.DependencyControl"
8
9
depctrl = DependencyControl {
9
10
feed : " https://raw.githubusercontent.com/PhosCity/Aegisub-Scripts/main/DependencyControl.json" ,
@@ -14,32 +15,33 @@ depctrl = DependencyControl{
14
15
feed : " https: //raw.githubusercontent.com/TypesettingTools/ASSFoundation/master/DependencyControl.json" } ,
15
16
{ " l0.Functional" , version : " 0.6.0" , url : " https://github.com/TypesettingTools/Functional" ,
16
17
feed : " https://raw.githubusercontent.com/TypesettingTools/Functional/master/DependencyControl.json" } ,
18
+ { " phos.AssfPlus" , version : " 1.0.4" , url : " https://github.com/PhosCity/Aegisub-Scripts" ,
19
+ feed : " https://raw.githubusercontent.com/PhosCity/Aegisub-Scripts/main/DependencyControl.json" } ,
17
20
}
18
21
}
19
- LineCollection , ASS , Functional = depctrl\ requireModules!
20
- logger = depctrl\ getLogger!
22
+ LineCollection , ASS , Functional , AssfPlus = depctrl\ requireModules!
21
23
{ : list} = Functional
22
24
23
25
24
- -- - Collects all the tags in the selected lines
25
- -- -@param sub table subtitle object
26
- -- -@param sel table selected lines
27
- -- -@return table collection that has tagList, buttons, top row options, tag groups, hints and more
28
- collectTags = ( sub, sel) ->
29
- collection =
30
- tagList : {}
31
- startTagIndex : nil
32
- buttons : { " Remove Tags" , " Remove All" , " Cancel" }
33
- topRow : { start_tag : false , inline_tags : false , transforms : false , invert : true }
34
- removeGroup : { color : false , alpha : false , rotation : false , scale : false , perspective : false , inline_except_last : false }
35
- removeGroupTags : {
26
+ collectTags = ( lines) ->
27
+
28
+ collection = AssfPlus . lineCollection. collectTags lines, true
29
+
30
+ with collection
31
+
32
+ . buttons = { " Remove Tags" , " Remove All" , " Cancel" }
33
+
34
+ . removeGroup = { color : false , alpha : false , rotation : false , scale : false , perspective : false , inline_except_last : . multiple_inline_tags}
35
+
36
+ . removeGroupTags = {
36
37
color : { " color1" , " color2" , " color3" , " color4" }
37
38
alpha : { " alpha" , " alpha1" , " alpha2" , " alpha3" , " alpha4" }
38
39
rotation : { " angle" , " angle_x" , " angle_y" }
39
40
scale : { " fontsize" , " scale_x" , " scale_y" }
40
41
perspective : { " angle" , " angle_x" , " angle_y" , " shear_x" , " shear_y" }
41
42
}
42
- hint : {
43
+
44
+ . hint = {
43
45
color : " c, 1c, 2c, 3c, 4c"
44
46
alpha : " alpha, 1a, 2a, 3a, 4a"
45
47
rotation : " frz, frx, fry"
@@ -50,102 +52,48 @@ collectTags = (sub, sel) ->
50
52
inline_tags : " Remove from inline tags only"
51
53
transform : " Remove from transform only"
52
54
invert : " Remove all except selected"
53
- }
55
+ }
56
+
57
+ . tagTypes. invert = true
58
+
59
+ for tag in * collection. tagList
60
+ with collection. removeGroup
61
+ if tag\ match " color"
62
+ . color = true
63
+ elseif tag\ match " alpha"
64
+ . alpha = true
65
+ elseif tag\ match " angle"
66
+ . rotation = true
67
+ . perspective = true
68
+ elseif tag\ match " shear" or tag == " origin"
69
+ . perspective = true
70
+ elseif tag\ match " scale" or tag == " fontsize"
71
+ . scale = true
72
+
73
+ collection
54
74
55
- lines = LineCollection sub, sel
56
- return if # lines. lines == 0
57
- lines\ runCallback ( lines, line, i) ->
58
- aegisub. cancel! if aegisub. progress. is_cancelled!
59
- aegisub. progress. task " Collecting tags from %d of %d lines..." \ format i, # lines. lines if i% 10 == 0
60
- aegisub. progress. set 100 * i/# lines. lines
61
- data = ASS \ parse line
62
- tagSectionCount = data\ getSectionCount ASS . Section . Tag
63
- collection. removeGroup. inline_except_last = tagSectionCount > 2 if not collection. removeGroup. inline_except_last
64
- collection. topRow. inline_tags = tagSectionCount > 1 if not collection. topRow. inline_tags
65
-
66
- -- Determine if there is a tag at the start of the line
67
- -- If we reach a Text section or Drawing section before we reach a Tag section, then there is no start tag block.
68
- for j = 1 , # data. sections
69
- if data. sections[ j] . instanceOf[ ASS . Section . Tag ]
70
- collection. topRow. start_tag = true
71
- collection. startTagIndex = j
72
- break
73
- elseif data. sections[ j] . instanceOf[ ASS . Section . Text ] or data. sections[ j] . instanceOf[ ASS . Section . Drawing ]
74
- break
75
-
76
- -- Collect all the tags in the line
77
-
78
- -- - Determines which group a tag belongs to
79
- -- -@param tagName string name of tag as ASSFoundation understands it
80
- tagSorter = ( tagName) ->
81
- with collection. removeGroup
82
- if tagName\ match " color"
83
- . color = true
84
- elseif tagName\ match " alpha"
85
- . alpha = true
86
- elseif tagName\ match " angle"
87
- . rotation = true
88
- . perspective = true
89
- elseif tagName\ match " shear" or tagName == " origin"
90
- . perspective = true
91
- elseif tagName\ match " scale" or tagName == " fontsize"
92
- . scale = true
93
-
94
- for tag in * data\ getTags!
95
- tagName = tag. __tag. name
96
- table.insert collection. tagList, tagName
97
- tagSorter tagName
98
-
99
- if tag. class == ASS . Tag . Transform
100
- collection. topRow. transforms = true
101
- for transformTag in * tag. tags\ getTags!
102
- table.insert collection. tagList, transformTag. __tag. name
103
- tagSorter transformTag. __tag. name
104
-
105
- -- No tags could be found in the selected lines
106
- if # collection. tagList == 0
107
- aegisub. dialog. display { { class : " label" , label : " No tags found in the selected lines." } } , { " Close" } , { cancel : " Close" }
108
- aegisub. cancel!
109
-
110
- -- Insert a button for removing transform if transform tag exists in the line
111
- -- table.insert collection.buttons, 3, "Transform" if collection.topRow.transforms
112
-
113
- -- Deduplicate the taglist
114
- collection. tagList = list. uniq collection. tagList
115
-
116
- -- Sort the taglist
117
- collection. tagList = [ tag for tag in * ( list. join( ASS . tagSortOrder, { " transform" } )) when list. find collection. tagList, ( value) -> value == tag ]
118
- return collection
119
-
120
-
121
- -- - Dynamically creates a gui depending on the options set in collection table
122
- -- -@param collection table collection table
123
- -- -@return table dialog table
124
- -- -@return table collection updated
125
75
createGui = ( collection) ->
76
+
126
77
y, dialog = 0 , {}
127
- -- Left portion of the GUI
128
78
for key, value in pairs collection. removeGroup
129
79
continue unless value
130
- table.insert collection. buttons, 1 , " Remove Group" if y == 0
131
80
label = ( " Remove all #{key}" ) \ gsub " _" , " "
132
- dialog[ # dialog+ 1 ] = { x : 0 , y : y, class : " checkbox" , label : label, name : key, hint : collection. hint[ key]}
81
+ dialog[ # dialog+ 1 ] = { x : 0 , y : y, class : " checkbox" , label : label, name : " group_ " .. key, hint : collection. hint[ key]}
133
82
y += 1
134
83
135
- -- Right portion of the GUI
136
84
startX = 0
137
- startX = 1 if collection. buttons[ 1 ] == " Remove Group"
85
+ if # dialog > 0
86
+ table.insert collection. buttons, 1 , " Remove Group"
87
+ startX = 1
138
88
x = startX
139
89
140
- for key in * [ item for item in * { " start_tag" , " inline_tags" , " transforms" , " invert" } when collection. topRow [ item]] -- Manually sorting since lua doesn't loop through table in order
90
+ for key in * [ item for item in * { " start_tag" , " inline_tags" , " transforms" , " invert" } when collection. tagTypes [ item]] -- Manually sorting since lua doesn't loop through table in order
141
91
label = key\ gsub( " ^%l" , string.upper ) \ gsub( " _%l" , string.upper ) \ gsub( " _" , " " )
142
92
dialog[ # dialog+ 1 ] = { x : x, y : 0 , class : " checkbox" , label : label, name : key, hint : collection. hint[ key]}
143
93
x += 1
144
94
145
- -- Determine the number of columns in gui
146
95
column = math.max math.ceil ( math.sqrt # collection. tagList) , x
147
96
148
- -- Dynamically create gui
149
97
count = 0
150
98
for y = 1 , column
151
99
for x = startX, column + startX - 1
@@ -154,49 +102,29 @@ createGui = (collection) ->
154
102
label = ( ASS . toFriendlyName[ collection. tagList[ count]] ) \ gsub( " \\ 1c%s+" , " " ) \ gsub( " \\ " , " " )
155
103
dialog[ # dialog+ 1 ] = { x : x, y : y, class : " checkbox" , label : label, name : collection. tagList[ count]}
156
104
157
- return dialog, collection
158
-
159
-
160
- -- - Removes tags in a line
161
- -- -@param data table line content object from ASSFoundation
162
- -- -@param tags table list of tags that must be removed from the line
163
- -- -@param start integer index of tag block from which to start removing tags
164
- -- -@param end_ integer index of tag block upto which to remove tags
165
- -- -@param transforms table ASSFoundation transform tag object
166
- -- -@param transformOnly boolean should tags only be removed inside transform
167
- -- -@return table data updated line content object
168
- removeTags = ( data, tags, start, end_, transforms, transformOnly) ->
169
- data\ removeTags tags, start, end_ unless transformOnly
170
- if # transforms > 0
171
- for _, tr in ipairs transforms
172
- toRemove = {}
173
- for index, tag in ipairs tr. tags. tags
174
- for i in * tags
175
- table.insert toRemove, index if tag. __tag. name == i and i != " transform"
176
- list. removeIndices tr. tags. tags, toRemove
177
- data
178
-
179
-
180
- -- - Main processing function that gathers and executes all other functions
181
- -- -@param sub table subtitle object
182
- -- -@param sel table selected lines
183
- main = ( sub, sel) ->
184
- collection = collectTags sub, sel
185
- dialog, collection = createGui collection
186
105
btn, res = aegisub. dialog. display dialog, collection. buttons, { " cancel" : " Cancel" }
187
106
aegisub. cancel! unless btn
188
107
108
+ btn, res
109
+
110
+ main = ( sub, sel) ->
111
+
189
112
lines = LineCollection sub, sel
190
113
return if # lines. lines == 0
114
+
115
+ collection = collectTags lines
116
+
117
+ btn, res = createGui collection
118
+
191
119
lines\ runCallback ( lines, line, i) ->
192
- aegisub. cancel! if aegisub. progress. is_cancelled!
193
- aegisub. progress. task " Removing tags from %d of %d lines..." \ format i, # lines. lines if i% 10 == 0
194
- aegisub. progress. set 100 * i/# lines. lines
120
+
121
+ AssfPlus . _util. checkCancellation!
122
+ AssfPlus . _util. progress " Removing tags" , i, # lines. lines
123
+
195
124
data = ASS \ parse line
196
125
tagSectionCount = data\ getSectionCount ASS . Section . Tag
197
126
transforms = data\ getTags " transform"
198
127
switch btn
199
- -- when "Transform" then removeTransformSection sub, sel
200
128
201
129
when " Remove All"
202
130
if res. start_tag
@@ -207,34 +135,42 @@ main = (sub, sel) ->
207
135
data\ stripTags!
208
136
209
137
when " Remove Group"
210
- if res[ " inline_except_last" ]
211
- data\ removeTags _, ( collection. startTagIndex and collection. startTagIndex + 1 or 1 ) , tagSectionCount - 1
138
+
139
+ if res[ " group_inline_except_last" ]
140
+ data\ removeTags _, ( collection. tagTypes. start_tag and 2 or 1 ) , tagSectionCount - 1
141
+
212
142
else
213
143
start, end_, tagsToDelete = 1 , tagSectionCount, {}
214
144
end_ = 1 if res. start_tag
215
145
start = 2 if res. inline_tags
216
146
for key, value in pairs collection. removeGroupTags
217
- continue unless res[ key]
147
+ continue unless res[ " group_ " .. key]
218
148
tagsToDelete = list. join tagsToDelete, value
219
149
tagsToDelete = list. diff ASS . tagSortOrder, tagsToDelete if res. invert
220
- data = removeTags( data , tagsToDelete, start, end_, transforms )
150
+ data\ removeTags tagsToDelete, start, end_
221
151
222
152
when " Remove Tags"
153
+
223
154
local tagsToDelete
224
155
if res. invert
225
156
tagsToDelete = [ tag for tag in * collection. tagList when not res[ tag]]
226
157
else
227
158
tagsToDelete = [ tag for tag in * collection. tagList when res[ tag]]
159
+
228
160
if res. start_tag
229
- data = removeTags( data, tagsToDelete, 1 , 1 , transforms)
161
+ data\ removeTags tagsToDelete, 1 , 1
162
+
230
163
elseif res. inline_tags
231
- data\ removeTags( tagsToDelete, 2 , tagSectionCount)
164
+ data\ removeTags tagsToDelete, 2 , tagSectionCount
165
+
232
166
elseif res. transforms
233
- data = removeTags( data, tagsToDelete, 1 , 1 , transforms, true )
167
+ for tr in * transforms
168
+ tr. tags\ removeTags tagsToDelete
169
+
234
170
else
235
- data = removeTags( data , tagsToDelete, _ , _ , transforms )
236
- data \ cleanTags 2
171
+ data\ removeTags tagsToDelete
172
+
237
173
data\ commit!
238
174
lines\ replaceLines!
239
175
240
- depctrl\ registerMacro main
176
+ depctrl\ registerMacro main
0 commit comments