Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 merge(captions):
"""
Merge list of captions into one caption. The start/end times from the first
caption are kept.
"""
new_nodes = []
for caption in captions:
if new_nodes:
new_nodes.append(CaptionNode.create_break())
for node in caption.nodes:
new_nodes.append(node)
caption = Caption(
captions[0].start, captions[0].end, new_nodes, captions[0].style)
return caption
self._still_editing = [caption]
for instruction in node_buffer:
# skip empty elements
if instruction.is_empty():
continue
elif instruction.requires_repositioning():
caption = PreCaption()
caption.start = start
caption.end = 0
self._still_editing.append(caption)
# handle line breaks
elif instruction.is_explicit_break():
caption.nodes.append(CaptionNode.create_break(
layout_info=_get_layout_from_tuple(instruction.position)
))
# handle open italics
elif instruction.sets_italics_on():
caption.nodes.append(
CaptionNode.create_style(
True, {'italics': True},
layout_info=_get_layout_from_tuple(
instruction.position
))
)
# handle clone italics
elif instruction.sets_italics_off():
caption.nodes.append(
"""
# convert text
if isinstance(tag, NavigableString):
# BeautifulSoup apparently handles unescaping character codes
# (e.g. &) automatically. The following variable, therefore,
# should contain a plain unicode string.
# strips indentation whitespace only
pattern = re.compile("^(?:[\n\r]+\s*)?(.+)")
result = pattern.search(tag)
if not result:
return
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:
elif '' == line:
if found_timing:
if not nodes:
raise CaptionReadSyntaxError(
'Cue without content. (line %d)' % timing_line)
else:
found_timing = False
caption = Caption(
start, end, nodes, layout_info=layout_info)
captions.append(caption)
nodes = []
else:
if found_timing:
if nodes:
nodes.append(CaptionNode.create_break())
nodes.append(CaptionNode.create_text(
self._decode(line)))
else:
# it's a comment or some metadata; ignore it
pass
# Add a last caption if there are remaining nodes
if nodes:
caption = Caption(start, end, nodes, layout_info=layout_info)
captions.append(caption)
return captions