From dc13555b230faca78afc00aa02e458a698c4d7f1 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Fri, 7 Feb 2014 20:27:48 +0530 Subject: [PATCH] Prepend / in Relative URLs in the whole HTML document --- webnotes/__init__.py | 3 +-- webnotes/templates/base.html | 8 ++++---- webnotes/templates/generators/web_page.html | 18 ++++-------------- webnotes/templates/generators/web_page.py | 2 +- webnotes/templates/includes/blogger.html | 2 +- webnotes/templates/includes/breadcrumbs.html | 2 +- webnotes/templates/includes/footer.html | 2 +- webnotes/templates/includes/inline_post.html | 4 ++-- webnotes/templates/includes/navbar.html | 6 +++--- webnotes/templates/includes/post_editor.html | 2 +- webnotes/templates/includes/slideshow.html | 2 +- webnotes/templates/pages/app.html | 4 ++-- webnotes/utils/__init__.py | 5 ----- webnotes/website/css/website.css | 1 + webnotes/webutils.py | 9 +++++++-- 15 files changed, 30 insertions(+), 40 deletions(-) diff --git a/webnotes/__init__.py b/webnotes/__init__.py index ba32c471f5..10fe74690a 100644 --- a/webnotes/__init__.py +++ b/webnotes/__init__.py @@ -570,7 +570,7 @@ def get_jenv(): return jenv def set_filters(jenv): - from webnotes.utils import global_date_format, scrub_relative_url + from webnotes.utils import global_date_format from webnotes.webutils import get_hex_shade from markdown2 import markdown from json import dumps @@ -578,7 +578,6 @@ def set_filters(jenv): jenv.filters["global_date_format"] = global_date_format jenv.filters["markdown"] = markdown jenv.filters["json"] = dumps - jenv.filters["scrub_relative_url"] = scrub_relative_url jenv.filters["get_hex_shade"] = get_hex_shade # load jenv_filters from hooks.txt diff --git a/webnotes/templates/base.html b/webnotes/templates/base.html index e61e5e4d47..2014fa00e7 100644 --- a/webnotes/templates/base.html +++ b/webnotes/templates/base.html @@ -7,8 +7,8 @@ - - + + {% block head -%} {%- if meta_description is defined -%} @@ -16,11 +16,11 @@ {%- endif -%} {%- for link in web_include_js -%} - + {%- endfor -%} {%- for link in web_include_css -%} - + {%- endfor -%} {% block script -%} diff --git a/webnotes/templates/generators/web_page.html b/webnotes/templates/generators/web_page.html index b406590b59..929d7c9cb7 100644 --- a/webnotes/templates/generators/web_page.html +++ b/webnotes/templates/generators/web_page.html @@ -2,27 +2,18 @@ {% block header %}{% if show_title %}

{{ title }}

{% endif %}{% endblock %} +{% block breadcrumbs %}{% include "templates/includes/breadcrumbs.html" %}{% endblock %} + {% block content %}
{# title, breadcrumbs, table of contents #} - {% block pre_content -%} - {% if show_breadcrumbs and breadcrumbs -%} - - {%- endif %} - {%- endblock %} - + {% include "templates/includes/slideshow.html" %}
{{ main_section or "" }}
{# toc, parent, child, next sibling #} - {% block post_content -%} {% if show_toc and toc_list -%}

Contents

@@ -53,7 +44,6 @@

Discuss

{% include 'templates/includes/comments.html' %} {%- endif %} - {%- endblock %}
{% for include in include_js -%} - + {%- endfor -%} \ No newline at end of file diff --git a/webnotes/utils/__init__.py b/webnotes/utils/__init__.py index ac5e7d7987..b838bceb62 100644 --- a/webnotes/utils/__init__.py +++ b/webnotes/utils/__init__.py @@ -842,11 +842,6 @@ def get_url_to_form(doctype, name, base_url=None, label=None): return """%(label)s""" % locals() -def scrub_relative_url(url): - if not url or url.startswith("http"): - return url - return "/" + url - def encode_dict(d, encoding="utf-8"): for key in d: if isinstance(d[key], basestring) and isinstance(d[key], unicode): diff --git a/webnotes/website/css/website.css b/webnotes/website/css/website.css index 2b78e09644..09696b671b 100644 --- a/webnotes/website/css/website.css +++ b/webnotes/website/css/website.css @@ -220,6 +220,7 @@ fieldset { .page-container { margin-top: 15px; + padding-bottom: 30px; } .page-header h1, diff --git a/webnotes/webutils.py b/webnotes/webutils.py index 7b0840375a..0de86d7755 100644 --- a/webnotes/webutils.py +++ b/webnotes/webutils.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals import webnotes -import json, os, time +import json, os, time, re from webnotes import _ import webnotes.utils from webnotes.utils import get_request_site_address, encode, cint @@ -76,6 +76,7 @@ def build_page(page_name): context = get_context(page_name) html = webnotes.get_template(context.base_template_path).render(context) + html = scrub_relative_urls(html) if can_cache(context.no_cache): webnotes.cache().set_value("page:" + page_name, html) @@ -430,6 +431,10 @@ def render_blocks(context): # render each block individually for block, render in template.blocks.items(): - out[block] = concat(render(context)) + out[block] = scrub_relative_urls(concat(render(context))) return out + +def scrub_relative_urls(html): + """prepend a slash before a relative url""" + return re.sub("""(src|href)[^\w'"]*['"](?!http|ftp|/|#)([^'" >]+)['"]""", '\g<1> = "/\g<2>"', html)