Add in syntax highlighting
This commit is contained in:
parent
9f9f6665f1
commit
2e3b194bcd
22
main.go
22
main.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/dchest/uniuri"
|
"github.com/dchest/uniuri"
|
||||||
|
"github.com/ewhal/pygments"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -10,9 +11,9 @@ import (
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DIRECTORY = "/tmp/"
|
DIRECTORY = "/tmp/"
|
||||||
ADDRESS = "http://localhost:8080"
|
ADDRESS = "http://localhost:8080/"
|
||||||
LENGTH = 4
|
LENGTH = 4
|
||||||
TEXT = "$ <command> | curl -F 'paste=<-'" + ADDRESS + "\n"
|
TEXT = "$ <command> | curl -F 'p=<-' lang='python'" + ADDRESS + "\n"
|
||||||
PORT = ":8080"
|
PORT = ":8080"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -57,19 +58,26 @@ func save(raw []byte) string {
|
||||||
func pasteHandler(w http.ResponseWriter, r *http.Request) {
|
func pasteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case "GET":
|
case "GET":
|
||||||
param := r.URL.RawQuery
|
param1 := r.URL.Query().Get("p")
|
||||||
if param != "" {
|
param2 := r.URL.Query().Get("lang")
|
||||||
d := DIRECTORY + param
|
if param1 != "" {
|
||||||
|
d := DIRECTORY + param1
|
||||||
s, err := ioutil.ReadFile(d)
|
s, err := ioutil.ReadFile(d)
|
||||||
check(err)
|
check(err)
|
||||||
io.WriteString(w, string(s))
|
|
||||||
|
if param2 != "" {
|
||||||
|
highlight := pygments.Highlight(string(s), param2, "html", "full, style=autumn,", "utf-8")
|
||||||
|
io.WriteString(w, string(highlight))
|
||||||
|
} else {
|
||||||
|
io.WriteString(w, string(s))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
io.WriteString(w, TEXT)
|
io.WriteString(w, TEXT)
|
||||||
}
|
}
|
||||||
case "POST":
|
case "POST":
|
||||||
buf, err := ioutil.ReadAll(r.Body)
|
buf, err := ioutil.ReadAll(r.Body)
|
||||||
check(err)
|
check(err)
|
||||||
io.WriteString(w, ADDRESS+"?"+save(buf)+"\n")
|
io.WriteString(w, ADDRESS+"?p="+save(buf)+"\n")
|
||||||
case "DELETE":
|
case "DELETE":
|
||||||
// Remove the record.
|
// Remove the record.
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue