Fix unclosed header in changelog
[blerg.git] / www / jssrc / blerg / Util.js
index d47770e..18f3169 100644 (file)
@@ -3,14 +3,41 @@ blerg = window.blerg || {};
 blerg.Util = {};
 
 // Dirty, terrible shim for rewritten links below
-blerg.Util.qlink = function() {
+blerg.Util.qlink = function(ev) {
     try {
-        location.href = event.target.href;
+        location.href = ev.target.href;
     } catch(e) { }
     enyo.$.blerg.bubble('onNavigate');
     return false;
 }
 
+blerg.Util.formatLinkHooks = [
+    BlergMedia
+];
+
+blerg.Util.formatMarkdownLinks = function(l) {
+        m = l.match(/(\s[()\[\]{}]?|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?|mailto:[^)"]*?)(\s([^)]*))?\)/);
+        if (!m)
+            return l;
+        var whitespace = m[1];
+        var data = {
+            label: m[2],
+            url: m[3],
+            extra: m[5]
+        };
+
+        for (var i = 0; i < blerg.Util.formatLinkHooks.length; i++) {
+            var newdata = blerg.Util.formatLinkHooks[i].process(data);
+            if (newdata)
+                data = newdata;
+            else
+                console.warn("Format Link Hook " + i + " returned null");
+        }
+        var s = whitespace + '<a href="' + data.url + '">' + data.label + '</a>' +
+            (data.widget ? ' ' + data.widget : '');
+        return l.slice(0, m.index) + s + blerg.Util.formatMarkdownLinks(l.slice(m.index + m[0].length));
+}
+
 blerg.Util.blergFormat = function(text) {
     var lines = text.split(/\r?\n/);
     if (lines[lines.length - 1] == '')
@@ -45,39 +72,18 @@ blerg.Util.blergFormat = function(text) {
         l = l.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
 
         // Turn HTTP URLs into links
-        l = l.replace(/(\s|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1<a href="$2">$2</a>');
+        l = l.replace(/(\s[()\[\]{}]?|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1<a href="$2">$2</a>');
 
         // Turn markdown links into links
-        var re;
-
-        /*
-        // Craft a regex that finds URLs that end in the extensions specified by BlergMedia.audioExtensions.
-        re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9]\\/[^)"]*?\\.(' + BlergMedia.audioExtensions.join('|') + '))\\)', 'g');
-        l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_audio(); return false"><img src="/images/play.png"></a>');
-
-        // Ditto, but use the extended markdown link syntax to specify the format
-        re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\\/[^)"]*?)?)\\s+audio:(' + BlergMedia.audioExtensions.join('|') + ')\\)', 'g');
-        l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_audio(); return false"><img src="/images/play.png"></a>');
-
-        // Craft a regex that finds URLs that end in the extensions specified by BlergMedia.videoExtensions.
-        re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9]\\/[^)"]*?\\.(' + BlergMedia.videoExtensions.join('|') + '))\\)', 'g');
-        l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_video(); return false"><img src="/images/play.png"></a>');
-
-        // Ditto, but use the extended markdown link syntax to specify the format
-        re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\\/[^)"]*?)?)\\s+video:(' + BlergMedia.videoExtensions.join('|') + ')\\)', 'g');
-        l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_video(); return false"><img src="/images/play.png"></a>');
-        */
-
-        // Regular markdown links
-        l = l.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/g, '$1<a href="$3">$2</a>');
+        l = blerg.Util.formatMarkdownLinks(l);
 
         // Turn *foo* into italics and **foo** into bold
         l = l.replace(/([^\w\\]|^)\*\*(\w[^*]*)\*\*(\W|$)/g, '$1<b>$2</b>$3');
         l = l.replace(/([^\w\\]|^)\*(\w[^*]*)\*(\W|$)/g, '$1<i>$2</i>$3');
 
         // Turn refs and tags into links
-        l = l.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#/tag/$2" class="ref" onclick="return blerg.Util.qlink()">#$2</a>');
-        l = l.replace(/(\s|^)@([A-Za-z0-9_-]+)(\/\d+)?/g, '$1<a href="#$2$3" class="ref" onclick="return blerg.Util.qlink()">@$2</a>');
+        l = l.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#/tag/$2" class="ref" onclick="return blerg.Util.qlink(event)">#$2</a>');
+        l = l.replace(/(\s|^)@([A-Za-z0-9_-]+)(\/\d+)?/g, '$1<a href="#$2$3" class="ref" onclick="return blerg.Util.qlink(event)">@$2</a>');
 
         // Create lists when lines begin with *
         if (l[0] == '*') {