def _subvalue_to_html(cls, value): if issubclass(cls.type, AnyUri): href = getattr(value, 'href', None) if href is None: # this is not a AnyUri.Value instance. href = value text = getattr(cls.type.Attributes, 'text', None) content = None else: text = getattr(value, 'text', None) if text is None: text = getattr(cls.type.Attributes, 'text', None) content = getattr(value, 'content', None) if issubclass(cls.type, ImageUri): retval = E.img(src=href) if text is not None: retval.attrib['alt'] = text # content is ignored with ImageUri. else: retval = E.a(href=href) retval.text = text if content is not None: retval.append(content) else: retval = cls.type.to_string(value) return retval
def replace_youtube_videos_with_links(self, doc): """Replace any iframe elements found with a link to the src and a placeholder image from youtube""" def get_yt_id(src): """Return the youtube video id""" split_src = src.split("/") if "embed" in split_src: yt_id_index = split_src.index("embed") + 1 return split_src[yt_id_index] iframes = doc.xpath("//iframe") for iframe in iframes: src = iframe.get("src") yt_id = get_yt_id(src) if not yt_id: continue else: yt_img = "https://img.youtube.com/vi/{0}/0.jpg".format(yt_id) yt_href = "https://youtu.be/{0}".format(yt_id) yt_link = E.a( E.img( src=yt_img, width="480", height="360", ), href=yt_href, target="_blank", ) parent = iframe.getparent() parent.replace(iframe, yt_link)
def imageuri_to_parent(self, ctx, cls, inst, parent, name, **kwargs): # with ImageUri, content is ignored. href = getattr(inst, 'href', None) if href is None: # this is not a AnyUri.Value instance. href = inst text = getattr(cls.Attributes, 'text', None) else: text = getattr(inst, 'text', None) if text is None: text = getattr(cls.Attributes, 'text', None) retval = E.img(src=href) if text is not None: retval.attrib['alt'] = text parent.write(retval)
def serialize_complex_model(self, cls, value, locale): sti = None fti = cls.get_flat_type_info(cls) is_array = False first_child = iter(fti.values()).next() if len(fti) == 1: fti = first_child.get_flat_type_info(first_child) first_child_2 = iter(fti.values()).next() if len(fti) == 1 and first_child_2.Attributes.max_occurs > 1: if issubclass(first_child_2, ComplexModelBase): sti = first_child_2.get_simple_type_info(first_child_2) is_array = True else: if issubclass(first_child, ComplexModelBase): sti = first_child.get_simple_type_info(first_child) value = value[0] else: raise NotImplementedError("Can only serialize single return types") tr = {} if self.row_class is not None: tr['class'] = self.row_class td = {} if self.cell_class is not None: td['class'] = self.cell_class th = {} if self.header_cell_class is not None: th['class'] = self.header_cell_class class_name = first_child.get_type_name() if sti is None: if self.field_name_attr is not None: td[self.field_name_attr] = class_name if is_array: for val in value: yield E.tr(E.td(first_child_2.to_string(val), **td), **tr) else: yield E.tr(E.td(first_child_2.to_string(value), **td), **tr) else: for k, v in sti.items(): row = E.tr(**tr) subvalue = value for p in v.path: subvalue = getattr(subvalue, p, None) if subvalue is None: if v.type.Attributes.min_occurs == 0: continue else: subvalue = "" else: subvalue = v.type.to_string(subvalue) text = getattr(v.type.Attributes,'text', None) if issubclass(v.type, ImageUri): subvalue = E.img(src=subvalue) if text is not None: subvalue.attrib['alt']=text elif issubclass(v.type, AnyUri): if text is None: text = subvalue subvalue = E.a(text, href=subvalue) if self.produce_header: header_text = translate(v.type, locale, k) if self.field_name_attr is None: row.append(E.th(header_text, **th)) else: th[self.field_name_attr] = k row.append(E.th(header_text, **th)) if self.field_name_attr is None: row.append(E.td(subvalue, **td)) else: td[self.field_name_attr] = k row.append(E.td(subvalue, **td)) yield row