forked from MFreidank/AnkiVim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anki-vim.py
executable file
·82 lines (66 loc) · 2.5 KB
/
anki-vim.py
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
#!/usr/bin/env python3
""" Using VIM to generate textfiles which are directly anki-importable. """
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
import argparse
from os import getenv
from os.path import abspath, join as path_join
from ankivim.cards import create_card
def main():
parser = argparse.ArgumentParser(
description="Use VIM (or the editor of your choice) "
"to comfortably and quickly write textfiles "
"which are directly anki-importable. "
)
parser.add_argument(
"deck", help='Name of the deck to write cards for.'
)
parser.add_argument(
"-e", "--editor",
help="Force using EDITOR, overwriting default behaviours.\n"
"If set to anything else but vim(1), '--editor-args' must "
"also be specified.\n"
"Default behaviour for editor choices:\n"
"1. read environment variable $EDITOR \n"
"2. fall back to vim(1) if $EDITOR is not set.",
action="store", dest="editor", default=None
)
parser.add_argument(
"--editor-args",
help="Arguments to pass to the editor upon calling. "
"Must be specified (simplest case: empty string) when '--editor' "
"is set.\n"
'Expected format:"--arg1 VALUE1 --arg2 VALUE2 ..."\t\n'
"(mind the quotes!)",
action="store", dest="editor_args", default=None
)
args = parser.parse_args()
if args.editor is not None and args.editor_args is None:
raise ValueError()
if args.editor is None:
editor = getenv("EDITOR", "vim")
else:
editor = args.editor
if args.editor_args is None:
# editor args below target vim 7.4, overwrite for other editor choices.
editor_args = (
# set cursor below headers
"-c {}".format(r'/\v\%\n\zs(^$|^[^\%]{1}.*$)'),
# use anki_vim snippets
"-c set filetype=anki_vim",
# latex syntax highlighting
"-c set syntax=tex"
)
else:
if args.editor_args == "":
editor_args = ()
else:
editor_args = args.editor_args.split(",")
deckpath = path_join(abspath("./decks"), args.deck)
content_added = True
while content_added:
# If a card is closed without content or changes, stop
content_added = create_card(
deckpath=deckpath, editor=editor, editor_args=editor_args
)
if __name__ == '__main__':
main()