def calculate(self): from app.db_manager.other_manager import get_image_by_id, filter_image_by_start_name if not self.args: self.args = '' self.args = self.args.strip() temp_args = self.args.split('|') img = None id_or_name = temp_args[0].strip() if re.search(r'=', id_or_name): k, v = id_or_name.split('=') k = k.strip() v = v.strip() if k == 'id': img = get_image_by_id(v) elif k == 'name': try: img = filter_image_by_start_name(v)[0] except: img = None else: img = get_image_by_id(id_or_name) if not img: try: img = filter_image_by_start_name(id_or_name)[0] except: img = None img_tag = HtmlTag('img') if img: img_tag.set_attr('src', img.img.url) for attr in temp_args[1:]: k, v = attr.split('=') img_tag.set_attr(k, v) self.result = img_tag
def format_right_ico_config(config): img = format_expression(config.get('img')) url = format_expression(config.get('url', '#')) img_tag = get_img_tag(img) a_tag = Tag('a', attrs={'href': url, 'class': 'navbar-item'}) a_tag.append(img_tag) return a_tag
def calculate(self): if not self.args: self.args = '' self.args = self.args.strip() attrs = {'class': 'icon'} svg = '' if re.search(r'\|', self.args): for k, v in [temp.split('=') for temp in self.args.split('|')]: k = k.strip() v = v.strip() if k == 'svg': svg = v elif k == 'style': attrs['style'] = v else: svg = self.args span = HtmlTag('span', attrs=attrs) span.append(HtmlTag('i', attrs={'class': svg})) self.result = span
def get_img_tag(img): if not img: return None if isinstance(img, BaseExpression) and isinstance(img.get_result(), Tag): return img.get_result() src = img.get('src', '') width = img.get('width', '') height = img.get('height', '') alt = img.get('alt', '') img_tag = Tag('img', attrs={ 'src': src, 'width': width, 'height': height, 'alt': alt }) if height: img_tag.set_attr('style', 'max-height:' + height) return img_tag
def category_to_aside_category_htmltag(id, name, has_child, is_child=False): div = Tag('div', attrs={'class': 'panel-block'}) name = Tag('a', name, attrs={ 'class': 'red_child_a' if is_child else 'red_a', 'href': reverse('app:category_article', args=(id, )) }) if not has_child: div.append(name) return div else: inner_div = Tag('div', attrs={'class': 'control'}) but = Tag('span', attrs={ 'class': 'icon', 'style': 'position: absolute;right: 0;' }) but.append( Tag('i', attrs={ 'class': 'fas fa-chevron-down', 'aria-hidden': 'true' })) inner_div.append(name) inner_div.append(but) div.append(inner_div) return div
def get_img_tag(img_cofig, allow_none=False): if not img_cofig: if allow_none: return None else: return Tag('p', '[图片配置错误]') img_tag = None if img_cofig['type'] == 'img': img_tag = Tag('img') img_tag.set_attr('src', img_cofig['src']) elif img_cofig['type'] == 'svg': img_tag = Tag.get_tag_from_bs(img_cofig['svg']) elif img_cofig['type'] == 'fa': img_tag = Tag('span', attrs={'class': 'icon'}) i_tag = Tag('i') i_tag.set_attr('class', img_cofig['class']) img_tag.append(i_tag) if not img_tag: return Tag('p', '图片配置错误') for k, v in img_cofig.get('attrs', {}).items(): img_tag.set_attr(k, v) return img_tag
def format_menu_config(config): img = config.get('img') name = config.get('name', '') url = config.get('url', '') line = config.get('line') children = config.get('children') if line: return Tag('hr', attrs={'class': "navbar-divider"}) img_tag = get_img_tag(img, allow_none=True) if children: children_tag = Tag('div', attrs={'class': 'navbar-dropdown is-boxed'}) for c in children: children_tag.append(format_menu_config(c)) name_tag = Tag('a', attrs={'class': 'navbar-link', 'href': url}) if img_tag: name_tag.append(img_tag) name_tag.append(Tag('span', text=name)) div_tag = Tag('div', attrs={'class': 'navbar-item has-dropdown is-hoverable'}) div_tag.append(name_tag) div_tag.append(children_tag) return div_tag else: div_tag = Tag('a', attrs={'class': 'navbar-item', 'href': url}) div_tag.append(img_tag) div_tag.append(Tag('span', text=name)) return div_tag
def get_aside_category_htmltag(category): nav = Tag('nav', attrs={'class': 'panel mpanel has-shadow'}) head = Tag('p', attrs={'class': 'panel-heading'}) head.append(Tag('strong', '分类')) nav.append(head) for id, v in category.items(): child = v.get('children') nav.append(category_to_aside_category_htmltag(id, v['name'], child)) if child: outer_div = Tag('div', attrs={'class': 'panel-child'}) for id2, v2 in child.items(): outer_div.append( category_to_aside_category_htmltag(id2, v2['name'], False, True)) nav.append(outer_div) return nav
def format_menu_config(config): img = format_expression(config.get('img')) name = format_expression(config.get('name', '')) url = format_expression(config.get('url', '')) line = config.get('line') children = config.get('children') if isinstance(name, BaseExpression): name = name.get_result() if isinstance(url, BaseExpression): url = url.get_result() if line: return Tag('hr', attrs={'class': "navbar-divider"}) img_tag = get_img_tag(img) if children: children_tag = Tag('div', attrs={'class': 'navbar-dropdown is-boxed'}) for c in children: children_tag.append(format_menu_config(c)) name_tag = Tag('a', attrs={'class': 'navbar-link', 'href': url}) name_tag.append(img_tag) name_tag.append(Tag('span', text=name)) div_tag = Tag('div', attrs={'class': 'navbar-item has-dropdown is-hoverable'}) div_tag.append(name_tag) div_tag.append(children_tag) return div_tag else: div_tag = Tag('a', attrs={'class': 'navbar-item', 'href': url}) div_tag.append(img_tag) div_tag.append(Tag('span', text=name)) return div_tag