forked from Abbbyccc/ascii_art_web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
104 lines (89 loc) · 2.18 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"bufio"
"html/template"
"net/http"
"os"
"strings"
)
var templates *template.Template
func main() {
templates = template.Must(template.ParseGlob("templates/*"))
style := http.FileServer(http.Dir("./templates"))
http.Handle("/", style)
http.HandleFunc("/asciiart", posthandler)
// http.HandleFunc("/", posthandler)
http.ListenAndServe(":8080", nil)
}
func posthandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
if r.URL.Path != "/" {
http.Error(w, "ERROR-404\nPage not found(", http.StatusNotFound)
return
}
templates.ExecuteTemplate(w, "index.html", nil)
}
if r.Method == "POST" {
text1 := r.FormValue("text")
font := r.FormValue("fonts")
text := ""
//check if press enter for a new line
if strings.Contains(text1, "\r\n") {
text = strings.ReplaceAll(text1, "\r\n", "\\n")
} else {
text = text1
}
// check if user type in proper ascii art
for _, v := range text {
if !(v >= 32 && v <= 126) {
http.Error(w, "ERROR-400\nBad request!", http.StatusBadRequest)
return
}
}
file, err := os.Open(font + ".txt")
if err != nil {
http.Error(w, "ERROR-400\nBad Request!! \nPlease make sure you select a font.", http.StatusBadRequest)
return
}
defer file.Close()
//read the file
Scanner := bufio.NewScanner(file)
//identify the letters with ascii code
var lines []string
for Scanner.Scan() {
lines = append(lines, Scanner.Text())
}
asciiChrs := make(map[int][]string)
dec := 31
for _, line := range lines {
if line == "" {
dec++
} else {
asciiChrs[dec] = append(asciiChrs[dec], line)
}
}
var c = ""
for i := 0; i < len(text); i++ {
if text[i] == 92 && text[i+1] == 110 {
c = PrintArt(text[:i], asciiChrs) + PrintArt(text[i+2:], asciiChrs)
}
}
if !strings.Contains(text, "\\n") {
c = PrintArt(text, asciiChrs)
}
templates.ExecuteTemplate(w, "index.html", c)
}
}
func PrintArt(n string, y map[int][]string) string {
//prints horizontally
a := []string{}
// prints horizontally
for j := 0; j < len(y[32]); j++ {
for _, letter := range n {
a = append(a, y[int(letter)][j])
}
a = append(a, "\n")
}
b := strings.Join(a, "")
return b
}