Updated cleantags script from ai-chan.

Originally committed to SVN as r2748.
This commit is contained in:
Niels Martin Hansen 2009-02-28 17:52:06 +00:00
parent ae9a23d848
commit 5e476ca821
2 changed files with 29 additions and 27 deletions

View File

@ -8,7 +8,7 @@ writing) @ http://www.malakith.net/aegiwiki
mechanism works. Even so, I am not resposible if it damages your subtitles permanently, so please mechanism works. Even so, I am not resposible if it damages your subtitles permanently, so please
back up your subtitle file before applying the cleaning up back up your subtitle file before applying the cleaning up
Copyright (c) 2007 ai-chan (Aegisub's forum member and registered nick holder of Rizon irc network) Copyright (c) 2007-2009 Muhammad Lukman Nasaruddin (aka ai-chan, Aegisub's forum member)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction, associated documentation files (the "Software"), to deal in the Software without restriction,
@ -28,9 +28,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
script_name = "Clean Tags" script_name = "Clean Tags"
script_description = "Clean subtitle lines by re-arranging ASS tags and override blocks within the lines" script_description = "Clean subtitle lines by re-arranging ASS tags and override blocks within the lines"
script_author = "ai-chan" script_author = "Muhammad Lukman Nasaruddin (ai-chan)"
script_version = "1.150" script_version = "1.20"
script_modified = "12 September 2007" script_modified = "25 February 2009"
include("cleantags.lua") include("cleantags.lua")
@ -38,11 +38,11 @@ function cleantags_subs(subtitles)
local linescleaned = 0 local linescleaned = 0
for i = 1, #subtitles do for i = 1, #subtitles do
aegisub.progress.set(i * 100 / #subtitles) aegisub.progress.set(i * 100 / #subtitles)
if subtitles[i].class == "dialogue" and subtitles[i].text ~= "" then if subtitles[i].class == "dialogue" and not subtitles[i].comment and subtitles[i].text ~= "" then
ntext = cleantags(subtitles[i].text) ntext = cleantags(subtitles[i].text)
local nline = subtitles[i] local nline = subtitles[i]
nline.text = ntext nline.text = ntext
subtitles[i] = nline -- I don't understand why we need these steps to incorporate new text subtitles[i] = nline
linescleaned = linescleaned + 1 linescleaned = linescleaned + 1
aegisub.progress.task(linescleaned .. " lines cleaned") aegisub.progress.task(linescleaned .. " lines cleaned")
end end

View File

@ -1,12 +1,11 @@
--[[ --[[
"Clean Tags" -- An Auto4 LUA script for cleaning up ASS subtitle lines of badly-formed override "Clean Tags" -- An Auto4 LUA script for cleaning up ASS subtitle lines of badly-formed override
blocks and redundant/duplicate tags blocks and redundant/duplicate tags
* Designed to work for Aegisub 2.0 and above (only pre-release version was available at the time of * Designed to work for Aegisub 2.0 and above
writing) @ http://www.malakith.net/aegiwiki
* include()'ed this file from any auto4 script to use the cleantags() function below * include()'ed this file from any auto4 script to use the cleantags() function below
* Might change from time to time so look out for cleantags_version below * Might change from time to time so look out for cleantags_version below
Copyright (c) 2007 ai-chan (Aegisub's forum member and registered nick holder of Rizon irc network) Copyright (c) 2007-2009 Muhammad Lukman Nasaruddin (aka ai-chan, Aegisub's forum member)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction, associated documentation files (the "Software"), to deal in the Software without restriction,
@ -24,8 +23,8 @@ OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]] ]]
cleantags_version = "1.200" cleantags_version = "1.300"
cleantags_modified = "13 September 2007" cleantags_modified = "27 February 2009"
ktag = "\\[kK][fo]?%d+" ktag = "\\[kK][fo]?%d+"
@ -34,13 +33,13 @@ Takes: text
Returns: cleaned up text Returns: cleaned up text
]] ]]
function cleantags(text) function cleantags(text)
--[[ Combine adjacentext override override blocks into one ]] --[[ Combine adjacent override override blocks into one ]]
function combineadjacentnotks(block1, block2) function combineadjacentnotks(block1, block2)
if string.find(block1, ktag) and string.find(block2, ktag) then if string.find(block1, ktag) and string.find(block2, ktag) then
-- if both adjacentext override blocks have \k , letext them be -- if both adjacent override blocks have \k , let them be
return "{" .. block1 .. "}" .. string.char(1) .. "{" .. block2 .. "}" -- char(1) prevents infinite loop return "{" .. block1 .. "}" .. string.char(1) .. "{" .. block2 .. "}" -- char(1) prevents infinite loop
else else
-- either one or both override blocks don'text have \k , so combine them into one override block -- either one or both override blocks don't have \k , so combine them into one override block
return "{" .. block1 .. block2 .. "}" return "{" .. block1 .. block2 .. "}"
end end
end end
@ -50,17 +49,17 @@ function cleantags(text)
until replaced == 0 until replaced == 0
text = string.gsub(text, string.char(1), "") -- removes all char(1) we inserted text = string.gsub(text, string.char(1), "") -- removes all char(1) we inserted
--[[ Move firstext \k tag in override blocks to the frontext ]] --[[ Move first \k tag in override blocks to the front ]]
text = string.gsub(text, "{([^{}]-)(" .. ktag .. ")(.-)}", "{%2%1%3}") text = string.gsub(text, "{([^{}]-)(" .. ktag .. ")(.-)}", "{%2%1%3}")
--[[ For some reasons if one override block has more than one \k tag, --[[ For some reasons if one override block has more than one \k tag,
push those to behind the firstext \k tag (which has been pushed to frontext already) ]] push those to behind the first \k tag (which has been pushed to front already) ]]
repeat repeat
if aegisub.progress.is_cancelled() then return end if aegisub.progress.is_cancelled() then return end
text, replaced = string.gsub(text, "{([^{}]-)(" .. ktag .. ")(\\[^kK][^}]-)(" .. ktag .. ")(.-)}", "{%1%2%4%3%5}") text, replaced = string.gsub(text, "{([^{}]-)(" .. ktag .. ")(\\[^kK][^}]-)(" .. ktag .. ")(.-)}", "{%1%2%4%3%5}")
until replaced == 0 until replaced == 0
--[[ Move to the frontext all tags thatext affectext the whole line (i.e. notext affected by their positions in the line) ]] --[[ Move to the front all tags that affect the whole line (i.e. not affected by their positions in the line) ]]
local linetags = "" local linetags = ""
function first(pattern) function first(pattern)
local p_s, _, p_tag = string.find(text, pattern) local p_s, _, p_tag = string.find(text, pattern)
@ -84,26 +83,29 @@ function cleantags(text)
first("(\\an?%d+)") first("(\\an?%d+)")
-- \org -- \org
first("(\\org%([^,%)]*,[^,%)]*%))") first("(\\org%([^,%)]*,[^,%)]*%))")
-- \move and \pos (the firstext one wins) -- \move and \pos (the first one wins)
firstoftwo("(\\move%([^,%)]*,[^,%)]*,[^,%)]*,[^,%)]*%))", "(\\pos%([^,%)]*,[^,%)]*%))") firstoftwo("(\\move%([^,%)]*,[^,%)]*,[^,%)]*,[^,%)]*%))", "(\\pos%([^,%)]*,[^,%)]*%))")
-- \fade and \fad (the firstext one wins) -- \fade and \fad (the first one wins)
firstoftwo("(\\fade%([^,%)]*,[^,%)]*,[^,%)]*,[^,%)]*,[^,%)]*,[^,%)]*,[^,%)]*%))", "(\\fad%([^,%)]*,[^,%)]*%))") firstoftwo("(\\fade%([^,%)]*,[^,%)]*,[^,%)]*,[^,%)]*,[^,%)]*,[^,%)]*,[^,%)]*%))", "(\\fad%([^,%)]*,[^,%)]*%))")
-- integrate -- integrate
if string.len(linetags) > 0 then if string.len(linetags) > 0 then
if string.sub(text, 1, 1) == "{" then if string.sub(text, 1, 1) == "{" then
text = "{" .. linetags .. string.sub(text, 2) text = "{" .. linetags .. string.sub(text, 2)
else else
text = "{" .. linetags .. "}" .. t text = "{" .. linetags .. "}" .. text
end end
end end
--[[ Remove any spaces within parenteses within override blocks ]] --[[ Remove any spaces within parenteses within override blocks except for \clip tags ]]
--[[ (removed in v 1.2) local comb = function(a,b,c,d,e)
repeat if c ~= "\\clip" or d:sub(-1):find("[,%({]") or e:sub(1,1):find("[,%)}]") then return a..b..d..e
if aegisub.progress.is_cancelled() then return end else return a..b..d..string.char(2)..e end
text, replaced2 = string.gsub(text, "({[^}]*%([^%s%)}]*,)%s+(.*%)[^}]*})", "%1%2") end
until replaced2 == 0 ]] repeat
text, replaced2 = string.gsub(text, "({[^}\\]*)([^}%s]*(\\[^%(}\\%s]*))%s*(%([^%s%)}]*)%s+([^}]*)", comb)
until replaced2 == 0
text, _ = text:gsub(string.char(2)," ")
--[[ Remove all empty override blocks ==> {} ]] --[[ Remove all empty override blocks ==> {} ]]
text = string.gsub(text, "{%s*}", "") text = string.gsub(text, "{%s*}", "")