1 // Copyright (C) 2013 Andy Spencer
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
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
14 var vpaste = 'http://vpaste.net/';
16 var light = '#f4f4f4';
18 var border = '#8888dd';
21 'background: ' + light + ';',
22 'border: solid 1px ' + border + ';',
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++) {
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;
49 style.innerHTML = lines.join('\n');
52 /* Embed paste into page */
53 function embed_paste(ajax, style, html, name) {
54 if (ajax.readyState != 4 && ajax.readyState != 'complete')
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';
67 /* Strip whitespace from a paste */
68 function strip_spaces(text) {
70 var lines = text.replace(/^\s*\n|\n\s*$/g,'').split('\n');
72 if (lines[i].match(/^\s*$/))
74 var white = lines[i].replace(/\S.*/, '')
75 if (prefix === null || white.length < prefix.length)
79 lines[i] = lines[i].replace(prefix, '');
80 return lines.join('\n');
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);
93 if (!ajax) ajax = new XMLHttpRequest();
94 if (!ajax) ajax = new ActiveXObject('Microsoft.XMLHTTP');
95 if (!ajax) throw new Error('Cannot get AJAX object');
97 /* Insert default query */
99 url = url.replace(/[?]/, '?' + query + ',');
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');
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.innerHTML);
116 var name = 'vpaste_s' + scripts.length;
117 var regex = /^[^?]*[?]?(([a-zA-Z0-9.]*)[?&, ]?(.*))$/;
118 var parts = script.src.match(regex);
120 /* Handle header tags */
121 if (!text && !parts[2] || !document.body)
122 return query = parts[1];
125 var html = document.createElement('pre');
126 html.innerHTML = 'Loading..';
127 html.className = script.className + ' vpaste ' + name;
128 document.body.appendChild(html);
130 /* Query the paste */
132 query_paste('POST', vpaste+'view?'+parts[1],
135 query_paste('GET', vpaste+parts[2]+'?'+parts[3],
139 /* Convert all code tags to pastes */
140 function format_code(type) {
141 var tags = document.getElementsByTagName('code');
142 for (var i = 0; i < tags.length; i++) {
144 if (type && tag.className != type)
146 var name = 'vpaste_c' + i;
147 var text = strip_spaces(tag.innerHTML);
148 tag.className += ' ' + name;
149 query_paste('POST', vpaste+'view?'+tag.title,