diff --git a/markdown_view/__init__.py b/markdown_view/__init__.py index 41aa256..09468fa 100644 --- a/markdown_view/__init__.py +++ b/markdown_view/__init__.py @@ -6,6 +6,8 @@ "MARKDOWN_VIEW_EXTENSIONS": list, "MARKDOWN_VIEW_BASE_DIR": str, "MARKDOWN_VIEW_TEMPLATE": str, + "MARKDOWN_VIEW_USE_REQUEST_CONTEXT": bool, + "MARKDOWN_VIEW_EXTRA_CONTEXT": dict } # Validate settings types. diff --git a/markdown_view/constants.py b/markdown_view/constants.py index 209b036..b723cd1 100644 --- a/markdown_view/constants.py +++ b/markdown_view/constants.py @@ -1,3 +1,7 @@ +from markdown_view.markdown_extensions import ImageExtension + DEFAULT_MARKDOWN_VIEW_LOADERS = ["markdown_view.loaders.MarkdownLoader", ] -DEFAULT_MARKDOWN_VIEW_EXTENSIONS = ["tables", "fenced_code", "toc", ] -DEFAULT_MARKDOWN_VIEW_TEMPLATE = "markdown_view/markdown.html" \ No newline at end of file +DEFAULT_MARKDOWN_VIEW_EXTENSIONS = ["tables", "fenced_code", "toc", ImageExtension(), ] +DEFAULT_MARKDOWN_VIEW_TEMPLATE = "markdown_view/markdown.html" +DEFAULT_MARKDOWN_VIEW_USE_REQUEST_CONTEXT = False +DEFAULT_MARKDOWN_VIEW_EXTRA_CONTEXT = {} diff --git a/markdown_view/views.py b/markdown_view/views.py index 72e0874..b16dfc5 100644 --- a/markdown_view/views.py +++ b/markdown_view/views.py @@ -3,12 +3,14 @@ import markdown from django.conf import settings from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin -from django.template import Engine +from django.template import Engine, Template, Context +from django.template.loader import render_to_string from django.utils.safestring import mark_safe from django.views.generic import TemplateView from markdown_view.constants import ( DEFAULT_MARKDOWN_VIEW_LOADERS, DEFAULT_MARKDOWN_VIEW_EXTENSIONS, DEFAULT_MARKDOWN_VIEW_TEMPLATE, + DEFAULT_MARKDOWN_VIEW_USE_REQUEST_CONTEXT, DEFAULT_MARKDOWN_VIEW_EXTRA_CONTEXT, ) logger = logging.getLogger(__name__) @@ -29,8 +31,26 @@ def get_context_data(self, *args, **kwargs): "MARKDOWN_VIEW_EXTENSIONS", DEFAULT_MARKDOWN_VIEW_EXTENSIONS )) + template = Template( + "{{% load static %}}{}".format(md.convert(template.source)) + ) + render_context_base = {} + if getattr( + settings, + "MARKDOWN_VIEW_USE_REQUEST_CONTEXT", + DEFAULT_MARKDOWN_VIEW_USE_REQUEST_CONTEXT + ): + render_context_base = context + render_context = Context({ + **render_context_base, + **(getattr( + settings, + "MARKDOWN_VIEW_EXTRA_CONTEXT", + DEFAULT_MARKDOWN_VIEW_EXTRA_CONTEXT + )) + }) context.update({ - "markdown_content": mark_safe(md.convert(template.source)), + "markdown_content": mark_safe(template.render(render_context)), "markdown_toc": mark_safe(md.toc), "page_title": mark_safe(md.toc_tokens[0]['name']), })