Italicize script explained
-- italicizes or unitalicizes text [based on style and tags] -- supports multiple \i tags in a line by switching 0 to 1 and vice versa script_name="Italicize" script_description="Italicizes or unitalicizes text" script_author="unanimated" script_version="1.6" -- let's say for the sake of clarity that we're using this on a line with style "sign" function italicize(subs, sel) for z, i in ipairs(sel) do local l=subs[i] text=l.text -- this runs function "stylechk" that you see further below -- you pass "l.style" to the function, ie. the name of the style this line is using, in our case "sign" styleref=stylechk(subs,l.style) -- styleref is now kind of like "line", but for the style -- just like you have line.text, you have styleref.name, which is now "sign", or styleref.italic, which is true or false -- we'll assign the italic element to "si" (style italics) -- it is either true for italics, or false for regular local si=styleref.italic -- now we want to transform true/false into 1/0, creating variable "it" -- the values are 1 for non-italics and 0 for italics, because it's the switched values i want to use if si==false then it="1" else it="0" end -- this is to deal with \i without a number, which resets \i to the style value; i need it to have a number -- "it" is the opposite of the style, so "1-it" is the style value - 1-0=1; 1-1=0 -- in other words, if style is not italics, we change \i to \i0 text=text:gsub("\\i([\\}])","\\i".. 1-it.."%1") -- now i have to consider 2 options - either there's italics tag at the beginning, or not if text:match("^{[^}]*\\i%d[^}]*}") then -- if there is, then we change it, and all subsequent \i tags, to the opposite value -- i take the number after \i, and change it to 1-number, thus turning 1 to 0 and vice versa text=text:gsub("\\i(%d)", function(num) return "\\i".. 1-num end) -- this is when there's no \i tag in the first block of tags else -- the line may have italics somewehre in the middle, so we capture the value if text:match("\\i([01])") then italix=text:match("\\i([01])") end -- this may get a bit confusing. we're dealing with a line that has italics in the middle. -- so style value is "not_it" (as in opposite of "it"), value we want for the start of line is "it", -- and value we want for the next \i tag, the one we captured, is again "not_it" -- so logically that value, italix, should be "it" -- if that's the case, all \i tags get switched -- the reason i check for this is that i assume you might be an idiot and have the wrong tag -- in which case i won't change it because it will thus become right if italix==it then text=text:gsub("\\i(%d)", function(num) return "\\i".. 1-num end) end -- we've changed all the existing tags (if there were any), so now we add a tag at the beginning, with the value "it" text="{\\i"..it.."}"..text -- and join it with other tags at the beginning, if they're there text=text:gsub("{\\i(%d)}({\\[^}]*)}","%2\\i%1}") end l.text=text subs[i]=l end aegisub.set_undo_point(script_name) return sel end -- function to read the style of the current line -- it got called with styleref=stylechk(subs,l.style), therefore given 2 objects - the whole subtitle object, and a style -- here we have stylechk(subs,stylename) - i use different names here, but that doesn't matter - subs stays, l.style becomes stylename -- l.style was "sign", so inside this function, stylename is now "sign" function stylechk(subs,stylename) for i=1, #subs do -- we're going through styles, looking for the one called "sign" if subs[i].class=="style" then -- this is pretty much the same thing you do with "line" style=subs[i] -- style.name is the name of the style. with every "i" we're going through another style. -- stylename will match style.name when we get to the "sign" style if stylename==style.name then -- now we have style "sign", and we copy it to a variable "styleref" styleref=style end end end -- this is what we're returning as the result of this function, so when we used styleref=stylechk(subs,l.style) above, -- we're passing this styleref, which is subs[i] for the "sign" style, to the styleref in the main function -- so styleref is now basically this "Style: sign,Arial,40,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,0,5,10,10,10,1" return styleref end aegisub.register_macro(script_name, script_description, italicize)
-- italicizes or unitalicizes text [based on style and tags]
-- supports multiple \i tags in a line by switching 0 to 1 and vice versa
script_name="Italicize"
script_description="Italicizes or unitalicizes text"
script_author="unanimated"
script_version="1.6"
-- let's say for the sake of clarity that we're using this on a line with style "sign"
function italicize(subs, sel)
for z, i in ipairs(sel) do
local l=subs[i]
text=l.text
-- this runs function "stylechk" that you see further below
-- you pass "l.style" to the function, ie. the name of the style this line is using, in our case "sign"
styleref=stylechk(subs,l.style)
-- styleref is now kind of like "line", but for the style
-- just like you have line.text, you have styleref.name, which is now "sign", or styleref.italic, which is true or false
-- we'll assign the italic element to "si" (style italics)
-- it is either true for italics, or false for regular
local si=styleref.italic
-- now we want to transform true/false into 1/0, creating variable "it"
-- the values are 1 for non-italics and 0 for italics, because it's the switched values i want to use
if si==false then it="1" else it="0" end
-- this is to deal with \i without a number, which resets \i to the style value; i need it to have a number
-- "it" is the opposite of the style, so "1-it" is the style value - 1-0=1; 1-1=0
-- in other words, if style is not italics, we change \i to \i0
text=text:gsub("\\i([\\}])","\\i".. 1-it.."%1")
-- now i have to consider 2 options - either there's italics tag at the beginning, or not
if text:match("^{[^}]*\\i%d[^}]*}") then
-- if there is, then we change it, and all subsequent \i tags, to the opposite value
-- i take the number after \i, and change it to 1-number, thus turning 1 to 0 and vice versa
text=text:gsub("\\i(%d)", function(num) return "\\i".. 1-num end)
-- this is when there's no \i tag in the first block of tags
else
-- the line may have italics somewehre in the middle, so we capture the value
if text:match("\\i([01])") then italix=text:match("\\i([01])") end
-- this may get a bit confusing. we're dealing with a line that has italics in the middle.
-- so style value is "not_it" (as in opposite of "it"), value we want for the start of line is "it",
-- and value we want for the next \i tag, the one we captured, is again "not_it"
-- so logically that value, italix, should be "it"
-- if that's the case, all \i tags get switched
-- the reason i check for this is that i assume you might be an idiot and have the wrong tag
-- in which case i won't change it because it will thus become right
if italix==it then text=text:gsub("\\i(%d)", function(num) return "\\i".. 1-num end) end
-- we've changed all the existing tags (if there were any), so now we add a tag at the beginning, with the value "it"
text="{\\i"..it.."}"..text
-- and join it with other tags at the beginning, if they're there
text=text:gsub("{\\i(%d)}({\\[^}]*)}","%2\\i%1}")
end
l.text=text
subs[i]=l
end
aegisub.set_undo_point(script_name)
return sel
end
-- function to read the style of the current line
-- it got called with styleref=stylechk(subs,l.style), therefore given 2 objects - the whole subtitle object, and a style
-- here we have stylechk(subs,stylename) - i use different names here, but that doesn't matter - subs stays, l.style becomes stylename
-- l.style was "sign", so inside this function, stylename is now "sign"
function stylechk(subs,stylename)
for i=1, #subs do
-- we're going through styles, looking for the one called "sign"
if subs[i].class=="style" then
-- this is pretty much the same thing you do with "line"
style=subs[i]
-- style.name is the name of the style. with every "i" we're going through another style.
-- stylename will match style.name when we get to the "sign" style
if stylename==style.name then
-- now we have style "sign", and we copy it to a variable "styleref"
styleref=style
end
end
end
-- this is what we're returning as the result of this function, so when we used styleref=stylechk(subs,l.style) above,
-- we're passing this styleref, which is subs[i] for the "sign" style, to the styleref in the main function
-- so styleref is now basically this "Style: sign,Arial,40,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,0,5,10,10,10,1"
return styleref
end
aegisub.register_macro(script_name, script_description, italicize)