#!/bin/bash # Copyright (C) 2009 Andy Spencer # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # Remove url codings form stdin function get_modeline { modeline=$( echo "$QUERY_STRING" | sed -e 's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g; s/[,&]/ /g' | xargs echo -e ) echo "vim: $modeline" } # Extract an uploaded file from standard input # $1 is the boundary delimiter for the file function cut_file { awk " /--$1/ {st=1}; st==2 {print \$0}; /$1--/ {st=0}; /^\\r$/ && st==1 {st=2}; " | head -c -2 # Remove trailing ^M's that come with CGI } # Print out a generic header function header { echo "Content-Type: $1; charset=UTF-8" echo } # Format a file for viewing function do_print { if [ -f "$1" ]; then input="$1" elif [ -f "db/$1" ]; then input="db/$1" trim='1d' # sed command to remove cruft else echo "Status: 404 Not Found" header text/plain echo "File '$1' not found" return fi if [[ "$REQUEST_URI" == *'?'* ]]; then # Create a temp file with the provided modeline output="$(mktemp)" tmp="$(mktemp)" cat "$input" >> "$tmp" get_modeline >> "$tmp" # - I have some plugins in ~/.vim # - Run ex in screen to trick it into thinking that it # has a real terminal, not that we also have to set # term=xterm-256color in vimrc HOME=/home/andy \ screen -D -m ex -u vimrc \ '+$d|'$trim \ '+%s/ //g' \ '+TOhtml' \ "+sav! $output" \ '+qall!' \ "$tmp" header text/html cat "$output" rm "$tmp" "$output" else header text/plain sed "$trim" "$input" fi } # Upload handler function do_upload { output="$(mktemp db/XXXXX)" uri="$SCRIPT_URI$(basename "$output")${QUERY_STRING:+"?"}" (get_modeline; cut_file "$1") > "$output" echo "Status: 302 Found" echo "Location: $uri" header text/plain echo "$uri" } # Default index page function do_help { filetypes=$( ls /usr/share/vim/vim{72,files}/syntax/ /home/andy/.vim/after/syntax/ | sed -n 's/.vim$//p' | sort | uniq ) uploads=$(ls -t db | head -n 5 | sed "s!^!$SCRIPT_URI!") header text/html cat - <


NAME

vpaste: Vim enabled pastebin

SYNOPSIS

 vpaste file [option=value,..]
 <command> | vpaste [option=value,..]

 <command> | curl -F 'x=<-' $SCRIPT_URI[?option=value,..]

DESCRIPTION

Add ?[option[=value],..] to make your text a rainbow.

Options specified when uploading are used as defaults.

OPTIONS

ft, filetype={filetype}
A filetype to use for highlighting, see above menu for supported types
bg, background={light|dark}
Background color to use for the page
et, expandtab
Expand tabs to spaces
ts, tabstop=[N]
Number of spaces to use for tabs when et is set
...
See :help modeline for more information

SOURCE

LATEST UPLOADS

EOF } # Main pathinfo="${SCRIPT_URL/*vpaste\/}" if [ "$pathinfo" ]; then do_print "$pathinfo" elif [ "$CONTENT_TYPE" ]; then do_upload ${CONTENT_TYPE/*boundary\=/} else do_help fi