invzhi / ankit

:hammer: CSV export tool for Anki.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ankit

πŸ”¨ CSV export tool for Anki.

Anki

leetcode2anki

leetcode2anki will print CSV data from your LeetCode repository and corresponding question data. It use SQLite to store LeetCode question data. If data is not exist, it will be fetched from LeetCode API.

Therefore, you need to import CSV to your Anki.

Export LeetCode Repository to CSV

leetcode2anki -lang golang > leetcode.csv

Customize for your repository structure

If your LeetCode repository is different from mine, you need to change question and code function in leetcode2anki/main.go:

My LeetCode Repository structure:

LeetCode
β”œβ”€β”€ 0003
β”‚   β”œβ”€β”€ code.go
β”‚   └── code_test.go
β”œβ”€β”€ 0004
β”‚   β”œβ”€β”€ code.go
β”‚   └── code_test.go
└── 0007
    β”œβ”€β”€ code.go
    └── code_test.go
func question(path string, info os.FileInfo) (leetcode.Key, error) {
	if path == "." || !info.IsDir() {
		return nil, nil
	}
	// only handle directory in repository
	id, err := strconv.Atoi(path)
	if err != nil {
		return nil, filepath.SkipDir
	}
	// identify leetcode question by id
	return leetcode.KeyID(id), filepath.SkipDir
}

func code(path string, _ leetcode.Lang) (string, error) {
	fset := token.NewFileSet()

	f, err := parser.ParseFile(fset, filepath.Join(path, "code.go"), nil, parser.ParseComments)
	if err != nil {
		return "", err
	}
	// start from import declarations, then format code
	var w strings.Builder
	if err := format.Node(&w, fset, f.Decls); err != nil {
		return "", err
	}

	return w.String(), nil
}

Python example:

LeetCode
β”œβ”€β”€add-two-numbers.py
β”œβ”€β”€reverse-integer.py
└──two-sum.py
func question(path string, info os.FileInfo) (leetcode.Key, error) {
	if path == "." {
		return nil, nil
	}
	// skip directory in repository
	if info.IsDir() {
		return nil, filepath.SkipDir
	}

	filename := filepath.Base(path)
	ext := filepath.Ext(filename)
	// only handle python file
	if ext != ".py" {
		return nil, nil
	}
	// identify leetcode question by title slug: filename
	slug := strings.TrimSuffix(filename, ext)
	return leetcode.KeyTitleSlug(slug), nil
}

func code(path string, _ leetcode.Lang) (string, error) {
	b, err := ioutil.ReadFile(path)
	return string(b), err
}

Import CSV to Anki

  1. Get Anki note type from Shared Deck.
  2. Import CSV file in Anki.

Import CSV

About

:hammer: CSV export tool for Anki.

License:MIT License


Languages

Language:Go 100.0%