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;
86 githubCodeMode = true;
91 // Create a code block when lines begin with at least four spaces
92 if (l.substr(0, 4) == ' ') {
97 out.push(l.substr(4) + "\n");
106 // Put quoted material into a special paragraph
108 var pi = out.lastIndexOf('<p>');
110 out[pi] = '<p class="quote">';
111 l = l.replace(/^>\s*/, '');
115 // Sanitize HTML input
116 l = l.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
118 // Turn HTTP URLs into links
119 l = l.replace(/(\s[()\[\]{}]?|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1<a href="$2">$2</a>');
121 // Turn markdown links into links
122 l = blerg.Util.formatMarkdownLinks(l);
124 // Turn *foo* into italics and **foo** into bold
125 l = l.replace(/([^\w\\]|^)\*\*(\w[^*]*)\*\*(\W|$)/g, '$1<b>$2</b>$3');
126 l = l.replace(/([^\w\\]|^)\*(\w[^*]*)\*(\W|$)/g, '$1<i>$2</i>$3');
128 // Turn ~~foo~~ into strikethrough
129 l = l.replace(/([^\w\\]|^)~~(\w[^~]*)~~(\W|$)/g, '$1<s>$2</s>$3');
131 // Turn refs and tags into links
132 l = l.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#/tag/$2" class="ref" onclick="return blerg.Util.qlink(event)">#$2</a>');
133 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>');
135 // Create lists when lines begin with *
138 var pi = out.lastIndexOf('<p>');
142 l = l.replace(/^\*\s*/, '');
147 // Create headers when lines begin with = or #
148 if (l[0] == '=' || l[0] == '#') {
149 var m = l.match(/^([=#]+)/);
150 var depth = m[1].length;
152 l = l.replace(/^[=#]+\s*/, '').replace(/\s*[=#]+$/, '');
153 out.push('<h' + depth + '>');
154 endpush = '</h' + depth + '>';
158 // Remove backslashes from escaped metachars
159 l = l.replace(/\\([*\[\]@#~])/g, '$1');
169 while (out[out.length - 1] == '<br>' || out[out.length - 1] == '<p>') {
175 if (codeMode || githubCodeMode) {