Browse Source

Shorthand image syntax added. #115

Ralfp 12 years ago
parent
commit
9c0ba21096

+ 28 - 0
misago/markdown/extensions/shorthandimgs.py

@@ -0,0 +1,28 @@
+#-*- coding: utf-8 -*-
+import re
+import markdown
+from markdown.inlinepatterns import LinkPattern
+from misago.utils.strings import html_escape
+from misago.utils.urls import is_inner, clean_inner
+from markdown.util import etree
+
+IMAGES_RE =  r'\!(\s?)\((<.*?>|([^\)]*))\)'
+
+class ShorthandImagesExtension(markdown.Extension):
+    def extendMarkdown(self, md):
+        md.registerExtension(self)
+        md.inlinePatterns.add('mi_shorthand_imgs',
+                              ShorthandImagePattern(IMAGES_RE, md),
+                              '_end')
+
+
+class ShorthandImagePattern(LinkPattern):
+    def handleMatch(self, m):
+        img_src = m.groups()[2].strip()
+        if is_inner(img_src):
+            img_src = clean_inner(img_src)
+        if img_src:
+            el = etree.Element("img")
+            el.set('alt', img_src)
+            el.set('src', img_src)
+            return el

+ 1 - 0
misago/markdown/factory.py

@@ -85,4 +85,5 @@ def finalize_markdown(text):
         return _("Posted by %(user)s") % {'user': match.group('content')}
     text = re.sub(r'<quotetitle>(?P<content>.+)</quotetitle>', trans_quotetitle, text)
     text = re.sub(r'<quotesingletitle>', _("Quote"), text)
+    text = re.sub(r'<imgalt>', _("Posted image"), text)
     return text

+ 1 - 0
misago/settings_base.py

@@ -152,6 +152,7 @@ MARKDOWN_EXTENSIONS = (
     'misago.markdown.extensions.mentions.MentionsExtension',
     'misago.markdown.extensions.magiclinks.MagicLinksExtension',
     'misago.markdown.extensions.cleanlinks.CleanLinksExtension',
+    'misago.markdown.extensions.shorthandimgs.ShorthandImagesExtension',
     # Uncomment for EXPERIMENTAL BBCode support
     #'misago.markdown.extensions.bbcodes.BBCodesExtension',
     # Uncomment for emoji support, requires emoji directory in static dir.

+ 4 - 2
static/cranefly/js/cranefly.js

@@ -76,9 +76,11 @@ function EnhancePostsMD() {
     // Add labels to images
     $('.markdown.js-extra img').not('.emoji').each(function() {
       $(this).addClass('img-rounded');
-      $(this).attr('title', $(this).attr('alt'));
+      if ($(this).attr('alt').length > 0 && $(this).attr('alt') != $(this).attr('src')) {
+        $(this).attr('title', $(this).attr('alt'));
+      }
       $(this).tooltip({placement: 'top', container: 'body'});
-      $(this).wrap('<a href="' + $(this).attr('src') + '" target="_blank"/>');
+      $(this).wrap('<a href="' + escape($(this).attr('src')) + '" target="_blank"/>');
     });
 
     // Automagically turn links into players

+ 1 - 1
static/cranefly/js/editor.js

@@ -121,7 +121,7 @@ $(function() {
       if (image_label.length > 0) {
         makeReplace(ta, '![' + image_label + '](' + image_url + ')');
       } else {
-        makeReplace(ta, '![' + image_url + '](' + image_url + ')');
+        makeReplace(ta, '!(' + image_url + ')');
       }
     }
     return false;