Sprunge plugin detects the syntax

This commit is contained in:
Colin Shea 2011-11-27 02:46:41 -07:00
commit 3f3da1eb47

View file

@ -1,62 +1,62 @@
# Contributed and SLIGHTLY modded by Matt Parnell/ilikenwf <parwok -at- gmail> # Contributed and SLIGHTLY modded by Matt Parnell/ilikenwf <parwok -at- gmail>
# Created by the blogger at the URL below...I don't know where to find his/her name # Created by the blogger at the URL below...I don't know where to find his/her name
# Original found at http://www.shellperson.net/sprunge-pastebin-script/ # Original found at http://www.shellperson.net/sprunge-pastebin-script/
usage() { usage() {
description | fmt -s >&2 description | fmt -s >&2
} }
description() { description() {
cat << HERE cat << HERE
DESCRIPTION DESCRIPTION
Upload data and fetch URL from the pastebin http://sprunge.us Upload data and fetch URL from the pastebin http://sprunge.us
USAGE USAGE
$0 filename.txt $0 filename.txt
$0 text string $0 text string
$0 < filename.txt $0 < filename.txt
piped_data | $0 piped_data | $0
NOTES NOTES
-------------------------------------------------------------------------- --------------------------------------------------------------------------
* INPUT METHODS * * INPUT METHODS *
$0 can accept piped data, STDIN redirection [<filename.txt], text strings following the command as arguments, or filenames as arguments. Only one of these methods can be used at a time, so please see the note on precedence. Also, note that using a pipe or STDIN redirection will treat tabs as spaces, or disregard them entirely (if they appear at the beginning of a line). So I suggest using a filename as an argument if tabs are important either to the function or readability of the code. $0 can accept piped data, STDIN redirection [<filename.txt], text strings following the command as arguments, or filenames as arguments. Only one of these methods can be used at a time, so please see the note on precedence. Also, note that using a pipe or STDIN redirection will treat tabs as spaces, or disregard them entirely (if they appear at the beginning of a line). So I suggest using a filename as an argument if tabs are important either to the function or readability of the code.
* PRECEDENCE * * PRECEDENCE *
STDIN redirection has precedence, then piped input, then a filename as an argument, and finally text strings as an arguments. STDIN redirection has precedence, then piped input, then a filename as an argument, and finally text strings as an arguments.
EXAMPLE: EXAMPLE:
echo piped | "$0" arguments.txt < stdin_redirection.txt echo piped | "$0" arguments.txt < stdin_redirection.txt
In this example, the contents of file_as_stdin_redirection.txt would be uploaded. Both the piped_text and the file_as_argument.txt are ignored. If there is piped input and arguments, the arguments will be ignored, and the piped input uploaded. In this example, the contents of file_as_stdin_redirection.txt would be uploaded. Both the piped_text and the file_as_argument.txt are ignored. If there is piped input and arguments, the arguments will be ignored, and the piped input uploaded.
* FILENAMES * * FILENAMES *
If a filename is misspelled or doesn't have the necessary path description, it will NOT generate an error, but will instead treat it as a text string and upload it. If a filename is misspelled or doesn't have the necessary path description, it will NOT generate an error, but will instead treat it as a text string and upload it.
-------------------------------------------------------------------------- --------------------------------------------------------------------------
HERE HERE
exit exit
} }
sprunge() { sprunge() {
if [ -t 0 ]; then if [ -t 0 ]; then
echo Running interactively, checking for arguments... >&2
if [ "$*" ]; then if [ "$*" ]; then
echo Arguments present... >&2
if [ -f "$*" ]; then if [ -f "$*" ]; then
echo Uploading the contents of "$*"... >&2 # Use python to attempt to detect the syntax
syntax=$(echo "try:
from pygments.lexers import get_lexer_for_filename
print(get_lexer_for_filename('$*').aliases[0])
except:
print('text')" | python)
cat "$*" cat "$*"
else else
echo Uploading the text: \""$*"\"... >&2
echo "$*" echo "$*"
fi | curl -F 'sprunge=<-' http://sprunge.us fi | curl -F 'sprunge=<-' http://sprunge.us
else else
echo No arguments found, printing USAGE and exiting. >&2
usage usage
fi fi
else else
echo Using input from a pipe or STDIN redirection... >&2
while read -r line ; do while read -r line ; do
echo $line echo $line
done | curl -F 'sprunge=<-' http://sprunge.us done | curl -F 'sprunge=<-' http://sprunge.us