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

Commit

Permalink
feat: produce cards only for updated markdown files
Browse files Browse the repository at this point in the history
  • Loading branch information
kadaliao committed Nov 27, 2019
1 parent 92d5475 commit 4141a1f
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions ankdown/ankdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@
--config CONFIG_STRING ankdown configuration as YAML string
--configFile CONFIG_FILE_PATH path to ankdown configuration as YAML file
--updatedOnly Only produce cards from updated markdown files
"""


import hashlib
import os
import json
import re
import tempfile
import textwrap
Expand Down Expand Up @@ -137,6 +140,8 @@ def blockcode(self, text, lang):
'recur_dir': '.',
'dollar': False,
'highlight': False,
'updated_only': True,
'version_log': '.mdvlog',
'card_model_name': 'Ankdown Model 2',
'card_model_css': """
.card {
Expand All @@ -160,6 +165,7 @@ def blockcode(self, text, lang):
]
}

VERSION_LOG = {}

def simple_hash(text):
"""MD5 of text, mod 2^63. Probably not a great hash function."""
Expand Down Expand Up @@ -322,16 +328,29 @@ def produce_cards(filename):

def cards_from_dir(dirname):
"""Walk a directory and produce the cards found there, one by one."""
global VERSION_LOG
global CONFIG
for parent_dir, _, files in os.walk(dirname):
for fn in files:
if fn.endswith(".md") or fn.endswith(".markdown"):
for card in produce_cards(os.path.join(parent_dir, fn)):
yield card
filepath = os.path.join(parent_dir, fn)
old_hash = VERSION_LOG.get(filepath, None)
cur_hash = simple_hash(open(filepath, 'r').read())

if old_hash != cur_hash or not CONFIG['updated_only']:
try:
for card in produce_cards(filepath):
yield card
except:
raise Exception('fail to produce cards for %s' % filepath)
else:
VERSION_LOG[filepath] = cur_hash



def cards_to_apkg(cards, output_name):
"""Take an iterable of the cards, and put a .apkg in a file called output_name.
NOTE: We _must_ be in a temp directory.
"""
decks = DeckCollection()
Expand Down Expand Up @@ -362,6 +381,8 @@ def apply_arguments(arguments):
CONFIG['recur_dir'] = arguments.get('-r')
if arguments.get('--highlight'):
CONFIG['highlight'] = True
if arguments.get('--updatedOnly'):
CONFIG['updated_only'] = True


def apply_highlight_css():
Expand All @@ -370,6 +391,10 @@ def apply_highlight_css():
with open(css_file_path) as css_file:
CONFIG['card_model_css'] += css_file.read().replace('\n', '')

def load_version_log(version_log):
global VERSION_LOG
if os.path.exists(version_log):
VERSION_LOG = json.load(open(version_log, 'r'))

def main():
"""Run the thing."""
Expand All @@ -378,10 +403,13 @@ def main():
initial_dir = os.getcwd()
recur_dir = os.path.abspath(os.path.expanduser(CONFIG['recur_dir']))
pkg_arg = os.path.abspath(os.path.expanduser(CONFIG['pkg_arg']))
version_log = os.path.abspath(os.path.expanduser(CONFIG['version_log']))

if CONFIG['highlight']:
apply_highlight_css()

load_version_log(version_log)

with tempfile.TemporaryDirectory() as tmpdirname:
os.chdir(tmpdirname) # genanki is very opinionated about where we are.

Expand All @@ -390,6 +418,8 @@ def main():

os.chdir(initial_dir)

json.dump(VERSION_LOG, open(version_log, 'w'))


if __name__ == "__main__":
exit(main())

0 comments on commit 4141a1f

Please sign in to comment.