PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/plaintextmachine/plaintextmachine/plaintextmachine.go

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