From 3d22aa63fdac50c9f675ba67c4f7a6e69c2ec0f1 Mon Sep 17 00:00:00 2001 From: Eliot Whalan Date: Thu, 23 Jun 2016 20:41:45 +1000 Subject: [PATCH] Try syntax highlighting --- assets/syntax.html | 54 ++++++++++++++++++++++++++++++++++++++++++++++ main.go | 36 ++++++++++++++++++++++--------- 2 files changed, 80 insertions(+), 10 deletions(-) create mode 100644 assets/syntax.html diff --git a/assets/syntax.html b/assets/syntax.html new file mode 100644 index 0000000..3d23d8d --- /dev/null +++ b/assets/syntax.html @@ -0,0 +1,54 @@ + + + + + + + + %s + + + + + + + + + + + + + + + + +
+ + +
+ %s +
+ +
+ Raw + +
+
+ + + + + + + + + + + diff --git a/main.go b/main.go index 4c48685..be9f394 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "html" "html/template" "io" + "io/ioutil" "log" "net/http" @@ -212,7 +213,14 @@ func getPaste(paste string, lang string) string { if err == sql.ErrNoRows { return "Error invalid paste" } else { - return html.UnescapeString(s) + if lang == "" { + return html.UnescapeString(s) + } else { + high, err := highlight(s, lang) + check(err) + return high + + } } } @@ -223,16 +231,24 @@ func pasteHandler(w http.ResponseWriter, r *http.Request) { lang := vars["lang"] s := getPaste(paste, lang) link := ADDRESS + "/raw/" + paste - p := &Page{ - Title: paste, - Body: []byte(s), - Link: link, + if lang == "" { + p := &Page{ + Title: paste, + Body: []byte(s), + Link: link, + } + t, err := template.ParseFiles("assets/paste.html") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + t.Execute(w, p) + + } else { + dat, err := ioutil.ReadFile("assets/syntax.html") + check(err) + fmt.Fprintf(w, string(dat), paste, paste, highlight, link) + } - t, err := template.ParseFiles("assets/paste.html") - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - } - t.Execute(w, p) } func rawHandler(w http.ResponseWriter, r *http.Request) {