Remove trailing whitespace from kara-templater.lua

This commit is contained in:
Thomas Goyne 2015-03-01 11:20:15 -08:00
parent 40ae2cdc35
commit 9466c98fd7
1 changed files with 44 additions and 44 deletions

View File

@ -115,7 +115,7 @@ function parse_code(meta, styles, line, templates, mods)
aegisub.debug.out(3, "Unknown modifier in code template: %s\nIn template code line: %s\nEffect field: %s\n\n", m, line.text, line.effect) aegisub.debug.out(3, "Unknown modifier in code template: %s\nIn template code line: %s\nEffect field: %s\n\n", m, line.text, line.effect)
end end
end end
if not inserted then if not inserted then
aegisub.debug.out(5, "Found implicit run-once code line: %s\n", line.text) aegisub.debug.out(5, "Found implicit run-once code line: %s\n", line.text)
table.insert(templates.once, template) table.insert(templates.once, template)
@ -145,7 +145,7 @@ function parse_template(meta, styles, line, templates, mods)
noblank = false noblank = false
} }
local inserted = false local inserted = false
local rest = mods local rest = mods
while rest ~= "" do while rest ~= "" do
local m, t = string.headtail(rest) local m, t = string.headtail(rest)
@ -239,7 +239,7 @@ function parse_template(meta, styles, line, templates, mods)
aegisub.debug.out(3, "Unknown modifier in template: %s\nIn template line: %s\nEffect field: %s\n\n", m, line.text, line.effect) aegisub.debug.out(3, "Unknown modifier in template: %s\nIn template line: %s\nEffect field: %s\n\n", m, line.text, line.effect)
end end
end end
if not inserted then if not inserted then
table.insert(templates.syl, template) table.insert(templates.syl, template)
end end
@ -299,9 +299,9 @@ function apply_templates(meta, styles, subs, templates)
_G = _G _G = _G
} }
tenv.tenv = tenv tenv.tenv = tenv
-- Define helper functions in tenv -- Define helper functions in tenv
tenv.retime = function(mode, addstart, addend) tenv.retime = function(mode, addstart, addend)
local line, syl = tenv.line, tenv.syl local line, syl = tenv.line, tenv.syl
local newstart, newend = line.start_time, line.end_time local newstart, newend = line.start_time, line.end_time
@ -346,20 +346,20 @@ function apply_templates(meta, styles, subs, templates)
line.duration = newend - newstart line.duration = newend - newstart
return "" return ""
end end
tenv.fxgroup = {} tenv.fxgroup = {}
tenv.relayer = function(layer) tenv.relayer = function(layer)
tenv.line.layer = layer tenv.line.layer = layer
return "" return ""
end end
tenv.restyle = function(style) tenv.restyle = function(style)
tenv.line.style = style tenv.line.style = style
tenv.line.styleref = styles[style] tenv.line.styleref = styles[style]
return "" return ""
end end
tenv.maxloop = function(newmaxj) tenv.maxloop = function(newmaxj)
tenv.maxj = newmaxj tenv.maxj = newmaxj
return "" return ""
@ -370,7 +370,7 @@ function apply_templates(meta, styles, subs, templates)
tenv.maxj = newmaxj tenv.maxj = newmaxj
return "" return ""
end end
tenv.recall = {} tenv.recall = {}
setmetatable(tenv.recall, { setmetatable(tenv.recall, {
decorators = {}, decorators = {},
@ -416,13 +416,13 @@ function apply_templates(meta, styles, subs, templates)
end end
return value return value
end end
-- run all run-once code snippets -- run all run-once code snippets
for k, t in pairs(templates.once) do for k, t in pairs(templates.once) do
assert(t.code, "WTF, a 'once' template without code?") assert(t.code, "WTF, a 'once' template without code?")
run_code_template(t, tenv) run_code_template(t, tenv)
end end
-- start processing lines -- start processing lines
local i, n = 0, #subs local i, n = 0, #subs
while i < n do while i < n do
@ -438,7 +438,7 @@ function apply_templates(meta, styles, subs, templates)
l.comment = true l.comment = true
l.effect = "karaoke" l.effect = "karaoke"
subs[i] = l subs[i] = l
end end
end end
end end
end end
@ -499,7 +499,7 @@ end
function apply_line(meta, styles, subs, line, templates, tenv) function apply_line(meta, styles, subs, line, templates, tenv)
-- Tell whether any templates were applied to this line, needed to know whether the original line should be removed from input -- Tell whether any templates were applied to this line, needed to know whether the original line should be removed from input
local applied_templates = false local applied_templates = false
-- General variable replacement context -- General variable replacement context
local varctx = { local varctx = {
layer = line.layer, layer = line.layer,
@ -527,7 +527,7 @@ function apply_line(meta, styles, subs, line, templates, tenv)
lx = math.floor(line.x+0.5), lx = math.floor(line.x+0.5),
ly = math.floor(line.y+0.5) ly = math.floor(line.y+0.5)
} }
tenv.orgline = line tenv.orgline = line
tenv.line = nil tenv.line = nil
tenv.syl = nil tenv.syl = nil
@ -537,7 +537,7 @@ function apply_line(meta, styles, subs, line, templates, tenv)
aegisub.debug.out(5, "Running line templates\n") aegisub.debug.out(5, "Running line templates\n")
for t in matching_templates(templates.line, line, tenv) do for t in matching_templates(templates.line, line, tenv) do
if aegisub.progress.is_cancelled() then break end if aegisub.progress.is_cancelled() then break end
-- Set varctx for per-line variables -- Set varctx for per-line variables
varctx["start"] = varctx.lstart varctx["start"] = varctx.lstart
varctx["end"] = varctx.lend varctx["end"] = varctx.lend
@ -555,7 +555,7 @@ function apply_line(meta, styles, subs, line, templates, tenv)
varctx.height = varctx.lheight varctx.height = varctx.lheight
varctx.x = varctx.lx varctx.x = varctx.lx
varctx.y = varctx.ly varctx.y = varctx.ly
for j, maxj in template_loop(tenv, t.loops) do for j, maxj in template_loop(tenv, t.loops) do
if t.code then if t.code then
aegisub.debug.out(5, "Code template, %s\n", t.code) aegisub.debug.out(5, "Code template, %s\n", t.code)
@ -605,29 +605,29 @@ function apply_line(meta, styles, subs, line, templates, tenv)
end end
end end
aegisub.debug.out(5, "Done running line templates\n\n") aegisub.debug.out(5, "Done running line templates\n\n")
-- Loop over syllables -- Loop over syllables
for i = 0, line.kara.n do for i = 0, line.kara.n do
if aegisub.progress.is_cancelled() then break end if aegisub.progress.is_cancelled() then break end
local syl = line.kara[i] local syl = line.kara[i]
aegisub.debug.out(5, "Applying templates to syllable: %s\n", syl.text) aegisub.debug.out(5, "Applying templates to syllable: %s\n", syl.text)
if apply_syllable_templates(syl, line, templates.syl, tenv, varctx, subs) then if apply_syllable_templates(syl, line, templates.syl, tenv, varctx, subs) then
applied_templates = true applied_templates = true
end end
end end
-- Loop over furigana -- Loop over furigana
for i = 1, line.furi.n do for i = 1, line.furi.n do
if aegisub.progress.is_cancelled() then break end if aegisub.progress.is_cancelled() then break end
local furi = line.furi[i] local furi = line.furi[i]
aegisub.debug.out(5, "Applying templates to furigana: %s\n", furi.text) aegisub.debug.out(5, "Applying templates to furigana: %s\n", furi.text)
if apply_syllable_templates(furi, line, templates.furi, tenv, varctx, subs) then if apply_syllable_templates(furi, line, templates.furi, tenv, varctx, subs) then
applied_templates = true applied_templates = true
end end
end end
return applied_templates return applied_templates
end end
@ -650,7 +650,7 @@ end
function run_text_template(template, tenv, varctx) function run_text_template(template, tenv, varctx)
local res = template local res = template
aegisub.debug.out(5, "Running text template '%s'\n", res) aegisub.debug.out(5, "Running text template '%s'\n", res)
-- Replace the variables in the string (this is probably faster than using a custom function, but doesn't provide error reporting) -- Replace the variables in the string (this is probably faster than using a custom function, but doesn't provide error reporting)
if varctx then if varctx then
aegisub.debug.out(5, "Has varctx, replacing variables\n") aegisub.debug.out(5, "Has varctx, replacing variables\n")
@ -669,7 +669,7 @@ function run_text_template(template, tenv, varctx)
res = string.gsub(res, "$([%a_]+)", var_replacer) res = string.gsub(res, "$([%a_]+)", var_replacer)
aegisub.debug.out(5, "Done replacing variables, new template string is '%s'\n", res) aegisub.debug.out(5, "Done replacing variables, new template string is '%s'\n", res)
end end
-- Function for evaluating expressions -- Function for evaluating expressions
local function expression_evaluator(expression) local function expression_evaluator(expression)
f, err = loadstring(string.format("return (%s)", expression)) f, err = loadstring(string.format("return (%s)", expression))
@ -691,24 +691,24 @@ function run_text_template(template, tenv, varctx)
aegisub.debug.out(5, "Now evaluating expressions\n") aegisub.debug.out(5, "Now evaluating expressions\n")
res = string.gsub(res , "!(.-)!", expression_evaluator) res = string.gsub(res , "!(.-)!", expression_evaluator)
aegisub.debug.out(5, "After evaluation: %s\nDone handling template\n\n", res) aegisub.debug.out(5, "After evaluation: %s\nDone handling template\n\n", res)
return res return res
end end
function apply_syllable_templates(syl, line, templates, tenv, varctx, subs) function apply_syllable_templates(syl, line, templates, tenv, varctx, subs)
local applied = 0 local applied = 0
-- Loop over all templates matching the line style -- Loop over all templates matching the line style
for t in matching_templates(templates, line, tenv) do for t in matching_templates(templates, line, tenv) do
if aegisub.progress.is_cancelled() then break end if aegisub.progress.is_cancelled() then break end
tenv.syl = syl tenv.syl = syl
tenv.basesyl = syl tenv.basesyl = syl
set_ctx_syl(varctx, line, syl) set_ctx_syl(varctx, line, syl)
applied = applied + apply_one_syllable_template(syl, line, t, tenv, varctx, subs, false, false) applied = applied + apply_one_syllable_template(syl, line, t, tenv, varctx, subs, false, false)
end end
return applied > 0 return applied > 0
end end
@ -716,7 +716,7 @@ function is_syl_blank(syl)
if syl.duration <= 0 then if syl.duration <= 0 then
return true return true
end end
-- try to remove common spacing characters -- try to remove common spacing characters
local t = syl.text_stripped local t = syl.text_stripped
if t:len() <= 0 then return true end if t:len() <= 0 then return true end
@ -729,26 +729,26 @@ function apply_one_syllable_template(syl, line, template, tenv, varctx, subs, sk
if aegisub.progress.is_cancelled() then return 0 end if aegisub.progress.is_cancelled() then return 0 end
local t = template local t = template
local applied = 0 local applied = 0
aegisub.debug.out(5, "Applying template to one syllable with text: %s\n", syl.text) aegisub.debug.out(5, "Applying template to one syllable with text: %s\n", syl.text)
-- Check for right inline_fx -- Check for right inline_fx
if t.fx and t.fx ~= syl.inline_fx then if t.fx and t.fx ~= syl.inline_fx then
aegisub.debug.out(5, "Syllable has wrong inline-fx (wanted '%s', got '%s'), skipping.\n", t.fx, syl.inline_fx) aegisub.debug.out(5, "Syllable has wrong inline-fx (wanted '%s', got '%s'), skipping.\n", t.fx, syl.inline_fx)
return 0 return 0
end end
if t.noblank and is_syl_blank(syl) then if t.noblank and is_syl_blank(syl) then
aegisub.debug.out(5, "Syllable is blank, skipping.\n") aegisub.debug.out(5, "Syllable is blank, skipping.\n")
return 0 return 0
end end
-- Recurse to per-char if required -- Recurse to per-char if required
if not skip_perchar and t.perchar then if not skip_perchar and t.perchar then
aegisub.debug.out(5, "Doing per-character effects...\n") aegisub.debug.out(5, "Doing per-character effects...\n")
local charsyl = table.copy(syl) local charsyl = table.copy(syl)
tenv.syl = charsyl tenv.syl = charsyl
local left, width = syl.left, 0 local left, width = syl.left, 0
for c in unicode.chars(syl.text_stripped) do for c in unicode.chars(syl.text_stripped) do
charsyl.text = c charsyl.text = c
@ -762,29 +762,29 @@ function apply_one_syllable_template(syl, line, template, tenv, varctx, subs, sk
charsyl.prespacewidth, charsyl.postspacewidth = 0, 0 -- whatever... charsyl.prespacewidth, charsyl.postspacewidth = 0, 0 -- whatever...
left = left + width left = left + width
set_ctx_syl(varctx, line, charsyl) set_ctx_syl(varctx, line, charsyl)
applied = applied + apply_one_syllable_template(charsyl, line, t, tenv, varctx, subs, true, false) applied = applied + apply_one_syllable_template(charsyl, line, t, tenv, varctx, subs, true, false)
end end
return applied return applied
end end
-- Recurse to multi-hl if required -- Recurse to multi-hl if required
if not skip_multi and t.multi then if not skip_multi and t.multi then
aegisub.debug.out(5, "Doing multi-highlight effects...\n") aegisub.debug.out(5, "Doing multi-highlight effects...\n")
local hlsyl = table.copy(syl) local hlsyl = table.copy(syl)
tenv.syl = hlsyl tenv.syl = hlsyl
for hl = 1, syl.highlights.n do for hl = 1, syl.highlights.n do
local hldata = syl.highlights[hl] local hldata = syl.highlights[hl]
hlsyl.start_time = hldata.start_time hlsyl.start_time = hldata.start_time
hlsyl.end_time = hldata.end_time hlsyl.end_time = hldata.end_time
hlsyl.duration = hldata.duration hlsyl.duration = hldata.duration
set_ctx_syl(varctx, line, hlsyl) set_ctx_syl(varctx, line, hlsyl)
applied = applied + apply_one_syllable_template(hlsyl, line, t, tenv, varctx, subs, true, true) applied = applied + apply_one_syllable_template(hlsyl, line, t, tenv, varctx, subs, true, true)
end end
return applied return applied
end end
@ -813,7 +813,7 @@ function apply_one_syllable_template(syl, line, template, tenv, varctx, subs, sk
applied = applied + 1 applied = applied + 1
end end
end end
return applied return applied
end end
@ -822,10 +822,10 @@ end
function filter_apply_templates(subs, config) function filter_apply_templates(subs, config)
aegisub.progress.task("Collecting header data...") aegisub.progress.task("Collecting header data...")
local meta, styles = karaskel.collect_head(subs, true) local meta, styles = karaskel.collect_head(subs, true)
aegisub.progress.task("Parsing templates...") aegisub.progress.task("Parsing templates...")
local templates = parse_templates(meta, styles, subs) local templates = parse_templates(meta, styles, subs)
aegisub.progress.task("Applying templates...") aegisub.progress.task("Applying templates...")
apply_templates(meta, styles, subs, templates) apply_templates(meta, styles, subs, templates)
end end