~ubuntu-branches/ubuntu/trusty/aegisub/trusty

« back to all changes in this revision

Viewing changes to automation/auto3/simple-k-replacer.auto3

  • Committer: Package Import Robot
  • Author(s): Sebastian Reichel
  • Date: 2012-03-16 22:58:00 UTC
  • Revision ID: package-import@ubuntu.com-20120316225800-yfb8h9e5n04rk46a
Tags: upstream-2.1.9
ImportĀ upstreamĀ versionĀ 2.1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
--[[
 
2
 Copyright (c) 2005, Niels Martin Hansen
 
3
 All rights reserved.
 
4
 
 
5
 Redistribution and use in source and binary forms, with or without
 
6
 modification, are permitted provided that the following conditions are met:
 
7
 
 
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.
 
16
 
 
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.
 
28
 ]]
 
29
 
 
30
-- Aegisub Automation factory-brewed script
 
31
-- "Basic \k replacer"
 
32
 
 
33
-- Use karaskel.lua for skeleton code
 
34
include("karaskel.lua")
 
35
 
 
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
 
42
configuration  = {
 
43
        -- First a label to descript what special variables can be used
 
44
        [1] = {
 
45
                name = "label1";
 
46
                kind = "label";
 
47
                label = [[Variable-names are prefixed with $,
 
48
expressions are enclosed in % pairs.
 
49
Variables:
 
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)
 
54
Calculation example:
 
55
  \t($start,%$start+$dur*2%,\fscx110)
 
56
  \t(%$start+$dur*2%,$end,\fscx90)]];
 
57
                hint = ""
 
58
                -- No "default", since a label doesn't have a value
 
59
        },
 
60
        -- Then a text field to input the string to replace \k's with
 
61
        -- Make the default a "NOP" string
 
62
        [2] = {
 
63
                name = "k_repstr";
 
64
                kind = "text";
 
65
                label = "\\k replacement";
 
66
                hint = "The string to replace \\k tags with. Should start and end with { } characters.";
 
67
                default = "{\\k$DUR}"
 
68
        },
 
69
        -- Allow the user to specify whether to strip tags or not
 
70
        [3] = {
 
71
                name = "striptags";
 
72
                kind = "bool";
 
73
                label = "Strip all tags";
 
74
                hint = "Strip all formatting tags apart from the processed karaoke tags?";
 
75
                default = false
 
76
        },
 
77
        [4] = {
 
78
                name = "workstyle";
 
79
                kind = "style";
 
80
                label = "Line style";
 
81
                hint = "Only apply the effect to lines with this style. Empty means apply to all lines.";
 
82
                default = ""
 
83
        }
 
84
}
 
85
-- Mandatory values
 
86
version, kind= 3, 'basic_ass'
 
87
 
 
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
 
91
        local text, ktext
 
92
        
 
93
        -- Prepare the stripped or unstripped main text
 
94
        if config.striptags then
 
95
                text = syl.text_stripped
 
96
        else
 
97
                text = syl.text
 
98
        end
 
99
        
 
100
        if syl.n == 0 then
 
101
                return text
 
102
        end
 
103
        
 
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
 
109
        
 
110
        ktext = config.k_repstr
 
111
        
 
112
        -- Function for replacing the variables
 
113
        local function var_replacer(varname)
 
114
                varname = string.lower(varname)
 
115
                if syl[varname] ~= nil then
 
116
                        return syl[varname]
 
117
                else
 
118
                        aegisub.output_debug(string.format("Unknown variable name: %s", varname))
 
119
                        return "$" .. varname
 
120
                end
 
121
        end
 
122
        -- Replace the variables in the ktext
 
123
        ktext = string.gsub(ktext, "$(%a+)", var_replacer)
 
124
        
 
125
        -- Function for evaluating expressions
 
126
        local function expression_evaluator(expression)
 
127
                chunk, err = loadstring(string.format("return (%s)", expression))
 
128
                if (err) ~= nil then
 
129
                        aegisub.output_debug(string.format("Error parsing expression:\n%s", expression, err))
 
130
                        return "%" .. expression .. "%"
 
131
                else
 
132
                        return chunk()
 
133
                end
 
134
        end
 
135
        -- Find and evaluate expressions
 
136
        ktext = string.gsub(ktext, "%%([^%%]*)%%", expression_evaluator)
 
137
        
 
138
        return ktext .. text
 
139
end
 
140
 
 
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)
 
144
        else
 
145
                return { n=1, [1]=line }
 
146
        end
 
147
end