Commits

David Chambers  committed 4a27f31

Added `Script` and `StyleSheet` classes for even greater convenience. :)

  • Participants
  • Parent commits 028f872

Comments (0)

Files changed (8)

         MARKDOWN_DATE_FORMAT, MARKDOWN_EXTENSIONS, MARKDOWN_TIME_FORMAT,
         POST_CACHE_SECONDS, PROJECT_PATH, SHORT_URL_BASE, STATIC_FILES,
         TIME_ZONES)
+from mango.template import Script, StyleSheet
 from mango.templatetags.mango_extras import _convert, convert_html_chars
 from mango.utils import canonicalize, logger, replace, slugify
 
         return urlpaths
 
     def scripts(self):
-        return [path for path in self.required_files() if path.endswith('.js')]
+        return [Script(path) for path in self.required_files() if path.endswith('.js')]
 
     def stylesheets(self):
-        return [path for path in self.required_files() if path.endswith('.css')]
+        return [StyleSheet(path) for path in self.required_files() if path.endswith('.css')]
 
 
 class Category(Resource):
+class Script(object):
+    def __init__(self, src):
+        self.src = src
+
+    def __unicode__(self):
+        return u'<script src="%s"></script>' % self.src
+
+
+class StyleSheet(object):
+    def __init__(self, href, media=None):
+        self.href = href
+        self.media = media
+
+    def __unicode__(self):
+        if self.media:
+            return u'<link rel="stylesheet" href="%s" media="%s" />' % (self.href, self.media)
+        else:
+            return u'<link rel="stylesheet" href="%s" />' % self.href

File templates/_base.dhtml

     <meta charset="utf-8" />
     <title>{% block title %}{{ SITE_TITLE }}{% endblock %}</title>{% block feeds %}
     <link rel="alternate" type="application/atom+xml" href="{% url mango.feeds.atom %}" />{% endblock %}{% for stylesheet in stylesheets %}
-    <link rel="stylesheet" href="{{ stylesheet.href }}" media="{{ stylesheet.media }}" />{% endfor %}
+    {{ stylesheet|safe }}{% endfor %}
     <!--[if lt IE 9]>
     <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
     <![endif]-->{% endblock %}
     <footer id="footer">{% block footer %}
         <p>Powered by <a href="http://mango.io/">Mango</a></p>{% endblock %}
     </footer>{% block scripts %}{% for script in scripts %}
