def read(self, content, lang='en-US'): caption_set = CaptionSet() lines = content.splitlines() start_line = 0 captions = [] while start_line < len(lines): if not lines[start_line].isdigit(): break caption = Caption() end_line = self._find_text_line(start_line, lines) timing = lines[start_line + 1].split('-->') caption.start = self._srttomicro(timing[0].strip(' \r\n')) caption.end = self._srttomicro(timing[1].strip(' \r\n')) for line in lines[start_line + 2:end_line - 1]: # skip extra blank lines if not caption.nodes or line != '': caption.nodes.append(CaptionNode.create_text(line)) caption.nodes.append(CaptionNode.create_break()) # remove last line break from end of caption list caption.nodes.pop() captions.append(caption) start_line = end_line caption_set.set_captions(lang, captions) return caption_set
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 _combine_matching_captions(self, caption_set): for lang in caption_set.get_languages(): captions = caption_set.get_captions(lang) new_caps = captions[:1] for caption in captions[1:]: if caption.start == new_caps[-1].start and caption.end == new_caps[-1].end: new_caps[-1].nodes.append(CaptionNode.create_break()) new_caps[-1].nodes.extend(caption.nodes) else: new_caps.append(caption) caption_set.set_captions(lang, new_caps) return caption_set
def _translate_tag(self, tag): # convert text if isinstance(tag, NavigableString): if tag.strip() != '': node = CaptionNode.create_text(tag.strip()) self.nodes.append(node) # convert line breaks elif tag.name == 'br': self.nodes.append(CaptionNode.create_break()) # convert italics elif tag.name == 'span': # convert span self._translate_span(tag) else: # recursively call function for any children elements for a in tag.contents: self._translate_tag(a)
def _translate_tag(self, tag): # convert text if isinstance(tag, NavigableString): self.line.append(CaptionNode.create_text(tag.strip())) # convert line breaks elif tag.name == 'br': self.line.append(CaptionNode.create_break()) # convert italics elif tag.name == 'i': self.line.append(CaptionNode.create_style(True, {'italics': True})) # recursively call function for any children elements for a in tag.contents: self._translate_tag(a) self.line.append( CaptionNode.create_style(False, {'italics': True})) elif tag.name == 'span': self._translate_span(tag) else: # recursively call function for any children elements for a in tag.contents: self._translate_tag(a)