/plaintextmachine/plaintextmachine/plaintextmachine.go
Go | 116 lines | 94 code | 13 blank | 9 comment | 29 complexity | a9093548b40c933a2fb18f1f5a601cf2 MD5 | raw file
1package plaintextmachine 2 3import ( 4 "fmt" 5 "http" 6 "utf8" 7) 8 9var editorHeader = "<!DOCTYPE html>\n" + 10 "<html><head><title>The Plain Text Machine</title>" + 11 "<style>body { " + 12 "font-family: Andale Mono, Courier New, Courier, monospace; " + 13 "margin-right: 10%; " + 14 "margin-left: 10%; " + 15 "margin-top: 2em; " + 16 "line-height: 130%; " + 17 "}" + 18 "textarea {" + 19 "font-family: Andale Mono, Courier New, Courier, monospace; " + 20 "}" + 21 "</style>" 22 23var textHeader = "<!DOCTYPE html>\n" + 24 "<html><head>" + 25 "<style>body { " + 26 "font-family: Andale Mono, Courier New, Courier, monospace; " + 27 "margin-right: 10%; " + 28 "margin-left: 10%; " + 29 "margin-top: 2em; " + 30 "line-height: 130%; " + 31 "}" + 32 "</style>" 33 34var script = "<script>" + 35 "function generate() {" + 36 "var input = document.getElementById('message');" + 37 "var output = document.getElementById('link');" + 38 "var escaped = " + 39 "encodeURIComponent(input.value).replace(/%20/g, '+');" + 40 "if (escaped.length > 2000) {" + 41 "output.innerHTML = 'Message is too long by ' + " + 42 "(escaped.length - 2000) + ' characters.'" + 43 "} else {" + 44 "output.innerHTML = '<a href=\"/show?msg=' + " + 45 "escaped +'\">Show Your Message</a>';" + 46 "}" + 47 "}" + 48 "</script>" 49 50var middle = "</head><body>" 51 52var form = "<h1>The Plain Text Machine</h1>Enter your message:<br>" + 53 "<textarea id=\"message\" cols=\"80\" rows=\"20\"></textarea><br>" + 54 "<button onclick=\"generate()\">Generate Link</button><br>" + 55 "<div id=\"link\"></div>" 56 57var footer = "</body></html>" 58 59func PrintHtml(text *utf8.String, out http.ResponseWriter) { 60 spaces := false 61 62 fmt.Fprint(out, textHeader, middle) 63 for i := 0; i < text.RuneCount(); i++ { 64 currentChar := text.At(i) 65 66 if currentChar == 32 && !spaces { 67 // A first space. 68 fmt.Fprint(out, " ") 69 spaces = true 70 } else { 71 if currentChar == 32 { 72 // Space following another space 73 fmt.Fprint(out, " ") 74 } else if currentChar == 10 { 75 // Newline 76 fmt.Fprint(out, "<br>") 77 } else if currentChar == 9 { 78 // Tab 79 fmt.Fprint(out, " ") 80 } else if currentChar == 38 { 81 // & 82 fmt.Fprint(out, "&") 83 } else if currentChar == 60 { 84 // < 85 fmt.Fprint(out, "<") 86 } else if currentChar == 62 { 87 // > 88 fmt.Fprint(out, ">") 89 } else if currentChar < 31 || currentChar == 128 { 90 // Skip control characters. 91 } else if currentChar < 127 { 92 fmt.Fprintf(out, "%c", currentChar) 93 } else { 94 fmt.Fprintf(out, "&#%d;", text.At(i)) 95 } 96 spaces = false 97 } 98 } 99 fmt.Fprint(out, footer) 100} 101 102func show(w http.ResponseWriter, r *http.Request) { 103 w.Header().Set("Content-Type", "text/html; charset=utf-8") 104 // Get the message from the URL. 105 PrintHtml(utf8.NewString(r.FormValue("msg")), w) 106} 107 108func handle(w http.ResponseWriter, r *http.Request) { 109 w.Header().Set("Content-Type", "text/html; charset=utf-8") 110 fmt.Fprint(w, editorHeader, script, middle, form, footer) 111} 112 113func init() { 114 http.HandleFunc("/", handle) 115 http.HandleFunc("/show", show) 116}