dodaj do kodu możliwość obsługi wszystkich plików tekstowych niesformatowanych np…
- User
dodaj do kodu możliwość obsługi wszystkich plików tekstowych niesformatowanych np html,htm,csv,svg i innych:
// +build utf8
package main
import ( "flag" "fmt" "io/ioutil" "strings" )
var ogonki = map[string]string{ "pl": "ąćłńóśźżęĄĆŁŃÓŚŹŻĘ", "de": "äöüßÄÖÜẞ", "fr": "éèàçêôûÉÈÀÇÊÔÛ", "es": "áíóúéüñÁÍÓÚÉÜÑ", }
func main() { flagHelp := flag.Bool("help", false, "Wyswietl pomoc") flagLang := flag.String("lang", "pl", "Jezyk ogonkow (pl, de, fr, es)")
codeflag.Parse() if *flagHelp { printHelp() return } inputFiles := flag.Args() if len(inputFiles) == 0 { fmt.Println("OGONKI => Blad: Nie podano sciezki do pliku. Wyswietlenie pomocy: OGONKI help") return } for _, inputFile := range inputFiles { content, err := ioutil.ReadFile(inputFile) if err != nil { fmt.Printf("Blad podczas wczytywania pliku %s: %s\n", inputFile, err) continue } newContent := replaceOgonki(string(content), *flagLang) outputFile := strings.TrimSuffix(inputFile, ".txt") + "_modified.txt" err = ioutil.WriteFile(outputFile, []byte(newContent), 0644) if err != nil { fmt.Printf("Blad podczas zapisywania pliku %s: %s\n", outputFile, err) continue } fmt.Printf("Zamiana ogonkow zakonczona pomyslnie. Nowy plik zostal zapisany jako %s.\n", outputFile) }}
func replaceOgonki(text string, lang string) string { ogonkiLetters, ok := ogonki[lang] if !ok { fmt.Printf("Blad: Nieznany jezyk ogonkow %s.\n", lang) return text }
codefor _, ogonkiLetter := range ogonkiLetters { replacement := string(ogonkiLetter) text = strings.ReplaceAll(text, replacement, getReplacement(replacement)) } return text}
func getReplacement(letter string) string { switch letter { case "ą": return "a"
codecase "Û": return "u" default: return letter }}
- Assistant
// +build utf8
package main
import ( "flag" "fmt" "io/ioutil" "path/filepath" "strings" )