def from_model( model_element: model.ContentElement ) -> typing.Optional[ContentElement]: if isinstance(model_element, model.Body): imsc_class = BodyElement elif isinstance(model_element, model.Div): imsc_class = DivElement elif isinstance(model_element, model.P): imsc_class = PElement elif isinstance(model_element, model.Span): imsc_class = SpanElement elif isinstance(model_element, model.Br): imsc_class = BrElement elif isinstance(model_element, model.Ruby): imsc_class = RubyElement elif isinstance(model_element, model.Rb): imsc_class = RbElement elif isinstance(model_element, model.Rt): imsc_class = RtElement elif isinstance(model_element, model.Rbc): imsc_class = RbcElement elif isinstance(model_element, model.Rtc): imsc_class = RtcElement elif isinstance(model_element, model.Region): imsc_class = RegionElement else: return None xml_element = imsc_class.make_ttml_element() if imsc_class.has_region: if model_element.get_region() is not None: imsc_attr.RegionAttribute.set( xml_element, model_element.get_region().get_id()) if imsc_class.has_timing: if model_element.get_begin() is not None: imsc_attr.BeginAttribute.set(xml_element, model_element.get_begin()) if model_element.get_end() is not None: imsc_attr.EndAttribute.set(xml_element, model_element.get_end()) if model_element.get_id() is not None: imsc_attr.XMLIDAttribute.set(xml_element, model_element.get_id()) if imsc_class.has_styles: ContentElement.from_model_style_properties(model_element, xml_element) ContentElement.from_model_animation(model_element, xml_element) if imsc_class.has_children: last_child_element = None for child in iter(model_element): if isinstance(child, model.Text): if last_child_element is None: xml_element.text = child.get_text() else: last_child_element.tail = child.get_text() child_element = ContentElement.from_model(child) if child_element is not None: xml_element.append(child_element) last_child_element = child_element return xml_element
def from_model( ctx: TTMLElement.WritingContext, model_element: model.ContentElement ) -> typing.Optional[et.Element]: '''Returns the TTML element corresponding to the model element `model_element`. `ctx` contains state information used in the process. ''' if isinstance(model_element, model.Body): imsc_class = BodyElement elif isinstance(model_element, model.Div): imsc_class = DivElement elif isinstance(model_element, model.P): imsc_class = PElement elif isinstance(model_element, model.Span): imsc_class = SpanElement elif isinstance(model_element, model.Br): imsc_class = BrElement elif isinstance(model_element, model.Ruby): imsc_class = RubyElement elif isinstance(model_element, model.Rb): imsc_class = RbElement elif isinstance(model_element, model.Rt): imsc_class = RtElement elif isinstance(model_element, model.Rbc): imsc_class = RbcElement elif isinstance(model_element, model.Rtc): imsc_class = RtcElement elif isinstance(model_element, model.Region): imsc_class = RegionElement else: return None xml_element = imsc_class.make_ttml_element() if (model_element.parent() is None and model_element.get_space() is model.WhiteSpaceHandling.PRESERVE) or \ (model_element.parent() is not None and model_element.parent().get_space() != model_element.get_space()): imsc_attr.XMLSpaceAttribute.set(xml_element, model_element.get_space()) if imsc_class.has_region: if model_element.get_region() is not None: imsc_attr.RegionAttribute.set(xml_element, model_element.get_region().get_id()) if imsc_class.has_timing: if model_element.get_begin() is not None: imsc_attr.BeginAttribute.set(ctx.temporal_context, xml_element, model_element.get_begin()) if model_element.get_end() is not None: imsc_attr.EndAttribute.set(ctx.temporal_context, xml_element, model_element.get_end()) if model_element.get_id() is not None: imsc_attr.XMLIDAttribute.set(xml_element, model_element.get_id()) if imsc_class.has_styles: ContentElement.from_model_style_properties(model_element, xml_element) ContentElement.from_model_animation(ctx, model_element, xml_element) if imsc_class.has_children: last_child_element = None for child in iter(model_element): if isinstance(child, model.Text): if last_child_element is None: xml_element.text = child.get_text() else: last_child_element.tail = child.get_text() child_element = ContentElement.from_model(ctx, child) if child_element is not None: xml_element.append(child_element) last_child_element = child_element return xml_element