Skip to content

Instantly share code, notes, and snippets.

@line0
Last active August 29, 2015 14:08
Show Gist options
  • Save line0/7a17ffc676fd677ca508 to your computer and use it in GitHub Desktop.
Save line0/7a17ffc676fd677ca508 to your computer and use it in GitHub Desktop.
script_name="Insert Line Breaks"
script_description=""
script_version="0.0.1"
script_author="line0"
local LineCollection = require("a-mo.LineCollection")
local util = require("aegisub.util")
local unicode = require("aegisub.unicode")
local l0Common = require("l0.Common")
local ASSTags = require("l0.ASSTags")
local re = require("aegisub.re")
local limit = 35
function insertLineBreaks(sub,sel)
local lines = LineCollection(sub,sel)
local curCnt, expr = limit, re.compile("\\s(?!.*\\s)")
lines:runCallback(function(lines, line)
local data = ASS.parse(line)
data:callback(function(section)
local j, n, len, split = 1, 1, unicode.len(section.value), {}
while j<=len do
local splitLen = math.min(curCnt,len-j+1)
split[n], j = unicode.sub(section.value, j, j+splitLen-1), j+curCnt
if splitLen-curCnt == 0 then
curCnt = limit
-- if the next character is a whitespace character, replace it with a line break
if re.match(unicode.sub(section.value,j,j), "\\s") then
j, split[n+1] = j+1, "\\N"
-- if it isn't, find the last whitespace character in our last <= n chars section
else
local matches = expr:find(split[n])
-- found one -> place the line break there and add the character count after that position
-- to the char count of the next section
if matches then
local pos = matches[1].last
split[n], split[n+1], split[n+2] = unicode.sub(split[n],1,pos-1), "\\N", unicode.sub(split[n],pos+1)
curCnt, n = curCnt-unicode.len(split[n+2]), n+1
-- no whitespace character found -> force the line break at n chars
else split[n+1] = "\\N" end
end
n=n+1
end
n=n+1
end
section.value = table.concat(split)
end,ASSLineTextSection)
data:commit()
end)
lines:replaceLines()
end
aegisub.register_macro(script_name, script_description, insertLineBreaks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment