2
"Clean Tags" -- An Auto4 LUA script for cleaning up ASS subtitle lines of badly-formed override
3
blocks and redundant/duplicate tags
4
* Designed to work for Aegisub 2.0 and above
5
* include()'ed this file from any auto4 script to use the cleantags() function below
6
* Might change from time to time so look out for cleantags_version below
8
Copyright (c) 2007-2009 Muhammad Lukman Nasaruddin (aka ai-chan, Aegisub's forum member)
10
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
11
associated documentation files (the "Software"), to deal in the Software without restriction,
12
including without limitation the rights to use, copy, modify, merge, publish, distribute,
13
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
14
furnished to do so, subject to the following conditions:
16
The above copyright notice and this permission notice shall be included in all copies or substantial
17
portions of the Software.
19
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
20
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
22
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
cleantags_version = "1.301"
27
cleantags_modified = "13 November 2009"
29
ktag = "\\[kK][fo]?%d+"
31
--[[ The main function that performs the cleaning up
33
Returns: cleaned up text
35
function cleantags(text)
36
--[[ Combine adjacent override override blocks into one ]]
37
function combineadjacentnotks(block1, block2)
38
if string.find(block1, ktag) and string.find(block2, ktag) then
39
-- if both adjacent override blocks have \k , let them be
40
return "{" .. block1 .. "}" .. string.char(1) .. "{" .. block2 .. "}" -- char(1) prevents infinite loop
42
-- either one or both override blocks don't have \k , so combine them into one override block
43
return "{" .. block1 .. block2 .. "}"
47
if aegisub.progress.is_cancelled() then return end
48
text, replaced = string.gsub(text,"{(.-)}{(.-)}", combineadjacentnotks)
50
text = string.gsub(text, string.char(1), "") -- removes all char(1) we inserted
52
--[[ Move first \k tag in override blocks to the front ]]
53
text = string.gsub(text, "{([^{}]-)(" .. ktag .. ")(.-)}", "{%2%1%3}")
55
--[[ For some reasons if one override block has more than one \k tag,
56
push those to behind the first \k tag (which has been pushed to front already) ]]
58
if aegisub.progress.is_cancelled() then return end
59
text, replaced = string.gsub(text, "{([^{}]-)(" .. ktag .. ")(\\[^kK][^}]-)(" .. ktag .. ")(.-)}", "{%1%2%4%3%5}")
62
--[[ Move to the front all tags that affect the whole line (i.e. not affected by their positions in the line) ]]
64
function first(pattern)
65
local p_s, _, p_tag = string.find(text, pattern)
67
text = string.gsub(text, pattern, "")
68
linetags = linetags .. p_tag
71
function firstoftwo(pattern1, pattern2)
72
local p1_s, _, p1_tag = string.find(text, pattern1)
73
local p2_s, _, p2_tag = string.find(text, pattern2)
74
text = string.gsub(text, pattern1, "")
75
text = string.gsub(text, pattern2, "")
76
if p1_s and (not p2_s or p1_s < p2_s) then
77
linetags = linetags .. p1_tag
79
linetags = linetags .. p2_tag
85
first("(\\org%([^,%)]*,[^,%)]*%))")
86
-- \move and \pos (the first one wins)
87
firstoftwo("(\\move%([^,%)]*,[^,%)]*,[^,%)]*,[^,%)]*%))", "(\\pos%([^,%)]*,[^,%)]*%))")
88
-- \fade and \fad (the first one wins)
89
firstoftwo("(\\fade%([^,%)]*,[^,%)]*,[^,%)]*,[^,%)]*,[^,%)]*,[^,%)]*,[^,%)]*%))", "(\\fad%([^,%)]*,[^,%)]*%))")
91
if string.len(linetags) > 0 then
92
if string.sub(text, 1, 1) == "{" then
93
text = "{" .. linetags .. string.sub(text, 2)
95
text = "{" .. linetags .. "}" .. text
99
--[[ Remove any spaces within parenteses within override blocks except for \clip tags ]]
100
local comb = function(a,b,c,d,e)
101
if c ~= "\\i?clip" or d:sub(-1):find("[,%({]") or e:sub(1,1):find("[,%)}]") then return a..b..d..e
102
else return a..b..d..string.char(2)..e end
105
text, replaced2 = string.gsub(text, "({[^}\\]*)([^}%s]*(\\[^%(}\\%s]*))%s*(%([^%s%)}]*)%s+([^}]*)", comb)
107
text, _ = text:gsub(string.char(2)," ")
109
--[[ Remove all empty override blocks ==> {} ]]
110
text = string.gsub(text, "{%s*}", "")
112
--[[ Finally, return the cleaned up text ]]