]> Pileus Git - vpaste/blob - embed.js
Use /bin/sh for vpaste script
[vpaste] / embed.js
1 // Copyright (C) 2013 Andy Spencer
2 //
3 // This program is free software: you can redistribute it and/or modify it under
4 // the terms of the GNU Affero General Public License as published by the Free
5 // Software Foundation, either version 3 of the License, or (at your option) any
6 // later version.
7 //
8 // This program is distributed in the hope that it will be useful, but WITHOUT
9 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 // FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
11 // details.
12
13 /* Constants */
14 var vpaste = 'http://vpaste.net/';
15
16 var light  = '#f4f4f4';
17 var dark   = '#111133';
18 var border = '#8888dd';
19
20 var styles = [
21         'background: ' + light + ';',
22         'border: solid 1px ' + border + ';',
23         'padding: 0.5em;',
24         'overflow: auto;',
25         'display: block;',
26         'white-space: pre;',
27         'text-align: left;',
28 ];
29
30 /* Globals */
31 var query;
32
33 /* Update Style Sheet */ 
34 function update_styles(style, name) {
35         var text  = style.innerHTML;
36         var lines = text.split(/\n/);
37         for (var i = 0; i < lines.length; i++) {
38                 var line = lines[i];
39                 line = line.replace(/#ffffff/, light);
40                 line = line.replace(/#000000/, dark);
41                 if (line.match(/^pre/))
42                         line = line.replace(/^pre/, '.'+name);
43                 else if (line.match(/^\./))
44                         line = '.'+name+' '+line;
45                 else
46                         line = '';
47                 lines[i] = line;
48         }
49         style.innerHTML = lines.join('\n');
50 }
51
52 /* Embed paste into page  */
53 function embed_paste(ajax, style, html, name) {
54         if (ajax.readyState != 4 && ajax.readyState != 'complete')
55                 return;
56         if (!ajax.responseXML)
57                 throw new Error('No response XML: ' + ajax.responseText);
58         var xml    = ajax.responseXML;
59         var vstyle = xml.getElementsByTagName('style')[0];
60         var vhtml  = xml.getElementsByTagName('pre')[0];
61         update_styles(vstyle, name);
62         style.innerHTML += vstyle.innerHTML;
63         html.innerHTML   = vhtml.innerHTML.replace(/^\n*/, '');
64         html.style.visibility = 'visible';
65 }
66
67 /* Strip whitespace from a paste */
68 function strip_spaces(text) {
69         var prefix = null;
70         var lines  = text.replace(/^\s*\n|\n\s*$/g,'').split('\n');
71         for (i in lines) {
72                 if (lines[i].match(/^\s*$/))
73                         continue;
74                 var white = lines[i].replace(/\S.*/, '')
75                 if (prefix === null || white.length < prefix.length)
76                         prefix = white;
77         }
78         for (i in lines)
79                 lines[i] = lines[i].replace(prefix, '');
80         return lines.join('\n');
81 }
82
83 /* Start embedding a paste */
84 function query_paste(method, url, body, html, name) {
85         /* Add style tag box */
86         var style = document.createElement('style');
87         style.type      = "text/css";
88         style.innerHTML = '.' + name + ' { ' + styles.join(' ') + ' }';
89         document.head.insertBefore(style, document.head.firstChild);
90
91         /* Get AJAX object */
92         var ajax = null;
93         if (!ajax) ajax = new XMLHttpRequest();
94         if (!ajax) ajax = new ActiveXObject('Microsoft.XMLHTTP');
95         if (!ajax) throw new Error('Cannot get AJAX object');
96
97         /* Insert default query */
98         if (query)
99                 url = url.replace(/[?]/, '?' + query + ',');
100
101         /* Run AJAX Request */
102         ajax.onreadystatechange = function() {
103                 embed_paste(ajax, style, html, name) };
104         ajax.open(method, url, true);
105         ajax.setRequestHeader('Accept', 'text/html');
106         ajax.overrideMimeType('application/xhtml+xml');
107         ajax.send(body);
108 }
109
110 /* Start embedding a paste */
111 function start_embed() {
112         /* Get current paste information */
113         var scripts = document.getElementsByTagName('script');
114         var script  = scripts[scripts.length-1];
115         var text    = strip_spaces(script.textContent);
116         var name    = 'vpaste_s' + scripts.length;
117         var regex   = /^[^?]*[?]?(([a-zA-Z0-9.]*)[?&, ]?(.*))$/;
118         var parts   = script.src.match(regex);
119
120         /* Handle header tags */
121         if (!text && !parts[2] || !document.body)
122                 return query = parts[1];
123
124         /* Add paste box */
125         var html = document.createElement('pre');
126         html.innerHTML = 'Loading..';
127         html.className = script.className + ' vpaste ' + name;
128         script.parentNode.appendChild(html);
129
130         /* Query the paste */
131         if (text)
132                 query_paste('POST', vpaste+'view?'+parts[1],
133                         text, html, name);
134         else
135                 query_paste('GET', vpaste+parts[2]+'?'+parts[3],
136                         null, html, name);
137 }
138
139 /* Convert all code tags to pastes */
140 function format_code(tagName, className) {
141         if (!tagName)
142                 tagName = 'code';
143         var tags = document.getElementsByTagName(tagName);
144         for (var i = 0; i < tags.length; i++) {
145                 var tag = tags[i];
146                 if (className && tag.className != className)
147                         continue;
148                 var name  = 'vpaste_c' + i;
149                 var text  = strip_spaces(tag.textContent);
150                 var query = tag.getAttribute('title');
151                 tag.className     += ' ' + name;
152                 query_paste('POST', vpaste+'view?'+query,
153                                 text, tag, name);
154         }
155 }
156
157 start_embed();