Skip to content

Commit

Permalink
Merge pull request #46 from Pseudonium/develop
Browse files Browse the repository at this point in the history
New GUI and option to disable GUI
  • Loading branch information
Pseudonium authored Sep 15, 2020
2 parents 82de134 + 5997cd3 commit 5a61ad6
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 33 deletions.
Binary file modified Images/GUI.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/GUI_config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/GUI_regex.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@ Current features:
The GUI of the script looks like this:
![GUI](Images/GUI.png)

Hopefully the options and path are self-explanatory. Note that you cannot directly choose a folder in the 'Browse' dialog, but can manually remove the filename at the end to create a path to the folder.
Hopefully the options and path are self-explanatory.

### Command line usage
If you set 'GUI' in the config file to False, the script is then run from the command line:
* Use `-h` to see help.
* Run the script as `obsidian_to_anki.py [path]`, where `[path]` is the path to the file or folder you wish to add notes from.
* Use `-c` to open up the config file for editing (not guaranteed to work on all operating systems, if it doesn't you'll have to find and edit it manually).
* Use `-u` to update the config file. Do this when you add new note types to Anki.
* Use `-m` to force the script to add all media files detected, instead of lazy addition of media files. Useful if you've e.g. resized the image, and want the changes to be reflected in Anki.
* Use `-r` to use custom regex syntax, ignoring the default syntax of the script.

## New users

