Commits

dja...@v026.medcn.uwcm.ac.uk  committed 09e9f45

rollback

  • Participants
  • Parent commits 203e04d

Comments (0)

Files changed (8)

File semanticeditor/cms_plugins.py

     name = _("Text/layout")
     admin_preview = False
 
-    def get_editor_widget(self, request, plugins):
-        return SemanticEditor(installed_plugins=plugins)
+    # A lot of duplication from TextPlugin because get_form needs to find out
+    # what page/template we are using, and pass that on to get_editor_widget
+
+    def get_editor_widget(self, request, plugins, page):
+        return SemanticEditor(installed_plugins=plugins,
+                              page=page)
+
+    def get_form_class(self, request, plugins, page):
+        """
+        Returns a subclass of Form to be used by this plugin
+        """
+        # We avoid mutating the Form declared above by subclassing
+        class TextPluginForm(self.form):
+            pass
+
+        widget = self.get_editor_widget(request, plugins, page)
+        TextPluginForm.declared_fields["body"] = CharField(widget=widget, required=False)
+        return TextPluginForm
+
+    def get_form(self, request, obj=None, **kwargs):
+        page = None
+        if obj:
+            page = obj.page
+        plugins = plugin_pool.get_text_enabled_plugins(self.placeholder, page)
+        form = self.get_form_class(request, plugins, page)
+        kwargs['form'] = form # override standard form
+        return super(TextPlugin, self).get_form(request, obj, **kwargs)
 
 plugin_pool.register_plugin(SemanticTextPlugin)

