--[[ Modified from the simple \k replacer script by Niels Martin Hansen Copyright (c) 2005 Modifications written by xess, Copyright (c) 2006 Copyright (c) 2005, Niels Martin Hansen All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the Aegisub Group nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ]] --[[ Hints: This program uses the same variables you see in the simple \k replacer program, $DUR, $END, etc. However, they are timed per line, not per syllable. This script is useful to insert any kind of text or substation alpha code into the front of each line. Examples are like {\fade} effects which needs to has the times entered manually. This script can help automate that. ]] include("karaskel.lua") name = "Inserter" Description ="Inserts code at beginning of each line." configuration = { [1] = { name="help"; kind="label"; label=[[Variables: $START = Start-time of syllable (ms), should be 0 $END = End-time of syllable (ms) $MID = Time midways through the syllable (ms) $DUR = Duration of line (ms) Enclose with % to do calculations e.g. %$DUR+200%]]; hint="" }; [2] ={ name = "insstr"; kind = "text"; label = "Code"; hint = "This line will be added to the start of every line."; default = "" } } version, kind =3, 'basic_ass' function do_line_start(meta, styles, config, line) local text line["dur"] = line.duration line["start"] = line.start_time line["end"] = line.end_time line["mid"] = line.duration/2 text = config.insstr -- Function for replacing the variables local function var_replacer(varname) varname = string.lower(varname) if line[varname] ~= nil then return line[varname] else aegisub.output_debug(string.format("Unknown variable name: %s", varname)) return "$" .. varname end end -- Replace the variables in the ktext text = string.gsub(text, "$(%a+)", var_replacer) -- Function for evaluating expressions local function expression_evaluator(expression) chunk, err = loadstring(string.format("return (%s)", expression)) if (err) ~= nil then aegisub.output_debug(string.format("Error parsing expression:\n%s", expression, err)) return "%" .. expression .. "%" else return chunk() end end -- Find and evaluate expressions text = string.gsub(text, "%%([^%%]*)%%", expression_evaluator) return text .. line.text end function do_syllable(meta, styles, config, line, syl) return "" end