Skip to content

Commit

Permalink
Merge pull request #46 from stuartmaxwell:change-markdown-renderer
Browse files Browse the repository at this point in the history
Move markdown rendering into separate module
  • Loading branch information
stuartmaxwell authored Nov 12, 2024
2 parents c88946b + ad73697 commit e293af1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 26 deletions.
27 changes: 14 additions & 13 deletions src/djpress/app_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@

# DJPress settings
DJPRESS_SETTINGS = {
"TRUNCATE_TAG": ("<!--more-->", str),
"CACHE_CATEGORIES": (True, bool),
"CACHE_RECENT_PUBLISHED_POSTS": (False, bool),
"RECENT_PUBLISHED_POSTS_COUNT": (20, int),
"MARKDOWN_EXTENSIONS": ([], list),
"MARKDOWN_EXTENSION_CONFIGS": ({}, dict),
"BLOG_TITLE": ("My DJ Press Blog", str),
"BLOG_DESCRIPTION": ("", str),
"POST_READ_MORE_TEXT": ("Read more...", str),
"POST_PREFIX": ("{{ year }}/{{ month }}/{{ day }}", str),
"ARCHIVE_ENABLED": (True, bool),
"ARCHIVE_PREFIX": ("", str),
"CATEGORY_ENABLED": (True, bool),
"CATEGORY_PREFIX": ("category", str),
"AUTHOR_ENABLED": (True, bool),
"AUTHOR_PREFIX": ("author", str),
"BLOG_DESCRIPTION": ("", str),
"BLOG_TITLE": ("My DJ Press Blog", str),
"CACHE_CATEGORIES": (True, bool),
"CACHE_RECENT_PUBLISHED_POSTS": (False, bool),
"CATEGORY_ENABLED": (True, bool),
"CATEGORY_PREFIX": ("category", str),
"MARKDOWN_EXTENSION_CONFIGS": ({}, dict),
"MARKDOWN_EXTENSIONS": ([], list),
"MARKDOWN_RENDERER": ("djpress.markdown_renderer.default_renderer", str),
"MICROFORMATS_ENABLED": (True, bool),
"POST_PREFIX": ("{{ year }}/{{ month }}/{{ day }}", str),
"POST_READ_MORE_TEXT": ("Read more...", str),
"RECENT_PUBLISHED_POSTS_COUNT": (20, int),
"RSS_ENABLED": (True, bool),
"RSS_PATH": ("rss", str),
"MICROFORMATS_ENABLED": (True, bool),
"THEME": ("default", str),
"TRUNCATE_TAG": ("<!--more-->", str),
}
18 changes: 18 additions & 0 deletions src/djpress/markdown_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Default Markdown renderer for Djpress."""

import markdown

from djpress.conf import settings as djpress_settings


def default_renderer(markdown_text: str) -> str:
"""Return the Markdown text as HTML."""
md = markdown.Markdown(
extensions=djpress_settings.MARKDOWN_EXTENSIONS,
extension_configs=djpress_settings.MARKDOWN_EXTENSION_CONFIGS,
output_format="html",
)
html = md.convert(markdown_text)
md.reset()

return html
4 changes: 3 additions & 1 deletion src/djpress/models/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
from djpress.conf import settings as djpress_settings
from djpress.exceptions import PageNotFoundError, PostNotFoundError
from djpress.models import Category
from djpress.utils import render_markdown
from djpress.utils import get_markdown_renderer

logger = logging.getLogger(__name__)

render_markdown = get_markdown_renderer()


PUBLISHED_POSTS_CACHE_KEY = "published_posts"

Expand Down
26 changes: 15 additions & 11 deletions src/djpress/utils.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
"""Utility functions that are used in the project."""

import markdown
from django.contrib.auth.models import User
from django.template.loader import TemplateDoesNotExist, select_template
from django.utils import timezone
from django.utils.module_loading import import_string

from djpress.conf import settings as djpress_settings


def render_markdown(markdown_text: str) -> str:
"""Return the Markdown text as HTML."""
md = markdown.Markdown(
extensions=djpress_settings.MARKDOWN_EXTENSIONS,
extension_configs=djpress_settings.MARKDOWN_EXTENSION_CONFIGS,
output_format="html",
)
html = md.convert(markdown_text)
md.reset()
def get_markdown_renderer() -> callable:
"""Get the configured markdown renderer function.
return html
Returns:
callable: The markdown renderer function.
"""
renderer_path = djpress_settings.MARKDOWN_RENDERER

try:
return import_string(renderer_path)
except ImportError as exc:
from django.core.exceptions import ImproperlyConfigured

msg = f"Could not import markdown renderer '{renderer_path}': {exc}"
raise ImproperlyConfigured(msg) from exc


def get_author_display_name(user: User) -> str:
Expand Down
13 changes: 12 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import pytest

from djpress.utils import get_author_display_name, render_markdown, get_template_name
from djpress.utils import get_author_display_name, get_markdown_renderer, get_template_name
from django.contrib.auth.models import User
from django.template.loader import TemplateDoesNotExist


render_markdown = get_markdown_renderer()


# create a parameterized fixture for a test user with first name, last name, and username
@pytest.fixture(
params=[
Expand Down Expand Up @@ -48,6 +51,14 @@ def test_get_author_display_name(test_user):
assert display_name == user_name


def test_render_markdown_does_not_exist(settings):
settings.DJPRESS_SETTINGS["MARKDOWN_RENDERER"] = "djpress.not_exists"
from django.core.exceptions import ImproperlyConfigured

with pytest.raises(ImproperlyConfigured):
get_markdown_renderer()


def test_render_markdown_basic():
markdown_text = "# Heading\n\nThis is some **bold** text. And this is *italic*.\n\nAnd a paragraph."
html = render_markdown(markdown_text)
Expand Down

0 comments on commit e293af1

Please sign in to comment.