def richtext(value): if value is not None: html = expand_db_html(value) else: html = '' return mark_safe(html)
def richtext(value): if value is not None: html = expand_db_html(value) else: html = '' return mark_safe('<div class="rich-text">' + html + '</div>')
def get_html(news_stories, additional_link_params, header_html, extra_html, footer_html): html = "<html><head></head><body>" + header_html html = html + "<ul>" for news_story in news_stories.order_by('-story_date'): url = 'https://loop.lib.uchicago.edu' + news_story.url_path.replace( '/loop', '', 1) + '?' + urllib.parse.urlencode(additional_link_params) html = html + "<li><a href='" + url + "'>" html = html + news_story.title html = html + "</a></li>" html = html + "</ul>" html = html + extra_html html = html + footer_html html = html + "</body></html>" # rough pass to clean strangely nested elements. soup = BeautifulSoup(html, "lxml") # do more cleanup work. html = clean_html(str(soup)) # get the real links for things like documents and internal pages. html = expand_db_html(html) return html
def richtext(value): if value is not None: html = expand_db_html(value) else: html = "" return mark_safe(html)
def parse_links(html, encoding=None): """Process all links in given html and replace them if markup is added.""" if encoding is None: encoding = settings.DEFAULT_CHARSET # The passed HTML may be a string or bytes, depending on what is calling # this method. For example, Django response.content is always bytes. We # always want this content to be a string for our purposes. html_as_text = force_text(html, encoding=encoding) # This call invokes Wagail-specific logic that converts references to # Wagtail pages, documents, and images to their proper link URLs. expanded_html = expand_db_html(html_as_text) soup = BeautifulSoup(expanded_html, 'html.parser') link_tags = get_link_tags(soup) for tag in link_tags: original_link = str(tag) link_with_markup = add_link_markup(tag) if link_with_markup: expanded_html = expanded_html.replace( original_link, link_with_markup ) return expanded_html
def raw(value): if value is not None: html = expand_db_html(value) else: html = '' return mark_safe(html)
def render(self, name, value, attrs=None): if value is None: translated_value = None else: translated_value = expand_db_html(value, for_editor=True) return super(HalloRichTextArea, self).render(name, translated_value, attrs)
def render(self, name, value, *args, **kwargs): if value is None: translated_value = None elif wagtail_version == 2: translated_value = self.converter.from_database_format(value) else: translated_value = expand_db_html(value, for_editor=True) return super().render(name, translated_value, *args, **kwargs)
def render(self, name, value, attrs=None): if value is None: translated_value = None else: if WAGTAIL_VERSION >= '2.0': translated_value = self.converter.from_database_format(value) else: translated_value = expand_db_html(value, for_editor=True) return super(TinyMCERichTextArea, self).render(name, translated_value, attrs)
def extended_body(self): body = json.loads(self.serializable_data()['body']) for row in body: if row['type'] == 'row_section': for paragraph in row['value']: paragraph['value'] = expand_db_html(paragraph['value']) return body
def simplerichtext(value): if isinstance(value, RichText): # passing a RichText value through the |richtext filter should have no effect return value elif value is None: html = '' else: html = expand_db_html(value) return mark_safe(html)
def richtext(value): if isinstance(value, RichText): # passing a RichText value through the |richtext filter should have no effect return value elif value is None: html = '' else: html = expand_db_html(value) return mark_safe('<div class="rich-text">' + html + '</div>')
def simplerichtext(value): """ Simple richtext filter to avoid the wrapping <div class='richtext'></div> markup """ if isinstance(value, SimpleRichText): html = value elif value is None: html = "" else: html = expand_db_html(value) return mark_safe(html)
def simplerichtext(value): """ Simple richtext filter to avoid the wrapping <div class='richtext'></div> markup """ if isinstance(value, SimpleRichText): html = value elif value is None: html = '' else: html = expand_db_html(value) return mark_safe(html)
def test_expand_db_html_with_embed(self, oembed): oembed.return_value = { "title": "test title", "author_name": "test author name", "provider_name": "test provider name", "type": "test type", "thumbnail_url": "test thumbnail url", "width": "test width", "height": "test height", "html": "test html", } html = '<embed embedtype="media" url="http://www.youtube.com/watch" />' result = expand_db_html(html) self.assertIn("test html", result)
def parse_links(value): if isinstance(value, RichText): soup = BeautifulSoup(expand_db_html(value.source), 'html.parser') else: soup = BeautifulSoup(expand_db_html(value), 'html.parser') # This removes style tags <style> for s in soup('style'): s.decompose() # This removes all inline style attr's for tag in soup.recursiveChildGenerator(): try: del tag['style'] except TypeError: # 'NavigableString' object has does not have attr's pass # This adds the link markup link_tags = get_link_tags(soup) add_link_markup(link_tags) return soup
def test_expand_db_html_with_embed(self, oembed): oembed.return_value = { 'title': 'test title', 'author_name': 'test author name', 'provider_name': 'test provider name', 'type': 'test type', 'thumbnail_url': 'test thumbnail url', 'width': 'test width', 'height': 'test height', 'html': 'test html' } html = '<embed embedtype="media" url="http://www.youtube.com/watch" />' result = expand_db_html(html) self.assertIn('test html', result)
def parse_links(value): if isinstance(value, RichText): soup = BeautifulSoup(expand_db_html(value.source), 'html.parser') else: soup = BeautifulSoup(expand_db_html(value), 'html.parser') # This removes style tags <style> for s in soup('style'): s.decompose() # This removes all inline style attr's for tag in soup.recursiveChildGenerator(): try: del tag['style'] except: # 'NavigableString' object has does not have attr's pass # This adds the link markup link_tags = get_link_tags(soup) add_link_markup(link_tags) return soup
def parse_links(html, encoding=None): """Process all links in given html and replace them if markup is added.""" if encoding is None: encoding = settings.DEFAULT_CHARSET html = html.decode(encoding) html = expand_db_html(html) soup = BeautifulSoup(html, 'html.parser') link_tags = get_link_tags(soup) for tag in link_tags: original_link = str(tag) link_with_markup = add_link_markup(tag) if link_with_markup: html = html.replace(original_link, link_with_markup) return html.encode(encoding)
def accessible_links(html): """ Makes footer links with surrounding text accessible to screen readers by adding an aria-label attribute containing the full text of the footer item to the link. """ soup = BeautifulSoup(expand_db_html(html), 'html.parser') for p in soup.find_all('p'): if p.find_all('a'): for child in p.children: if child.name == 'a': if child.string != p.text: child['aria-label'] = p.text else: if child.name != 'span': child = child.wrap(soup.new_tag('span')) child['aria-hidden'] = 'true' return text_type(soup).replace(u'\xa0', ' ')
def parse_links(html, encoding=None): """Process all links in given html and replace them if markup is added.""" if encoding is None: encoding = settings.DEFAULT_CHARSET # The passed HTML may be a string or bytes, depending on what is calling # this method. For example, Django response.content is always bytes. We # always want this content to be a string for our purposes. html_as_text = force_text(html, encoding=encoding) # This call invokes Wagail-specific logic that converts references to # Wagtail pages, documents, and images to their proper link URLs. expanded_html = expand_db_html(html_as_text) link_tags = get_link_tags(expanded_html) for tag in link_tags: tag_with_markup = add_link_markup(tag) if tag_with_markup: expanded_html = expanded_html.replace(tag, tag_with_markup) return expanded_html
def item_extra_kwargs(self, item): """ Returns an extra keyword arguments dictionary that is used with the 'add_item' call of the feed generator. Add the fields of the item, to be used by the custom feed generator. """ if use_feed_image: feed_image = item.feed_image if feed_image: image_complete_url = urljoin( self.get_site_url(), feed_image.file.url ) else: image_complete_url = "" content_field = getattr(item, self.item_content_field) try: content = expand_db_html(content_field) except: content = content_field.__html__() soup = BeautifulSoup(content, 'html.parser') # Remove style attribute to remove large botton padding for div in soup.find_all("div", {'class': 'responsive-object'}): del div['style'] # Add site url to image source for img_tag in soup.findAll('img'): if img_tag.has_attr('src'): img_tag['src'] = urljoin(self.get_site_url(), img_tag['src']) fields_to_add = { 'content': soup.prettify(formatter="html"), } if use_feed_image: fields_to_add['image'] = image_complete_url else: fields_to_add['image'] = "" return fields_to_add
def item_extra_kwargs(self, item): """ Returns an extra keyword arguments dictionary that is used with the 'add_item' call of the feed generator. Add the fields of the item, to be used by the custom feed generator. """ feed_image = item.feed_image if feed_image: filter, _ = Filter.objects.get_or_create(spec='width-1200') img = feed_image.get_rendition(filter) image_complete_url = urljoin(self.get_site_url(), img.url) content = expand_db_html(getattr(item, self.item_content_field)) soup = BeautifulSoup(content, 'html.parser') for img_tag in soup.findAll('img'): img_tag['src'] = urljoin(self.get_site_url(), img_tag['src']) return { 'image': image_complete_url if feed_image else "", 'content': soup.prettify(formatter="html") }
def item_extra_kwargs(self, item): """ Returns an extra keyword arguments dictionary that is used with the 'add_item' call of the feed generator. Add the fields of the item, to be used by the custom feed generator. """ if use_feed_image: feed_image = item.feed_image if feed_image: image_complete_url = urljoin(self.get_site_url(), feed_image.file.url) else: image_complete_url = "" content_field = getattr(item, self.item_content_field) try: content = expand_db_html(content_field) except: content = content_field.__html__() soup = BeautifulSoup(content, 'html.parser') # Remove style attribute to remove large botton padding for div in soup.find_all("div", {'class': 'responsive-object'}): del div['style'] # Add site url to image source for img_tag in soup.findAll('img'): if img_tag.has_attr('src'): img_tag['src'] = urljoin(self.get_site_url(), img_tag['src']) fields_to_add = { 'content': soup.prettify(formatter="html"), } if use_feed_image: fields_to_add['image'] = image_complete_url else: fields_to_add['image'] = "" return fields_to_add
def get_html(news_stories, additional_link_params, header_html, extra_html, footer_html): html = "<html><head></head><body>" + header_html html = html + "<ul>" for news_story in news_stories.order_by('-story_date'): url = 'https://loop.lib.uchicago.edu' + news_story.url_path.replace('/loop', '', 1) + '?' + urllib.parse.urlencode(additional_link_params) html = html + "<li><a href='" + url + "'>" html = html + news_story.title html = html + "</a></li>" html = html + "</ul>" html = html + extra_html html = html + footer_html html = html + "</body></html>" # rough pass to clean strangely nested elements. soup = BeautifulSoup(html, "lxml") # do more cleanup work. html = clean_html(str(soup)) # get the real links for things like documents and internal pages. html = expand_db_html(html) return html
def __str__(self): return mark_safe(expand_db_html(self.source))
def external_links_filter(value): if isinstance(value, RichText): return parse_links( BeautifulSoup(expand_db_html(value.source), 'html.parser')) return value
def item_description(self, item): return expand_db_html(item.body)
def richtext(value): return mark_safe('<div class="rich-text">' + expand_db_html(value) + '</div>')
def test_expand_db_html_no_linktype(self): html = '<a id="1">foo</a>' result = expand_db_html(html) self.assertEqual(result, '<a id="1">foo</a>')
def item_content(self, item): return expand_db_html(item.body)
def richtext(value): return mark_safe(expand_db_html(value))
def __html__(self): return expand_db_html(self.source)
def test_expand_db_html_with_embed(self, get_embed): from wagtail.wagtailembeds.models import Embed get_embed.return_value = Embed(html='test html') html = '<embed embedtype="media" url="http://www.youtube.com/watch" />' result = expand_db_html(html) self.assertIn('test html', result)
def render_basic(self, value): return mark_safe('<div class="rich-text">' + expand_db_html(value) + '</div>')
def test_expand_db_html_with_linktype(self): html = '<a id="1" linktype="document">foo</a>' result = expand_db_html(html) self.assertEqual(result, '<a>foo</a>')
def get_context(self, request, page): from wagtail.wagtailcore.rich_text import expand_db_html context = super().get_context(request, page) context['content'] = expand_db_html(self.body) return context
def to_representation(self, value): return expand_db_html(value)