diff --git a/ankdown/ankdown.py b/ankdown/ankdown.py index 3f347e3..4138008 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,11 +58,9 @@ -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 + --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 +83,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: @@ -97,11 +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.
@@ -135,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 {
@@ -263,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'] == 'dollars':
for (sep, (op, cl)) in [("$$", (r"\\[", r"\\]")), ("$", (r"\\(", r"\\)"))]:
escaped_sep = sep.replace(r"$", r"\$")
# ignore escaped dollar signs when splitting the field
@@ -271,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'] == '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):
@@ -350,26 +353,23 @@ 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))
- if arguments.get('--config') is not None:
+ CONFIG.update(yaml.load(config_file, yaml.FullLoader))
+ 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')
- 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'] == '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."""
@@ -379,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.
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