aboutsummaryrefslogtreecommitdiffstats
path: root/restyler.lua
blob: 38aa031dadd4211f3bf4ac10baf8a2befe616eba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
script_name="Restyler"
script_description="become a fansubber with a click of a button"
script_author = "garret"
script_version = "2.1.1"
script_namespace = "garret.restyler"

local haveDepCtrl, DependencyControl, depctrl = pcall(require, "l0.DependencyControl")
if haveDepCtrl then
	depctrl = DependencyControl {
		--feed="TODO",
	}
end

include("karaskel.lua")
include("cleantags.lua")

-- local config = simpleconf.get_config(aegisub.decode_path(config_dir.."/"..script_namespace..".conf"), {new_style = "Default"})

-- TODO: detect pre-existing inline tags

function add_tags(txt, italic, align) -- everything except txt is boolean. nil = don't change, !nil = change to this value

--[[not quite happy with this, it overwrites the alignment - ie line is "{\an4} blah blah" and style is an8, it just changes it to an8
realisticly this _probably_ won't be a problem, but still would like to try and stop it at some point to be safe
italics is fine, it just does {\i1\i0}, which is jank and bad but works fine so i won't worry about it too much]]
	if italic == true then
		txt="{\\i1}"..txt
	elseif italic == false then
		txt="{\\i0}"..txt
	end
	if align ~= nil then
		txt="{\\an"..align.."}"..txt
	end
	txt = cleantags(txt)
	return txt
end

function get_new(old, new)
	local i = nil
	if old ~= new then
		i = old
	end
	return i
end

function main(sub, sel)
	local _, styles = karaskel.collect_head(sub, false)
	local new_style_name = "Default"
	local new_style = styles[new_style_name]

	if new_style == nil then
		local style_names = {}
		for k, v in ipairs(styles) do
			table.insert(style_names, v.name)
		end

		local button_ok, result = aegisub.dialog.display({
			{x=0, y=0,class="label", label="You don't have a \""..new_style_name.."\" style. Select a style to change the selected lines to:"},
			{x=0, y=1, class="dropdown", items=style_names, value=style_names[1], name="style"},
		})
		if not button_ok then aegisub.cancel() end

		new_style_name = result.style
		new_style = styles[new_style_name]
	end


	for h, i in ipairs(sel) do
		-- TODO: automatically exclude styles (also configurable)
		local line = sub[i]
		local old_style = styles[line.style]
		local italic = get_new(old_style.italic, new_style.italic)
		local align = get_new(old_style.align, new_style.align)
		line.style = new_style_name
		line.text = add_tags(line.text, italic, align)
		sub[i] = line
	end
	aegisub.set_undo_point(script_name)
end

if haveDepCtrl then
	depctrl:registerMacro(main)
else
	aegisub.register_macro(script_name, script_description, main)
end