Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.first_element = True
# split into elements (e.g. break, italics, text)
for element in buffer.split('<$>'):
# skip empty elements
if element.strip() == '':
continue
# handle line breaks
elif element == '{break}':
self._translate_break(caption)
# handle open italics
elif element == '{italic}':
# add italics
caption.nodes.append(CaptionNode.create_style(True, {'italics': True}))
# open italics, no longer first element
self.open_italic = True
self.first_element = False
# handle clone italics
elif element == '{end-italic}' and self.open_italic:
caption.nodes.append(CaptionNode.create_style(False, {'italics': True}))
self.open_italic = False
# handle text
else:
# add text
caption.nodes.append(CaptionNode.create_text(' '.join(element.split())))
# no longer first element
self.first_element = False
# handle clone italics
elif element == '{end-italic}' and self.open_italic:
caption.nodes.append(CaptionNode.create_style(False, {'italics': True}))
self.open_italic = False
# handle text
else:
# add text
caption.nodes.append(CaptionNode.create_text(' '.join(element.split())))
# no longer first element
self.first_element = False
# close any open italics left over
if self.open_italic == True:
caption.nodes.append(CaptionNode.create_style(False, {'italics': True}))
# remove extraneous italics tags in the same caption
self._remove_italics(caption)
# only add captions to list if content inside exists
if caption.nodes:
self.scc.append(caption)
def _translate_span(self, tag):
# convert tag attributes
args = self._translate_style(tag)
# only include span tag if attributes returned
# TODO - this is an obvious very old bug. args will be a dictionary.
# but since nobody complained, I'll leave it like that.
# Happy investigating!
if args != '':
node = CaptionNode.create_style(
True, args, layout_info=tag.layout_info)
node.start = True
node.content = args
self.nodes.append(node)
# recursively call function for any children elements
for a in tag.contents:
self._translate_tag(a)
node = CaptionNode.create_style(
False, args, layout_info=tag.layout_info)
node.start = False
node.content = args
self.nodes.append(node)
else:
for a in tag.contents:
self._translate_tag(a)
def _translate_break(self, caption):
# if break appears at start of caption, skip break
if self.first_element == True:
return
# if the last caption was a break, skip this break
elif caption.nodes[-1].type == CaptionNode.BREAK:
return
# close any open italics
elif self.open_italic == True:
caption.nodes.append(CaptionNode.create_style(False, {'italics': True}))
self.open_italic = False
# add line break
caption.nodes.append(CaptionNode.create_break())
def _translate_span(self, tag, inherit_from=None):
# convert tag attributes
args = self._translate_attrs(tag)
# only include span tag if attributes returned
if args:
layout_info = self._build_layout(args, inherit_from)
# OLD: Create legacy style node
# NEW: But pass new layout object
node = CaptionNode.create_style(True, args, layout_info)
self.line.append(node)
# recursively call function for any children elements
for a in tag.contents:
# NEW: Pass the layout along so that it's eventually attached
# to leaf nodes (e.g. text or break)
self._translate_tag(a, layout_info)
node = CaptionNode.create_style(False, args, layout_info)
self.line.append(node)
else:
for a in tag.contents:
self._translate_tag(a, inherit_from)
tag_text = result.groups()[0]
self.line.append(CaptionNode.create_text(tag_text, inherit_from))
# convert line breaks
elif tag.name == 'br':
self.line.append(CaptionNode.create_break(inherit_from))
# convert italics, bold, and underline
elif tag.name == 'i' or tag.name == 'b' or tag.name == 'u':
style_name = self._get_style_name_from_tag(tag.name)
self.line.append(
CaptionNode.create_style(True, {style_name: True})
)
# recursively call function for any children elements
for a in tag.contents:
self._translate_tag(a, inherit_from)
self.line.append(
CaptionNode.create_style(False, {style_name: True}))
elif tag.name == 'span':
self._translate_span(tag, inherit_from)
else:
# recursively call function for any children elements
for a in tag.contents:
self._translate_tag(a, inherit_from)
def _translate_span(self, tag, inherit_from=None):
# convert tag attributes
args = self._translate_attrs(tag)
# only include span tag if attributes returned
if args:
layout_info = self._build_layout(args, inherit_from)
# OLD: Create legacy style node
# NEW: But pass new layout object
node = CaptionNode.create_style(True, args, layout_info)
self.line.append(node)
# recursively call function for any children elements
for a in tag.contents:
# NEW: Pass the layout along so that it's eventually attached
# to leaf nodes (e.g. text or break)
self._translate_tag(a, layout_info)
node = CaptionNode.create_style(False, args, layout_info)
self.line.append(node)
else:
for a in tag.contents:
self._translate_tag(a, inherit_from)
args = self._translate_style(tag)
# only include span tag if attributes returned
# TODO - this is an obvious very old bug. args will be a dictionary.
# but since nobody complained, I'll leave it like that.
# Happy investigating!
if args != '':
node = CaptionNode.create_style(
True, args, layout_info=tag.layout_info)
node.start = True
node.content = args
self.nodes.append(node)
# recursively call function for any children elements
for a in tag.contents:
self._translate_tag(a)
node = CaptionNode.create_style(
False, args, layout_info=tag.layout_info)
node.start = False
node.content = args
self.nodes.append(node)
else:
for a in tag.contents:
self._translate_tag(a)