Ejemplo n.º 1
0
    def append_element(self, element: model.ContentElement, begin: Fraction,
                       end: Optional[Fraction]):
        """Converts model element to SRT content"""

        if isinstance(element, model.Div):
            for elem in list(element):
                self.append_element(elem, begin, end)

        if isinstance(element, model.P):

            self._captions_counter += 1

            self._paragraphs.append(SrtParagraph(self._captions_counter))
            self._paragraphs[-1].set_begin(begin)
            self._paragraphs[-1].set_end(end)

            for elem in list(element):
                self.append_element(elem, begin, end)

            self._paragraphs[-1].normalize_eol()

            if self._paragraphs[-1].is_only_whitespace():
                LOGGER.debug("Removing empty paragraph.")
                self._paragraphs.pop()

        if isinstance(element, model.Span):
            is_bold = style.is_element_bold(element)
            is_italic = style.is_element_italic(element)
            is_underlined = style.is_element_underlined(element)
            font_color = style.get_font_color(element)

            if font_color is not None:
                self._paragraphs[-1].append_text(
                    style.FONT_COLOR_TAG_IN.format(font_color))

            if is_bold:
                self._paragraphs[-1].append_text(style.BOLD_TAG_IN)
            if is_italic:
                self._paragraphs[-1].append_text(style.ITALIC_TAG_IN)
            if is_underlined:
                self._paragraphs[-1].append_text(style.UNDERLINE_TAG_IN)

            for elem in list(element):
                self.append_element(elem, begin, end)

            if is_underlined:
                self._paragraphs[-1].append_text(style.UNDERLINE_TAG_OUT)
            if is_italic:
                self._paragraphs[-1].append_text(style.ITALIC_TAG_OUT)
            if is_bold:
                self._paragraphs[-1].append_text(style.BOLD_TAG_OUT)
            if font_color is not None:
                self._paragraphs[-1].append_text(style.FONT_COLOR_TAG_OUT)

        if isinstance(element, model.Br):
            self._paragraphs[-1].append_text("\n")

        if isinstance(element, model.Text):
            self._paragraphs[-1].append_text(element.get_text())
Ejemplo n.º 2
0
    def append_element(self, element: model.ContentElement, offset: Fraction):
        """Converts model element to SRT content"""

        if isinstance(element, model.Div):
            for elem in list(element):
                self.append_element(elem, offset)

        if isinstance(element, model.P):

            if self._paragraphs:
                self._paragraphs[-1].set_end(offset)

            self._captions_counter += 1

            self._paragraphs.append(SrtParagraph(self._captions_counter))
            self._paragraphs[-1].set_begin(offset)

            for elem in list(element):
                self.append_element(elem, offset)

        if isinstance(element, model.Span):
            is_bold = style.is_element_bold(element)
            is_italic = style.is_element_italic(element)
            is_underlined = style.is_element_underlined(element)
            font_color = style.get_font_color(element)

            if font_color is not None:
                self._paragraphs[-1].append_text(
                    style.FONT_COLOR_TAG_IN.format(font_color))

            if is_bold:
                self._paragraphs[-1].append_text(style.BOLD_TAG_IN)
            if is_italic:
                self._paragraphs[-1].append_text(style.ITALIC_TAG_IN)
            if is_underlined:
                self._paragraphs[-1].append_text(style.UNDERLINE_TAG_IN)

            for elem in list(element):
                self.append_element(elem, offset)

            if is_underlined:
                self._paragraphs[-1].append_text(style.UNDERLINE_TAG_OUT)
            if is_italic:
                self._paragraphs[-1].append_text(style.ITALIC_TAG_OUT)
            if is_bold:
                self._paragraphs[-1].append_text(style.BOLD_TAG_OUT)
            if font_color is not None:
                self._paragraphs[-1].append_text(style.FONT_COLOR_TAG_OUT)

        if isinstance(element, model.Br):
            self._paragraphs[-1].append_text("\n")

        if isinstance(element, model.Text):
            self._paragraphs[-1].append_text(element.get_text())
Ejemplo n.º 3
0
  def process_inline_element(self, element: model.ContentElement, begin: Fraction, end: Optional[Fraction]):
    """Converts inline element (span and br) to VTT content"""

    if isinstance(element, model.Span):
      is_bold = style.is_element_bold(element)
      is_italic = style.is_element_italic(element)
      is_underlined = style.is_element_underlined(element)
      color = style.get_color(element)
      bg_color = style.get_background_color(element)

      if color is not None:
        if self._colors_used.get(color) is None:
          color_classname = style.get_color_classname(color)
          self._colors_used[color] = color_classname
          self._css_classes.append(CssClass("color", color, color_classname))
        else:
          color_classname = self._colors_used[color]
        self._paragraphs[-1].append_text(style.COLOR_TAG_IN.format(color_classname))

      if bg_color is not None:
        if self._background_colors_used.get(bg_color) is None:
          bg_color_classname = style.get_background_color_classname(bg_color)
          self._background_colors_used[bg_color] = bg_color_classname
          self._css_classes.append(CssClass("background-color", bg_color, bg_color_classname))
        else:
          bg_color_classname = self._background_colors_used[bg_color]
        self._paragraphs[-1].append_text(style.BG_COLOR_TAG_IN.format(bg_color_classname))

      if is_bold:
        self._paragraphs[-1].append_text(style.BOLD_TAG_IN)
      if is_italic:
        self._paragraphs[-1].append_text(style.ITALIC_TAG_IN)
      if is_underlined:
        self._paragraphs[-1].append_text(style.UNDERLINE_TAG_IN)

      for elem in list(element):
        self.process_inline_element(elem, begin, end)

      if is_underlined:
        self._paragraphs[-1].append_text(style.UNDERLINE_TAG_OUT)
      if is_italic:
        self._paragraphs[-1].append_text(style.ITALIC_TAG_OUT)
      if is_bold:
        self._paragraphs[-1].append_text(style.BOLD_TAG_OUT)
      if color is not None:
        self._paragraphs[-1].append_text(style.COLOR_TAG_OUT)
      if bg_color is not None:
        self._paragraphs[-1].append_text(style.BG_COLOR_TAG_OUT)

    if isinstance(element, model.Br):
      self._paragraphs[-1].append_text("\n")

    if isinstance(element, model.Text):
      self._paragraphs[-1].append_text(element.get_text())
    def _get_text_from_children(element: ContentElement) -> str:
        if isinstance(element, Text):
            return element.get_text()

        for child in element:
            return ParagraphsMergingFilterTest._get_text_from_children(child)