Rejigger BlergMedia (now more robust and filling!)
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] == '')
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');
videoExtensions: []
};
-function media_init() {
+BlergMedia.media_init = function() {
var e = document.createElement('audio');
if (!!e.canPlayType) {
BlergMedia.has_audio = true;
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');
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);
enyo.depends(
'$lib/onyx',
'$lib/OldSchoolSpinner.js',
+ '$lib/blergmedia.js',
'blerg'
);