Skip to content
This repository has been archived by the owner on Mar 20, 2020. It is now read-only.

dev: update dependencies #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 29 additions & 30 deletions ankdown/ankdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -96,10 +97,6 @@ def blockcode(self, text, lang):
return '\n<pre><code>{}</code></pre>\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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -261,24 +259,31 @@ 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':
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want ==, not is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh... coffeescript sneaks in ;-) I've pushed a fix

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"(?<!\\){}".format(escaped_sep), 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':
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also should be ==

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):
Expand Down Expand Up @@ -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':
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also should be ==

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."""
Expand All @@ -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.

Expand Down