Expand All @@ -71,7 +80,8 @@ The sections below describe the default syntax of the script (with the 'Regex' o

### DEFAULT section
Allows you to change the default deck and tag of the script.
New in v2.2.2 - allows you to enable/disable the 'CurlyCloze' option, which is explained in [Cloze formatting](#cloze-formatting)
New in v2.2.2 - allows you to enable/disable the 'CurlyCloze' option, which is explained in [Cloze formatting](#cloze-formatting)
New in v2.4.0 - allows you to enable/disable the GUI of the script - see [Command line usage](#command-line-usage).

### Syntax
Note that START, END, TARGET DECK, FILE TAGS and DELETE all require an **exact match** on the line - you cannot have spaces afterwards.
Expand Down
85 changes: 64 additions & 21 deletions obsidian_to_anki.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import markdown
import base64
import gooey
import argparse

MEDIA_PATHS = set()

Expand Down Expand Up @@ -596,6 +597,9 @@ def update_config():
config["DEFAULT"].setdefault(
"CurlyCloze", "False"
)
config["DEFAULT"].setdefault(
"GUI", "True"
)
# Setting up Custom Regexps
config.setdefault("Custom Regexps", dict())
for note in note_types:
Expand Down Expand Up @@ -653,6 +657,9 @@ def load_config():
CONFIG_DATA["CurlyCloze"] = config.getboolean(
"DEFAULT", "CurlyCloze"
)
CONFIG_DATA["GUI"] = config.getboolean(
"DEFAULT", "GUI"
)
Config.config = config # Can access later if need be
print("Loaded successfully!")

Expand All @@ -664,23 +671,35 @@ class App:

def __init__(self):
"""Execute the main functionality of the script."""
self.setup_parser()
try:
Config.load_config()
except configparser.Error as e:
print("Error:", e)
print("Attempting to fix config file...")
Config.update_config()
Config.load_config()
if CONFIG_DATA["GUI"]:
self.setup_gui_parser()
else:
self.setup_cli_parser()
args = self.parser.parse_args()
# no_args = True
if CONFIG_DATA["GUI"] and args.dirpath:
args.path = args.dirpath
no_args = True
if args.update:
# no_args = False
no_args = False
Config.update_config()
Config.load_config()
Config.load_config()
if args.mediaupdate:
# no_args = False
no_args = False
CONFIG_DATA["Added Media"].clear()
self.gen_regexp()
if args.config:
# no_args = False
no_args = False
webbrowser.open(CONFIG_PATH)
return
if args.path:
# no_args = False
no_args = False
if args.path == "False":
return
current = os.getcwd()
Expand Down Expand Up @@ -724,21 +743,11 @@ def __init__(self):
file.write_file()
self.requests_2()
os.chdir(current)
# if no_args:
# self.parser.print_help()
if no_args:
self.parser.print_help()

@gooey.Gooey(use_cmd_args=True)
def setup_parser(self):
"""Set up the argument parser."""
self.parser = gooey.GooeyParser(
description="Add cards to Anki from an Obsidian markdown file."
)
self.parser.add_argument(
"path",
default=False,
help="Path to the file or directory you want to scan.",
widget="FileChooser"
)
def setup_parser_optionals(self):
"""Set up optional arguments for the parser."""
self.parser.add_argument(
"-c", "--config",
action="store_true",
Expand All @@ -764,6 +773,40 @@ def setup_parser(self):
help="Force addition of media files."
)

@gooey.Gooey(use_cmd_args=True)
def setup_gui_parser(self):
"""Set up the GUI argument parser."""
self.parser = gooey.GooeyParser(
description="Add cards to Anki from a markdown or text file."
)
path_group = self.parser.add_mutually_exclusive_group(required=False)
path_group.add_argument(
"-f", "--file",
help="Choose a file to scan.",
dest="path",
widget='FileChooser'
)
path_group.add_argument(
"-d", "--dir",
help="Choose a directory to scan.",
dest="dirpath",
widget='DirChooser'
)
self.setup_parser_optionals()

def setup_cli_parser(self):
"""Setup the command-line argument parser."""
self.parser = argparse.ArgumentParser(
description="Add cards to Anki from a markdown or text file."
)
self.parser.add_argument(
"path",
default=False,
nargs="?",
help="Path to the file or directory you want to scan."
)
self.setup_parser_optionals()

def gen_regexp(self):
"""Generate the regular expressions used by the app."""
setattr(
Expand Down
1 change: 1 addition & 0 deletions obsidian_to_anki_config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Tag = Obsidian_to_Anki
Deck = Default
CurlyCloze = False
GUI = True

[Basic]
Front = Front:
Expand Down
20 changes: 10 additions & 10 deletions regex.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The script won't see things outside of it.
You can have::multiple notes in the same file
</pre>
3. Run the script, and check 'Config' to open up the config file:
![GUI](Images/GUI.png)
![GUI](Images/GUI_config.png)
4. Navigate to the "Custom Regexps" section
5. Change the line
<pre>
Expand All @@ -36,7 +36,7 @@ Basic = ^(.*[^\n:]{1}):{2}([^\n:]{1}.*)
</pre>
6. Save the config file
7. Run the script on the file, with 'Regex' checked:
![GUI](Images/GUI.png)
![GUI](Images/GUI_regex.png)
8. You should see these cards in Anki:
![remnote_1](Images/Remnote_1.png)
![remnote_2](Images/Remnote_2.png)
Expand Down Expand Up @@ -65,7 +65,7 @@ Span over
Multiple lines, and ignore preceding whitespace
</pre>
3. Run the script, and check 'Config' to open up the config file:
![GUI](Images/GUI.png)
![GUI](Images/GUI_config.png)
4. Navigate to the "Custom Regexps" section
5. Change the line
<pre>
Expand All @@ -77,7 +77,7 @@ Basic = ^#+(.+)\n+((?:[^\n#][\n]?)+)
</pre>
6. Save the config file
7. Run the script on the file, with 'Regex' checked:
![GUI](Images/GUI.png)
![GUI](Images/GUI_regex.png)
8. You should see these cards in Anki:
![header_1](Images/Header_1.png)
![header_2](Images/Header_2.png)
Expand Down Expand Up @@ -117,7 +117,7 @@ Q: How is this possible?
A: The 'magic' of regular expressions!
</pre>
3. Run the script, and check 'Config' to open up the config file:
![GUI](Images/GUI.png)
![GUI](Images/GUI_config.png)
4. Navigate to the "Custom Regexps" section
5. Change the line
<pre>
Expand All @@ -129,7 +129,7 @@ Basic = ^Q: ((?:[^\n][\n]?)+)\n+A: ((?:[^\n][\n]?)+)
</pre>
6. Save the config file
7. Run the script on the file, with 'Regex' checked:
![GUI](Images/GUI.png)
![GUI](Images/GUI_regex.png)
8. You should see these cards in Anki:
![question_1](Images/Question_1.png)
![question_2](Images/Question_2.png)
Expand Down Expand Up @@ -159,7 +159,7 @@ Whitespace is ignored!

</pre>
3. Run the script, and check 'Config' to open up the config file:
![GUI](Images/GUI.png)
![GUI](Images/GUI_config.png)
4. Navigate to the "Custom Regexps" section
5. Change the line
<pre>
Expand All @@ -171,7 +171,7 @@ Basic = ((?:[^\n][\n]?)+) #flashcard\n+((?:[^\n][\n]?)+)
</pre>
6. Save the config file
7. Run the script on the file, with 'Regex' checked:
![GUI](Images/GUI.png)
![GUI](Images/GUI_regex.png)
8. You should see these cards in Anki:
![neuracache_1](Images/Neuracache_1.png)
![neuracache_2](Images/Neuracache_2.png)
Expand All @@ -197,7 +197,7 @@ Yes, but you need the front and back
directly before and after the ruler.
</pre>
3. Run the script, and check 'Config' to open up the config file:
![GUI](Images/GUI.png)
![GUI](Images/GUI_config.png)
4. Navigate to the "Custom Regexps" section
5. Change the line
<pre>
Expand All @@ -209,7 +209,7 @@ Basic = ((?:[^\n][\n]?)+\n)-{3,}\n((?:[^\n][\n]?)+)
</pre>
6. Save the config file
7. Run the script on the file, with 'Regex' checked:
![GUI](Images/GUI.png)
![GUI](Images/GUI_regex.png)
8. You should see these cards in Anki:
![ruled_1](/Images/Ruled_1.png)
![ruled_2](/Images/Ruled_2.png)
Expand Down

0 comments on commit 5a61ad6

Please sign in to comment.