1 blerg = window.blerg || {};
5 // Dirty, terrible shim for rewritten links below
6 blerg.Util.qlink = function(ev) {
8 location.href = ev.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] == '')
50 var githubCodeMode = false;
51 lines.forEach(function(l) {
54 // Turn single linebreaks into paragraphs
55 if (out[out.length - 1] == '<br>') {
56 out[out.length - 1] = '<p>';
59 if (out[out.length - 1] == '</li>') {
73 // Sometimes I wish more languages had Perl's bistable .. operator.
78 githubCodeMode = false;
80 // Sanitize HTML input
81 l = l.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
88 githubCodeMode = true;
93 // Create a code block when lines begin with at least four spaces
94 if (l.substr(0, 4) == ' ') {
99 // Sanitize HTML input
100 l = l.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
101 out.push(l.substr(4) + "\n");
110 // Put quoted material into a special paragraph
112 var pi = out.lastIndexOf('<p>');
114 out[pi] = '<p class="quote">';
115 l = l.replace(/^>\s*/, '');
119 // Sanitize HTML input
120 l = l.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
122 // Turn HTTP URLs into links
123 l = l.replace(/(\s[()\[\]{}]?|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1<a href="$2">$2</a>');
125 // Turn markdown links into links
126 l = blerg.Util.formatMarkdownLinks(l);
128 // Turn *foo* into italics and **foo** into bold
129 l = l.replace(/([^\w\\]|^)\*\*(\w[^*]*)\*\*(\W|$)/g, '$1<b>$2</b>$3');
130 l = l.replace(/([^\w\\]|^)\*(\w[^*]*)\*(\W|$)/g, '$1<i>$2</i>$3');
132 // Turn ~~foo~~ into strikethrough
133 l = l.replace(/([^\w\\]|^)~~(\w[^~]*)~~(\W|$)/g, '$1<s>$2</s>$3');
135 // Turn refs and tags into links
136 l = l.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#/tag/$2" class="ref" onclick="return blerg.Util.qlink(event)">#$2</a>');
137 l = l.replace(/(\s|^)@([A-Za-z0-9_-]+)(\/\d+)?/g, '$1<a href="#$2$3" class="ref" onclick="return blerg.Util.qlink(event)">@$2</a>');
139 // Create lists when lines begin with *
142 var pi = out.lastIndexOf('<p>');
146 l = l.replace(/^\*\s*/, '');
151 // Create headers when lines begin with = or #
152 if (l[0] == '=' || l[0] == '#') {
153 var m = l.match(/^([=#]+)/);
154 var depth = m[1].length;
156 l = l.replace(/^[=#]+\s*/, '').replace(/\s*[=#]+$/, '');
157 out.push('<h' + depth + '>');
158 endpush = '</h' + depth + '>';
162 // Remove backslashes from escaped metachars
163 l = l.replace(/\\([*\[\]@#~])/g, '$1');
173 while (out[out.length - 1] == '<br>' || out[out.length - 1] == '<p>') {
179 if (codeMode || githubCodeMode) {