forked from unanimated/luaegisub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchange_capitalization.lua
182 lines (166 loc) · 6.52 KB
/
change_capitalization.lua
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
-- Capitalize text or make it lowercase / uppercase. Select lines, run the script, choose from the 5 options.
script_name="Change capitalization"
script_description="Capitalizes text or makes it lowercase or uppercase"
script_author="unanimated"
script_version="2.1"
-- Unicode support: this is used for capitalisation of non-standard characters. Add more if your language requires it.
unilow={"ä","ö","ü","ë","å","ø","æ","á","é","í","ó","ú","ý","à","è","ì","ò","ù","ç","ï","â","ê","î","ô","û","č","ď","ě","ň","ř","š","ť","ž","ů","ñ","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}
unihigh={"Ä","Ö","Ü","Ë","Å","Ø","Æ","Á","É","Í","Ó","Ú","Ý","À","È","Ì","Ò","Ù","Ç","Ï","Â","Ê","Î","Ô","Û","Č","Ď","Ě","Ň","Ř","Š","Ť","Ž","Ů","Ñ","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}
function lowercase(subs, sel)
for x, i in ipairs(sel) do
line=subs[i]
text=line.text
text=text
:gsub("^","}")
:gsub("\\n","small_break")
:gsub("\\N","large_break")
:gsub("\\h","hard_space")
:gsub("}([^{]*)",function(l) return "}"..l:lower() end)
for u=1,#unilow do
text=text:gsub(unihigh[u],unilow[u])
end
text=text
:gsub("small_break","\\n")
:gsub("large_break","\\N")
:gsub("hard_space","\\h")
:gsub("^}","")
line.text=text
subs[i]=line
end
end
function uppercase(subs, sel)
for x, i in ipairs(sel) do
line=subs[i]
text=line.text
text=text
:gsub("^","}")
:gsub("\\n","SMALL_BREAK")
:gsub("\\N","LARGE_BREAK")
:gsub("\\h","HARD_SPACE")
:gsub("}([^{]*)", function (u) return "}"..u:upper() end)
for u=1,#unilow do
text=text:gsub(unilow[u],unihigh[u])
end
text=text
:gsub("SMALL_BREAK","\\n")
:gsub("LARGE_BREAK","\\N")
:gsub("HARD_SPACE","\\h")
:gsub("^}","")
line.text=text
subs[i]=line
end
end
function capitalines(subs, sel)
for x, i in ipairs(sel) do
line=subs[i]
text=line.text
text=text
:gsub("^([^{])","{}%1")
:gsub("^({[^}]-}['\"]?)(%l)([^{]-)", function (e,f,g) return e..f:upper()..g end)
:gsub(" i([ ',])"," I%1")
:gsub("\\Ni([ ',])","\\NI%1")
for u=1,#unilow do
text=text:gsub("^({[^}]-}['\"]?)"..unilow[u],"%1"..unihigh[u])
end
text=text
:gsub("^{}","")
line.text=text
subs[i]=line
end
end
function sentences(subs, sel)
for x, i in ipairs(sel) do
line=subs[i]
text=line.text
text=text
:gsub("^([^{])","{}%1")
:gsub("^({[^}]-}['\"]?)(%l)([^{]-)", function (e,f,g) return e..f:upper()..g end)
:gsub("([%.?!]%s)(%l)", function (k,l) return k..l:upper() end)
:gsub("([%.?!]%s\\N)(%l)", function (k,l) return k..l:upper() end)
:gsub(" i([ ',])"," I%1")
:gsub("\\Ni([ ',])","\\NI%1")
for u=1,#unilow do
text=text
:gsub("^({[^}]-}['\"]?)"..unilow[u],"%1"..unihigh[u])
:gsub("([%.?!]%s)"..unilow[u],"%1"..unihigh[u])
:gsub("([%.?!]%s?\\N)"..unilow[u],"%1"..unihigh[u])
end
text=text
:gsub("^{}","")
line.text=text
subs[i]=line
end
end
word={"The","A","An","At","As","On","Of","Or","For","Nor","With","Without","Within","To","Into","Onto","Unto","And","But","In","Inside","By","Till","From","Over","Above","About","Around","After","Against","Along","Below","Beneath","Beside","Between","Beyond","Under","Until","Via"}
vord={"the","a","an","at","as","on","of","or","for","nor","with","without","within","to","into","onto","unto","and","but","in","inside","by","till","from","over","above","about","around","after","against","along","below","beneath","beside","between","beyond","under","until","via"}
function capitalize(subs, sel)
for x, i in ipairs(sel) do
line=subs[i]
text=line.text
text=text
:gsub("^","}")
:gsub("\\n","*small_break*")
:gsub("\\N","*large_break*")
:gsub("\\h","*hard_space*")
:gsub("([%s\"}%(%-%=]['\"]?)(%l)(%l-)",function(e,f,g) return e..f:upper()..g end) -- after: space " } ( - =
:gsub("(break%*)(%l)(%l-)",function(h,j,k) return h..j:upper()..k end) -- after \N
for u=1,#unilow do
text=text:gsub("([%s\"}%(%-%=]['\"]?)"..unilow[u].."(%l-)","%1"..unihigh[u].."%2")
end
text=text:gsub("^}","")
for r=1,#word do
w=word[r] v=vord[r]
text=text:gsub("([^%.%:])%s"..w.."%s","%1 "..v.." ")
text=text:gsub("([^%.%:])%s({[^}]-})"..w.."%s","%1 %2"..v.." ")
end
-- other stuff
text=text
:gsub("$","#")
:gsub("(%s?)([IVXLCDM])([ivxlcdm]+)([%s%p#])",function (s,r,m,e) return s..r..m:upper()..e end) -- Roman numbers
:gsub("LID","Lid")
:gsub("DIM","Dim")
:gsub("Ok([%s%p#])","OK%1")
:gsub("%-San([%s%p#])","-san%1")
:gsub("%-Kun([%s%p#])","-kun%1")
:gsub("%-Chan([%s%p#])","-chan%1")
:gsub("%-Sama([%s%p#])","-sama%1")
:gsub("%-Dono([%s%p#])","-dono%1")
:gsub("#$","")
:gsub("%*small_break%*","\\n")
:gsub("%*large_break%*","\\N")
:gsub("%*hard_space%*","\\h")
line.text=text
subs[i]=line
end
end
function capital(subs, sel)
dialog_config=
{
{x=1,y=0,width=1,height=1,class="label",
label="Words - Capitalize All Words Like in Titles",
},
{x=1,y=1,width=1,height=1,class="label",
label=" Lines - Capitalize first word in selected lines",
},
{x=1,y=2,width=1,height=1,class="label",
label=" Sentences - Capitalize first word in each sentence",
},
{x=1,y=3,width=1,height=1,class="label",
label=" Lowercase - make text in selected lines lowercase",
},
{x=1,y=4,width=1,height=1,class="label",
label=" Uppercase - MAKE TEXT IN SELECTED LINES UPPERCASE",
},
}
pressed, results=aegisub.dialog.display(dialog_config,
{"Words","Lines","Sentences","lowercase","UPPERCASE","Cancel"},{close='Cancel'})
if pressed=="Cancel" then aegisub.cancel() end
if pressed=="Words" then lowercase(subs, sel) capitalize(subs, sel) end
if pressed=="Lines" then capitalines(subs, sel) end
if pressed=="Sentences" then sentences(subs, sel) end
if pressed=="lowercase" then lowercase(subs, sel) end
if pressed=="UPPERCASE" then uppercase(subs, sel) end
aegisub.set_undo_point(script_name)
return sel
end
aegisub.register_macro(script_name, script_description, capital)