#!/usr/bin/ruby -w
######
# A ruby no paste script
# author - Miles Crawford, mcrawfor at u dot washington dot edu
# May 05
# Contents of this file available under the GPL, http://www.gnu.org/copyleft/gpl.html
#
# This is syntax coloring heavy lifting is
# via highlight, available at http://www.andre-simon.de
#libraries
require 'cgi'
require 'digest/md5'
#Configuration
highlight_command = "highlight -I"
base_url = "http://endtable.net/paste/"
#prepare some lists
theme_handle = IO.popen("highlight -w", "r")
themes = theme_handle.readlines
theme_handle.close
themes = themes.slice(3, (themes.length - 6))
themes.map!{ |theme|
theme.strip!
}
language_handle = IO.popen("highlight -p", "r")
languages = language_handle.readlines
language_handle.close
languages = languages.slice(3, (languages.length - 6))
languages.map!{ |language|
language.strip!
}
cgi = CGI.new()
if cgi.has_key?("paste")
pasted = cgi["paste"]
language = cgi['language']
theme = cgi['theme']
unless themes.index(theme) && languages.index(language)
exit!
end
#Determine a filename
filename = (Digest::MD5.hexdigest(pasted).slice(1,8)) + ".html"
#Give highlight the pasted text
fh = File.new(filename, "w")
pasted.gsub!(/\r/, '')
fh.write( pasted )
fh.close()
#gather the highlighted HTML
fh = IO.popen(highlight_command + " -S #{language} -s #{theme} #{filename}", "r")
output = fh.read
fh.close()
fh = IO.popen(highlight_command + " -S #{language} -l -s #{theme} #{filename}", "r")
output_ln = fh.read
fh.close()
#add our own links
output_ln.gsub!(/<body>/){
"<body>
<style type='text/css'>
a{
color:green;
}
</style>
<table style='margin:0;background-color:white;width:100%; border: thin green solid;
-moz-border-radius:10px; padding:2px;'>
<tr>
<td syntax='text-align:left;'><a href='#{base_url}#{filename}'>No line Numbers</a></td>
<td style='text-align:center;'><a href='#{base_url}'>New Paste</a></td>
<td style='text-align:center;'>#{base_url}ln#{filename} </td>
<td style='text-align:right;'><a href='#{base_url}ln#{filename}'>Link to this paste</a></td>
</tr>
</table>
</div>"
}
output.gsub!(/<body>/){
"<body>
<style type='text/css'>
a{
color:green;
}
</style>
<table style='margin:0;background-color:white;width:100%; border: thin green solid;
-moz-border-radius:10px; padding:2px;'>
<tr>
<td syntax='text-align:left;'><a href='#{base_url}ln#{filename}'>Line Numbers</a></td>
<td style='text-align:center;'><a href='#{base_url}'>New Paste</a></td>
<td style='text-align:center;'>#{base_url}#{filename} </td>
<td style='text-align:right;'><a href='#{base_url}#{filename}'>Link to this paste</a></td>
</tr>
</table>
</div>"
}
output_ln.gsub!(/Gruby Paste/){
"Gruby Paste"
}
output.gsub!(/Gruby Paste/){
"Gruby Paste"
}
#write out the highlighted text
of = File.open(filename, "w")
of.write(output)
of.close()
of = File.open("ln"+filename, "w")
of.write(output_ln)
of.close()
puts "Location: #{filename}"
puts
else
theme_dropdown = "<select style='display:none;width: 100%;' name='dummytheme' id='dummytheme'>"
themes.each{|theme|
if theme == 'typical'
theme_dropdown += "<option selected value='#{theme}'>#{theme}</option>\n"
else
theme_dropdown += "<option value='#{theme}'>#{theme}</option>\n"
end
}
theme_dropdown += "</select>"
language_dropdown = "<select id='dummy' style='display:none; width: 100%;' name='dummy'>"
languages.each{|language|
if language == 'nedit'
language_dropdown += "<option selected value='#{language}'>#{language}</option>\n"
else
language_dropdown += "<option value='#{language}'>#{language}</option>\n"
end
}
language_dropdown += "</select>"
puts "Content-type: text/html"
puts
puts "
<html>
<head>
<title>Gruby Paste</title>
</head>
<body>
<span style='color:green; font-size:1.2em;'>Gruby No-Paste</span>
<br/>
<form method=post>
<script>
function el(x) { return document.getElementById(x); }
function rtrim(x)
{
var lines = x.split('\\n');
var newlines = new Array();
var counter = 0;
for (var i=0;i<lines.length; i++)
{
newline = lines[i].replace(/ +$/, '');
if (newline != lines[i]) { counter += 1; }
newlines.push(newline);
}
alert('Trimmed ' + counter + (counter==1?' line':' lines'));
return newlines.join('\\n');
}
var more = 0;
function swapdrop(){
next = document.getElementById('dummy');
cur = document.getElementById('language');
if( more > 0){
more = 0;
document.getElementById('swapper').innerHTML = 'More languages';
}else{
more = 1;
document.getElementById('swapper').innerHTML = 'Less languages';
}
next.id = 'language';
next.name= 'language';
cur.id = 'dummy';
cur.name='dummy';
cur.style.display = 'none';
next.style.display = 'block';
}
var theme_more = 0;
function swapthemedrop(){
next = document.getElementById('dummytheme');
cur = document.getElementById('theme');
if( theme_more > 0){
theme_more = 0;
document.getElementById('themeswapper').innerHTML = 'More schemes';
}else{
theme_more = 1;
document.getElementById('themeswapper').innerHTML = 'Less schemes';
}
next.id = 'theme';
next.name= 'theme';
cur.id = 'dummytheme';
cur.name='dummytheme';
cur.style.display = 'none';
next.style.display = 'block';
}
</script>
<style>
.fakelink { cursor: pointer; color: green; border:thin green solid; background-color:white; }
a {color:green;}
</style>
Resize box:
<input type=button value=Thinner class='fakelink' onclick='el(\"pastebox\").cols-=10' />
<input type=button value=Taller class='fakelink' onclick='el(\"pastebox\").rows+=5' />
<input type=button value=Shorter class='fakelink' onclick='el(\"pastebox\").rows-=5' />
<input type=button value=Wider class='fakelink' onclick='el(\"pastebox\").cols+=10' />
<input type=button value='Trim Whitespace' class='fakelink' onclick='el(\"pastebox\").value = rtrim(el(\"pastebox\").value)' />
<br>
<textarea id='pastebox' style='border:thin green solid;padding:8px; -moz-border-radius: 10px;' rows=20 cols=70 name=paste></textarea>
<table>
<tr><td>Language:</td><td>
<select name='language' id='language' style='width:100%;'>
<option value='txt'>Text</option>
<option value='pl'>Perl</option>
<option value='py'>Python</option>
<option value='rb'>Ruby</option>
<option value='html'>HTML</option>
<option value='php'>PHP</option>
<option value='sql'>SQL</option>
<option value='xml'>XML</option>
<option value='js'>JavaScript</option>
<option value='c'>C/C++</option>
</select>
#{language_dropdown} </td>
<td style='width:150px;font-size:.7em;'><a id='swapper' href='javascript:void(0);' onclick='javascript:swapdrop();'>More languages</a></td>
<td style='padding-left:30px;' text-align:right;' rowspan=2><input style='height:30px; width:60px;' class=fakelink type=submit value=Paste!></td></tr>
<tr><td>Color Scheme:</td>
<td>
<select name=theme id=theme style='width:100%;'>
<option value='nedit'>Light</option>
<option value='darkness'>Dark</option>
</select>
#{theme_dropdown}
</td>
<td style='font-size:.7em;'><a id='themeswapper' href='javascript:void(0);' onclick='javascript:swapthemedrop();'>More schemes</a></td>
</tr>
</table>
</form>
<div style='width:100%; text-align:right; font-size: .7em;'>
<a href='http://endtable.net/paste/13f4a228.html'>Source Code</a>
</div>
</body>
</html>
"
end