-    <script src="{{ script }}"></script>{% endfor %}
+    {{ script|safe }}{% endfor %}
     <script type="text/javascript">
         (function () {
 

File templates/category.dhtml

 {% extends "base.dhtml" %}
 {% load mango_extras %}
 {% block title %}{{ category.name|smart_capfirst }}{% endblock %}
-{% block stylesheets %}{{ block.super }}{% for href in category.pages|combine:category.posts|required:"stylesheets" %}
-    <link rel="stylesheet" href="{{ href }}" />{% endfor %}{% endblock %}
+{% block stylesheets %}{{ block.super }}{% for stylesheet in category.pages|combine:category.posts|required:"stylesheets" %}
+    {{ stylesheet|safe }}{% endfor %}{% endblock %}
 {% block content %}
             <h1>{{ category.name|smart_capfirst }}</h1>
             {% if category.pages %}
             </ol>{% endif %}{% if not category.pages and not category.posts and not category.subcategories %}
             <p>This category is currently empty.</p>{% endif %}
 {% endblock %}
-{% block scripts %}{{ block.super }}{% for src in category.pages|combine:category.posts|required:"scripts" %}
-    <script src="{{ src }}" />{% endfor %}{% endblock %}
+{% block scripts %}{{ block.super }}{% for script in category.pages|combine:category.posts|required:"scripts" %}
+    {{ script|safe }}{% endfor %}{% endblock %}

File templates/document.dhtml

 {% load mango_extras %}
 {% block title %}{{ document.title_text }}{% endblock %}
 {% block head %}
-    {{ block.super }}{% for href in document.stylesheets %}
-    <link rel="stylesheet" href="{{ href }}" />{% endfor %}{% endblock %}
+    {{ block.super }}{% for stylesheet in document.stylesheets %}
+    {{ stylesheet|safe }}{% endfor %}{% endblock %}
 {% block content %}
                 <article>
                     <header>
                 </article>
 {% endblock %}
 {% block scripts %}
-    {{ block.super }}{% for src in document.scripts %}
-    <script src="{{ src }}"></script>{% endfor %}{% endblock %}
+    {{ block.super }}{% for script in document.scripts %}
+    {{ script|safe }}{% endfor %}{% endblock %}

File templates/index.dhtml

 {% extends "base.dhtml" %}
 {% load mango_extras %}
-{% block stylesheets %}{{ block.super }}{% for href in posts|slice:":5"|required:"stylesheets" %}
-    <link rel="stylesheet" href="{{ href }}" />{% endfor %}{% endblock %}
+{% block stylesheets %}{{ block.super }}{% for stylesheet in posts|slice:":5"|required:"stylesheets" %}
+    {{ stylesheet|safe }}{% endfor %}{% endblock %}
 {% block header_title %}<h1>{{ SITE_TITLE }}</h1>{% endblock %}
 {% block content %}{% for document in posts|slice:":5" %}{% include "excerpt.dhtml" %}{% empty %}
             <h2>Welcome to your new blog</h2>
             <p>It's lookin' a bit empty, though. Get to work!</p>{% endfor %}{% if posts|length > 5 %}
             <h2>Want more?</h2>
             <p>Check out the <a href="{% url mango.views.archives %}">archives</a>.</p>{% endif %}{% endblock %}
-{% block scripts %}{{ block.super }}{% for src in posts|slice:":5"|required:"scripts" %}
-    <script src="{{ src }}"></script>{% endfor %}{% endblock %}
+{% block scripts %}{{ block.super }}{% for script in posts|slice:":5"|required:"scripts" %}
+    {{ script|safe }}{% endfor %}{% endblock %}

File templates/tag.dhtml

 {% load mango_extras %}
 {% block title %}Posts tagged "{{ tag }}"{% endblock %}{% block feeds %}{{ block.super }}
     <link rel="alternate" type="application/atom+xml" href="{% url mango.feeds.atom tag %}" />{% endblock %}
-{% block stylesheets %}{{ block.super }}{% for href in documents|required:"stylesheets" %}
-    <link rel="stylesheet" href="{{ href }}" />{% endfor %}{% endblock %}
+{% block stylesheets %}{{ block.super }}{% for stylesheet in documents|required:"stylesheets" %}
+    {{ stylesheet|safe }}{% endfor %}{% endblock %}
 {% block content %}
             <h1>Posts tagged "{{ tag }}"</h1>{% for document in documents %}{% include "excerpt.dhtml" %}{% empty %}
             <p>There are currently no posts tagged "{{ tag }}".</p>{% endfor %}
 {% endblock %}
-{% block scripts %}{{ block.super }}{% for src in documents|required:"scripts" %}
-    <script src="{{ src }}"></script>{% endfor %}{% endblock %}
+{% block scripts %}{{ block.super }}{% for script in documents|required:"scripts" %}
+    {{ script|safe }}{% endfor %}{% endblock %}
 from mango.exceptions import EmptySettingError, InvalidSettingError
 from mango.settings import (AKISMET_API_KEY, BASE_URL, CSS, DOCUMENTS_PATH,
         JS, MANGO_PATH, PROJECT_PATH, REPLACEMENTS)
+from mango.template import Script, StyleSheet
 
 RE = {
     'fragment': re.compile(r'(?s)(<code>.*?</code>|<pre>.*?</pre>|<skip>.*?</skip>)'),
             scripts_root = prefix = JS[0]
             if prefix and not urlparse(prefix).scheme and not prefix.startswith('/'):
                 scripts_root = '%s%s' % (reverse('mango.views.index'), prefix)
-            _.cache = ['%s%s' % (scripts_root, filename) for filename in JS[1:]]
+            _.cache = [Script('%s%s' % (scripts_root, filename)) for filename in JS[1:]]
         return _.cache
     return wrapper
 scripts = scripts()
             stylesheets_root = prefix = CSS[0]
             if prefix and not urlparse(prefix).scheme and not prefix.startswith('/'):
                 stylesheets_root = '%s%s' % (reverse('mango.views.index'), prefix)
-            _.cache = [{'media': media, 'href': '%s%s' % (stylesheets_root, filename)}
+            _.cache = [StyleSheet('%s%s' % (stylesheets_root, filename), media)
                     for media, filenames in CSS[1:]
                     for filename in ((filenames,) if isinstance(
                     filenames, basestring) else filenames)]