diff --git a/detsember/forum/server.go b/detsember/forum/server.go new file mode 100644 index 0000000..a3484b6 --- /dev/null +++ b/detsember/forum/server.go @@ -0,0 +1,55 @@ +package main + +import ( + "fmt" + "html/template" + "net/http" +) + +var page *template.Template + +func main() { + fmt.Println("welcome to forum") + + page = template.Must(template.ParseGlob("templates/*.html")) + + http.Handle("/styles/", http.StripPrefix("/styles/", http.FileServer(http.Dir("styles")))) + + http.HandleFunc("/", IndexHandler) + http.HandleFunc("/profile/", ProfileHandler) + http.HandleFunc("/register/", LoginHandler) + http.HandleFunc("/signin/", LoginHandler) + + http.HandleFunc("/profile/posts/", LoginHandler) // css not reached + http.HandleFunc("/profile/likes/", LoginHandler) // css not reached + + http.HandleFunc("/posts/", ErrHandler) // temporary + http.HandleFunc("/categories/", ErrHandler) // temporary + + fmt.Println("http://localhost:8080") + http.ListenAndServe(":8080", nil) +} + +func IndexHandler(w http.ResponseWriter, r *http.Request) { + if err := page.ExecuteTemplate(w, "home.html", nil); err != nil { + http.Error(w, "Internal Error", http.StatusInternalServerError) + } +} + +func ProfileHandler(w http.ResponseWriter, r *http.Request) { + if err := page.ExecuteTemplate(w, "profile.html", nil); err != nil { + http.Error(w, "Internal Error", http.StatusInternalServerError) + } +} + +func LoginHandler(w http.ResponseWriter, r *http.Request) { + if err := page.ExecuteTemplate(w, "login.html", nil); err != nil { + http.Error(w, "Internal Error", http.StatusInternalServerError) + } +} + +func ErrHandler(w http.ResponseWriter, r *http.Request) { + if err := page.ExecuteTemplate(w, "error.html", nil); err != nil { + http.Error(w, "Internal Error", http.StatusInternalServerError) + } +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..d7aa29d --- /dev/null +++ b/main.go @@ -0,0 +1,269 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "strconv" + "strings" +) + +// 20OCT21 review commit verison + +func main() { + file_name := string(os.Args[1]) + input_text, err_01 := ioutil.ReadFile(file_name) + + if err_01 != nil { + fmt.Println("ebaõnnestus") + os.Exit(1) + } + + // fmt.Println(string(input_text)) + + text_slice := SplitBySpace(string(input_text)) + + // fmt.Println(text_slice) + + text_slice = FindHexBinArticles(text_slice) // convert nr systems + + text := RemoveDoubleSpaces(strings.Join(text_slice, " ")) // empty slice units removed + + // fmt.Println(text) + + text = FixPunctuation(text) // first punctuation check + + next_slice := SplitBySpace(text) + + // strings.Fields(sisesta_string) + + next_slice = FindTriggers(next_slice) // upper lower capitalize + + output_text := RemoveDoubleSpaces(strings.Join(next_slice, " ")) // slice back to single string + + output_text = FixPunctuation(output_text) // final punctuation control + + // fmt.Println(string(output_text)) + + output_text = FixApostrophes(output_text) // apostrophes being checked + + // fmt.Println(string(output_text)) + + os.WriteFile(string(os.Args[2]), []byte(string(output_text)), os.ModePerm) +} + +func RemoveDoubleSpaces(text string) string { + for x := 0; x < len(text)-1; x++ { + if x < len(text)-1 && rune(text[x]) == 32 && rune(text[x+1]) == 32 { + text = text[:x] + text[x+1:] + x = 0 + } + } + return text +} + +func FixPunctuation(text string) string { + text = RemoveDoubleSpaces(text) + + for i := 0; i < len(text); i++ { + if string(text[i]) == "." || string(text[i]) == "," || string(text[i]) == "!" || + string(text[i]) == "?" || string(text[i]) == ":" || string(text[i]) == ";" { + + if i > 0 && string(text[i-1]) == " " { + text = text[:(i-1)] + text[i:] + } + } else if string(text[i]) != " " { + if i > 0 && (string(text[i-1]) == "." || string(text[i-1]) == "," || string(text[i-1]) == "!" || + string(text[i-1]) == "?" || string(text[i-1]) == ":" || string(text[i-1]) == ";") { + text = text[:i] + " " + text[i:] + } + } + } + return text +} + +func FixApostrophes(text string) string { + pair_count := true // after finding first apostrophe will turn false, after second back to true + runes := []rune(text) + mark1 := '‘' + mark2 := '’' + for i, letter := range runes { + if letter == mark1 || letter == mark2 { + + if i > 0 && i < len(runes)-1 && runes[i-1] != 32 && runes[i+1] != 32 { + runes[i] = mark2 + continue + } + + if pair_count { + if i < len(runes)-2 && runes[i+1] == rune(32) { + runes[i] = mark1 + runes = append(runes[:i+1], runes[i+2:]...) + pair_count = false + continue + } else { + runes[i] = mark1 + pair_count = false + continue + } + } + + if !pair_count { + if i > 1 && runes[i-1] == rune(32) { + runes[i] = mark2 + runes = append(runes[:i-1], runes[i:]...) + pair_count = true + continue + } else { + runes[i] = mark2 + pair_count = true + /* + if i < len(runes)-1 { + if rune(runes[i+1]) != ' ' { + helper := runes + runes = append(runes[:i+1], rune(32)) + runes = append(runes, helper[i+1:]...) + } + } + */ + continue + } + } + } + } + text = "" + for _, char := range runes { + text += string(char) + } + return text +} + +func FindHexBinArticles(slice []string) []string { + var x int + for i := range slice { + if strings.Contains(string(slice[i]), "(bin)") || strings.Contains(string(slice[i]), "(hex)") { + x = Converter(strings.ToUpper(slice[i-1]), slice[i][:5]) + slice[i-1] = strconv.Itoa(x) + + slice[i] = strings.TrimPrefix(slice[i], "(bin)") + slice[i] = strings.TrimPrefix(slice[i], "(hex)") + } + if i+1 != len(slice) && (slice[i] == "a" || slice[i] == "A") && len(slice[i+1]) != 0 { + if strings.ContainsAny(string(slice[i+1][0]), "aeiouh") { + slice[i] += "n" + } + } + } + return slice +} + +func FindTriggers(slice []string) []string { // all of those add extra space when completed (fix implemented) + + var x int // how many words to modify + for i := range slice { + + if strings.Contains(string(slice[i]), "(up)") { + slice[i-1] = strings.ToUpper(slice[i-1]) + slice[i] = strings.TrimPrefix(slice[i], "(up)") + } + if strings.Contains(string(slice[i]), "(up,") { + x = int(slice[i][5]) - 48 + for a := i - x; a < i; a++ { + slice[a] = strings.ToUpper(slice[a]) + } + slice[i] = slice[i][7:] + } + + if strings.Contains(string(slice[i]), "(low)") { + slice[i-1] = strings.ToLower(slice[i-1]) + slice[i] = strings.TrimPrefix(slice[i], "(low)") + } + if strings.Contains(string(slice[i]), "(low,") { + x = int(slice[i][6]) - 48 + for b := i - x; b < i; b++ { + slice[b] = strings.ToLower(slice[b]) + } + slice[i] = slice[i][8:] + } + + if strings.Contains(string(slice[i]), "(cap)") { + slice[i-1] = strings.ToUpper(string(slice[i-1][0])) + slice[i-1][1:] + slice[i] = strings.TrimPrefix(slice[i], "(cap)") + } + if strings.Contains(string(slice[i]), "(cap,") { + x = int(slice[i][6]) - 48 + for c := i - x; c < i; c++ { + slice[c] = strings.ToUpper(string(slice[c][0])) + slice[c][1:] + } + slice[i] = slice[i][8:] + } + + } + return slice +} + +func SplitBySpace(text string) []string { + separator := " " + str_temp := "" // temporary string + text_slice := make([]string, 0) + + for i, char := range text { + + if string(char) != separator && i != len(text)-1 { + str_temp += string(char) + } else if str_temp == "(low," || str_temp == "(up," || str_temp == "(cap," { + str_temp += string(char) + continue + } else { + text_slice = append(text_slice, str_temp) + str_temp = "" + } + + if i == len(text)-1 { + text_slice = append(text_slice, str_temp) + } + + } + return text_slice +} + +func Converter(str, system string) int { // converting ok (input check missing) + var x int + var base string + + if system == "(hex)" { + base = "0123456789ABCDEF" + } + if system == "(bin)" { + base = "01" + } + + temp_result := 0 + result := 0 + + for id, digit := range str { + + for base_id, base_nr := range base { + if digit == base_nr { + x = base_id + } + } + z := len(str) - 1 - id + y := len(base) + yVar := y + if z == 0 { + temp_result = x + } else if z == 1 { + temp_result = x * y + } else { + for i := 1; i < z; i++ { + yVar *= y + } + temp_result = x * yVar + } + result += temp_result + y = 0 + z = 0 + } + return result +}