1 blerg = window.blerg || {};
5 // Dirty, terrible shim for rewritten links below
6 blerg.Util.qlink = function() {
8 location.href = event.target.href;
10 enyo.$.blerg.bubble('onNavigate');
14 blerg.Util.blergFormat = function(text) {
15 var lines = text.split(/\r?\n/);
16 if (lines[lines.length - 1] == '')
22 lines.forEach(function(l) {
24 if (out[out.length - 1] == '<br>') {
25 out[out.length - 1] = '<p>';
27 if (out[out.length - 1] == '</li>') {
35 // Put quoted material into a special paragraph
37 var pi = out.lastIndexOf('<p>');
39 out[pi] = '<p class="quote">';
40 l = l.replace(/^>\s*/, '');
44 // Sanitize HTML input
45 l = l.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
47 // Turn HTTP URLs into links
48 l = l.replace(/(\s|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1<a href="$2">$2</a>');
50 // Turn markdown links into links
54 // Craft a regex that finds URLs that end in the extensions specified by BlergMedia.audioExtensions.
55 re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9]\\/[^)"]*?\\.(' + BlergMedia.audioExtensions.join('|') + '))\\)', 'g');
56 l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_audio(); return false"><img src="/images/play.png"></a>');
58 // Ditto, but use the extended markdown link syntax to specify the format
59 re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\\/[^)"]*?)?)\\s+audio:(' + BlergMedia.audioExtensions.join('|') + ')\\)', 'g');
60 l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_audio(); return false"><img src="/images/play.png"></a>');
62 // Craft a regex that finds URLs that end in the extensions specified by BlergMedia.videoExtensions.
63 re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9]\\/[^)"]*?\\.(' + BlergMedia.videoExtensions.join('|') + '))\\)', 'g');
64 l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_video(); return false"><img src="/images/play.png"></a>');
66 // Ditto, but use the extended markdown link syntax to specify the format
67 re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\\/[^)"]*?)?)\\s+video:(' + BlergMedia.videoExtensions.join('|') + ')\\)', 'g');
68 l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_video(); return false"><img src="/images/play.png"></a>');
71 // Regular markdown links
72 l = l.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/g, '$1<a href="$3">$2</a>');
74 // Turn *foo* into italics and **foo** into bold
75 l = l.replace(/([^\w\\]|^)\*\*(\w[^*]*)\*\*(\W|$)/g, '$1<b>$2</b>$3');
76 l = l.replace(/([^\w\\]|^)\*(\w[^*]*)\*(\W|$)/g, '$1<i>$2</i>$3');
78 // Turn refs and tags into links
79 l = l.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#/tag/$2" class="ref" onclick="return blerg.Util.qlink()">#$2</a>');
80 l = l.replace(/(\s|^)@([A-Za-z0-9_-]+)(\/\d+)?/g, '$1<a href="#$2$3" class="ref" onclick="return blerg.Util.qlink()">@$2</a>');
82 // Create lists when lines begin with *
85 var pi = out.lastIndexOf('<p>');
89 l = l.replace(/^\*\s*/, '');
94 // Create headers when lines begin with = or #
95 if (l[0] == '=' || l[0] == '#') {
96 var m = l.match(/^([=#]+)/);
97 var depth = m[1].length;
99 l = l.replace(/^[=#]+\s*/, '').replace(/\s*[=#]+$/, '');
100 out.push('<h' + depth + '>');
101 endpush = '</h' + depth + '>';
105 // Remove backslashes from escaped metachars
106 l = l.replace(/\\([*\[\]@#])/g, '$1');
116 while (out[out.length - 1] == '<br>' || out[out.length - 1] == '<p>')