Permalink
Browse files

Refactor snippet editor and save snippet icon url.

Switches to using ICanHaz for JS templating and reorganizes the
snippet editor to be a bit more manageable.

In addition, the snippet editor saves the icon URL in a comment
in the snippet so the url box keeps the icon URL between saves.
  • Loading branch information...
1 parent 34fc1f2 commit 83c230e9f921c36ad1bd7960992fb4ab9001cf53 @Osmose Osmose committed Oct 21, 2011
@@ -120,7 +120,9 @@ class Media:
css = {
'all': ('snippetBodyWidget.css',)
}
- js = ('jquery-1.6.1.min.js', 'jquery.easytabs.min.js',
+ js = ('jquery-1.6.1.min.js',
+ 'jquery.easytabs.min.js',
+ 'ICanHaz.js',
'snippetBodyWidget.js')
def render(self, name, value, attrs=None):
@@ -1,4 +1,19 @@
+{% load icanhaz %}
<div id="snippet-editor">
+ {# ICanHaz Templates #}
+ {% icanhaz %}
+ <script type="text/html" id="snippet_template">
+ <!--basic-->
+ <div class="snippet">
+ [[#icon]]
+ <!--icon:[[ url ]]-->
+ <img class="icon" src="[[ data ]]" />
+ [[/icon]]
+ <p>[[[ text ]]]</p>
+ </div>
+ </script>
+ {% endicanhaz %}
+
<ul id="snippet-tabs">
<li><a href="#snippet-basic">{{ _('Basic') }}</a></li>
<li><a href="#snippet-advanced">{{ _('Advanced') }}</a></li>
No changes.
@@ -0,0 +1,34 @@
+from django import template
+
+register = template.Library()
+
+
+SYMBOLS = [
+ ('[[[', '{{{'),
+ (']]]', '}}}'),
+ ('[[', '{{'),
+ (']]', '}}'),
+]
+
+
+@register.tag
+def icanhaz(parser, token):
+ """Replaces double and triple square brackets with curly brackets.
+
+ Used for embedding ICanHaz/Mustache templates in django templates.
+ """
+ nodelist = parser.parse(('endicanhaz',))
+ parser.delete_first_token()
+ return ICanHazNode(nodelist)
+
+
+class ICanHazNode(template.Node):
+ """Parses template code and replaces certain symbols."""
+ def __init__(self, nodelist):
+ self.nodelist = nodelist
+
+ def render(self, context):
+ output = self.nodelist.render(context)
+ for find, replace in SYMBOLS:
+ output = output.replace(find, replace)
+ return output
@@ -0,0 +1,27 @@
+from django.template import Context
+from django.template.loader import get_template_from_string
+from django.test import TestCase
+
+from nose.tools import eq_
+
+class TestICanHaz(TestCase):
+ """Test the icanhaz template tag."""
+
+ def setUp(self):
+ """Create empty context to render test templates with."""
+ self.context = Context()
+
+ def test_basic(self):
+ """Tests icanhaz bracket replacement."""
+ template_str = """
+ {% load icanhaz %}
+ {% icanhaz %}
+ Nyan [Nyan] [[Nyan]] [[[Nyan]]]
+ {% endicanhaz %}
+ """
+ template = get_template_from_string(template_str)
+ result = template.render(self.context).strip()
+
+ eq_(result, 'Nyan [Nyan] {{Nyan}} {{{Nyan}}}',
+ "Single brackets should' be preserved, but double and triple "
+ "brackets should be replaced.")
Oops, something went wrong.

0 comments on commit 83c230e

Please sign in to comment.