From dde8633d97ec09c159e97c92796a5b14582cb935 Mon Sep 17 00:00:00 2001 From: Philip Janssen Date: Sun, 24 Mar 2019 23:02:37 +0100 Subject: [PATCH 1/3] Adds an option to use dollar sign as math separator --- .gitignore | 6 +++++- ankdown/ankdown.py | 24 ++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index d674044..a81e07e 100644 --- a/.gitignore +++ b/.gitignore @@ -103,5 +103,9 @@ ENV/ # Friggin Apple .DS_Store -#PyCharm +# PyCharm .idea/ + +# VSCode +.vscode +*.code-workspace \ No newline at end of file diff --git a/ankdown/ankdown.py b/ankdown/ankdown.py index 73b9c68..f4e4b42 100755 --- a/ankdown/ankdown.py +++ b/ankdown/ankdown.py @@ -33,7 +33,7 @@ ``` Usage: - ankdown.py [-r DIR] [-p PACKAGENAME] [--css CSS_STRING] + ankdown.py [-r DIR] [-p PACKAGENAME] [--css CSS_STRING] [--dollar] Options: -h --help Show this help message @@ -44,6 +44,8 @@ -p PACKAGE Instead of a .txt file, produce a .apkg file. recommended. --css CSS_STRING CSS Config as String + + --dollar Uses dollar sign as math separator instead of brackets """ @@ -61,6 +63,7 @@ from docopt import docopt VERSION = "0.5.2" +FLAG_DOLLAR = False def simple_hash(text): """MD5 of text, mod 2^63. Probably not a great hash function.""" @@ -210,11 +213,18 @@ def __getitem__(self, deckname): return super(DeckCollection, self).__getitem__(deckname) def field_to_html(field): - # Need to extract the math in brackets so that it doesn't get - # markdowned. - for bracket in ["(", ")", "[", "]"]: - field = field.replace(r"\{}".format(bracket), r"\\{}".format(bracket)) - # backslashes, man. + """Need to extract the math in brackets so that it doesn't get markdowned. + If math is separated with dollar sign it is converted to brackets.""" + if FLAG_DOLLAR: + for sep_dollar, sep_bracket in zip(["$", "$$"], [[r"\\(", r"\\)"], [r"\\[", r"\\]"]]): + field = field.split(sep_dollar) + field[1::2] = [sep_bracket[0] + e for e in field[1::2]] # add starting bracket to every second element of the list + field[2::2] = [sep_bracket[1] + e for e in field[2::2]] # add ending bracket to every other element of the list + field = "".join(field) + else: + for bracket in ["(", ")", "[", "]"]: + field = field.replace(r"\{}".format(bracket), r"\\{}".format(bracket)) + # backslashes, man. return misaka.html(field, extensions=("fenced-code", "math")) @@ -286,6 +296,8 @@ def main(): """Run the thing.""" arguments = docopt(__doc__, version=VERSION) + global FLAG_DOLLAR + FLAG_DOLLAR = arguments.get('--dollar') css_config = arguments.get('--css') if css_config is not None: Card.MODEL_CSS = css_config From 9c605d353d081c1397a06d59b4e710559fcbb9f7 Mon Sep 17 00:00:00 2001 From: Philip Janssen Date: Mon, 25 Mar 2019 19:28:28 +0100 Subject: [PATCH 2/3] Adds possibility to escape the dollar sign and fixes a bug. --- ankdown/ankdown.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ankdown/ankdown.py b/ankdown/ankdown.py index f4e4b42..fd0b3e4 100755 --- a/ankdown/ankdown.py +++ b/ankdown/ankdown.py @@ -216,10 +216,12 @@ def field_to_html(field): """Need to extract the math in brackets so that it doesn't get markdowned. If math is separated with dollar sign it is converted to brackets.""" if FLAG_DOLLAR: - for sep_dollar, sep_bracket in zip(["$", "$$"], [[r"\\(", r"\\)"], [r"\\[", r"\\]"]]): - field = field.split(sep_dollar) - field[1::2] = [sep_bracket[0] + e for e in field[1::2]] # add starting bracket to every second element of the list - field[2::2] = [sep_bracket[1] + e for e in field[2::2]] # add ending bracket to every other element of the list + for (sep, (op, cl)) in [("$$", (r"\\[", r"\\]")), ("$", (r"\\(", r"\\)"))]: + escaped_sep = sep.replace(r"$", r"\$") + # ignore escaped dollar signs when splitting the field + field = re.split(r"(? Date: Sun, 7 Jul 2019 17:13:20 +0200 Subject: [PATCH 3/3] Opens files in utf8 --- ankdown/ankdown.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ankdown/ankdown.py b/ankdown/ankdown.py index 3f347e3..ade72e1 100644 --- a/ankdown/ankdown.py +++ b/ankdown/ankdown.py @@ -294,7 +294,7 @@ def compile_field(field_lines, is_markdown): def produce_cards(filename): """Given the markdown in infile, produce the intended result cards.""" - with open(filename, "r") as f: + with open(filename, "r", encoding="utf8") as f: current_field_lines = [] i = 0 current_card = Card(filename, file_index=i)