def transpose_headlines(value, x): """ Transpose existing headlines within the given html by the given amount of levels, for example a transpose of headlines by a level of 1 would change every h1 headline into a h2 headline, every h2 headline into a h3 headline and so forth. The max. number of headlines supported by html is h6, therefore transposing an h6 would result into an h6. """ try: level = int(x) except ValueError: return value return transpose_html_headlines(value, level)
def cms_content(context, content, headline_transpose=0, image_shape=None): """ Renders cms content. """ if content is None: return '' # run through content pipeline cms = get_cms() request = context.get('request') content = cms.on_render_content_pipeline(request, content, context) # make sure that headline transpose is an integer we can work with try: headline_transpose = int(headline_transpose) except ValueError: headline_transpose = 0 preview = value_or_default('cms_preview', context, False) # lazy-loaded images (not in preview mode) if not preview and 'cubane.media' in settings.INSTALLED_APPS: from cubane.media.templatetags.media_tags import render_image images = context.get('images', {}) images = load_images_for_content(content, images) noscript = value_or_default('noscript', context, False) content = rewrite_images(content, images, render_image, noscript, image_shape) # page links if not preview: page_links = context.get('page_links', {}) page_links = get_page_links_from_content(content, preview) content = rewrite_page_links(content, page_links) # transpose headlines if headline_transpose > 0: content = transpose_html_headlines(content, headline_transpose) # cleanup markup content = cleanup_html(content) # frontend editing return mark_safe(content)
def render(self, context): """ Render slot content. """ slotname = value_or_literal(self.slotname, context) headline_transpose = value_or_literal(self.headline_transpose, context) image_shape = value_or_literal(self.image_shape, context) page = value_or_none('page', context) child_page = value_or_none('child_page', context) preview = value_or_default('cms_preview', context, False) noscript = value_or_default('noscript', context, False) images = context.get('images', {}) is_enquiry_template = value_or_default('is_enquiry_template', context, False) # make sure that this slot actually exists if slotname not in settings.CMS_SLOTNAMES: return template_error( "Slot '%s' does not exist (referenced via %s)" % (slotname, self.slotname)) # switch page to child_page if present if child_page: page = child_page # extract correct content from page based on the slotname provided if page: content = page.get_slot_content(slotname) else: content = '' # make sure that headline transpose is an integer we can work with try: headline_transpose = int(headline_transpose) except ValueError: headline_transpose = 0 # run through content pipeline cms = get_cms() request = context.get('request') content = cms.on_render_content_pipeline(request, content, context) # rewrite image url to use responsive lazy-load mechanism for images, # (we are not doing this in preview mode). if not preview and not is_enquiry_template and 'cubane.media' in settings.INSTALLED_APPS: from cubane.media.templatetags.media_tags import render_image content = rewrite_images(content, images, render_image, noscript, image_shape) # page links if not preview: page_links = context.get('page_links', {}) content = rewrite_page_links(content, page_links) # transpose headlines content = transpose_html_headlines(content, headline_transpose) # cleanup markup content = cleanup_html(content) # mark content as safe content = mark_safe(content) # wrap content into a seperate slot container if we are configured # to do so... if settings.CMS_RENDER_SLOT_CONTAINER: content = '<div class="cms-slot-container">' + content + '</div>' # frontend editing? if settings.CUBANE_FRONTEND_EDITING: ref = get_edit_reference(request, page, ['slot_%s' % slotname]) if ref: content = '<div edit="%s">%s</div>' % (ref, content) # in preview mode, we wrap the content into a container, so that we # can identify the content in the backend and provide live-editing # preview capabilities... if preview: return '<div class="cms-slot" data-slotname="%s" data-headline-transpose="%d">%s</div>' % ( slotname, headline_transpose, content) else: return content
def test_should_not_transpose_h6_any_further(self): self.assertEqual( transpose_html_headlines('<h4>4</h4> <h5>5</h5> <h6>6</h6>', 2), '<h6>4</h6> <h6>5</h6> <h6>6</h6>')
def test_should_transpose_case_insensetively(self): self.assertEqual( transpose_html_headlines( '<H1>1</h1> <H2>2</h2> <h3>3</H3> <h4>4</H4> <H5>5</H5>', 1), '<h2>1</h2> <h3>2</h3> <h4>3</h4> <h5>4</h5> <h6>5</h6>')
def test_should_transpose_by_level(self): self.assertEqual( transpose_html_headlines( '<h1>1</h1> <h2>2</h2> <h3>3</h3> <h4>4</h4> <h5>5</h5>', 1), '<h2>1</h2> <h3>2</h3> <h4>3</h4> <h5>4</h5> <h6>5</h6>')
def test_should_not_transpose_by_zero_or_negative_levels(self): for i in range(0, 2): self.assertEqual(transpose_html_headlines('<h3>x</h3>', -i), '<h3>x</h3>')