Skip to content

Commit e9692b3

Browse files
committed
Support :ATTR_ID: property
1 parent d0f17f7 commit e9692b3

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

README.org

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ file. Just make some Anki cards from org files and Press =Ctrl+O= key in Anki wh
77
are reviewing one of the cards. This plugin makes workflow that going back to your original
88
org-heading and text easily without any extra backlink field.
99

10-
* How to
10+
* How to Use
1111
#+ATTR_HTML: :width 500px
1212
[[file:screencast/sc_1.gif]]
1313

1414
1) Install [[https://ankiweb.net/shared/info/1094177960][this plugin]].
15-
- please copy and paste =1094177960= into your Anki 2.1.
15+
- Please copy and paste =1094177960= into your Anki 2.1.
1616
2) Change your default =org-directory= path(=~/org=) in the =open-org-note= plugin.
1717
- Go to =tools= menu -> Click =Add-ons= -> Select =Open Org Note= -> Click the
1818
=Config= Button.
@@ -26,7 +26,7 @@ org-heading and text easily without any extra backlink field.
2626
have [[https://github.com/BurntSushi/ripgrep][ripgrep]] program in your system, this searching will much faster.
2727
* TODOs
2828
- [X] Implement a initial anki plugin.
29-
- [ ] Support [[https://github.com/vascoferreira25/org-mode-incremental-reading][the org-mode-incremental-reading]]
29+
- [X] Support [[https://github.com/vascoferreira25/org-mode-incremental-reading][the org-mode-incremental-reading]]
3030
- =org-anki= and =anki-editor= use the tree structure for card design. But
3131
=org-mode-incremental-reading= adds a [[https://orgmode.org/manual/Drawers.html][property drawer]] for the card design to
3232
handle the long text such as book and article sytles.

__init__.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ def search_in_org(file, note_id):
4949
data = open(file, "r").read()
5050
except Exception as e:
5151
print("Openfile: open: %s" % str(e))
52-
return None
52+
return ()
5353

5454
res = config["note_match"].format(note_id=note_id)
5555
found = re.compile(res, flags=re.MULTILINE).search(data)
5656
if found:
57-
return (found.start(0), found.end(0))
57+
return (found.group(1), found.start(0), found.end(0))
5858

59-
return None
59+
return ()
6060

6161

6262
def lru_file_cache(func):
@@ -89,6 +89,8 @@ def find_anki_note(note_id, org_dir='~/org'):
8989
search_path = os.path.expanduser(org_dir)
9090
use_ripgrep = config["use_ripgrep"]
9191
rg_opts = config["ripgrep_opts"].split(" ")
92+
93+
note_type = "ANKI_NOTE_ID"
9294
if use_ripgrep and shutil.which(rg_opts[0]):
9395
pat = config["note_match"].format(note_id="(.+?)")
9496
rg_ret = subprocess.run(rg_opts.split(' ') + ['--json', pat, f'"{search_path}"'], stdout=subprocess.PIPE).stdout.decode('utf-8')
@@ -105,7 +107,14 @@ def find_anki_note(note_id, org_dir='~/org'):
105107
abs_offset = js_line["data"]["absolute_offset"]
106108
sub_start = js_line["data"]["submatches"][0]["start"]
107109
sub_end = js_line["data"]["submatches"][0]["end"]
108-
return org_name, (abs_offset + sub_start, abs_offset+sub_end)
110+
match_text = js_line["data"]["submatches"][0]["match"]
111+
112+
if match_text:
113+
found_note_type = re.findall(pat, match_text["text"])
114+
if found_note_type:
115+
note_type = found_note_type[0]
116+
117+
return org_name, (note_type, abs_offset + sub_start, abs_offset+sub_end)
109118
else:
110119
for org_name in glob(os.path.join(search_path, '**/*.org'), recursive=True):
111120
docs = search_in_org(org_name, note_id)
@@ -120,8 +129,10 @@ def open_anki_note(note_id):
120129
org_file, found_note = find_anki_note(note_id, org_dir)
121130
# print('debug:', org_file, found_note)
122131
if org_file and found_note:
123-
char_pos = found_note[0]
124-
os.system(config["exec"].format(org_file=org_file, char_pos=char_pos))
132+
note_type = found_note[0]
133+
char_pos_begin = found_note[1]
134+
char_pos_end = found_note[2]
135+
os.system(config["exec"].format(org_file=org_file, note_type=note_type, char_pos_begin=char_pos_begin, char_pos_end=char_pos_end))
125136
found = True
126137
break
127138

config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"use_index_cache": true,
66
"use_ripgrep": true,
77
"ripgrep_opts": "ripgrep -ni",
8-
"note_match": "^\\s*:ANKI_NOTE_ID:\\s*\\\"?{note_id}\\\"?\\b",
9-
"exec": "emacsclient -nce '(progn (select-frame-set-input-focus (selected-frame)) (find-file \"{org_file}\") (goto-char {char_pos}) (org-back-to-heading t) (recenter))'",
8+
"note_match": "^\\s*:(ANKI_NOTE_ID|ATTR_ID):\\s*\\\"?{note_id}\\\"?\\b",
9+
"exec": "emacsclient -nce '(progn (select-frame-set-input-focus (selected-frame)) (find-file \"{org_file}\") (goto-char {char_pos_end}) (when (string-equal \"{note_type}\" \"ANKI_NOTE_ID\") (org-back-to-heading t)) (recenter))'",
1010
"shortcuts": {
1111
"open": "Ctrl+O"
1212
},

0 commit comments

Comments
 (0)