Ejemplo n.º 1
0
func main() {
	format := flag.String("f", "", "use Golang template for log message")
	listOnly := flag.Bool("l", false, "only print unknown word")
	lineOnly := flag.Bool("L", false, "print line with unknown word")

	// TODO based on OS (Windows vs. Linux)
	dictPath := flag.String("path", ".:/usr/local/share/hunspell:/usr/share/hunspell", "Search path for dictionaries")

	// TODO based on environment variable settings
	dicts := flag.String("d", "en_US", "dictionaries to load")

	personalDict := flag.String("p", "", "personal wordlist file")

	flag.Parse()
	args := flag.Args()

	if *listOnly {
		defaultLog = defaultWord
	}

	if *lineOnly {
		defaultLog = defaultLine
	}

	if len(*format) > 0 {
		t, err := template.New("custom").Parse(*format)
		if err != nil {
			log.Fatalf("Unable to compile log format: %s", err)
		}
		defaultLog = t
	}

	affFile := ""
	dicFile := ""
	for _, base := range filepath.SplitList(*dictPath) {
		affFile = filepath.Join(base, *dicts+".aff")
		dicFile = filepath.Join(base, *dicts+".dic")
		//log.Printf("Trying %s", affFile)
		_, err1 := os.Stat(affFile)
		_, err2 := os.Stat(dicFile)
		if err1 == nil && err2 == nil {
			break
		}
		affFile = ""
		dicFile = ""
	}

	if affFile == "" {
		log.Fatalf("Unable to load %s", *dicts)
	}

	log.Printf("Loading %s %s", affFile, dicFile)
	timeStart := time.Now()
	h, err := gospell.NewGoSpell(affFile, dicFile)
	timeEnd := time.Now()

	// note: 10x too slow
	log.Printf("Loaded in %v", timeEnd.Sub(timeStart))
	if err != nil {
		log.Fatalf("%s", err)
	}

	if *personalDict != "" {
		raw, err := ioutil.ReadFile(*personalDict)
		if err != nil {
			log.Fatalf("Unable to load personal dictionary %s: %s", *personalDict, err)
		}
		duplicates, err := h.AddWordList(bytes.NewReader(raw))
		if err != nil {
			log.Fatalf("Unable to process personal dictionary %s: %s", *personalDict, err)
		}
		if len(duplicates) > 0 {
			for _, word := range duplicates {
				log.Printf("Word %q in personal dictionary already exists in main dictionary", word)
			}
		}
	}

	// stdin support
	if len(args) == 0 {
		raw, err := ioutil.ReadAll(os.Stdin)
		if err != nil {
			log.Fatalf("Unable to read Stdin: %s", err)
		}
		pt, _ := plaintext.NewIdentity()
		out := gospell.SpellFile(h, pt, raw)
		for _, diff := range out {
			diff.Filename = "stdin"
			diff.Path = ""
			buf := bytes.Buffer{}
			defaultLog.Execute(&buf, diff)
			// goroutine-safe print to os.Stdout
			stdout.Println(buf.String())
		}
	}
	for _, arg := range args {
		// ignore directories
		if f, err := os.Stat(arg); err != nil || f.IsDir() {
			continue
		}

		raw, err := ioutil.ReadFile(arg)
		if err != nil {
			log.Fatalf("Unable to read %q: %s", arg, err)
		}
		pt, err := plaintext.ExtractorByFilename(arg)
		if err != nil {
			continue
		}
		out := gospell.SpellFile(h, pt, raw)
		for _, diff := range out {
			diff.Filename = filepath.Base(arg)
			diff.Path = arg
			buf := bytes.Buffer{}
			defaultLog.Execute(&buf, diff)
			// goroutine-safe print to os.Stdout
			stdout.Println(buf.String())
		}
	}
}
Ejemplo n.º 2
0
func main() {
	config, err := ioutil.ReadFile(".spelling.toml")
	if err != nil {
		log.Fatalf("Unable to reading config: %s", err)
	}
	//log.Printf("JSON: %s", cson.ToJSON([]byte(config)))
	s := Dictionary{}
	err = toml.Unmarshal([]byte(config), &s)
	if err != nil {
		log.Printf("out : %+v", s)
		log.Fatalf("err = %v", err)
	}
	if s.Language == "" {
		s.Language = "en_US"
	}
	if s.Language != "en_US" {
		log.Fatalf("Only support en_US: got %q", s.Language)
	}
	gs, err := gospell.NewGoSpell("/usr/local/share/hunspell/en_US.aff", "/usr/local/share/hunspell/en_US.dic")
	if err != nil {
		log.Fatalf("Unable to load dictionary: %s", err)
	}

	for _, wordfile := range s.Extra {
		_, err := gs.AddWordListFile(wordfile)
		if err != nil {
			log.Printf("Unable to read word list %s: %s", wordfile, err)
		}
	}

	for _, word := range s.Additions {
		log.Printf("Adding %q", word)
		gs.AddWordRaw(word)
	}

	if len(s.FileSet) == 0 {
		s.FileSet = append(s.FileSet, DictionaryFileSet{
			FileSet: FileSet{
				Path:    ".",
				Include: []string{"*"},
				Exclude: []string{".git"},
			},
		})
	}
	finalExit := 0
	for _, fs := range s.FileSet {
		if fs.Path == "" {
			fs.Path = "."
		}
		filepath.Walk(fs.Path, fs.visit)
		for _, filename := range fs.Matches {
			raw, err := ioutil.ReadFile(filename)
			if err != nil {
				log.Printf("Unable to read %q: %s", filename, err)
				finalExit = finalExit | 2
				continue
			}
			pt, err := plaintext.ExtractorByFilename(filename)
			if err != nil {
				continue
			}
			out := gospell.SpellFile(gs, pt, raw)
			for _, diff := range out {
				diff.Filename = filepath.Base(filename)
				diff.Path = filename
				finalExit = finalExit | 1
				log.Printf("Got a %s:%d %s", diff.Path, diff.LineNum, diff.Original)
			}
		}
	}
	os.Exit(finalExit)
}