]> Pileus Git - vpaste/blob - index.cgi
(no commit message)
[vpaste] / index.cgi
1 #!/bin/bash
2
3 # Copyright (C) 2009 Andy Spencer
4
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # Remove url codings form stdin
16 function get_modeline {
17         modeline=$(
18                 echo "$QUERY_STRING" | 
19                 sed -e 's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g; s/[,&]/ /g' |
20                 xargs echo -e
21         )
22         echo "vim: $modeline"
23 }
24
25 # Extract an uploaded file from standard input
26 # $1 is the boundary delimiter for the file
27 function cut_file {
28         awk "
29                 /--$1/ {st=1};
30                 st==2  {print \$0};
31                 /$1--/ {st=0};
32                 /^\\r$/ && st==1 {st=2};
33         " | head -c -2
34         # Remove trailing ^M's that come with CGI
35 }
36
37 # Print out a generic header
38 function start_html {
39         echo "Content-Type: text/html; charset=UTF-8"
40         echo
41 }
42
43 # Print out a generic header
44 function start_text {
45         echo "Content-Type: text/plain; charset=UTF-8"
46         echo
47 }
48
49 # Format a file for viewing 
50 function do_print {
51         if [ -f "$1" ]; then
52                 input="$1"
53         elif [ -f "db/$1" ]; then
54                 input="db/$1"
55                 trim='1d' # sed command to remove cruft
56         else
57                 echo "Status: 404"
58                 start_text
59                 echo "File '$1' not found"
60                 return
61         fi
62
63
64         if [[ "$REQUEST_URI" == *'?'* ]]; then
65                 # Create a temp file with the provided modeline
66                 output="$(mktemp)"
67                 tmp="$(mktemp)"
68                 cat  "$input" >> "$tmp"
69                 get_modeline  >> "$tmp"
70
71                 # - I have some plugins in ~/.vim
72                 # - Run ex in screen to trick it into thinking that it
73                 #   has a real terminal, not that we also have to set
74                 #   term=xterm-256color in vimrc
75                 HOME=/home/andy \
76                 screen -D -m ex -u vimrc \
77                         '+$d|'$trim     \
78                         '+TOhtml'       \
79                         "+sav! $output" \
80                         '+qall!'        \
81                         "$tmp"
82
83                 start_html
84                 cat "$output" 
85                 rm "$tmp" "$output"
86         else
87                 start_text
88                 sed "$trim" "$input"
89         fi
90 }
91
92
93 # Upload handler
94 function do_upload {
95         output="$(mktemp db/XXXXX)"
96         uri="$SCRIPT_URI$(basename "$output")"
97         (get_modeline; cut_file "$1") > "$output"
98         start_text
99         echo "$uri${QUERY_STRING:+"?"}"
100 }
101
102 # Default index page
103 function do_help {
104 filetypes=$(
105         ls /usr/share/vim/vim{72,files}/syntax/ /home/andy/.vim/after/syntax/ |
106         sed -n 's/.vim$//p' | sort | uniq
107 )
108 uploads=$(ls -t db | head -n 5 | sed "s!^!$SCRIPT_URI!")
109
110 start_html
111 cat - <<EOF
112 <html>
113         <head>
114                 <style>
115                 * { margin:0; padding:0; }
116                 body { margin:1em; }
117                 h4 { margin:1em 0 0 0; }
118                 p,ul,dl,dd,pre,blockquote { margin:0 0 0 2em; }
119                 dt { font-weight:bold; padding:0.5em 0 0 0; }
120                 blockquote { width:50em; font-size:small; }
121                 </style>
122         </head>
123         <body>
124                 <h4>NAME</h4>
125                 <p>vpaste: Vim enabled pastebin</p>
126
127                 <h4>SYNOPSIS</h4>
128                 <pre> vpaste file [option=value,..]</pre>
129                 <pre> &lt;command&gt; | vpaste [option=value,..]</pre>
130                 <br>
131                 <pre> &lt;command&gt; | curl -F 'x=<-' $SCRIPT_URI[?option=value,..]</pre>
132
133                 <h4>DESCRIPTION</h4>
134                 <p>Add <b>?[option[=value],..]</b> to make your text a rainbow.</p>
135                 <p>Options specified when uploading are used as defaults.</p>
136
137                 <h4>OPTIONS</h4>
138                 <dl>
139                 <dt>ft, filetype={filetype}</dt>
140                 <dd>A filetype to use for highlighting, see FILETYPES</dd>
141                 <dt>bg, background={light|dark}</dt>
142                 <dd>Background color to use for the page</dd>
143                 <dt>et, expandtab</dt>
144                 <dd>Expand tabs to spaces</dd>
145                 <dt>ts, tabstop=[N]</dt>
146                 <dd>Number of spaces to use for tabs when <b>et</b> is set</dd>
147                 <dt>...</dt>
148                 <dd>See :help modeline for more information</dd>
149                 </dl>
150
151                 <h4>SOURCE</h4>
152                 <ul>
153                 <li><a href="vpaste?ft=sh">vpaste</a>
154                 <li><a href="index.cgi?ft=sh">index.cgi</a>
155                     <a href="vimrc?ft=vim">vimrc</a>
156                     <a href="htaccess?ft=apache">htaccess</a>
157                 <li><a href="2html-et.patch?ft=diff">2html-et.patch</a>
158                 </ul>
159
160                 <h4>LATEST UPLOADS</h4>
161                 <ul>$(for uri in ${uploads[@]}; do
162                         echo "<li><a href='$uri'>$uri</a>"
163                 done)</ul>
164
165                 <h4>FILETYPES</h4>
166                 <blockquote>$filetypes</blockquote>
167         </body>
168 </html>
169 EOF
170 }
171
172 # Main
173 pathinfo="${SCRIPT_URL/*vpaste\/}"
174 boundary="${CONTENT_TYPE/*boundary\=/}"
175
176 if [ "$pathinfo" ]; then
177         do_print "$pathinfo"
178 elif [ "$CONTENT_TYPE" ]; then
179         do_upload "$boundary"
180 else
181         do_help
182 fi