From 5f32df1fdd820fffd78518f1c1783c357388b548 Mon Sep 17 00:00:00 2001 From: silviana amethyst <1388063+ofloveandhate@users.noreply.github.com> Date: Wed, 24 Jul 2024 10:00:36 -0500 Subject: [PATCH] factored out the markdown to html extensions list now provides defaults OUTSIDE the function, and the extensions are customizable via the defaults.json file they are NOT over-rideable per-object, though --- markdown2canvas/canvas_objects.py | 8 ++-- markdown2canvas/translation_functions.py | 51 +++++++++++++++++------- 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/markdown2canvas/canvas_objects.py b/markdown2canvas/canvas_objects.py index cd8f09a..38ed0b9 100644 --- a/markdown2canvas/canvas_objects.py +++ b/markdown2canvas/canvas_objects.py @@ -131,6 +131,7 @@ def __init__(self,folder,course=None): self.name = None self.style_path = None self.replacements_path = None + self.markdown_extensions = None # populate the above variables from the meta.json file self._set_from_metadata() @@ -173,8 +174,7 @@ def _set_from_metadata(self): if self.replacements_path: self.replacements_path = find_in_containing_directory_path(self.replacements_path) - - + self.markdown_extensions = get_default_markdown_extensions() def translate_to_html(self,course): """ @@ -196,7 +196,7 @@ def translate_to_html(self,course): outname = join(self.folder,"extra_styled_source.md") apply_style_html(with_header_md, self.style_path, outname) - self._translated_html = markdown2html(outname, course, self.replacements_path) + self._translated_html = markdown2html(outname, course, self.replacements_path, self.markdown_extensions) @@ -204,7 +204,7 @@ def translate_to_html(self,course): # self._translated_html = apply_style_html(translated_html_without_hf, self.style_path, outname) else: - self._translated_html = markdown2html(self.sourcename,course, self.replacements_path) + self._translated_html = markdown2html(self.sourcename,course, self.replacements_path, self.markdown_extensions) self._local_images = find_local_images(self._translated_html) diff --git a/markdown2canvas/translation_functions.py b/markdown2canvas/translation_functions.py index f1af2dd..6babc1b 100644 --- a/markdown2canvas/translation_functions.py +++ b/markdown2canvas/translation_functions.py @@ -12,6 +12,8 @@ 'get_default_property', 'get_default_style_name', 'get_default_replacements_name', + 'default_markdown_extensions', + 'get_default_markdown_extensions', 'apply_style_markdown', 'apply_style_html', 'markdown2html', @@ -118,34 +120,42 @@ def preprocess_markdown_images(contents,style_path): return contents -def get_default_property(key, helpstr): - +def get_default_property(key, helpstr, default_value = None): + defaults_name = find_in_containing_directory_path(path.join("_course_metadata","defaults.json")) - + try: logger.info(f'trying to use defaults from {defaults_name}') with open(defaults_name,'r',encoding='utf-8') as f: import json defaults = json.loads(f.read()) + except Exception as e: + print(f'⚠️ failed to load defaults from `{defaults_name}`. either you are not at the correct location to be doing this, or you need to create a json file at {defaults_name}. returning a default value {default_value}') + return default_value + + if key in defaults: + return defaults[key] + else: + print(f'no default `{key}` specified in {defaults_name}. add an entry with key `{key}`, being {helpstr}. returning a default value of {default_value}') + return default_value - if key in defaults: - return defaults[key] - else: - print(f'no default `{key}` specified in {defaults_name}. add an entry with key `{key}`, being {helpstr}') - return None - except Exception as e: - print(f'WARNING: failed to load defaults from `{defaults_name}`. either you are not at the correct location to be doing this, or you need to create a json file at {defaults_name}.') - return None def get_default_style_name(): - return get_default_property(key='style', helpstr='a path to a file relative to the top course folder') + return get_default_property(key='style', + helpstr='a path to a file relative to the top course folder') def get_default_replacements_name(): - return get_default_property(key='replacements', helpstr='a path to a json file containing key:value pairs of text-to-replace. this path should be expressed relative to the top course folder') + return get_default_property(key='replacements', + helpstr='a path to a json file containing key:value pairs of text-to-replace. this path should be expressed relative to the top course folder') +default_markdown_extensions = ['codehilite','fenced_code','md_in_html','tables','nl2br'] +def get_default_markdown_extensions(): + return get_default_property(key='markdown_extensions', + helpstr='a list of strings being extensions to the `markdown` library used to translate from markdown to html before uploading to canvas.', + default_value = default_markdown_extensions) def apply_style_markdown(sourcename, style_path, outname): @@ -196,7 +206,7 @@ def apply_style_html(translated_html_without_hf, style_path, outname): -def markdown2html(filename, course, replacements_path): +def markdown2html(filename, course, replacements_path, markdown_extensions): """ This is the main routine in the library. @@ -207,6 +217,17 @@ def markdown2html(filename, course, replacements_path): If `course` is None, then you won't get some of the functionality. In particular, you won't get link replacements for references to other content on Canvas. If `replacements_path` is None, then no replacements, duh. Otherwise it should be a string or Path object to an existing json file containing key-value pairs of strings to replace with other strings. + + `markdown_extensions` is a list of strings (or functions) specifying extensions to the `markdown` library to use during + translation from markdown to HTML. + The list I've been using for DS710/DS150 has been + ['codehilite','fenced_code','md_in_html','tables','nl2br']. + You can find more on available extensions, and writing your own, at https://python-markdown.github.io/extensions/ + + You can provide your own list of extensions in the `_course_metadata/defaults.json` file + via the `markdown_extensions` key-value pair. + If no such key/value pair exists in `defaults.json`, then the following default will be provided for you: + `['codehilite','fenced_code','md_in_html','tables','nl2br']` """ logger.debug(f'Translating `{filename}` from markdown to html using replacements from `{replacements_path}`.') @@ -230,7 +251,7 @@ def markdown2html(filename, course, replacements_path): emojified = emoji.emojize(markdown_source) - html = markdown.markdown(emojified, extensions=['codehilite','fenced_code','md_in_html','tables','nl2br']) # see https://python-markdown.github.io/extensions/ + html = markdown.markdown(emojified, extensions=markdown_extensions) # see https://python-markdown.github.io/extensions/ soup = BeautifulSoup(html,features="lxml") all_imgs = soup.findAll("img")