def test_can_render_a_simple_tree_of_cms_pages_that_are_in_navigation(self): # Setup placeholder = Placeholder.objects.create(slot='test') model_instance = add_plugin( placeholder, cms_plugins.HtmlSitemapPlugin, 'fr', in_navigation=True, ) # Run html = model_instance.render_plugin({}) html = strip_spaces_between_tags(html) # Check assert html.strip() == strip_spaces_between_tags( """ <div id="sitemap"> <ul> <li> <a href="/" title="Index">Index</a> <ul> <li><a href="/depth-2-page-1/" title="Depth 2 page 1">Depth 2 page 1</a></li> </ul> </li> </ul> </div> """).strip()
def test_can_render_a_simple_tree_of_cms_pages_from_a_minimum_depth_to_a_maximum_depth(self): # Setup placeholder = Placeholder.objects.create(slot='test') model_instance = add_plugin( placeholder, cms_plugins.HtmlSitemapPlugin, 'fr', min_depth=2, max_depth=2, ) # Run html = model_instance.render_plugin({}) html = strip_spaces_between_tags(html) # Check assert html.strip() == strip_spaces_between_tags( """ <div id="sitemap"> <ul> <li><a href="/depth-2-page-1/" title="Depth 2 page 1">Depth 2 page 1</a></li> <li> <a href="/depth-2-page-2/" title="Depth 2 page 2">Depth 2 page 2</a> </li> <li><a href="/depth-2-page-3/" title="Depth 2 page 3">Depth 2 page 3</a></li> <li> <a href="/depth-2-page-4/" title="Depth 2 page 4">Depth 2 page 4</a> </li> </ul> </div> """).strip()
def process_response(self, request, response): if response.streaming: return response if not self.minify: return response if response.status_code != 200 or ('text/html' not in response.get('Content-Type', '')): return response host = request.get_host() for r in self.exclude_subdomains_regexes: if r.match(host): return response # Simple workaround - do not minify elements which match OMMIT_TAG_PATTERNS response.content = strip_spaces_between_tags(response.content.strip()) sliced_content = [SliceObj('', response.content)] for pattern in OMIT_TAG_PATTERNS: new_content = [] for slice_of_content in sliced_content: if slice_of_content.pattern is '': for to_parser in pattern.split(slice_of_content.content): if pattern.match(to_parser): s_obj = SliceObj(pattern.pattern, to_parser) else: s_obj = SliceObj('', to_parser) new_content.append(s_obj) else: new_content.append(slice_of_content) sliced_content = new_content response.content = ''.join( [s.content if s.pattern is not '' else RE_MULTISPACE.sub(' ', s.content) for s in sliced_content]) return response
def get_extra_data(self, context, toolbar, **kwargs): new_context = RequestContext(toolbar.request) rendered = render_to_string(self.template, new_context) stripped = strip_spaces_between_tags(rendered.strip()) return { 'html': stripped, }
def process_response(self, request, response): if 'text/html' in response['Content-Type'] and settings.COMPRESS_HTML: response.content = strip_spaces_between_tags( response.content.strip()) response.content = RE_MULTISPACE.sub(' ', response.content) response.content = RE_NEWLINE.sub(' ', response.content) return response
def simple_minify(html): """ Minify HTML with very simple algorithm. This function tries to minify HTML by stripping all spaces between all html tags (e.g. ``</div> <div>`` -> ``</div><div>``). This step is accomplished by using Django's ``strip_spaces_between_tags`` method. In addition to that, this function replaces all whitespace (more then two consecutive whitespace characters or new line) with a space character except inside excluded tags such as ``pre`` or ``textarea``. *Though process* To minify everything except content of excluded tags in one step requires very complex regular expression. The disadvantage is the regular expression will involve look-behinds and look-aheads. Those operations make regex much more resource-hungry which eats precious server memory. In addition, complex regex are hard to understand and can be hard to maintain. That is why this function splits the task into multiple sections. The first step is it executes Django's ``strip_spaces_between_tags``. After that, it uses regex expression which matches all exclude tags within the html to split the whole html string. However since the regex expression is wrapped inside a group, the content of the exclude tags is also included inside the resulting split list. Due to that it is guaranteed that every odd element (if there are any) will be the excluded tags. That means that all the whitespace within the even elements can be removed using the whitespace regex and ``re.sub`` method. """ components = RE_EXCLUDE_TAGS.split(html) html = '' for i, component in enumerate(components): if i % 2 == 0: component = strip_spaces_between_tags(component.strip()) component = RE_WHITESPACE.sub(' ', component) html += component return html
def clean(self): try: position = self.cleaned_data['position'] if isinstance(position, six.string_types): position = json.loads(position) elif not isinstance(position, dict): raise ValueError except (ValueError, KeyError): raise ValidationError("Invalid internal position data. Check your Javascript imports.") else: if 'lat' not in position or 'lng' not in position: # place the marker in the center of the current map position = {k: v for k, v in self.instance.cascade_element.glossary['map_position'].items() if k in ['lat', 'lng']} self.instance.glossary.update(position=position) marker_image = self.cleaned_data.pop('marker_image', None) if marker_image: image_data = {'pk': marker_image.pk, 'model': 'filer.Image'} self.instance.glossary.update(image=image_data) else: self.instance.glossary.pop('image', None) popup_text = self.cleaned_data.pop('popup_text', None) if strip_tags(popup_text): popup_text = strip_spaces_between_tags(popup_text) self.cleaned_data.update(popup_text=popup_text) for key in self.glossary_field_order: self.instance.glossary.update({key: self.cleaned_data.get(key)})
def spaceless_deep(value): out = n1.sub(r'> \2', value) out = n2.sub(r'\1 <', out) out = n3.sub(r'>\1</', out) out = n4.sub(r' ', out) out = strip_spaces_between_tags(out) return out
def render_html(self, product, postfix): """ Return a HTML snippet containing a rendered summary for the given product. This HTML snippet typically contains a ``<figure>`` element with a sample image ``<img src="..." >`` and a ``<figcaption>`` containing a short description of the product. Build a template search path with `postfix` distinction. """ if not self.label: msg = "The Product Serializer must be configured using a `label` field." raise exceptions.ImproperlyConfigured(msg) app_label = product._meta.app_label.lower() request = self.context['request'] cache_key = 'product:{0}|{1}-{2}-{3}-{4}-{5}'.format(product.id, app_label, self.label, product.product_model, postfix, get_language_from_request(request)) content = cache.get(cache_key) if content: return mark_safe(content) params = [ (app_label, self.label, product.product_model, postfix), (app_label, self.label, 'product', postfix), ('shop', self.label, product.product_model, postfix), ('shop', self.label, 'product', postfix), ] try: template = select_template(['{0}/products/{1}-{2}-{3}.html'.format(*p) for p in params]) except TemplateDoesNotExist: return SafeText("<!-- no such template: '{0}/products/{1}-{2}-{3}.html' -->".format(*params[0])) # when rendering emails, we require an absolute URI, so that media can be accessed from # the mail client absolute_base_uri = request.build_absolute_uri('/').rstrip('/') context = {'product': product, 'ABSOLUTE_BASE_URI': absolute_base_uri} content = strip_spaces_between_tags(template.render(context, request).strip()) cache.set(cache_key, content, app_settings.CACHE_DURATIONS['product_html_snippet']) return mark_safe(content)
def events_embed_legacy(request): # prepare template context feedlist, feedtitle = get_feed_list(request) try: count = int(request.GET.get('count', '')) except ValueError: count = 10 events = Feed.get_events_for(feedlist, count) context = { "feeds": feedlist, "events": events, "title": feedtitle, "link": feedlist[0].link if len(feedlist) == 1 else None } # render the HTML from django.template import Template, Context, RequestContext, loader html = loader.get_template("events/events_embed_legacy.html")\ .render(RequestContext(request, context)) # convert to Javascript document.write statements. from django.utils.html import strip_spaces_between_tags from django.template.defaultfilters import escapejs html = strip_spaces_between_tags(html) js = "" while len(html) > 0: js += "document.write(\"" + escapejs(html[0:128]) + "\");\n" html = html[128:] return HttpResponse(js, mimetype="application/x-javascript; charset=UTF-8")
def render_html(self, product, postfix): """ Return a HTML snippet containing a rendered summary for this product. Build a template search path with `postfix` distinction. """ if not self.label: msg = "The Product Serializer must be configured using a `label` field." raise exceptions.ImproperlyConfigured(msg) app_label = product._meta.app_label.lower() product_type = product.__class__.__name__.lower() request = self.context["request"] cache_key = "product:{0}|{1}-{2}-{3}-{4}-{5}".format( product.id, app_label, self.label, product_type, postfix, get_language_from_request(request) ) content = cache.get(cache_key) if content: return mark_safe(content) params = [ (app_label, self.label, product_type, postfix), (app_label, self.label, "product", postfix), ("shop", self.label, "product", postfix), ] try: template = select_template(["{0}/products/{1}-{2}-{3}.html".format(*p) for p in params]) except TemplateDoesNotExist: return SafeText("<!-- no such template: '{0}/products/{1}-{2}-{3}.html' -->".format(*params[0])) # when rendering emails, we require an absolute URI, so that media can be accessed from # the mail client absolute_base_uri = request.build_absolute_uri("/").rstrip("/") context = RequestContext(request, {"product": product, "ABSOLUTE_BASE_URI": absolute_base_uri}) content = strip_spaces_between_tags(template.render(context).strip()) cache.set(cache_key, content, shop_settings.CACHE_DURATIONS["product_html_snippet"]) return mark_safe(content)
def _render_row(self, key, value, field_name, key_attrs, val_attrs): """ Renders to HTML a single key/value pair row. :param key: (str) key :param value: (str) value :param field_name: (str) String name of this field :param key_attrs: (dict) HTML attributes to be applied to the key input :param val_attrs: (dict) HTML attributes to be applied to the value input """ template = """ <div class="form-row attributes-pair"> <div class="field-box"> <input type="text" class="attributes-key" name="attributes_key[{field_name}]" value="{key}" {key_attrs}> </div> <div class="field-box"> <input type="text" class="attributes-value" name="attributes_value[{field_name}]" value="{value}" {val_attrs}> <a class="delete-attributes-pair deletelink" href="#" title="{remove}"></a> </div> </div> """.format( key=escape(key), value=escape(value), field_name=field_name, key_attrs=key_attrs, val_attrs=val_attrs, remove=_('Remove'), ) return strip_spaces_between_tags(template.strip())
def testFormGen(self): # Since the secret key cannot be distributed settings.WORLDPAY_MD5_SECRET_KEY = "test" tmpl = Template("{% load billing_tags %}{% world_pay obj %}") form = tmpl.render(Context({"obj": self.wp})) pregen_form = """<form method='post' action='https://select-test.wp3.rbsworldpay.com/wcc/purchase'><input type="hidden" name="futurePayType" value="regular" id="id_futurePayType" /><input type="hidden" name="intervalUnit" value="3" id="id_intervalUnit" /><input type="hidden" name="intervalMult" value="1" id="id_intervalMult" /><input type="hidden" name="option" value="0" id="id_option" /><input type="hidden" name="noOfPayments" value="12" id="id_noOfPayments" /><input type="hidden" name="normalAmount" value="1" id="id_normalAmount" /><input type="hidden" name="startDelayUnit" value="3" id="id_startDelayUnit" /><input type="hidden" name="startDelayMult" value="1" id="id_startDelayMult" /><input type="hidden" name="instId" value="12345" id="id_instId" /><input type="hidden" name="cartId" value="TEST123" id="id_cartId" /><input type="hidden" name="amount" value="1" id="id_amount" /><input type="hidden" name="currency" value="USD" id="id_currency" /><input type="hidden" name="desc" id="id_desc" /><input type="hidden" name="testMode" value="100" id="id_testMode" /><input type="hidden" name="signatureFields" value="instId:amount:cartId" id="id_signatureFields" /><input type="hidden" name="signature" value="75be85df059de95a3407334b7d21273c" id="id_signature" /><input type='submit' value='Pay through WorldPay'/></form>""" self.assertEquals(pregen_form, strip_spaces_between_tags(form).strip())
def offers(doc, categories): queryset = Product.objects.filter(is_visible=True, is_retail=True, quantity__gt=0, price__gt=0) doc.write('<offers>') for product in queryset: try: category_id = product.categories.filter(parent__isnull=False, id__in=categories).values_list('id', flat=True)[0] except IndexError: try: category_id = product.categories.filter(parent__isnull=True, id__in=categories).values_list('id', flat=True)[0] except IndexError: continue text = product.description.strip() if text: text = strip_tags(strip_spaces_between_tags(product.description)).strip() text = typograph(WHITESPACE_RE.sub(' ', text)) truncator = Truncator(text) text = truncator.chars(512) doc.write('<offer>') doc.write('<url>http://tomat-podarky.ru{}</url>'.format(product.get_absolute_url())) doc.write('<price>{}</price>'.format(product.price)) doc.write('<currencyId>RUR</currencyId>') doc.write('<categoryId>{}</categoryId>'.format(category_id)) doc.write('<delivery>true</delivery>') doc.write('<name>') doc.write(_(typograph(product.title))) doc.write('</name>') if text: doc.write('<description>') doc.write(_(text)) doc.write('</description>') doc.write('</offer>\n') doc.write('</offers>')
def compress_html(value, html=1): ssbt = strip_spaces_between_tags(value.strip()) ssbt = RE_MULTISPACE.sub(" ", ssbt) ssbt = RE_NEWLINE.sub("", ssbt) if html == 0: ssbt = striptags( ssbt ) return ssbt
def process_response(self, request, response): if 'text/html' in response['Content-Type'] and settings.COMPRESS_HTML: response.content = strip_spaces_between_tags( response.content.strip()) response.content = RE_MULTISPACE.sub( " ", response.content.decode('utf-8')) response.content = RE_NEWLINE.sub("", response.content.decode('utf-8')) return response
def testFormGen2(self): # Since the secret key cannot be distributed self.wp.add_field("signatureFields", "instId:amount:currency:cartId") self.wp.fields.pop("signature", None) settings.WORLDPAY_MD5_SECRET_KEY = "test" tmpl = Template("{% load billing_tags %}{% world_pay obj %}") form = tmpl.render(Context({"obj": self.wp})) pregen_form = """<form method='post' action='https://select-test.wp3.rbsworldpay.com/wcc/purchase'><input type="hidden" name="futurePayType" value="regular" id="id_futurePayType" /><input type="hidden" name="intervalUnit" value="3" id="id_intervalUnit" /><input type="hidden" name="intervalMult" value="1" id="id_intervalMult" /><input type="hidden" name="option" value="0" id="id_option" /><input type="hidden" name="noOfPayments" value="12" id="id_noOfPayments" /><input type="hidden" name="normalAmount" value="1" id="id_normalAmount" /><input type="hidden" name="startDelayUnit" value="3" id="id_startDelayUnit" /><input type="hidden" name="startDelayMult" value="1" id="id_startDelayMult" /><input type="hidden" name="instId" value="12345" id="id_instId" /><input type="hidden" name="cartId" value="TEST123" id="id_cartId" /><input type="hidden" name="amount" value="1" id="id_amount" /><input type="hidden" name="currency" value="USD" id="id_currency" /><input type="hidden" name="desc" id="id_desc" /><input type="hidden" name="testMode" value="100" id="id_testMode" /><input type="hidden" name="signatureFields" value="instId:amount:currency:cartId" id="id_signatureFields" /><input type="hidden" name="signature" value="9be5c6334f058a294b1ea352634a3dd7" id="id_signature" /><input type='submit' value='Pay through WorldPay'/></form>""" self.assertEquals(pregen_form, strip_spaces_between_tags(form).strip())
def render_real(self, context): html = self.render_actual(context, self.name + '-html') if html: return html md = self.render_actual(context, self.name + '-md') if md: return markdown.markdown(strip_spaces_between_tags(md), extensions=['extra']) return ''
def display_plain_mail_preview(self, obj=None): content_preview = render_to_template_email(obj.html_content.replace( '{{', '{').replace('}}', '}'), {}, is_plain_text=True) return mark_safe( strip_spaces_between_tags( mark_safe(""" <div style='width: 100%;'> <iframe width='97%' height='480px' srcdoc='{mail_message}'>PREVIEW</iframe> <div class='help' style='margin-left:0;padding-left:0'>{help_text}</div> </div> """.format( **{ 'help_text': _('*The field in brackets are variables!'), 'mail_message': escape(strip_spaces_between_tags(content_preview)) }))))
def generate_js_statement(self): js_expr = '' implicit_vars = self.context.get_vars_of_type('implicit') if len(implicit_vars): js_expr += 'var ' + ','.join(implicit_vars) + ';' js_expr += self._nodes_to_js_str(self.js_nodes) rendered_content = self._wrap_expr_in_js_anon_func(js_expr = js_expr, execute_now = False, var_list = self.var_list, show_params = True) rendered_content = strip_spaces_between_tags(rendered_content) return remove_whitespaces.sub(' ', rendered_content.strip())
def test_heading_6(self): t = template.Template(""" {% load html_tags %} {% heading 1 "Test" %} {% heading 1 "Test" %} """) c = template.Context() o = html.strip_spaces_between_tags(t.render(c).strip()) self.assertEquals(o, """<h1 id="test" class="heading ">Test</h1><h1 id="test-0" class="heading ">Test</h1>""")
def middleware(request): response = get_response(request) if 'text/html' in response.get('Content-Type', []) and settings.COMPRESS_HTML: response.content = strip_spaces_between_tags(response.content) # response.content = RE_MULTISPACE.sub(" ", force_text(response.content)) # response.content = RE_NEWLINE.sub("", force_text(response.content)) response['Content-Length'] = len(response.content) return response
def render(self, context): from django.utils.html import strip_spaces_between_tags ret = strip_spaces_between_tags(self.nodelist.render(context).strip()) ret = ret.replace("\n", ' ') while ret.find(' ') >= 0: ret = ret.replace(" ", ' ') # (!) si retours à la ligne dans les templates "toto ," -> "toto," ret = ret.replace(" ,", ',') ret = ret.replace(" .", '.') return ret
def get_infowindow(self): if not self.infowindow or not settings.DJANGOCMS_GMAPS_INFOWINDOW_ENABLED: return '' rendered_infowindow = render_to_string( settings.DJANGOCMS_GMAPS_INFOWINDOW_TEMPLATE, {'location': self, }) return { 'content': strip_spaces_between_tags(rendered_infowindow.strip()), 'maxWidth': settings.DJANGOCMS_GMAPS_INFOWINDOW_MAXWIDTH }
def clean(self, x): """ Before counting the number of characters in the text submitted by CKEditor: - normalize it, - unescape it, - strip all HTML tags, - strip useless spaces. """ x = normalize("NFKC", unescape(x)) x = strip_tags(strip_spaces_between_tags(x)) return len(x)
def process_response(self, request, response): try: compress_html = settings.COMPRESS_HTML except AttributeError: compress_html = False if RESPONSE_CONTENT_TYPE in response\ and TYPE_HTML in response[RESPONSE_CONTENT_TYPE] and compress_html: response.content = strip_spaces_between_tags( response.content.strip()) return response
def render_template(cls, ctx): ctx = cls.update_context(ctx) obj = ctx.get('object') ctx.update({ 'class_name': cls.name, 'context_class_name': obj.__class__.__name__.lower(), }) s = render_to_string('olmarkdown/tag/%s.html' % cls.name, ctx).strip() # clean up output s = html.strip_spaces_between_tags(s) return s
def process_response(self, request, response): if 'text/html' in response['Content-Type']: content = response.content if not settings.DEBUG: # Remove spaces content = strip_spaces_between_tags(content) # Remove HTML comments exp = re.compile('\<![ \r\n\t]*(-- ([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>') content = exp.sub('', content) response.content = content return response
def testFormGen(self): tmpl = Template( "{% load google_checkout from google_checkout_tags %}{% google_checkout obj %}" ) form = tmpl.render(Context({"obj": self.gc})) pregen_form = """<form action="https://sandbox.google.com/checkout/api/checkout/v2/checkout/Merchant/%(mid)s" method="post"><input type="hidden" name="cart" value="PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48Y2hlY2tvdXQtc2hvcHBpbmctY2FydCB4bWxucz0iaHR0cDovL2NoZWNrb3V0Lmdvb2dsZS5jb20vc2NoZW1hLzIiPjxzaG9wcGluZy1jYXJ0PjxpdGVtcz48aXRlbT48aXRlbS1uYW1lPm5hbWUgb2YgdGhlIGl0ZW08L2l0ZW0tbmFtZT48aXRlbS1kZXNjcmlwdGlvbj5JdGVtIGRlc2NyaXB0aW9uPC9pdGVtLWRlc2NyaXB0aW9uPjx1bml0LXByaWNlIGN1cnJlbmN5PSJVU0QiPjE8L3VuaXQtcHJpY2U+PHF1YW50aXR5PjE8L3F1YW50aXR5PjxtZXJjaGFudC1pdGVtLWlkPjk5OUFYWjwvbWVyY2hhbnQtaXRlbS1pZD48L2l0ZW0+PC9pdGVtcz48bWVyY2hhbnQtcHJpdmF0ZS1kYXRhPjwvbWVyY2hhbnQtcHJpdmF0ZS1kYXRhPjwvc2hvcHBpbmctY2FydD48L2NoZWNrb3V0LXNob3BwaW5nLWNhcnQ+" /><input type="hidden" name="signature" value="E+hwrsOjpJYbfy3abEcyCynzurs=" /><input type="image" name="Google Checkout" alt="Fast checkout through Google" src="http://sandbox.google.com/checkout/buttons/checkout.gif?merchant_id=%(mid)s&w=180&h=46&style=white&variant=text&loc=en_US" height="46" width="180" /></form>""" % ( { "mid": settings.MERCHANT_SETTINGS['google_checkout']['MERCHANT_ID'] }) self.assertEquals(pregen_form, strip_spaces_between_tags(form).strip())
def process_response(self, request, response): if 'text/html' in response['Content-Type']: content = response.content if not settings.DEBUG: # Remove spaces content = strip_spaces_between_tags(content) # Remove HTML comments exp = re.compile( '\<![ \r\n\t]*(-- ([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>') content = exp.sub('', content) response.content = content return response
def prince(html, base_url=None, ttl=300, **kwargs): SERVER = getattr(settings, "PRINCE_SERVER", None) BINARY = getattr(settings, "PRINCE_BINARY", "/usr/local/bin/prince") if base_url is None: base_url = getattr(settings, "PRINCE_BASE_URL", None) # When celery and django-tenant-schemas are involved, this get's a bit # weird. This has bitten once in production so lets log it and see else # might cause grief. for kw, arg in kwargs.items(): logger.warn("Unexpected keyword argument: %s=%r", kw, arg) logger.debug("base_url: %s", base_url) logger.debug("ttl: %s", ttl) logger.debug("content-length: %s", len(html)) html = strip_spaces_between_tags(html) logger.debug("content-length-stripped: %s", len(html)) if SERVER: logger.debug("server: %s", SERVER) html = html.encode("utf8") headers = {} if base_url: headers["base_url"] = base_url for key, value in headers.items(): logger.debug("header: %s=%s", key, value) res = requests.post(f"https://{SERVER}/", data=html, headers=headers) res.raise_for_status() out = res.content logger.info("Remote PDF generation via %s complete.", SERVER) else: command = [BINARY, "--input=html"] if base_url: command += ["--baseurl=%s" % base_url] command += ["-"] logger.debug("command: %s", " ".join(command)) p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE) out, err = p.communicate(html.encode("utf8")) logger.info("Local PDF generation complete.") if err: logger.error(err) return out
def _clean(text, as_tooltip): """ Helper to prepare the help text """ if not text: return None js_encoded = strip_spaces_between_tags(text.strip()) if as_tooltip: js_encoded = escape(js_encoded) js_encoded = re.sub(r'\r\n|\r|\n', '', js_encoded) js_encoded = re.sub(r'\"', "'", js_encoded) js_encoded = js_encoded.replace('"', '\"').replace( "&", '\&').replace("'", '\'') return mark_safe(js_encoded)
def _render_string_to_string(self, template_code, context): """ Renders template code to a string and removes some whitespaces, so that the comparison between theoric output and effective one can be compared with more flexibility. This fits HTML needs. You may not use it for any "whitespace sensitive" code. """ t = Template(template_code) c = Context(context) return strip_spaces_between_tags(t.render(c)).strip()
def mail_preview(self, obj=None): content_preview = add_style_inline(obj.content, POSTOFFICE_TAGS_STYLES) content_preview = content_preview.replace('{{', '{').replace('}}', '}') context = {} content_preview = POSTOFFICE_TEMPLATE_LIBS_TO_LOAD + content_preview content_preview = render_to_temporary_file(content_preview, context) context.update({'content': content_preview}) html_content_preview = render_to_temporary_file( obj.html_content, context) help_text = '<div class="help">%s</div>' % ( _('*I dati in questa preview sono fittizzi e del tutto casuali')) return strip_spaces_between_tags( mark_safe( "{help_text}<div style='width:860px; height:500px;'><iframe style='margin-left:107px;' width='97%' height='480px' srcdoc='{mail_message}'>PREVIEW</iframe></div>\ ".format( **{ 'help_text': help_text, 'mail_message': escape(strip_spaces_between_tags(html_content_preview)) })))
def test_unbound_form(): product_form = ProductForm() assert product_form.is_bound is False expected = BeautifulSoup( strip_spaces_between_tags(""" <li><label for="id_active">Active:</label> <input type="checkbox" name="active" required id="id_active"></li> <li><label for="id_name">Name:</label> <input type="text" name="name" required id="id_name"></li> <li><label for="id_tenant">Tenant:</label> <select name="tenant" id="id_tenant"> <option value="1">John</option> <option value="2">Mary</option> </select></li> <li><label for="id_description">Description:</label> <textarea name="description" cols="40" rows="10" id="id_description"> </textarea></li> <li><label for="id_categories">Categories:</label> <select name="categories" id="id_categories" multiple> <option value="1">Paraphernalia</option> <option value="2">Detergents</option> </select></li>"""), features='lxml', ) assert BeautifulSoup(strip_spaces_between_tags(product_form.as_ul()), features='lxml') == expected
def test_can_render_a_simple_tree_of_cms_pages(self): # Setup placeholder = Placeholder.objects.create(slot='test') model_instance = add_plugin( placeholder, cms_plugins.HtmlSitemapPlugin, 'en', ) # Run html = self.render_plugin(model_instance) html = strip_spaces_between_tags(html) # Check assert html.strip() == strip_spaces_between_tags(""" <div id="sitemap"> <ul> <li> <a href="/" title="Index">Index</a> <ul> <li><a href="/depth-2-page-1/" title="Depth 2 page 1">Depth 2 page 1</a></li> <li> <a href="/depth-2-page-2/" title="Depth 2 page 2">Depth 2 page 2</a> <ul> <li><a href="/depth-2-page-2/depth-3-page-1/" title="Depth 3 page 1">Depth 3 page 1</a></li> <li><a href="/depth-2-page-2/depth-3-page-2/" title="Depth 3 page 2">Depth 3 page 2</a></li> </ul> </li> <li><a href="/depth-2-page-3/" title="Depth 2 page 3">Depth 2 page 3</a></li> <li> <a href="/depth-2-page-4/" title="Depth 2 page 4">Depth 2 page 4</a> <ul> <li><a href="/depth-2-page-4/depth-3-page-3/" title="Depth 3 page 3">Depth 3 page 3</a></li> </ul> </li> </ul> </li> </ul> </div> """).strip()
def generate_js_statement(self): js_expr = '' implicit_vars = self.context.get_vars_of_type('implicit') if len(implicit_vars): js_expr += 'var ' + ','.join(implicit_vars) + ';' js_expr += self._nodes_to_js_str(self.js_nodes) rendered_content = self._wrap_expr_in_js_anon_func( js_expr=js_expr, execute_now=False, var_list=self.var_list, show_params=True) rendered_content = strip_spaces_between_tags(rendered_content) return remove_whitespaces.sub(' ', rendered_content.strip())
def test_can_render_sitemap_in_other_language(self): create_title('fr', 'Index fr', self.index_page) create_title('fr', 'Niveau 2 Page 1', self.depth2_page1) publish_page(self.index_page, self.user, 'fr') publish_page(self.depth2_page1, self.user, 'fr') activate('fr') self.request.LANGUAGE_CODE = 'fr' # Setup placeholder = Placeholder.objects.create(slot='test') model_instance = add_plugin( placeholder, cms_plugins.HtmlSitemapPlugin, 'fr', in_navigation=True, ) # Run html = self.render_plugin(model_instance) html = strip_spaces_between_tags(html) # Check assert html.strip() == strip_spaces_between_tags(""" <div id="sitemap"> <ul> <li> <a href="/" title="Index fr">Index fr</a> <ul> <li> <a href="/niveau-2-page-1/" title="Niveau 2 Page 1">Niveau 2 Page 1</a> </li> </ul> </li> </ul> </div> """).strip()
def m_record_edit(request): if request.method == 'POST': MetalId = json.loads(request.body.decode('utf-8'))['pkey'] bform = MetalsModelForm( instance = models.MetalsTable.objects.get(pk=MetalId) ) template = loader.get_template('djangoform_for_modal.html') return JsonResponse( { 'result':'editform', 'pkey': MetalId, 'update_url': reverse('metals:metals-record-update'), 'form': mark_safe( strip_spaces_between_tags( template.render({'form': bform}))), } ) else: # method not POST return JsonResponse({'result': 'nopost'})
def render_html(self, prefix, product, postfix): """ Render a HTML snippet to be stored inside the index database. """ app_label = product._meta.app_label.lower() params = [ (app_label, prefix, product.product_model, postfix), (app_label, prefix, 'product', postfix), ('shop', prefix, 'product', postfix), ] template = select_template(['{0}/products/{1}-{2}-{3}.html'.format(*p) for p in params]) context = Context({'product': product}) content = strip_spaces_between_tags(template.render(context).strip()) return mark_safe(content)
def testFormGen(self): p24 = Privat24Integration() fields = { "amt": "20.00", "ccy": "UAH", "order": "222", "details": "Сплата за питання: Короткий змист тестового питання", } p24.add_fields(fields) tmpl = Template("{% load privat24_form from privat24_tags %}{% privat24_form obj %}") form = tmpl.render(Context({"obj": p24})) pregen_form = u"""<form method="post" action="https://api.privatbank.ua/p24api/ishop"><p><label for="id_amt">Amt:</label><input id="id_amt" name="amt" type="text" value="20.00" /></p><p><label for="id_ccy">Ccy:</label><input id="id_ccy" name="ccy" type="text" value="UAH" /><input id="id_merchant" name="merchant" type="hidden" value="100000" /><input id="id_order" name="order" type="hidden" value="222" /><input id="id_details" name="details" type="hidden" value="\u0421\u043f\u043b\u0430\u0442\u0430 \u0437\u0430 \u043f\u0438\u0442\u0430\u043d\u043d\u044f: \u041a\u043e\u0440\u043e\u0442\u043a\u0438\u0439 \u0437\u043c\u0438\u0441\u0442 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u043f\u0438\u0442\u0430\u043d\u043d\u044f" /><input id="id_pay_way" name="pay_way" type="hidden" value="privat24" /><input id="id_return_url" name="return_url" type="hidden" value="http://example.com/payments/" /><input id="id_server_url" name="server_url" type="hidden" value="http://example.com/p24/notify-handler/" /><input id="id_ext_details" name="ext_details" type="hidden" /></p></form>""" self.assertEquals(pregen_form, strip_spaces_between_tags(form).strip())
def process_response( self, request, response ): x_forwarded_for = request.META.get( 'HTTP_X_FORWARDED_FOR' ) if x_forwarded_for: ip = x_forwarded_for.split( ',' )[0] else: ip = request.META.get( 'REMOTE_ADDR' ) AcilanSayfa = request.build_absolute_uri( ) # logging.info("\n\n") # logging.info(ip == "88.250.159.71") # logging.info("online-satin-al" in AcilanSayfa) # logging.info(not request.user.is_authenticated( )) if "yeni-urunler" in AcilanSayfa and not request.user.is_authenticated( ) and ip == "88.250.159.71": try: obj = CeM.objects.get( url = AcilanSayfa ).htmldosyasi logging.info( AcilanSayfa + " zaten vardı" ) # cont = strip_spaces_between_tags( obj.icerik ) # icerik = re.sub( r'^\s+<', '<', cont ) return render_to_response( 'cachem/' + obj + '.html', context_instance = RequestContext( request ) ) # return HttpResponse( icerik ) except CeM.DoesNotExist: htmldosya = str( ''.join( random.choice( string.ascii_lowercase ) for i in range( 12 ) ) ) obj = CeM( url = AcilanSayfa, icerik = response.content, htmldosyasi = htmldosya ) obj.save( ) logging.info( AcilanSayfa + " yeni kayıt açıldı" ) cont = strip_spaces_between_tags( response.content ) response.content = re.sub( r'^\s+<', '<', cont ) file = open( '/home/muslu/django/teslabb/templates/cachem/' + htmldosya + '.html', 'w+' ) # fiziksel yolu sonra değiştirmek lazım gibi file.write( response.content ) file.close( ) return response return response
def render_html(self, prefix, product, postfix): """ Render a HTML snippet to be stored inside the index database, so that rendering of the product's list views can be performed without database queries. """ app_label = product._meta.app_label.lower() params = [ (app_label, prefix, product.product_model, postfix), (app_label, prefix, 'product', postfix), ('shop', prefix, 'product', postfix), ] template = select_template(['{0}/products/{1}-{2}-{3}.html'.format(*p) for p in params]) context = {'product': product} content = strip_spaces_between_tags(template.render(context).strip()) return mark_safe(content)
def strip_tags(value): value = strip_spaces_between_tags(value) # compact all white spaces, except r'\n' value = re.sub(r'[ \t\r\f\v]+', ' ', force_text(value)) old_value = None while value != old_value: old_value = value value = re.sub( r'<(?P<pre>[^>]+)\s' # <...\s r'(?P<attr>[\w\-]+)=[\'"](?P<value>[\w\-\.:]+)[\'"]' # a="b" r'(?P<post>[^<]*)>', # ...> '<\g<pre> \g<attr>=\g<value>\g<post>>', # <...\s a=b..,> value ) return value
def simple_minify(html): """ Minify HTML with very simple algorithm. This function tries to minify HTML by stripping all spaces between all html tags (e.g. ``</div> <div>`` -> ``</div><div>``). This step is accomplished by using Django's ``strip_spaces_between_tags`` method. In addition to that, this function replaces all whitespace (more then two consecutive whitespace characters or new line) with a space character except inside excluded tags such as ``pre`` or ``textarea``. **Though process**: To minify everything except content of excluded tags in one step requires very complex regular expression. The disadvantage is the regular expression will involve look-behinds and look-aheads. Those operations make regex much more resource-hungry which eats precious server resources. In addition, complex regex are hard to understand and can be hard to maintain. That is why this function splits the task into multiple sections. #. Regex expression which matches all exclude tags within the html is used to split the HTML split into components. Since the regex expression is wrapped inside a group, the content of the exclude tags is also included inside the resulting split list. Due to that it is guaranteed that every odd element (if there are any) will be the excluded tags. #. All the split components are looped and processed in order to construct final minified HTML. #. All odd indexed elements are not processed and are simply appended to final HTML since as explained above, they are guaranteed to be content of excluded tags hence do not require minification. #. All even indexed elements are minified by stripping whitespace between tags by using Django's ``strip_spaces_between_tags`` and redundant whitespace is stripped in general via simple regex. You can notice that the process does not involve parsing HTML since that usually adds some overhead (e.g. using beautiful soup). By using 2 regex passes this achieves very similar result which performs much better. """ components = RE_EXCLUDE_TAGS.split(html) html = '' for i, component in enumerate(components): if i % 2 == 0: component = strip_spaces_between_tags(component.strip()) component = RE_WHITESPACE.sub(' ', component) html += component else: html += component return html
def testFormGen(self): p24 = Privat24Integration() fields = { "amt": "20.00", "ccy": "UAH", "order": "222", "details": "Сплата за питання: Короткий змист тестового питання", } p24.add_fields(fields) tmpl = Template( "{% load privat24_form from privat24_tags %}{% privat24_form obj %}" ) form = tmpl.render(Context({"obj": p24})) pregen_form = u"""<form method="post" action="https://api.privatbank.ua/p24api/ishop"><p><label for="id_amt">Amt:</label><input id="id_amt" name="amt" type="text" value="20.00" /></p><p><label for="id_ccy">Ccy:</label><input id="id_ccy" name="ccy" type="text" value="UAH" /><input id="id_merchant" name="merchant" type="hidden" value="100000" /><input id="id_order" name="order" type="hidden" value="222" /><input id="id_details" name="details" type="hidden" value="\u0421\u043f\u043b\u0430\u0442\u0430 \u0437\u0430 \u043f\u0438\u0442\u0430\u043d\u043d\u044f: \u041a\u043e\u0440\u043e\u0442\u043a\u0438\u0439 \u0437\u043c\u0438\u0441\u0442 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u043f\u0438\u0442\u0430\u043d\u043d\u044f" /><input id="id_pay_way" name="pay_way" type="hidden" value="privat24" /><input id="id_return_url" name="return_url" type="hidden" value="http://example.com/payments/" /><input id="id_server_url" name="server_url" type="hidden" value="http://example.com/p24/notify-handler/" /><input id="id_ext_details" name="ext_details" type="hidden" /></p></form>""" self.assertEquals(pregen_form, strip_spaces_between_tags(form).strip())
def render_html(self, prefix, product, postfix): """ Render a HTML snippet to be stored inside the index database. """ app_label = product._meta.app_label.lower() product_type = product.__class__.__name__.lower() params = [ (app_label, prefix, product_type, postfix), (app_label, prefix, 'product', postfix), ('shop', prefix, 'product', postfix), ] template = select_template( ['{0}/products/{1}-{2}-{3}.html'.format(*p) for p in params]) context = Context({'product': product}) content = strip_spaces_between_tags(template.render(context).strip()) return mark_safe(content)
def get_context_data(self, **kwargs): context = super(ProjectDetail, self).get_context_data(**kwargs) context.update({ 'site_title': 'Ariano Ângelo', 'site_description': format(self.object.name), 'seo_description': strip_spaces_between_tags(strip_tags(self.object.text)), 'seo_image': self.object.portfolio_image.url, 'seo_article': True }) return context
def excerpt_with_ptag_spacing(value, arg): try: limit = int(arg) except ValueError: return 'Invalid literal for int().' # remove spaces between tags value = strip_spaces_between_tags(value) # add space before each P end tag (</p>) value = value.replace("</p>"," </p>") value = value.replace("""," ") # strip HTML tags value = strip_tags(value) # other usage: return Truncator(value).words(length, html=True, truncate=' see more') return Truncator(value).words(limit)
def form_valid(self, form): if self.request.is_ajax(): self.object = form.save() context = {} if self.json_template_name: json_context = self.get_json_context_data( **{self.get_context_object_name(self.object): self.object}) html = render_to_string(template_name=self.json_template_name, dictionary=json_context) context['html'] = strip_spaces_between_tags(html.strip()) else: context.update(self.get_json_context_data()) return JSONResponseMixin.render_to_response(self, context=context) return super(JSONHybridProcessFormViewMixin, self).form_valid(form)
def xmlrpc(request): if request.POST: redirect = request.POST.get('redirect_to') if redirect: return HttpResponseRedirect(redirect) output = dispatcher.run(request.raw_post_data) host = request.get_host() domain = Site.objects.get_current().domain if host != domain: for protocol in ('http', 'https'): output = output.replace( '%s://%s/' % (protocol, domain), '%s://%s/' % (protocol, host), ) response = HttpResponse(mimetype='application/xml') response.write(strip_spaces_between_tags(output)) return response methods = {} for method_name in dispatcher.system_listMethods(): namespace = method_name.split('.')[0] methods.setdefault(namespace, SortedDict()) methods[namespace][method_name] = get_method_info( dispatcher.funcs[method_name]) method_lists = [] for namespace in sorted(methods.keys()): method_lists.append(( namespace, NAMESPACE_TITLES.get(namespace, namespace), methods[namespace].items(), )) return render_to_response( template_name='xmlrpc/xmlrpc.html', dictionary={ 'method_lists': method_lists, }, context_instance=RequestContext(request), )
def form_valid(self, form): if self.request.is_ajax(): self.object = form.save() context = {} if self.json_template_name: json_context = self.get_json_context_data(**{ self.get_context_object_name(self.object): self.object }) html = render_to_string(template_name=self.json_template_name, dictionary=json_context) context['html'] = strip_spaces_between_tags(html.strip()) else: context.update(self.get_json_context_data()) return JSONResponseMixin.render_to_response(self, context=context) return super(JSONHybridProcessFormViewMixin, self).form_valid(form)
def xmlrpc(request): if request.POST: redirect = request.POST.get('redirect_to') if redirect: return HttpResponseRedirect(redirect) output = dispatcher.run(request.raw_post_data) host = request.get_host() domain = Site.objects.get_current().domain if host != domain: for protocol in ('http', 'https'): output = output.replace( '%s://%s/' % (protocol, domain), '%s://%s/' % (protocol, host), ) response = HttpResponse(mimetype='application/xml') response.write(strip_spaces_between_tags(output)) return response methods = {} for method_name in dispatcher.system_listMethods(): namespace = method_name.split('.')[0] methods.setdefault(namespace, SortedDict()) methods[namespace][method_name] = get_method_info(dispatcher.funcs[method_name]) method_lists = [] for namespace in sorted(methods.keys()): method_lists.append(( namespace, NAMESPACE_TITLES.get(namespace, namespace), methods[namespace].items(), )) return render_to_response( template_name='xmlrpc/xmlrpc.html', dictionary={ 'method_lists': method_lists, }, context_instance=RequestContext(request), )
def render_html(self, product, postfix): """ Return a HTML snippet containing a rendered summary for the given product. This HTML snippet typically contains a ``<figure>`` element with a sample image ``<img src="..." >`` and a ``<figcaption>`` containing a short description of the product. Build a template search path with `postfix` distinction. """ if not self.label: msg = "The Product Serializer must be configured using a `label` field." raise exceptions.ImproperlyConfigured(msg) app_label = product._meta.app_label.lower() request = self.context['request'] cache_key = 'product:{0}|{1}-{2}-{3}-{4}-{5}'.format( product.id, app_label, self.label, product.product_model, postfix, get_language_from_request(request)) content = cache.get(cache_key) if content: return mark_safe(content) params = [ (app_label, self.label, product.product_model, postfix), (app_label, self.label, 'product', postfix), ('shop', self.label, product.product_model, postfix), ('shop', self.label, 'product', postfix), ] try: template = select_template( ['{0}/products/{1}-{2}-{3}.html'.format(*p) for p in params]) except TemplateDoesNotExist: return SafeText( "<!-- no such template: '{0}/products/{1}-{2}-{3}.html' -->". format(*params[0])) # when rendering emails, we require an absolute URI, so that media can be accessed from # the mail client absolute_base_uri = request.build_absolute_uri('/').rstrip('/') context = {'product': product, 'ABSOLUTE_BASE_URI': absolute_base_uri} content = strip_spaces_between_tags( template.render(context, request).strip()) cache.set(cache_key, content, app_settings.CACHE_DURATIONS['product_html_snippet']) return mark_safe(content)
def _render_photo(photo, params): sizes = PhotoSizeCache().sizes size = 'thumbnail' rel = None for param in params: if param in sizes.keys(): size = param params.remove(param) if params: rel = params[0] thumbnail_url = getattr(photo, 'get_%s_url' % size)() if photo.caption: caption = '<span class="caption">%s</span>' % photo.caption else: caption = '' display_size = sizes['display'] im = Image.open(photo.image.path) if im.size[0] > display_size.size[0] or im.size[1] > display_size.size[1]: full_url = photo.image.url full_specs = '%sx%s' % im.size display_url = photo.get_display_url() else: full_url = photo.get_display_url() display_url = full_specs = '' kw = { 'display_url': display_url, 'full_url': full_url, 'thumbnail_url': thumbnail_url, 'title': photo.title, 'full_specs': full_specs, 'rel': rel, 'class': '', 'caption': caption, } tag = PHOTO_TAG % kw return strip_spaces_between_tags(tag)
def validate_and_load( self, xml_raw ): """ Validate XML content :param: xml_raw :return: Mixed - False on failure, lxml root on success """ # Removes space in the beginning and end xml_raw = xml_raw.strip() # Tests if it's empty if xml_raw == '': self.error_message = 'The content was empty' return False # Checks if the xml came URL encoded if xml_raw[:5] == 'data=': url_encoded = xml_raw.split( '=', 1 ) xml_raw = url_encoded[1] # Removes empty lines and spaces between tags xml = re.sub( r'\r|\n|\t|\b|\v', '', xml_raw ) xml = strip_spaces_between_tags( xml ) # Tests if it starts with a tag if xml == '' or xml[0] != '<': self.error_message = 'Invalid XML - Missing starting tag' return False # Tries to parse with lxml try: parser = etree.XMLParser( remove_blank_text = True ) xml_root = etree.fromstring( codecs.encode( xml, 'utf-8' ), parser ) self.xml_root = self.cleanup( xml_root ) return True except etree.XMLSyntaxError as error: self.error_message = "Malformed xml (" + force_str( error ) + ")" return False
def process_response(self, request, response): """ Method that minimize the HTML code first the spaces between tags are stripped, then the newlines are converted to singles spaces, and finally the multispaces are converted to a single space Arguments: - `request`: - `response`: content of the pages that are minized if the type of content in the response is text/html """ if settings.DEBUG: return response if 'text/html' in response['Content-Type'] and settings.COMPRESS_HTML: response.content = strip_spaces_between_tags( response.content.strip()) response.content = RE_NEWLINE.sub(" ", response.content) response.content = RE_MULTISPACE.sub(" ", response.content) response.content = RE_SPACETAG1.sub(">", response.content) response.content = RE_SPACETAG2.sub("<", response.content) return response