From 8817098a004912b78ecf4ec7fa18c6e75ec805ce Mon Sep 17 00:00:00 2001 From: pitti Date: Sat, 13 Jul 2019 00:03:10 +0200 Subject: [PATCH 1/3] dev: update dependencies --- ankdown/ankdown.py | 10 ++++------ requirements.txt | 14 +++++++------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/ankdown/ankdown.py b/ankdown/ankdown.py index 3f347e3..86c1167 100755 --- a/ankdown/ankdown.py +++ b/ankdown/ankdown.py @@ -45,7 +45,7 @@ A configuration can also be passed as a string: `"{dollar: True, card_model_name: CustomModelName, card_model_css: \".card {text-align: left;}\"}"` Usage: - ankdown.py [-r DIR] [-p PACKAGENAME] [--highlight] [--config CONFIG_STRING] [--configFile CONFIG_FILE_PATH] + ankdown.py [-r DIR] [-p PACKAGENAME] [--highlight] [--config=CONFIG_STRING] [--configFile=CONFIG_FILE_PATH] Options: -h --help Show this help message @@ -57,9 +57,9 @@ --highlight Enable syntax highlighting for code - --config CONFIG_STRING ankdown configuration as YAML string + --config=CONFIG_STRING ankdown configuration as YAML string - --configFile CONFIG_FILE_PATH path to ankdown configuration as YAML file + --configFile=CONFIG_FILE_PATH path to ankdown configuration as YAML file """ @@ -82,7 +82,6 @@ from pygments.formatters import HtmlFormatter, ClassNotFound from pygments.lexers import get_lexer_by_name - class HighlighterRenderer(misaka.HtmlRenderer): def blockcode(self, text, lang): try: @@ -101,7 +100,6 @@ def blockcode(self, text, lang): renderer = HighlighterRenderer() highlight_markdown = misaka.Markdown(renderer, extensions=("fenced-code", "math")) - VERSION = "0.7.1" # Anki 2.1 has mathjax built in, but ankidroid and other clients don't. @@ -353,7 +351,7 @@ def apply_arguments(arguments): if arguments.get('--configFile') is not None: config_file_path = os.path.abspath(os.path.expanduser(arguments.get('--configFile'))) with open(config_file_path, 'r') as config_file: - CONFIG.update(yaml.load(config_file)) + CONFIG.update(yaml.load(config_file, yaml.FullLoader)) if arguments.get('--config') is not None: CONFIG.update(yaml.load(arguments.get('--config'))) if arguments.get('-p') is not None: diff --git a/requirements.txt b/requirements.txt index 0384175..b47e300 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,11 +1,11 @@ -cached-property==1.3.0 -cffi==1.10.0 +cached-property==1.5.1 +cffi==1.12.3 docopt==0.6.2 frozendict==1.2 -genanki==0.6.3 -misaka==2.1.0 -pycparser==2.18 +genanki==0.7.0 +misaka==2.1.1 +pycparser==2.19 pystache==0.5.4 -PyYAML==3.13 -Pygments==2.4.0 +PyYAML==5.1.1 +Pygments==2.4.2 houdini.py==0.1.0 From aef39c04f884a473eac9577a7f1f20a36a671aad Mon Sep 17 00:00:00 2001 From: pitti Date: Fri, 19 Jul 2019 17:29:01 +0200 Subject: [PATCH 2/3] cleanup app structure --- ankdown/ankdown.py | 59 +++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/ankdown/ankdown.py b/ankdown/ankdown.py index 86c1167..0ccaabd 100755 --- a/ankdown/ankdown.py +++ b/ankdown/ankdown.py @@ -37,15 +37,18 @@ ```yaml recur_dir: ~/ankdown_cards pkg_arg: ~/ankdown_cards.apkg +math_mode: dollars +misaka_renderer: HighlighterRenderer, +misaka_extensions: [fenced-code, math, tables], card_model_name: CustomModelName card_model_css: ".card {font-family: 'Crimson Pro', 'Crimson Text', 'Cardo', 'Times', 'serif'; text-align: left; color: black; background-color: white;}" -dollar: True + ``` -A configuration can also be passed as a string: `"{dollar: True, card_model_name: CustomModelName, card_model_css: \".card {text-align: left;}\"}"` +A configuration can also be passed as a string: `"{card_model_name: CustomModelName, card_model_css: \".card {text-align: left;}\"}"` Usage: - ankdown.py [-r DIR] [-p PACKAGENAME] [--highlight] [--config=CONFIG_STRING] [--configFile=CONFIG_FILE_PATH] + ankdown.py [-r DIR] [-p PACKAGENAME] [--config=CONFIG_STRING] [--configFile=CONFIG_FILE_PATH] Options: -h --help Show this help message @@ -55,8 +58,6 @@ -p PACKAGE Instead of a .txt file, produce a .apkg file. recommended. - --highlight Enable syntax highlighting for code - --config=CONFIG_STRING ankdown configuration as YAML string --configFile=CONFIG_FILE_PATH path to ankdown configuration as YAML file @@ -96,10 +97,6 @@ def blockcode(self, text, lang): return '\n
{}
\n'.format( h.escape_html(text.strip())) - -renderer = HighlighterRenderer() -highlight_markdown = misaka.Markdown(renderer, extensions=("fenced-code", "math")) - VERSION = "0.7.1" # Anki 2.1 has mathjax built in, but ankidroid and other clients don't. @@ -133,8 +130,9 @@ def blockcode(self, text, lang): CONFIG = { 'pkg_arg': 'AnkdownPkg.apkg', 'recur_dir': '.', - 'dollar': False, - 'highlight': False, + 'math_mode': 'brackets', + 'misaka_renderer': 'HtmlRenderer', + 'misaka_extensions': ['fenced-code', 'math'], 'card_model_name': 'Ankdown Model 2', 'card_model_css': """ .card { @@ -261,7 +259,7 @@ def __getitem__(self, deckname): 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 CONFIG['dollar']: + if CONFIG['math_mode'] is 'dollars': for (sep, (op, cl)) in [("$$", (r"\\[", r"\\]")), ("$", (r"\\(", r"\\)"))]: escaped_sep = sep.replace(r"$", r"\$") # ignore escaped dollar signs when splitting the field @@ -269,16 +267,23 @@ def field_to_html(field): # add op(en) and cl(osing) brackets to every second element of the list field[1::2] = [op + e + cl for e in field[1::2]] field = "".join(field) + # + # CONFIG['math_mode'] is 'brackets' OR fallback case + # else: for bracket in ["(", ")", "[", "]"]: field = field.replace(r"\{}".format(bracket), r"\\{}".format(bracket)) # backslashes, man. - if CONFIG['highlight']: - return highlight_markdown(field) - - - return misaka.html(field, extensions=("fenced-code", "math")) + if CONFIG['misaka_renderer'] is 'HighlighterRenderer': + renderer = HighlighterRenderer() + # + # CONFIG['misakq_renderer'] is 'HtmlRenderer' OR fallback case + # + else: + renderer = misaka.HtmlRenderer() + + return misaka.Markdown(renderer, CONFIG['misaka_extensions'])(field) def compile_field(field_lines, is_markdown): @@ -358,16 +363,13 @@ def apply_arguments(arguments): CONFIG['pkg_arg'] = arguments.get('-p') if arguments.get('-r') is not None: CONFIG['recur_dir'] = arguments.get('-r') - if arguments.get('--highlight'): - CONFIG['highlight'] = True - - -def apply_highlight_css(): - global CONFIG - css_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'highlight.css') - with open(css_file_path) as css_file: - CONFIG['card_model_css'] += css_file.read().replace('\n', '') - + # + # add highlight css rules + # + if CONFIG['misaka_renderer'] is 'HighlighterRenderer': + highlight_css_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'highlight.css') + with open(highlight_css_file_path) as highlight_css_file: + CONFIG['card_model_css'] += highlight_css_file.read().replace('\n', '') def main(): """Run the thing.""" @@ -377,9 +379,6 @@ def main(): recur_dir = os.path.abspath(os.path.expanduser(CONFIG['recur_dir'])) pkg_arg = os.path.abspath(os.path.expanduser(CONFIG['pkg_arg'])) - if CONFIG['highlight']: - apply_highlight_css() - with tempfile.TemporaryDirectory() as tmpdirname: os.chdir(tmpdirname) # genanki is very opinionated about where we are. From d121ea24a7d601fa514a7e0ed2812e9b21c2e2ce Mon Sep 17 00:00:00 2001 From: pitti Date: Thu, 19 Sep 2019 11:46:45 +0200 Subject: [PATCH 3/3] fix: equal operators --- ankdown/ankdown.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ankdown/ankdown.py b/ankdown/ankdown.py index 0ccaabd..4138008 100755 --- a/ankdown/ankdown.py +++ b/ankdown/ankdown.py @@ -259,7 +259,7 @@ def __getitem__(self, deckname): 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 CONFIG['math_mode'] is 'dollars': + if CONFIG['math_mode'] == 'dollars': for (sep, (op, cl)) in [("$$", (r"\\[", r"\\]")), ("$", (r"\\(", r"\\)"))]: escaped_sep = sep.replace(r"$", r"\$") # ignore escaped dollar signs when splitting the field @@ -275,7 +275,7 @@ def field_to_html(field): field = field.replace(r"\{}".format(bracket), r"\\{}".format(bracket)) # backslashes, man. - if CONFIG['misaka_renderer'] is 'HighlighterRenderer': + if CONFIG['misaka_renderer'] == 'HighlighterRenderer': renderer = HighlighterRenderer() # # CONFIG['misakq_renderer'] is 'HtmlRenderer' OR fallback case @@ -353,20 +353,20 @@ def cards_to_apkg(cards, output_name): def apply_arguments(arguments): global CONFIG - if arguments.get('--configFile') is not None: + if arguments.get('--configFile') != None: config_file_path = os.path.abspath(os.path.expanduser(arguments.get('--configFile'))) with open(config_file_path, 'r') as config_file: CONFIG.update(yaml.load(config_file, yaml.FullLoader)) - if arguments.get('--config') is not None: + if arguments.get('--config') != None: CONFIG.update(yaml.load(arguments.get('--config'))) - if arguments.get('-p') is not None: + if arguments.get('-p') != None: CONFIG['pkg_arg'] = arguments.get('-p') - if arguments.get('-r') is not None: + if arguments.get('-r') != None: CONFIG['recur_dir'] = arguments.get('-r') # # add highlight css rules # - if CONFIG['misaka_renderer'] is 'HighlighterRenderer': + if CONFIG['misaka_renderer'] == 'HighlighterRenderer': highlight_css_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'highlight.css') with open(highlight_css_file_path) as highlight_css_file: CONFIG['card_model_css'] += highlight_css_file.read().replace('\n', '')