Skip to content

Commit d63cee9

Browse files
committed
[RemoveTags] Minor rewrite
changed: defer tag collection to assfplus fixed: group name and tag name conflict fixed: inline tags except last did not work correctly when there was no start tags changed: tags are removed inside transform only when it is explicitly specified fixed: finding of correct start tag index fixed: a bunch of niche errors that people will never encounter
1 parent c30fb62 commit d63cee9

File tree

2 files changed

+374
-138
lines changed

2 files changed

+374
-138
lines changed

macros/phos.RemoveTags.moon

+74-138
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
export script_name = "Remove Tags"
22
export script_description = "Dynamically remove tags based on selection"
3-
export script_version = "1.0.1"
3+
export script_version = "1.0.2"
44
export script_author = "PhosCity"
55
export script_namespace = "phos.RemoveTags"
66

7+
78
DependencyControl = require "l0.DependencyControl"
89
depctrl = DependencyControl{
910
feed: "https://raw.githubusercontent.com/PhosCity/Aegisub-Scripts/main/DependencyControl.json",
@@ -14,32 +15,33 @@ depctrl = DependencyControl{
1415
feed: "https: //raw.githubusercontent.com/TypesettingTools/ASSFoundation/master/DependencyControl.json"},
1516
{"l0.Functional", version: "0.6.0", url: "https://github.com/TypesettingTools/Functional",
1617
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"},
1720
}
1821
}
19-
LineCollection, ASS, Functional = depctrl\requireModules!
20-
logger = depctrl\getLogger!
22+
LineCollection, ASS, Functional, AssfPlus = depctrl\requireModules!
2123
{:list} = Functional
2224

2325

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 = {
3637
color: {"color1", "color2", "color3", "color4"}
3738
alpha: {"alpha", "alpha1", "alpha2", "alpha3", "alpha4"}
3839
rotation: {"angle", "angle_x", "angle_y"}
3940
scale: {"fontsize", "scale_x", "scale_y"}
4041
perspective: {"angle", "angle_x", "angle_y", "shear_x", "shear_y"}
4142
}
42-
hint: {
43+
44+
.hint = {
4345
color: "c, 1c, 2c, 3c, 4c"
4446
alpha: "alpha, 1a, 2a, 3a, 4a"
4547
rotation: "frz, frx, fry"
@@ -50,102 +52,48 @@ collectTags = (sub, sel) ->
5052
inline_tags: "Remove from inline tags only"
5153
transform: "Remove from transform only"
5254
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
5474

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
12575
createGui = (collection) ->
76+
12677
y, dialog = 0, {}
127-
-- Left portion of the GUI
12878
for key, value in pairs collection.removeGroup
12979
continue unless value
130-
table.insert collection.buttons, 1, "Remove Group" if y == 0
13180
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]}
13382
y += 1
13483

135-
-- Right portion of the GUI
13684
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
13888
x = startX
13989

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
14191
label = key\gsub("^%l", string.upper)\gsub("_%l", string.upper)\gsub("_", " ")
14292
dialog[#dialog+1]= {x: x, y: 0, class: "checkbox", label: label, name: key, hint: collection.hint[key]}
14393
x += 1
14494

145-
-- Determine the number of columns in gui
14695
column = math.max math.ceil(math.sqrt #collection.tagList), x
14796

148-
-- Dynamically create gui
14997
count = 0
15098
for y = 1, column
15199
for x = startX, column + startX - 1
@@ -154,49 +102,29 @@ createGui = (collection) ->
154102
label = (ASS.toFriendlyName[collection.tagList[count]])\gsub("\\1c%s+", "")\gsub("\\", "")
155103
dialog[#dialog+1] = {x: x, y: y, class: "checkbox", label: label, name: collection.tagList[count]}
156104

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
186105
btn, res = aegisub.dialog.display dialog, collection.buttons, {"cancel": "Cancel"}
187106
aegisub.cancel! unless btn
188107

108+
btn, res
109+
110+
main = (sub, sel) ->
111+
189112
lines = LineCollection sub, sel
190113
return if #lines.lines == 0
114+
115+
collection = collectTags lines
116+
117+
btn, res = createGui collection
118+
191119
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+
195124
data = ASS\parse line
196125
tagSectionCount = data\getSectionCount ASS.Section.Tag
197126
transforms = data\getTags "transform"
198127
switch btn
199-
-- when "Transform" then removeTransformSection sub, sel
200128

201129
when "Remove All"
202130
if res.start_tag
@@ -207,34 +135,42 @@ main = (sub, sel) ->
207135
data\stripTags!
208136

209137
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+
212142
else
213143
start, end_, tagsToDelete = 1, tagSectionCount, {}
214144
end_ = 1 if res.start_tag
215145
start = 2 if res.inline_tags
216146
for key, value in pairs collection.removeGroupTags
217-
continue unless res[key]
147+
continue unless res["group_" .. key]
218148
tagsToDelete = list.join tagsToDelete, value
219149
tagsToDelete = list.diff ASS.tagSortOrder, tagsToDelete if res.invert
220-
data = removeTags(data, tagsToDelete, start, end_, transforms)
150+
data\removeTags tagsToDelete, start, end_
221151

222152
when "Remove Tags"
153+
223154
local tagsToDelete
224155
if res.invert
225156
tagsToDelete = [tag for tag in *collection.tagList when not res[tag]]
226157
else
227158
tagsToDelete = [tag for tag in *collection.tagList when res[tag]]
159+
228160
if res.start_tag
229-
data = removeTags(data, tagsToDelete, 1, 1, transforms)
161+
data\removeTags tagsToDelete, 1, 1
162+
230163
elseif res.inline_tags
231-
data\removeTags(tagsToDelete, 2, tagSectionCount)
164+
data\removeTags tagsToDelete, 2, tagSectionCount
165+
232166
elseif res.transforms
233-
data = removeTags(data, tagsToDelete, 1, 1, transforms, true)
167+
for tr in *transforms
168+
tr.tags\removeTags tagsToDelete
169+
234170
else
235-
data = removeTags(data, tagsToDelete, _, _, transforms)
236-
data\cleanTags 2
171+
data\removeTags tagsToDelete
172+
237173
data\commit!
238174
lines\replaceLines!
239175

240-
depctrl\registerMacro main
176+
depctrl\registerMacro main

0 commit comments

Comments
 (0)