From 2e3b194bcd6b34c841f2a761d66d270062d78037 Mon Sep 17 00:00:00 2001 From: Eliot Whalan Date: Sat, 11 Jun 2016 13:09:28 +1000 Subject: [PATCH] Add in syntax highlighting --- main.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 1455362..5dbc999 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "github.com/dchest/uniuri" + "github.com/ewhal/pygments" "io" "io/ioutil" "net/http" @@ -10,9 +11,9 @@ import ( const ( DIRECTORY = "/tmp/" - ADDRESS = "http://localhost:8080" + ADDRESS = "http://localhost:8080/" LENGTH = 4 - TEXT = "$ | curl -F 'paste=<-'" + ADDRESS + "\n" + TEXT = "$ | curl -F 'p=<-' lang='python'" + ADDRESS + "\n" PORT = ":8080" ) @@ -57,19 +58,26 @@ func save(raw []byte) string { func pasteHandler(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": - param := r.URL.RawQuery - if param != "" { - d := DIRECTORY + param + param1 := r.URL.Query().Get("p") + param2 := r.URL.Query().Get("lang") + if param1 != "" { + d := DIRECTORY + param1 s, err := ioutil.ReadFile(d) 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 { io.WriteString(w, TEXT) } case "POST": buf, err := ioutil.ReadAll(r.Body) check(err) - io.WriteString(w, ADDRESS+"?"+save(buf)+"\n") + io.WriteString(w, ADDRESS+"?p="+save(buf)+"\n") case "DELETE": // Remove the record. }