Try syntax highlighting

This commit is contained in:
Eliot Whalan 2016-06-23 20:41:45 +10:00
parent 673e6ceb4e
commit 5c9c3ef187
2 changed files with 80 additions and 10 deletions

54
assets/syntax.html Normal file
View File

@ -0,0 +1,54 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>%s</title>
<!-- Material Design fonts -->
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Roboto:300,400,500,700">
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/bootstrap.material-design/0.5.10/css/bootstrap-material-design.min.css" integrity="sha256-j3CLSRG31GkOu6kaeLh7XsRgL2YNvRl9aOtXoAYt320=" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/bootstrap.material-design/0.5.10/css/ripples.min.css" integrity="sha256-+Og2qJI9qzvKYwhGo/LYXg0FzE1BhEQfDsUSjKXQ3Bg=" crossorigin="anonymous">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="page-header">
<h1>%s</h1>
</div>
<div class="well" style="padding-top: 10px;">
%s
</div>
<div class="pull-right">
<a href="%s" class="btn btn-raised btn-primary">Raw</a>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/bootstrap.material-design/0.5.10/js/material.min.js" integrity="sha256-uZbIqasulk7Y9yEwknbeQ0FpF3aUhtPwuggbpvQaI8Y=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/bootstrap.material-design/0.5.10/js/ripples.min.js" integrity="sha256-TY/EO/++Ug/P+fSBjaqlmtuphCBKwlP7TOnS+SGnN8g=" crossorigin="anonymous"></script>
<script>
$.material.init();
</script>
</body>
</html>

16
main.go
View File

@ -10,6 +10,7 @@ import (
"html" "html"
"html/template" "html/template"
"io" "io"
"io/ioutil"
"log" "log"
"net/http" "net/http"
@ -212,7 +213,14 @@ func getPaste(paste string, lang 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 {
high, err := highlight(s, lang)
check(err)
return high
}
} }
} }
@ -223,6 +231,7 @@ func pasteHandler(w http.ResponseWriter, r *http.Request) {
lang := vars["lang"] lang := vars["lang"]
s := getPaste(paste, lang) s := getPaste(paste, lang)
link := ADDRESS + "/raw/" + paste link := ADDRESS + "/raw/" + paste
if lang == "" {
p := &Page{ p := &Page{
Title: paste, Title: paste,
Body: []byte(s), Body: []byte(s),
@ -233,6 +242,13 @@ func pasteHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} }
t.Execute(w, p) t.Execute(w, p)
} else {
dat, err := ioutil.ReadFile("assets/syntax.html")
check(err)
fmt.Fprintf(w, string(dat), paste, paste, highlight, link)
}
} }
func rawHandler(w http.ResponseWriter, r *http.Request) { func rawHandler(w http.ResponseWriter, r *http.Request) {