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.formatLinkHooks = [
18 blerg.Util.formatMarkdownLinks = function(l) {
19 m = l.match(/(\s[()\[\]{}]?|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?|mailto:[^)"]*?)(\s([^)]*))?\)/);
22 var whitespace = m[1];
29 for (var i = 0; i < blerg.Util.formatLinkHooks.length; i++) {
30 var newdata = blerg.Util.formatLinkHooks[i].process(data);
34 console.warn("Format Link Hook " + i + " returned null");
36 var s = whitespace + '<a href="' + data.url + '">' + data.label + '</a>' +
37 (data.widget ? ' ' + data.widget : '');
38 return l.slice(0, m.index) + s + blerg.Util.formatMarkdownLinks(l.slice(m.index + m[0].length));
41 blerg.Util.blergFormat = function(text) {
42 var lines = text.split(/\r?\n/);
43 if (lines[lines.length - 1] == '')
49 lines.forEach(function(l) {
51 if (out[out.length - 1] == '<br>') {
52 out[out.length - 1] = '<p>';
54 if (out[out.length - 1] == '</li>') {
62 // Put quoted material into a special paragraph
64 var pi = out.lastIndexOf('<p>');
66 out[pi] = '<p class="quote">';
67 l = l.replace(/^>\s*/, '');
71 // Sanitize HTML input
72 l = l.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
74 // Turn HTTP URLs into links
75 l = l.replace(/(\s[()\[\]{}]?|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1<a href="$2">$2</a>');
77 // Turn markdown links into links
78 l = blerg.Util.formatMarkdownLinks(l);
80 // Turn *foo* into italics and **foo** into bold
81 l = l.replace(/([^\w\\]|^)\*\*(\w[^*]*)\*\*(\W|$)/g, '$1<b>$2</b>$3');
82 l = l.replace(/([^\w\\]|^)\*(\w[^*]*)\*(\W|$)/g, '$1<i>$2</i>$3');
84 // Turn refs and tags into links
85 l = l.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#/tag/$2" class="ref" onclick="return blerg.Util.qlink()">#$2</a>');
86 l = l.replace(/(\s|^)@([A-Za-z0-9_-]+)(\/\d+)?/g, '$1<a href="#$2$3" class="ref" onclick="return blerg.Util.qlink()">@$2</a>');
88 // Create lists when lines begin with *
91 var pi = out.lastIndexOf('<p>');
95 l = l.replace(/^\*\s*/, '');
100 // Create headers when lines begin with = or #
101 if (l[0] == '=' || l[0] == '#') {
102 var m = l.match(/^([=#]+)/);
103 var depth = m[1].length;
105 l = l.replace(/^[=#]+\s*/, '').replace(/\s*[=#]+$/, '');
106 out.push('<h' + depth + '>');
107 endpush = '</h' + depth + '>';
111 // Remove backslashes from escaped metachars
112 l = l.replace(/\\([*\[\]@#])/g, '$1');
122 while (out[out.length - 1] == '<br>' || out[out.length - 1] == '<p>')