Add function ASSEscape

This commit is contained in:
Star Brilliant 2013-11-17 14:59:25 +08:00
parent 89037da921
commit aefd39014c

View File

@ -261,7 +261,7 @@ def WriteCommentBilibiliPositioned(f, c, width, height, styleid):
try:
comment_args = safe_list(json.loads(c[3]))
text = str(comment_args[4]).replace('\\', '\\\\').replace('/n', '\\N').replace('{', '\\{').replace('}', '\\}')
text = ASSEscape(str(comment_args[4]).replace('/n', '\n'))
from_x = comment_args.get(0, 0)
from_y = comment_args.get(1, 0)
to_x = comment_args.get(7, from_x)
@ -382,7 +382,9 @@ def WriteASSHead(f, width, height, fontface, fontsize, alpha, styleid):
[Script Info]
; Script generated by Danmaku2ASS
; https://github.com/m13253/danmaku2ass
Script Updated By: Danmaku2ASS (https://github.com/m13253/danmaku2ass)
ScriptType: v4.00+
WrapStyle: 2
Collisions: Normal
PlayResX: %(width)s
PlayResY: %(height)s
@ -398,7 +400,7 @@ Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
def WriteComment(f, c, row, width, height, bottomReserved, fontsize, lifetime, styleid):
text = c[3].replace('\\', '\\\\').replace('\n', '\\N').replace('{', '\\{').replace('}', '\\}')
text = ASSEscape(c[3])
styles = []
if c[4] == 1:
styles.append('\\an8\\pos(%(halfwidth)s, %(row)s)' % {'halfwidth': round(width/2), 'row': row})
@ -417,6 +419,10 @@ def WriteComment(f, c, row, width, height, bottomReserved, fontsize, lifetime, s
f.write('Dialogue: 2,%(start)s,%(end)s,%(styleid)s,,0000,0000,0000,,{%(styles)s}%(text)s\n' % {'start': ConvertTimestamp(c[0]), 'end': ConvertTimestamp(c[0]+lifetime), 'styles': ''.join(styles), 'text': text, 'styleid': styleid})
def ASSEscape(s):
return '\\N'.join((i or ' ' for i in str(s).replace('\\', '\\\\').replace('{', '\\{').replace('}', '\\}').split('\n')))
def CalculateLength(s):
return max(map(len, s.split('\n'))) # May not be accurate