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: try:
comment_args = safe_list(json.loads(c[3])) 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_x = comment_args.get(0, 0)
from_y = comment_args.get(1, 0) from_y = comment_args.get(1, 0)
to_x = comment_args.get(7, from_x) to_x = comment_args.get(7, from_x)
@ -382,7 +382,9 @@ def WriteASSHead(f, width, height, fontface, fontsize, alpha, styleid):
[Script Info] [Script Info]
; Script generated by Danmaku2ASS ; Script generated by Danmaku2ASS
; https://github.com/m13253/danmaku2ass ; https://github.com/m13253/danmaku2ass
Script Updated By: Danmaku2ASS (https://github.com/m13253/danmaku2ass)
ScriptType: v4.00+ ScriptType: v4.00+
WrapStyle: 2
Collisions: Normal Collisions: Normal
PlayResX: %(width)s PlayResX: %(width)s
PlayResY: %(height)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): 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 = [] styles = []
if c[4] == 1: if c[4] == 1:
styles.append('\\an8\\pos(%(halfwidth)s, %(row)s)' % {'halfwidth': round(width/2), 'row': row}) 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}) 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): def CalculateLength(s):
return max(map(len, s.split('\n'))) # May not be accurate return max(map(len, s.split('\n'))) # May not be accurate