Rejigger BlergMedia (now more robust and filling!)
authorChip Black <bytex64@bytex64.net>
Sat, 26 May 2012 22:12:40 +0000 (15:12 -0700)
committerChip Black <bytex64@bytex64.net>
Sat, 26 May 2012 22:12:40 +0000 (15:12 -0700)
www/jssrc/blerg/Util.js
www/jssrc/lib/blergmedia.js
www/jssrc/package.js

index d47770e..78081ed 100644 (file)
@@ -11,6 +11,33 @@ blerg.Util.qlink = function() {
     return false;
 }
 
+blerg.Util.formatLinkHooks = [
+    BlergMedia
+];
+
+blerg.Util.formatMarkdownLinks = function(l) {
+        m = l.match(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)(\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] == '')
@@ -48,28 +75,7 @@ blerg.Util.blergFormat = function(text) {
         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');
index 0528a9b..8a91f3e 100644 (file)
@@ -3,7 +3,7 @@ var BlergMedia = {
     videoExtensions: []
 };
 
-function media_init() {
+BlergMedia.media_init = function() {
     var e = document.createElement('audio');
     if (!!e.canPlayType) {
         BlergMedia.has_audio = true;
@@ -19,6 +19,8 @@ function media_init() {
             BlergMedia.has_wav = true;
             BlergMedia.audioExtensions.push('wav');
         }
+        BlergMedia.audioTypeRegex =      new RegExp("audio:(" + BlergMedia.audioExtensions.join('|') + ")$");
+        BlergMedia.audioExtensionRegex = new RegExp("\\.(" + BlergMedia.audioExtensions.join('|') + ")$");
     }
 
     var e = document.createElement('video');
@@ -36,23 +38,43 @@ function media_init() {
             BlergMedia.has_webm = true;
             BlergMedia.videoExtensions.push('webm');
         }
+        BlergMedia.videoTypeRegex =      new RegExp("video:(" + BlergMedia.videoExtensions.join('|') + ")$");
+        BlergMedia.videoExtensionRegex = new RegExp("\\.(" + BlergMedia.videoExtensions.join('|') + ")$");
     }
 }
 
-function play_audio() {
+BlergMedia.play_audio = function() {
     var e = event.target.parentElement;
     var url = event.target.parentElement.href;
-    var audio_element = new Element('audio', {src: url, controls: 1, autoplay: 1});
-    e.replace(audio_element);
+    var audio_element = document.createElement('audio');
+    audio_element.src = url;
+    audio_element.controls = 1;
+    audio_element.autoplay = 1;
+    e.parentElement.replaceChild(audio_element, e);
 }
 
-function play_video() {
+BlergMedia.play_video = function() {
     var e = event.target.parentElement;
     var url = event.target.parentElement.href;
-    var p = new Element('p');
-    var video_element = new Element('video', {src: url, controls: 1, autoplay: 1});
-    p.insert(video_element);
-    e.replace(p);
+    var p = document.createElement('p');
+    var video_element = document.createElement('video');
+    video_element.src = url;
+    video_element.controls = 1;
+    video_element.autoplay = 1;
+    p.appendChild(video_element);
+    e.parentElement.replaceChild(video_element, e);
 }
 
-window.addEventListener('load', media_init, false);
+BlergMedia.process = function(data) {
+    if (BlergMedia.audioExtensionRegex.test(data.url) ||
+        (data.extra && BlergMedia.audioTypeRegex.test(data.extra))) {
+        data.widget = '<a href="' + data.url + '" onclick="BlergMedia.play_audio(); return false"><img src="/images/play.png"></a>';
+    } else if (BlergMedia.videoExtensionRegex.test(data.url) ||
+               (data.extra && BlergMedia.videoTypeRegex.test(data.extra))) {
+        data.widget = '<a href="' + data.url + '" onclick="BlergMedia.play_video(); return false"><img src="/images/play.png"></a>';
+    }
+
+    return data;
+}
+
+window.addEventListener('load', BlergMedia.media_init, false);
index 07e84bd..c611a9d 100644 (file)
@@ -1,5 +1,6 @@
 enyo.depends(
     '$lib/onyx',
     '$lib/OldSchoolSpinner.js',
+    '$lib/blergmedia.js',
     'blerg'
 );