d47770eb8dcbc148cd2cb548b6535efea8681f5a
[blerg.git] / www / jssrc / blerg / Util.js
1 blerg = window.blerg || {};
2
3 blerg.Util = {};
4
5 // Dirty, terrible shim for rewritten links below
6 blerg.Util.qlink = function() {
7     try {
8         location.href = event.target.href;
9     } catch(e) { }
10     enyo.$.blerg.bubble('onNavigate');
11     return false;
12 }
13
14 blerg.Util.blergFormat = function(text) {
15     var lines = text.split(/\r?\n/);
16     if (lines[lines.length - 1] == '')
17         lines.pop();
18
19     var out = ['<p>'];
20     var endpush = null;
21     var listMode = false;
22     lines.forEach(function(l) {
23         if (l == '') {
24             if (out[out.length - 1] == '<br>') {
25                 out[out.length - 1] = '<p>';
26             }
27             if (out[out.length - 1] == '</li>') {
28                 out.push('</ul>');
29                 out.push('<p>');
30                 listMode = false;
31             }
32             return;
33         }
34
35         // Put quoted material into a special paragraph
36         if (l[0] == '>') {
37             var pi = out.lastIndexOf('<p>');
38             if (pi != -1) {
39                 out[pi] = '<p class="quote">';
40                 l = l.replace(/^>\s*/, '');
41             }
42         }
43
44         // Sanitize HTML input
45         l = l.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
46
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>');
49
50         // Turn markdown links into links
51         var re;
52
53         /*
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>');
57
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>');
61
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>');
65
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>');
69         */
70
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>');
73
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');
77
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>');
81
82         // Create lists when lines begin with *
83         if (l[0] == '*') {
84             if (!listMode) {
85                 var pi = out.lastIndexOf('<p>');
86                 out[pi] = '<ul>';
87                 listMode = true;
88             }
89             l = l.replace(/^\*\s*/, '');
90             out.push('<li>');
91             endpush = '</li>';
92         }
93
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;
98             if (depth <= 5) {
99                 l = l.replace(/^[=#]+\s*/, '').replace(/\s*[=#]+$/, '');
100                 out.push('<h' + depth + '>');
101                 endpush = '</h' + depth + '>';
102             }
103         }
104
105         // Remove backslashes from escaped metachars
106         l = l.replace(/\\([*\[\]@#])/g, '$1');
107
108         out.push(l);
109         if (endpush) {
110             out.push(endpush);
111             endpush = null;
112         } else {
113             out.push('<br>');
114         }
115     });
116     while (out[out.length - 1] == '<br>' || out[out.length - 1] == '<p>')
117         out.pop();
118     if (listMode)
119         out.push('</ul>');
120
121     return out.join('');
122 }