Skip to content

Commit

Permalink
Merge pull request #9 from stuartmaxwell/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartmaxwell authored Jun 4, 2024
2 parents 84abdd1 + 7318fda commit ff5c789
Show file tree
Hide file tree
Showing 9 changed files with 395 additions and 83 deletions.
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Django",
"type": "debugpy",
"request": "launch",
"args": ["runserver"],
"django": true,
"autoStartBrowser": false,
"program": "${workspaceFolder}/example/manage.py"
}
]
}
1 change: 1 addition & 0 deletions djpress/app_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
MARKDOWN_EXTENSIONS: list = ["fenced_code", "codehilite", "tables"]
BLOG_TITLE: str = "My DJ Press Blog"
BLOG_DESCRIPTION: str = ""
POST_READ_MORE_TEXT: str = "Read more..."

# DJPress URL settings
CATEGORY_PATH_ENABLED: bool = True
Expand Down
6 changes: 1 addition & 5 deletions djpress/templates/djpress/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ <h1>{% post_title_link %}</h1>
<p>By {% post_author %}. {% post_date %}</p>
</header>
<section>
{{ post.truncated_content_markdown|safe }}

{% if post.is_truncated %}
<p><a href="{% url 'djpress:post_detail' post.permalink %}">Read more</a></p>
{% endif %}
{% post_content %}
</section>
<footer>
<p>Categories: {% post_categories "span" link_class="badge" %}</p>
Expand Down
34 changes: 31 additions & 3 deletions djpress/templatetags/djpress_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

from djpress.conf import settings
from djpress.models import Category, Post
from djpress.templatetags.helpers import categories_html, category_link
from djpress.templatetags.helpers import (
categories_html,
category_link,
post_read_more_link,
)
from djpress.utils import get_author_display_name

register = template.Library()
Expand Down Expand Up @@ -269,19 +273,43 @@ def post_date_link(context: Context, link_class: str = "") -> str:


@register.simple_tag(takes_context=True)
def post_content(context: Context) -> str:
def post_content(
context: Context,
read_more_link_class: str = "",
read_more_text: str = "",
) -> str:
"""Return the content of a post.
If there's no post in the context, then we return an empty string. If there are
multiple posts in the context, then we return the truncated content of the post with
the read more link. Otherwise, we return the full content of the post.
Args:
context: The context.
read_more_link_class: The CSS class(es) for the read more link.
read_more_text: The text for the read more link.
Returns:
str: The content of the post.
"""
content: str = ""

# Check if there's a post or posts in the context.
post: Post | None = context.get("post")
posts: models.QuerySet[Post] | None = context.get("posts")

# If there's no post, then we return an empty string.
if not post:
return ""
return content

# If there are posts, then we return the truncated content of the post.
if posts:
content = mark_safe(post.truncated_content_markdown)
if post.is_truncated:
content += post_read_more_link(post, read_more_link_class, read_more_text)
return mark_safe(content)

# If there
return mark_safe(post.content_markdown)


Expand Down
27 changes: 26 additions & 1 deletion djpress/templatetags/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from django.db import models
from django.urls import reverse

from djpress.models import Category
from djpress.conf import settings
from djpress.models import Category, Post


def categories_html(
Expand Down Expand Up @@ -70,3 +71,27 @@ def category_link(category: Category, link_class: str = "") -> str:
f'<a href="{category_url}" title="View all posts in the {category.name} '
f'category"{link_class_html}>{ category.name }</a>'
)


def post_read_more_link(
post: Post,
link_class: str = "",
read_more_text: str = "",
) -> str:
"""Return the read more link for a post.
Args:
post: The post.
link_class: The CSS class(es) for the link.
read_more_text: The text for the read more link.
Returns:
str: The read more link.
"""
read_more_text = read_more_text if read_more_text else settings.POST_READ_MORE_TEXT
link_class_html = f' class="{link_class}"' if link_class else ""

return (
f'<p><a href="{reverse("djpress:post_detail", args=[post.permalink])}"'
f'{link_class_html}>{read_more_text}</a></p>'
)
1 change: 1 addition & 0 deletions example/config/settings_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
CACHE_CATEGORIES = True
RECENT_PUBLISHED_POSTS_COUNT = 3
DATE_ARCHIVES_ENABLED = True
POST_READ_MORE_TEXT = "Test read more..."
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "djpress"
version = "0.3.4"
version = "0.3.5"
authors = [{ name = "Stuart Maxwell", email = "[email protected]" }]
description = "A blog application for Django sites, inspired by classic WordPress."
readme = "README.md"
Expand Down
Loading

0 comments on commit ff5c789

Please sign in to comment.