1 blerg = window.blerg || {};
5 blerg.Util.blergFormat = function(text) {
6 var lines = text.split(/\r?\n/);
7 if (lines[lines.length - 1] == '')
13 lines.forEach(function(l) {
15 if (out[out.length - 1] == '<br>') {
16 out[out.length - 1] = '<p>';
18 if (out[out.length - 1] == '</li>') {
26 // Put quoted material into a special paragraph
28 var pi = out.lastIndexOf('<p>');
30 out[pi] = '<p class="quote">';
31 l = l.replace(/^>\s*/, '');
35 // Sanitize HTML input
36 l = l.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
38 // Turn HTTP URLs into links
39 l = l.replace(/(\s|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1<a href="$2">$2</a>');
41 // Turn markdown links into links
45 // Craft a regex that finds URLs that end in the extensions specified by BlergMedia.audioExtensions.
46 re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9]\\/[^)"]*?\\.(' + BlergMedia.audioExtensions.join('|') + '))\\)', 'g');
47 l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_audio(); return false"><img src="/images/play.png"></a>');
49 // Ditto, but use the extended markdown link syntax to specify the format
50 re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\\/[^)"]*?)?)\\s+audio:(' + BlergMedia.audioExtensions.join('|') + ')\\)', 'g');
51 l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_audio(); return false"><img src="/images/play.png"></a>');
53 // Craft a regex that finds URLs that end in the extensions specified by BlergMedia.videoExtensions.
54 re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9]\\/[^)"]*?\\.(' + BlergMedia.videoExtensions.join('|') + '))\\)', 'g');
55 l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_video(); return false"><img src="/images/play.png"></a>');
57 // Ditto, but use the extended markdown link syntax to specify the format
58 re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\\/[^)"]*?)?)\\s+video:(' + BlergMedia.videoExtensions.join('|') + ')\\)', 'g');
59 l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_video(); return false"><img src="/images/play.png"></a>');
62 // Regular markdown links
63 l = l.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/g, '$1<a href="$3">$2</a>');
65 // Turn *foo* into italics and **foo** into bold
66 l = l.replace(/([^\w\\]|^)\*\*(\w[^*]*)\*\*(\W|$)/g, '$1<b>$2</b>$3');
67 l = l.replace(/([^\w\\]|^)\*(\w[^*]*)\*(\W|$)/g, '$1<i>$2</i>$3');
69 // Turn refs and tags into links
70 l = l.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#/tag/$2" class="ref" onclick="return qlink()">#$2</a>');
71 l = l.replace(/(\s|^)@([A-Za-z0-9_-]+)(\/\d+)?/g, '$1<a href="#$2$3" class="ref" onclick="return qlink()">@$2</a>');
73 // Create lists when lines begin with *
76 var pi = out.lastIndexOf('<p>');
80 l = l.replace(/^\*\s*/, '');
85 // Create headers when lines begin with = or #
86 if (l[0] == '=' || l[0] == '#') {
87 var m = l.match(/^([=#]+)/);
88 var depth = m[1].length;
90 l = l.replace(/^[=#]+\s*/, '').replace(/\s*[=#]+$/, '');
91 out.push('<h' + depth + '>');
92 endpush = '</h' + depth + '>';
96 // Remove backslashes from escaped metachars
97 l = l.replace(/\\([*\[\]@#])/g, '$1');
107 while (out[out.length - 1] == '<br>' || out[out.length - 1] == '<p>')