Rework syntax highlighting

This commit is contained in:
Eliot Whalan 2016-06-23 19:21:37 +10:00
parent 0ecfffe397
commit a778cefe6d
No known key found for this signature in database
GPG Key ID: C0A42175139840D6
1 changed files with 15 additions and 13 deletions

26
main.go
View File

@ -191,22 +191,17 @@ func saveHandler(w http.ResponseWriter, r *http.Request) {
} }
func langHandler(w http.ResponseWriter, r *http.Request) { func highlight(s string, lang string) (string, error) {
vars := mux.Vars(r)
paste := vars["pasteId"]
lang := vars["lang"]
s := getPaste(paste)
highlight, err := pygments.Highlight(html.UnescapeString(s), html.EscapeString(lang), "html", "full, style=autumn,linenos=True, lineanchors=True,anchorlinenos=True,", "utf-8") highlight, err := pygments.Highlight(html.UnescapeString(s), html.EscapeString(lang), "html", "full, style=autumn,linenos=True, lineanchors=True,anchorlinenos=True,", "utf-8")
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) return "", err
return
} }
io.WriteString(w, highlight) return highlight, nil
} }
func getPaste(paste string) string { func getPaste(paste string, lang string) string {
param1 := html.EscapeString(paste) param1 := html.EscapeString(paste)
db, err := sql.Open("mysql", DATABASE) db, err := sql.Open("mysql", DATABASE)
var s string var s string
@ -217,7 +212,13 @@ func getPaste(paste string) string {
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return "Error invalid paste" return "Error invalid paste"
} else { } else {
if lang == "" {
return html.UnescapeString(s) return html.UnescapeString(s)
} else {
lang, err := highlight(s, lang)
check(err)
return lang
}
} }
} }
@ -225,7 +226,8 @@ func getPaste(paste string) string {
func pasteHandler(w http.ResponseWriter, r *http.Request) { func pasteHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
paste := vars["pasteId"] paste := vars["pasteId"]
s := getPaste(paste) lang := vars["lang"]
s := getPaste(paste, lang)
link := ADDRESS + "/raw/" + paste link := ADDRESS + "/raw/" + paste
p := &Page{ p := &Page{
Title: paste, Title: paste,
@ -242,7 +244,7 @@ func pasteHandler(w http.ResponseWriter, r *http.Request) {
func rawHandler(w http.ResponseWriter, r *http.Request) { func rawHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
paste := vars["pasteId"] paste := vars["pasteId"]
s := getPaste(paste) s := getPaste(paste, "")
io.WriteString(w, s) io.WriteString(w, s)
} }
@ -251,7 +253,7 @@ func main() {
router := mux.NewRouter() router := mux.NewRouter()
router.HandleFunc("/p/{pasteId}", pasteHandler) router.HandleFunc("/p/{pasteId}", pasteHandler)
router.HandleFunc("/raw/{pasteId}", rawHandler) router.HandleFunc("/raw/{pasteId}", rawHandler)
router.HandleFunc("/p/{pasteId}/{lang}", langHandler) router.HandleFunc("/p/{pasteId}/{lang}", pasteHandler)
router.HandleFunc("/save", saveHandler) router.HandleFunc("/save", saveHandler)
router.HandleFunc("/save/{output}", saveHandler) router.HandleFunc("/save/{output}", saveHandler)
router.HandleFunc("/del/{pasteId}/{delKey}", delHandler) router.HandleFunc("/del/{pasteId}/{delKey}", delHandler)