diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..0689859 --- /dev/null +++ b/config.toml @@ -0,0 +1,56 @@ +deck_name = "Export" +skip = false +mathjax = true +tags = [ "#Export", ] + +[log_config] +version = 1 +disable_existing_loggers = false + +[notetype.Cloze] +clozePrefix = "\\*\\*" +clozeSuffix = "\\*\\*" +clozeNumberPrefix = "\\[" +clozeNumberSuffix = "\\]" + +[notetype.Choices] + +[notetype.ListCloze] + +[notetype.QA] + +[notetype.TableCloze] + +[notetype.MQA] + +[log_config.loggers.main] +level = "DEBUG" +handlers = [ "console", "log_file",] + +[log_config.loggers.notetype] +level = "DEBUG" +handlers = [ "console", "log_file",] + +[log_config.loggers.parser] +level = "DEBUG" +handlers = [ "console", "log_file",] + +[log_config.loggers.helper] +level = "DEBUG" +handlers = [ "console", "log_file",] + +[log_config.handlers.console] +level = "WARNING" +formatter = "standard" +class = "logging.StreamHandler" +stream = "ext://sys.stdout" + +[log_config.handlers.log_file] +level = "DEBUG" +formatter = "standard" +class = "logging.FileHandler" +filename = "log.txt" +mode = "a" + +[log_config.formatters.standard] +format = "%(asctime)s [%(levelname)s] %(name)s: %(message)s" diff --git a/src/cli.py b/src/cli.py index 033ba51..0dd6d44 100644 --- a/src/cli.py +++ b/src/cli.py @@ -4,8 +4,10 @@ from AnkiIn.helper.ankiConnectHelper import add_notes, check_online from AnkiIn.helper.formatHelper import get_title, remove_suffix from AnkiIn import config +from AnkiIn.config import dict as conf from AnkiIn.parser import markdown as markdown_parser import config_parser +import config def execute_from_commandline(): @@ -24,7 +26,7 @@ def execute_from_commandline(): f.close() noteLists += noteList print("Done.") - if config.output is False: + if not config.output: add_notes(noteLists) else: if config.output_path is None: @@ -36,10 +38,12 @@ def getRaw(name: str) -> str: if len(config.file_list) > 1: for i, x in enumerate(config.file_list): - config.output_path += (titles[i] if titles[i] is not None else getRaw(x)) + "_" + config.output_path += (titles[i] if titles[i] + is not None else getRaw(x)) + "_" config.output_path = remove_suffix(config.output_path, "_") else: - config.output_path = titles[0] if titles[0] is not None else getRaw(config.file_list[0]) + config.output_path = titles[0] if titles[0] is not None else getRaw( + config.file_list[0]) config.output_path += ".apkg" print("No filename provided. Exporting to " + config.output_path) export_notes(noteLists, config.output_path) diff --git a/src/config.py b/src/config.py new file mode 100644 index 0000000..65f6b1b --- /dev/null +++ b/src/config.py @@ -0,0 +1,5 @@ +file_list = [] +output = False +output_path = "" +config_path = "config.toml" +log_debug = False diff --git a/src/config_parser.py b/src/config_parser.py index 9b6fa12..c2453b0 100644 --- a/src/config_parser.py +++ b/src/config_parser.py @@ -1,14 +1,19 @@ -import sys import argparse -from AnkiIn import config +import os.path +from AnkiIn.config import dict as conf +from AnkiIn import config as AnkiIn_config +import config +import toml -version_name = "2.3.0" +version_name = "2.3.1" help_info = { "filename": "Files to be converted. You can specify multi files.", "deckname": "Deck to put your notes on. (default \"Export\")", "tags": "Tags to append to your notes. (default [\"#Export\"])", - "output": "The path to output the notes to a apkg file, directly to Anki if left blank." + "output": "The path to output the notes to a apkg file, infer the filename if left blank.", + "debug": "Enable this to be in debug mode.", + "config": "The path of the config file" } parser = argparse.ArgumentParser( @@ -17,32 +22,46 @@ version="Anki Importer v{}".format(version_name)) parser.add_argument("filename", metavar="filename", nargs="+", help=help_info["filename"]) -parser.add_argument("-d", "--deckname", metavar="deckname", nargs="?", default=config.deck_name, +parser.add_argument("-d", "--deckname", metavar="deckname", nargs="+", default=conf["deck_name"], help=help_info["deckname"]) -parser.add_argument("-t", "--tags", metavar="tags", nargs="*", default=config.tags, +parser.add_argument("-c", "--config", metavar="config", nargs="+", default=config.config_path, + help=help_info["config"]) +parser.add_argument("-t", "--tags", metavar="tags", nargs="*", default=conf["tags"], help=help_info["tags"]) -parser.add_argument("-o", "--output", nargs="?", metavar="output", help=help_info["output"]) +parser.add_argument("--debug", action="store_true", default=config.log_debug, + help=help_info["debug"]) +parser.add_argument("-o", "--output", nargs="?", const="yes", + default="no", metavar="output", help=help_info["output"]) def enable_log_file(): - config.log_config["handlers"]["log_file"] = { - "level": "INFO", + conf["log_config"]["handlers"]["log_file"] = { + "level": "DEBUG" if config.log_debug else "INFO", "formatter": "standard", "class": "logging.FileHandler", "filename": "log.txt", "mode": "a" } - for x in config.log_config["loggers"].keys(): - config.log_config["loggers"][x]["handlers"].append("log_file") + for x in conf["log_config"]["loggers"].keys(): + conf["log_config"]["loggers"][x]["handlers"].append("log_file") def parse(): args = parser.parse_args() - global deck_name, tags, file_list, output, outputpath - config.deck_name = args.deckname - config.tags = args.tags + conf["deck_name"] = args.deckname + conf["tags"] = args.tags config.file_list = args.filename - config.output = "-o" in sys.argv or "--output" in sys.argv - config.output_path = args.output + config.output = args.output != "no" + config.output_path = None if args.output and args.output == "yes" else args.output + config.log_debug = args.debug + config.config_path = args.config enable_log_file() - config.complete_config() + load_config_file() + AnkiIn_config.update_config() + + +def load_config_file(): + if os.path.isfile(config.config_path): + AnkiIn_config.execute_config(toml.load(config.config_path)) + else: + print("Warning: Config File {} doesn't exist!".format(config.config_path)) diff --git a/src/requirements.txt b/src/requirements.txt index aea3b11..d8fa32c 100644 --- a/src/requirements.txt +++ b/src/requirements.txt @@ -1 +1 @@ -AnkiIn==0.0.6 +AnkiIn==0.0.8 diff --git a/test.sh b/test.sh index 2d1135d..fba6bbb 100755 --- a/test.sh +++ b/test.sh @@ -1,2 +1,2 @@ -python src/cli.py tests/test1.md tests/test2.md tests/test3.md tests/test4.md tests/test5.md tests/test6.md tests/test7.md -python src/cli.py tests/test1.md tests/test2.md tests/test3.md tests/test4.md tests/test5.md tests/test6.md tests/test7.md -o test.apkg \ No newline at end of file +python src/cli.py tests/test1.md tests/test2.md tests/test3.md tests/test4.md tests/test5.md tests/test6.md tests/test7.md tests/test8.md +python src/cli.py tests/test1.md tests/test2.md tests/test3.md tests/test4.md tests/test5.md tests/test6.md tests/test7.md tests/test8.md -o test.apkg \ No newline at end of file diff --git a/tests/test7.md b/tests/test7.md index ddf2ccc..02adb5d 100644 --- a/tests/test7.md +++ b/tests/test7.md @@ -3,14 +3,14 @@ ## Heading 1 [config] -deck_name = Export2 -tags = [test1,test2] +deck_name = "Export2" +tags = ["test1","test2"] Q. A. [inlineconfig] -tags = [inline,config] +tags = ["inline","config"] [/inlineconfig] Inline config is possible. This would only affect the current block. @@ -24,7 +24,7 @@ The config will only affect the blocks under it having the same ancestor. That's implemented by a Depth-First Search. [config] -tags = [test3,test4] +tags = ["test3","test4"] Overriding is **possible**. @@ -36,14 +36,14 @@ And cards here would be affected too. Hmm. [inlineconfig] -skip = True +skip = true [/inlineconfig] skip this block! [inlineconfig] skip = true [/inlineconfig] -case not sensitive. +please use lowercase. [config] skip = true @@ -57,7 +57,7 @@ And when we exit this heading, the config will be reverted. ## Heading 4 [config] -tags = [test5] +tags = ["test5"] Old time returns. Here we are. @@ -66,3 +66,15 @@ Here we are. mathjax = false [/inlineconfig] $100 free of **mathjax**! + +#THIS won't be recognized as a heading when being parsed. +interesting right? Avoid this improper syntax. + +[config] +[notetype.Cloze] +clozePrefix = "\\(" +clozeSuffix = "\\)" +clozeNumberPrefix = "\\{" +clozeNumberSuffix = "\\}" + +Special (Cloze) and ({1}Numbering). diff --git a/tests/test8.md b/tests/test8.md new file mode 100644 index 0000000..0000226 --- /dev/null +++ b/tests/test8.md @@ -0,0 +1,18 @@ +# Multiple Line Question & Answer + +!Question Line 1 +!Question Line 2 +Answer1 +Answer2 + +\! for Question1 +\! for Answer 1 + +!Question +Answer + +!Question1 +!Question2 +!Question3 +!Question4 +Answer