Skip to content

Commit

Permalink
update AnkiIn version to v0.0.8
Browse files Browse the repository at this point in the history
close #41, close #42
  • Loading branch information
Clouder0 committed Jul 4, 2021
1 parent 2a195a5 commit 3f68532
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 30 deletions.
56 changes: 56 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -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"
10 changes: 7 additions & 3 deletions src/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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:
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
file_list = []
output = False
output_path = ""
config_path = "config.toml"
log_debug = False
53 changes: 36 additions & 17 deletions src/config_parser.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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))
2 changes: 1 addition & 1 deletion src/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
AnkiIn==0.0.6
AnkiIn==0.0.8
4 changes: 2 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
@@ -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
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
26 changes: 19 additions & 7 deletions tests/test7.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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**.

Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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).
18 changes: 18 additions & 0 deletions tests/test8.md
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3f68532

Please sign in to comment.