Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/bash
# Author:    Rainer Mueller <raimue@codingfarm.de>
# Version:   1.6
# License:   Public Domain

usage() {
    cat <<END<<END
Usage: $argv0 [options] [files...]

Options:
    -h, --help          display this help
    -l, --lang <lang>   set language of the paste
    -p, --private       make paste private

If --lang is not specified, this script will try to determine the type of each
file automatically based on the extension. If no files are given on the
command line it reads from standard input.
ENDEND
}

map-lang() {
    local file="$1"
    local ext=${file##*.}
    local lang=""

    # Based on file name
    case $ext in
        css|d|diff|go|ini|io|java|lisp|lua|nu|php|scala|sql|tex)
            lang=$ext
            ;;
        as)
            lang=actionscript
            ;;
        c|cpp|cc|cxx|h|hpp|hh|hxx)
            lang=c++
            ;;
        clj)
            lang=clojure
            ;;
        conf)
            lang=apache
            ;;
        cs)
            lang=csharp
            ;;
        erb|rhtml)
            lang=html_rails
            ;;
        erl)
            lang=erlang
            ;;
        f|for|f90|f95)
            lang=fortran
            ;;
        js)
            lang=javascript
            ;;
        htm|html|xml)
            lang=html
            ;;
        hs)
            lang=haskell
            ;;
        lhs)
            lang=literate_haskell
            ;;
        m|mm)
            lang=objective-c++
            ;;
        Makefile|makefile|GNUmakefile)
            lang=makefile
            ;;
        py)
            lang=python
            ;;
        pas)
            lang=pascal
            ;;
        pl)
            lang=perl
            ;;
        rb)
            lang=ruby_on_rails
            ;;
        scm|ss)
            lang=scheme
            ;;
        *sh)
            lang=shell-unix-generic
            ;;
        tpl)
            lang=smarty
            ;;
        yaml|yml)
            lang=yaml
            ;;
    esac

    if [ "$lang" != "" ]; then
        echo $lang
        return
    fi

    # Executable files are usually shell scripts (who would pastie a binary?)
    if [ -x "$file" ]; then
        echo shell-unix-generic
        return
    fi
}

# default settings
private=0
lang="auto"
file="-"
files=()

# save name of the script
argv0=$(basename $0)

# parse arguments
while [ $# -gt 0 ]; do
    case $1 in
        -h|--help)
            usage
            exit 1
            ;;
        -l|--lang)
            shift
            lang=$1
            ;;
        -p|--private)
            private=1
            ;;
        *)
            # Everything else is a file
            files=( ${files[@]} $1 )
            ;;
    esac
    shift
done

# read files
if [ ${#files[@]} -gt 0 ]; then
    # Create temporary file
    tmpfile=$(mktemp -t $argv0.XXXXX)
    trap "rm -f $tmpfile" EXIT

    for f in ${files[@]}; do
        if [ ! -r "$f" ]; then
            echo "$f: cannot read file" >&2
            exit 1
        fi
        if [ ! -f "$f" ]; then
            echo "$f: not a regular file" >&2
            exit 1
        fi
        if [ "$lang" == "auto" ]; then
            flang=$(map-lang "$f")
        fi
        if [ ${#files[@]} -gt 1 ]; then
            if [ -z "$flang" ]; then
                echo "## $f" >> $tmpfile
            else
                echo "## $f [$flang]" >> $tmpfile
            fi
        else
            if [ "$lang" == "auto" ]; then
                lang=$flang
            fi
        fi
        cat $f >> $tmpfile
    done
    file="$tmpfile"
fi

# pastie now!

if [ "$lang" == "auto" ]; then
    lang="plain_text"
fi

auth='burger'
# Enable this in case the authorization token ever changes
#REGEX="\$('paste_authorization')\.value='\(.*\)';"
#auth=$(curl -s -L http://pastie.org/pastes/new \
#        | sed -e "/$REGEX/!d" -e "/$REGEX/s/$REGEX/\1/")

url=$(curl http://pastie.org/pastes \
    -F "paste[parser]=$lang" \
    -F "paste[body]=<$file" \
    -F "paste[restricted]=$private" \
    -F "paste[authorization]=$auth" \
    -s -L -o /dev/null -w "%{url_effective}")

echo "$url"