Skip to content

Commit

Permalink
Adds complex image processing
Browse files Browse the repository at this point in the history
  • Loading branch information
rgs258 committed Jun 16, 2020
1 parent e597461 commit 4865ece
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 2 additions & 0 deletions markdown_view/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions markdown_view/constants.py
Original file line number Diff line number Diff line change
@@ -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"
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 = {}
24 changes: 22 additions & 2 deletions markdown_view/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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']),
})
Expand Down

0 comments on commit 4865ece

Please sign in to comment.