-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add new option to set custom scan directory (#154)
* Add custom scan dir * Update test suites data.json with new option * Account for new setting * Fix undefined variable * Replace spaces with tabs in changed files Seems like source code uses tabs (for most part), so replacing spaces with tabs in files that I've changed for the PR * Add e2e tests * Update filename to fix spec requirement * Add subdir to scan and fix headings in md files * Update readme with new feature description * fix tests --------- Co-authored-by: Harsha Raghu <[email protected]>
- Loading branch information
1 parent
72becdf
commit 3839220
Showing
24 changed files
with
328 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
|
||
import re | ||
import pytest | ||
from anki.errors import NotFoundError # noqa | ||
from anki.collection import Collection | ||
from anki.collection import SearchNode | ||
# from conftest import col | ||
|
||
test_name = 'folder_scan' | ||
col_path = 'tests/test_outputs/{}/Anki2/User 1/collection.anki2'.format(test_name) | ||
|
||
test_file_paths = [ | ||
'tests/test_outputs/{}/Obsidian/{}/scan_dir/{}.md'.format(test_name, test_name, test_name), | ||
'tests/test_outputs/{}/Obsidian/{}/scan_dir/also_scan/folder_scan_subdir.md'.format(test_name, test_name), | ||
] | ||
|
||
test_file_no_cards_paths = [ | ||
'tests/test_outputs/{}/Obsidian/{}/should_not_scan_dir/should_not_scan.md'.format(test_name, test_name), | ||
'tests/test_outputs/{}/Obsidian/{}/{}.md'.format(test_name, test_name, test_name), | ||
] | ||
|
||
@pytest.fixture() | ||
def col(): | ||
col = Collection(col_path) | ||
yield col | ||
col.close() | ||
|
||
def test_col_exists(col): | ||
assert not col.is_empty() | ||
|
||
def test_deck_default_exists(col: Collection): | ||
assert col.decks.id_for_name('Default') is not None | ||
|
||
def test_cards_count(col: Collection): | ||
assert len(col.find_cards( col.build_search_string(SearchNode(deck='Default')) )) == 8 | ||
|
||
def test_cards_ids_from_obsidian(col: Collection): | ||
|
||
ID_REGEXP_STR = r'\n?(?:<!--)?(?:ID: (\d+).*)' | ||
|
||
for obsidian_test_md in test_file_paths: | ||
obs_IDs = [] | ||
with open(obsidian_test_md) as file: | ||
for line in file: | ||
output = re.search(ID_REGEXP_STR, line.rstrip()) | ||
if output is not None: | ||
output = output.group(1) | ||
obs_IDs.append(output) | ||
|
||
anki_IDs = col.find_notes( col.build_search_string(SearchNode(deck='Default')) ) | ||
for aid, oid in zip(anki_IDs, obs_IDs): | ||
assert str(aid) == oid | ||
|
||
def test_no_cards_added_from_obsidian(col: Collection): | ||
|
||
ID_REGEXP_STR = r'\n?(?:<!--)?(?:ID: (\d+).*)' | ||
|
||
for obsidian_test_md in test_file_no_cards_paths: | ||
obs_IDs = [] | ||
with open(obsidian_test_md) as file: | ||
for line in file: | ||
output = re.search(ID_REGEXP_STR, line.rstrip()) | ||
if output is not None: | ||
output = output.group(1) | ||
obs_IDs.append(output) | ||
|
||
assert len(obs_IDs) == 0 | ||
|
||
def test_cards_front_back_tag_type(col: Collection): | ||
|
||
anki_IDs = col.find_notes( col.build_search_string(SearchNode(deck='Default')) ) | ||
|
||
# assert that should_not_add_notes weren't added | ||
assert len(anki_IDs) == 8 | ||
|
||
note1 = col.get_note(anki_IDs[0]) | ||
assert note1.fields[0] == "Style Subdir" | ||
assert note1.fields[1] == "This style is suitable for having the header as the front, and the answer as the back" | ||
|
||
note2 = col.get_note(anki_IDs[1]) | ||
assert note2.fields[0] == "Subheading 1 Subdir" | ||
assert note2.fields[1] == "You're allowed to nest headers within each other" | ||
|
||
note3 = col.get_note(anki_IDs[2]) | ||
assert note3.fields[0] == "Subheading 2 Subdir" | ||
assert note3.fields[1] == "It'll take the deepest level for the question" | ||
|
||
note4 = col.get_note(anki_IDs[3]) | ||
assert note4.fields[0] == "Subheading 3 Subdir" | ||
assert note4.fields[1] == "It'll even<br />\nSpan over<br />\nMultiple lines, and ignore preceding whitespace" | ||
|
||
|
||
note5 = col.get_note(anki_IDs[4]) | ||
assert note5.fields[0] == "Style" | ||
assert note5.fields[1] == "This style is suitable for having the header as the front, and the answer as the back" | ||
|
||
note6 = col.get_note(anki_IDs[5]) | ||
assert note6.fields[0] == "Subheading 1" | ||
assert note6.fields[1] == "You're allowed to nest headers within each other" | ||
|
||
note7 = col.get_note(anki_IDs[6]) | ||
assert note7.fields[0] == "Subheading 2" | ||
assert note7.fields[1] == "It'll take the deepest level for the question" | ||
|
||
note8 = col.get_note(anki_IDs[7]) | ||
assert note8.fields[0] == "Subheading 3" | ||
assert note8.fields[1] == "It'll even<br />\nSpan over<br />\nMultiple lines, and ignore preceding whitespace" | ||
|
||
assert note1.note_type()["name"] == "Basic" | ||
assert note2.note_type()["name"] == "Basic" | ||
assert note3.note_type()["name"] == "Basic" | ||
assert note4.note_type()["name"] == "Basic" | ||
|
||
assert note5.note_type()["name"] == "Basic" | ||
assert note6.note_type()["name"] == "Basic" | ||
assert note7.note_type()["name"] == "Basic" | ||
assert note8.note_type()["name"] == "Basic" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...efaults/test_vault_suites/folder_scan/.obsidian/plugins/obsidian-to-anki-plugin/data.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
{ | ||
"settings": { | ||
"CUSTOM_REGEXPS": { | ||
"Basic": "^#+(.+)\n*((?:\n(?:^[^\n#].{0,2}$|^[^\n#].{3}(?<!<!--).*))+)", | ||
"Basic (and reversed card)": "", | ||
"Basic (optional reversed card)": "", | ||
"Basic (type in the answer)": "", | ||
"Cloze": "" | ||
}, | ||
"FILE_LINK_FIELDS": { | ||
"Basic": "Front", | ||
"Basic (and reversed card)": "Front", | ||
"Basic (optional reversed card)": "Front", | ||
"Basic (type in the answer)": "Front", | ||
"Cloze": "Text" | ||
}, | ||
"CONTEXT_FIELDS": {}, | ||
"FOLDER_DECKS": {}, | ||
"FOLDER_TAGS": {}, | ||
"Syntax": { | ||
"Begin Note": "START", | ||
"End Note": "END", | ||
"Begin Inline Note": "STARTI", | ||
"End Inline Note": "ENDI", | ||
"Target Deck Line": "TARGET DECK", | ||
"File Tags Line": "FILE TAGS", | ||
"Delete Note Line": "DELETE", | ||
"Frozen Fields Line": "FROZEN" | ||
}, | ||
"Defaults": { | ||
"Scan Directory": "folder_scan/scan_dir", | ||
"Tag": "Obsidian_to_Anki", | ||
"Deck": "Default", | ||
"Scheduling Interval": 0, | ||
"Add File Link": false, | ||
"Add Context": false, | ||
"CurlyCloze": true, | ||
"CurlyCloze - Highlights to Clozes": false, | ||
"ID Comments": true, | ||
"Add Obsidian Tags": false | ||
} | ||
}, | ||
"Added Media": [], | ||
"File Hashes": {}, | ||
"fields_dict": { | ||
"Basic": [ | ||
"Front", | ||
"Back" | ||
], | ||
"Basic (and reversed card)": [ | ||
"Front", | ||
"Back" | ||
], | ||
"Basic (optional reversed card)": [ | ||
"Front", | ||
"Back", | ||
"Add Reverse" | ||
], | ||
"Basic (type in the answer)": [ | ||
"Front", | ||
"Back" | ||
], | ||
"Cloze": [ | ||
"Text", | ||
"Back Extra" | ||
] | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/defaults/test_vault_suites/folder_scan/folder_scan.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Style fsmd | ||
This style is suitable for having the header as the front, and the answer as the back | ||
# Overall heading fsmd | ||
|
||
## Subheading 1 fsmd | ||
You're allowed to nest headers within each other | ||
|
||
## Subheading 2 fsmd | ||
It'll take the deepest level for the question | ||
|
||
## Subheading 3 fsmd | ||
|
||
|
||
|
||
It'll even | ||
Span over | ||
Multiple lines, and ignore preceding whitespace |
Oops, something went wrong.