File semanticeditor/media/semanticeditor/javascript/wymeditor/plugins/semantic/wymeditor.semantic.js

 PresentationControls.prototype.retrieve_styles = function() {
     // Retrieve via AJAX
     var self = this;
-    jQuery.getJSON(this.opts.retrieve_styles_url, {},
+    jQuery.getJSON(this.opts.retrieve_styles_url, {'template':this.opts.template,
+                                                   'page_id':this.opts.page_id
+                                                  },
 		   function (data) {
 		       self.with_good_data(data, function(value) {
 			   self.available_styles = data.value;

File semanticeditor/models.py

 from django.db import models
+from semanticeditor.fields import MultiSelectField
+from django.conf import settings
+
+template_list = [(f,n) for (f,n) in settings.CMS_TEMPLATES if f != settings.CMS_TEMPLATE_INHERITANCE_MAGIC]
 
 class CssClass(models.Model):
     name = models.CharField("CSS class name", max_length=255, unique=True,
                                    "when the user hovers over the name in the "
                                    "list of styles.  The data will be interpreted "
                                    "as raw HTML, so it can include an example.")
+    templates = MultiSelectField("Templates", choices=template_list, blank=True,
+                                 default="")
+
     allowed_elements = models.CharField("Allowed HTML elements", max_length=255,
                                         help_text="A space separated list of HTML "
                                         "element names.  Use 'row' or 'column' to indicate "
     column_equiv = models.IntegerField("Column count equivalent", null=True, blank=True,
                                        help_text="For classes designed to be applied to "
                                        "columns only, this is the number of columns this "
-                                       "be considered as equivalent too.  This can be "
+                                       "should be considered as equivalent to. This can be "
                                        "useful for generating double width columns etc. "
                                        "within a column layout.")
     def __unicode__(self):

File semanticeditor/templates/semanticeditor/editorwidget.html

             .append(html);
 
             // Enable the 'semantic' plugin
+            // 'template' attribute can be overridden by a '?template' query
+            // string parameter on parent frame
+            var qs = jQuery.query.load(window.parent.location.search);
+            var template = qs.get('template');
+            if (template == "") {
+                template = "{{ page.template }}";
+            }
+
             symanticopts = {
                 extract_structure_url: "{% url semantic.extract_structure %}",
                 retrieve_styles_url: "{% url semantic.retrieve_styles %}",
                 separate_presentation_url: "{% url semantic.separate_presentation %}",
                 combine_presentation_url: "{% url semantic.combine_presentation %}",
                 clean_html_url: "{% url semantic.clean_html %}",
-                preview_url: "{% url semantic.preview %}"
+                preview_url: "{% url semantic.preview %}",
+                template: template,
+                page_id: "{{ page.id }}"
             };
             wym.semantic(symanticopts);
 

File semanticeditor/tests.py

 <h1>Heading two</h1>
 """)],
         [(1, "h1_1", u"Heading with embedded stuff in it", u"h1"),
-         (2, "p_1", u"A long paragraph wit...", u"p"),
+         (2, "p_1", u"A long paragraph with some actual content...", u"p"),
          (2, "h2_1", u"A sub heading", u"h2"),
          (3, "p_2", u"Another para...", u"p"),
          (3, "h3_1", u"level 3", u"h3"),
-         (4, "p_3", u"A long paragraph wit...2", u"p"),
+         (4, "p_3", u"A long paragraph with some actual content...2", u"p"),
          (4, "h4_1", u"level 4", u"h4"),
          (5, "p_4", u"Another para...2", u"p"),
          (5, "h5_1", u"level 5", u"h5"),
-         (6, "p_5", u"nasty  ééééééééééééé...", u"p"),
+         (6, "p_5", u"nasty  éééééééééééééééééééééééééé...", u"p"),
          (6, "h6_1", u"level 6", u"h6"),
          (1, "h1_2", u"Heading two", u"h1"),
          ])
     def test_remove_span(self):
         self.assertEqualClean("<p>Some <span>text</span> with a silly span</p>",
                               "<p>Some text with a silly span</p>")
+
+    def test_inline_wrapping_block(self):
+        self.assertEqualClean("<strong><p>A test</p></strong>",
+                              "<p>A test</p>")
+        self.assertEqualClean("<em><ul><li>A test</li></ul></strong>",
+                              "<ul><li>A test</li></ul>")
+

File semanticeditor/utils/presentation.py

 technical_blockdef = set(['h1','h2','h3','h4','h5','h6', 'p', 'ol', 'ul', 'blockquote']) # according to HTML4
 additional_blockdef = set(['li']) # li really act like block elements
 blockdef = technical_blockdef | additional_blockdef
+blockdef_selector = ",".join(blockdef)
 headingdef = set(['h1','h2','h3','h4','h5','h6'])
 preview_blockdef = technical_blockdef
 
     for x in ['table', 'tbody', 'thead', 'tr', 'td', 'span', 'li p:only-child']:
         for n in doc(x):
             pull_up(n)
+    # "li p:only-child" appears to be buggy.  It works like
+    # "li p:only-descendent" or something.
+
+    for x in ['strong','em','b','i']:
+        for n in doc(x):
+            if pq(n).is_(blockdef_selector):
+                pull_up(n)
 
     for x in ['br + br', 'p + br', 'p:empty']:
         doc(x).remove()

File semanticeditor/views.py

+from cms.models import Page
 from django.http import HttpResponse
 from django.utils import simplejson
 from django.core.mail import mail_admins
+from django.conf import settings
 from django.utils.translation import ugettext as _
 from semanticeditor.utils import extract_structure, extract_presentation, format_html, preview_html, AllUserErrors, NEWROW, NEWCOL, PresentationInfo, PresentationClass, clean_html
 from semanticeditor.models import CssClass
 
 @json_view
 def retrieve_styles(request):
+    template = request.GET['template']
+    page_id = request.GET['page_id']
+    if template == settings.CMS_TEMPLATE_INHERITANCE_MAGIC:
+        # Need to look up page to find out what template to use
+        p = Page.objects.get(pk=page_id)
+        template = p.get_template()
+    classes = CssClass.objects.all().order_by('verbose_name')
+    # Can't do filter in DB easily, because 'templates' is actually
+    # a comma separated list in DB.
+    classes = filter(lambda c: template in c.templates, classes)
     retval = map(css_class_to_presentation_class,
-                 CssClass.objects.all().order_by('verbose_name'))
+                 classes)
     return success(map(PI_to_dict,retval))
 
 @json_view

File semanticeditor/widgets.py

               ('javascript/wymeditor/plugins/semantic/wymeditor.semantic.js',
                'javascript/json2.js',
                'javascript/orbitaltooltip.js',
+               'javascript/jquery.query-2.1.7.js',
                )]
 
+    def __init__(self, attrs=None, installed_plugins=None, page=None):
+        self.page = page
+        super(SemanticEditor, self).__init__(attrs=attrs, installed_plugins=installed_plugins)
+
     def render_additions(self, name, value, attrs=None):
         language = get_language()
         context = {
             'WYM_CONTAINERS': mark_safe(text_settings.WYM_CONTAINERS),
             'WYM_CLASSES': mark_safe(text_settings.WYM_CLASSES),
             'installed_plugins': self.installed_plugins,
+            'page': self.page,
         }
 
         return mark_safe(render_to_string(