2
Copyright (c) 2005, Niels Martin Hansen
5
Redistribution and use in source and binary forms, with or without
6
modification, are permitted provided that the following conditions are met:
8
* Redistributions of source code must retain the above copyright notice,
9
this list of conditions and the following disclaimer.
10
* Redistributions in binary form must reproduce the above copyright notice,
11
this list of conditions and the following disclaimer in the documentation
12
and/or other materials provided with the distribution.
13
* Neither the name of the Aegisub Group nor the names of its contributors
14
may be used to endorse or promote products derived from this software
15
without specific prior written permission.
17
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
POSSIBILITY OF SUCH DAMAGE.
30
-- Aegisub Automation factory-brewed script
31
-- "Basic \k replacer"
33
-- Use karaskel.lua for skeleton code
34
include("karaskel.lua")
36
-- Define the name of the script
37
name = "Basic \\k replacer"
38
-- Define a description of the script
39
description = "Makes basic karaoke effects. Replace all \\k tags (and variations) with a custom string, where some variables can be substituted in."
40
-- Define the script variables that can be configured graphically
41
-- This is the primary point of the script, being able to configure it graphically
43
-- First a label to descript what special variables can be used
47
label = [[Variable-names are prefixed with $,
48
expressions are enclosed in % pairs.
50
$START = Start-time of syllable (ms)
51
$END = End-time of syllable (ms)
52
$MID = Time midways through the syllable (ms)
53
$DUR = Duration of syllable (cs)
55
\t($start,%$start+$dur*2%,\fscx110)
56
\t(%$start+$dur*2%,$end,\fscx90)]];
58
-- No "default", since a label doesn't have a value
60
-- Then a text field to input the string to replace \k's with
61
-- Make the default a "NOP" string
65
label = "\\k replacement";
66
hint = "The string to replace \\k tags with. Should start and end with { } characters.";
69
-- Allow the user to specify whether to strip tags or not
73
label = "Strip all tags";
74
hint = "Strip all formatting tags apart from the processed karaoke tags?";
81
hint = "Only apply the effect to lines with this style. Empty means apply to all lines.";
86
version, kind= 3, 'basic_ass'
88
function do_syllable(meta, styles, config, line, syl)
89
-- text is the replacement text for the syllable
90
-- ktext is the karaoke effect string
93
-- Prepare the stripped or unstripped main text
94
if config.striptags then
95
text = syl.text_stripped
104
-- Add the variable names to the syllable data
105
syl["dur"] = syl.duration
106
syl["start"] = syl.start_time
107
syl["end"] = syl.end_time
108
syl["mid"] = syl.start_time + syl.duration*5
110
ktext = config.k_repstr
112
-- Function for replacing the variables
113
local function var_replacer(varname)
114
varname = string.lower(varname)
115
if syl[varname] ~= nil then
118
aegisub.output_debug(string.format("Unknown variable name: %s", varname))
119
return "$" .. varname
122
-- Replace the variables in the ktext
123
ktext = string.gsub(ktext, "$(%a+)", var_replacer)
125
-- Function for evaluating expressions
126
local function expression_evaluator(expression)
127
chunk, err = loadstring(string.format("return (%s)", expression))
129
aegisub.output_debug(string.format("Error parsing expression:\n%s", expression, err))
130
return "%" .. expression .. "%"
135
-- Find and evaluate expressions
136
ktext = string.gsub(ktext, "%%([^%%]*)%%", expression_evaluator)
141
function do_line(meta, styles, config, line)
142
if config.workstyle == "" or config.workstyle == line.style then
143
return karaskel.do_line(meta, styles, config, line)
145
return { n=1, [1]=line }