Skip to content

Commit 2b4384a

Browse files
committed
FixKFXBoilerplate: Add script
1 parent 26ccac6 commit 2b4384a

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

DependencyControl.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,32 @@
578578
"Move repository to TypesettingTools/arch1t3cht-Aegisub-Scripts"
579579
]
580580
}
581+
},
582+
"arch.FixKFXBoilerplate": {
583+
"fileBaseUrl": "@{fileBaseUrl}/macros/@{namespace}",
584+
"url": "@{baseUrl}#@{namespace}",
585+
"author": "arch1t3cht",
586+
"name": "Fix KFX Boilerplate",
587+
"description": "Fix certain errors in boilerplate lines in old KFX templates on new Aegisub versions",
588+
"channels": {
589+
"release": {
590+
"version": "1.0.0",
591+
"released": "2025-06-14",
592+
"default": true,
593+
"files": [
594+
{
595+
"name": ".moon",
596+
"url": "@{fileBaseUrl}@{fileName}",
597+
"sha1": ""
598+
}
599+
]
600+
}
601+
},
602+
"changelog": {
603+
"1.0.0": [
604+
"Initial Release"
605+
]
606+
}
581607
}
582608
},
583609
"modules": {

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ My automation scripts for Aegisub. In my opinion, the coolest thing here is Aegi
77
- [AegisubChain](#aegisubchain)
88
- [Scripts for Typesetting](#scripts-for-typesetting)
99
- [Focus Lines](#focus-lines)
10+
- [FBF-ifier](#fbf-ifier)
1011
- [PerspectiveMotion](#perspectivemotion)
1112
- [Derive Perspective Track](#derive-perspective-track)
1213
- [Resample Perspective](#resample-perspective)
@@ -20,6 +21,7 @@ My automation scripts for Aegisub. In my opinion, the coolest thing here is Aegi
2021
- [Center Times](#center-times)
2122
- [Other Scripts](#other-scripts)
2223
- [Convert Folds](#convert-folds)
24+
- [Fix KFX Boilerplate](#fix-kfx-boilerplate)
2325
- [Blender Export Scripts for After Effects Tracking Data](#blender-export-scripts-for-after-effects-tracking-data)
2426
- [See also](#see-also)
2527

@@ -128,6 +130,17 @@ To use it, either
128130
- click the "From File" button to automatically read this line from the subtitle file. This only works if the file is saved on your disk.
129131
This will work on any version of Aegisub (i.e. an Aegisub version using extradata folds will be able to load folds from the resulting file), but in order for the folds to be displayed inside of Aegisub, you obviously need a build that supports extradata folds.
130132

133+
### Fix KFX Boilerplate
134+
There is a large collection of old (stock templater) karaoke templates that all use the same boilerplate lines
135+
(which are often bad or even completely unnecessary, but that is another discussion).
136+
One of these boilerplate lines [broke after argument checking was added to the Lua utility libraries](https://github.com/TypesettingTools/Aegisub/issues/141#issuecomment-2561017082),
137+
which breaks these templates.
138+
This script automatically applies one of the simple workarounds to fix the template.
139+
140+
Let me once again stress that the *proper* solution for such templates is to rewrite them to not need this boilerplate at all,
141+
using [a better templater](https://github.com/The0x539/Aegisub-Scripts/blob/trunk/doc/0x.KaraTemplater.md) if needed.
142+
I only wrote this script so certain people will stop complaining.
143+
131144
### Blender Export Scripts for After Effects Tracking Data
132145
You might be looking for my patched version of the After Effects Blender export script that adds the ability to export Power Pin data. This script has been superseded by Akatsumekusa's version, which is an almost complete rewrite with more features and a more user-friendly GUI. Go [here](https://github.com/Akatmks/Akatsumekusa-Aegisub-Scripts) to download this version.
133146

macros/arch.FixKFXBoilerplate.moon

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
export script_name = "Fix KFX Boilerplate"
2+
export script_description = "Fix certain errors in boilerplate lines in old KFX templates on new Aegisub versions"
3+
export script_author = "arch1t3cht"
4+
export script_namespace = "arch.FixKFXBoilerplate"
5+
export script_version = "1.0.0"
6+
7+
haveDepCtrl, DependencyControl = pcall(require, "l0.DependencyControl")
8+
local depctrl
9+
if haveDepCtrl
10+
depctrl = DependencyControl{
11+
feed: "https://raw.githubusercontent.com/TypesettingTools/arch1t3cht-Aegisub-Scripts/main/DependencyControl.json",
12+
}
13+
14+
-- For now we just apply the least invasive fix of replacing _G.unicode.len(foo) with _G.unicode.len((foo)).
15+
-- If this turns out to miss too many files we can do something simpler like just patch _G.unicode.len with a wrapper function.
16+
17+
find_matching_paren = (str, n) ->
18+
return nil if str\sub(n, n) ~= "("
19+
20+
depth = 1
21+
22+
while n <= #str
23+
n += 1
24+
c = str\sub(n, n)
25+
26+
depth += 1 if c == "("
27+
depth -= 1 if c == ")"
28+
29+
return n if depth == 0
30+
return nil
31+
32+
33+
fix_boilerplate = (subs, sel) ->
34+
patched = 0
35+
for li, line in ipairs(subs)
36+
continue unless line.class == "dialogue"
37+
continue unless line.comment
38+
continue unless line.effect\match("template") or line.effect\match("code")
39+
40+
while true
41+
i = 1
42+
newtext = line.text
43+
while true
44+
_, i = line.text\find("unicode.len(", i, true)
45+
break if i == nil
46+
matching = find_matching_paren(line.text, i)
47+
matching2 = find_matching_paren(line.text, i + 1)
48+
49+
continue if matching == nil
50+
continue if matching2 == matching - 1
51+
52+
newtext = line.text\sub(1, i) .. "(" .. line.text\sub(i + 1, matching) .. ")" .. line.text\sub(matching + 1)
53+
patched += 1
54+
55+
break if newtext == line.text
56+
line.text = newtext
57+
58+
subs[li] = line
59+
60+
aegisub.log("Patched #{patched} instances of _G.unicode.len.")
61+
62+
63+
if haveDepCtrl
64+
depctrl\registerMacro fix_boilerplate
65+
else
66+
aegisub.register_macro(script_name, script_description, fix_boilerplate)

0 commit comments

Comments
 (0)