forked from TypesettingTools/Myaamori-Aegisub-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myaa.PasteFromPad.moon
158 lines (135 loc) · 4.81 KB
/
myaa.PasteFromPad.moon
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
export script_name = "Paste From Pad"
export script_description = "Paste text from pad over existing lines"
export script_version = "0.0.2"
export script_author = "Myaamori"
export script_namespace = "myaa.PasteFromPad"
DependencyControl = require('l0.DependencyControl') {
{
{"l0.Functional", version: "0.6.0", url: "https://github.com/TypesettingTools/Functional",
feed: "https://raw.githubusercontent.com/TypesettingTools/Functional/master/DependencyControl.json"}
}
}
F = DependencyControl\requireModules!
ConfigHandler = DependencyControl\getConfigHandler {
settings: {
separator: ":",
comment: "#",
include_blank: false,
always_separate: true
}
}
settings = ConfigHandler.c.settings
starts_with = (str, start)->
return (str\sub 1, #start) == start
paste = (subtitles, selected_lines, active_line)->
dialog = {
{
class: "textbox", x: 0, y: 0, width: 30, height: 15, name: "text"
},
{
class: "label", label: "Actor separator", x: 0, y: 15, width: 20, height: 1
},
{
class: "edit", text: settings.separator, name: "separator",
x: 20, y: 15, width: 10, height: 1
},
{
class: "label", label: "Comment starter", x: 0, y: 16, width: 20, height: 1
},
{
class: "edit", text: settings.comment, name: "comment",
x: 20, y: 16, width: 10, height: 1
},
{
class: "checkbox", label: "Include blank lines", value: settings.include_blank,
name: "include_blank", x: 0, y: 17, width: 30, height: 1
}
}
button, result = aegisub.dialog.display dialog
if not button
return nil
settings.separator = result.separator
settings.comment = result.comment
settings.include_blank = result.include_blank
ConfigHandler\write!
for text in *F.string.split result.text, "\n"
if #text == 0 and not result.include_blank
continue
comment = false
actor = ""
if #result.comment > 0 and starts_with text, result.comment
comment = true
text = text\sub 2
if not comment and #result.separator > 0 and #text > 0
if not starts_with(text, " ") and not starts_with(text, "\t")
pos = text\find result.separator, 1, true
if pos
actor = F.string.trim text\sub 1, pos - 1
text = text\sub pos + 1
text = F.string.trimLeft text
if #text == 0
comment = true
if active_line <= #subtitles
line = subtitles[active_line]
line.actor = actor
line.text = text
line.comment = comment
subtitles[active_line] = line
else
subtitles[0] = {class: "dialogue", :actor, :text, :comment,
layer: 0, start_time: 0, end_time: 0, style: "Default",
margin_l: 0, margin_r: 0, margin_t: 0, effect: ""}
active_line += 1
copy = (subtitles, selected_lines)->
dialog = {
{
class: "label", label: "Actor separator", x: 0, y: 0, width: 20, height: 1
},
{
class: "edit", text: settings.separator, name: "separator",
x: 20, y: 0, width: 10, height: 1
},
{
class: "label", label: "Comment starter", x: 0, y: 1, width: 20, height: 1
},
{
class: "edit", text: settings.comment, name: "comment",
x: 20, y: 1, width: 10, height: 1
},
{
class: "checkbox", label: "Always include separator", value: settings.always_separate,
hint: "Includes actor separator even if the actor field is empty",
name: "always_separate", x: 0, y: 2, width: 30, height: 1
}
}
button, result = aegisub.dialog.display dialog
if not button
return nil
settings.separator = result.separator
settings.comment = result.comment
settings.always_separate = result.always_separate
ConfigHandler\write!
text = {}
for i in *selected_lines
line = subtitles[i]
if line.comment
table.insert text, "#{result.comment}#{line.text}"
elseif #line.actor > 0 or result.always_separate
table.insert text, "#{line.actor}#{result.separator} #{line.text}"
else
table.insert text, line.text
aegisub.dialog.display {
{class: "textbox", x: 0, y: 0, width: 30, height: 15, value: table.concat text, "\n"}
}, {"OK"}
DependencyControl\registerMacros {
{
"Paste over from pad",
"Paste over lines starting at the active line",
paste,
},
{
"Copy to pad",
"Copy selected lines with the actor included",
copy,
}
}