Skip to content

Commit

Permalink
Renamed blog_post_wrapper to post_wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartmaxwell committed Oct 23, 2024
1 parent 3951592 commit 54ce464
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 44 deletions.
8 changes: 4 additions & 4 deletions src/djpress/templates/djpress/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h1>{% blog_title_link %}</h1>

{% if post %}

{% blog_post %}
{% post_wrap %}

<header>
{% post_title outer_tag="h1" %}
Expand All @@ -36,15 +36,15 @@ <h1>{% blog_title_link %}</h1>
<p>Categories: {% post_categories_link "span" "badge" %}</p>
</footer>

{% end_blog_post %}
{% end_post_wrap %}

{% else %}

<h1>Latest Posts</h1>

{% for post in posts %}

{% blog_post %}
{% post_wrap %}

<header>
{% post_title outer_tag="h2" %}
Expand All @@ -57,7 +57,7 @@ <h1>Latest Posts</h1>
<p>Categories: {% post_categories_link "span" "badge" %}</p>
</footer>

{% end_blog_post %}
{% end_post_wrap %}

{% empty %}

Expand Down
17 changes: 12 additions & 5 deletions src/djpress/templatetags/djpress_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,14 +816,14 @@ def rss_url() -> str:
return url_utils.get_rss_url()


@register.tag(name="blog_post")
def blog_post_wrapper_tag(parser: template.base.Parser, token: template.base.Token) -> helpers.BlogPostWrapper:
@register.tag(name="post_wrap")
def post_wrapper_tag(parser: template.base.Parser, token: template.base.Token) -> helpers.BlogPostWrapper:
"""Parse the blog post wrapper tag.
This is a template tag that wraps the blog post content in a configurable HTML tag with a CSS class.
Example usage:
{% blog_post "article" "post" %}<p>Post content</p>{% end_blog_post %}
{% post_wrap "article" "post" %}<p>Post content</p>{% end_post_wrap %}
Args:
parser: The template parser.
Expand All @@ -834,9 +834,16 @@ def blog_post_wrapper_tag(parser: template.base.Parser, token: template.base.Tok
"""
params = token.split_contents()[1:] # skip the tag name

tag, css_class = helpers.parse_blog_post_wrapper_params(params)
tag, css_class = helpers.parse_post_wrapper_params(params)

nodelist = parser.parse(("end_blog_post",))
# If microformats are enabled, add the h-entry class before the css class
if djpress_settings.MICROFORMATS_ENABLED:
css_class = f"h-entry {css_class}" if css_class else "h-entry"

if css_class:
css_class = f' class="{css_class}"'

nodelist = parser.parse(("end_post_wrap",))
parser.delete_first_token()

return helpers.BlogPostWrapper(nodelist, tag, css_class)
13 changes: 3 additions & 10 deletions src/djpress/templatetags/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class BlogPostWrapper(template.Node):
def __init__(self, nodelist: template.NodeList, tag: str = "", css_class: str = "") -> None:
"""Initialize the BlogPostWrapper."""
self.nodelist = nodelist
self.tag = tag
self.tag = "article" if tag == "" else tag
self.css_class = css_class

def render(self, context: template.Context) -> str:
Expand All @@ -180,17 +180,10 @@ def render(self, context: template.Context) -> str:
if self.tag not in ["div", "span", "section", "article"]:
return mark_safe(content)

# If microformats are enabled, add the h-entry class before the css class
if djpress_settings.MICROFORMATS_ENABLED:
self.css_class = f"h-entry {self.css_class}" if self.css_class else "h-entry"

if self.css_class:
self.css_class = f' class="{self.css_class}"'

return mark_safe(f"<{self.tag}{self.css_class}>{content}</{self.tag}>")


def parse_blog_post_wrapper_params(params: list) -> tuple[str, str]:
def parse_post_wrapper_params(params: list) -> tuple[str, str]:
"""Parse the parameters for the template tag.
Args:
Expand All @@ -199,7 +192,7 @@ def parse_blog_post_wrapper_params(params: list) -> tuple[str, str]:
Returns:
tuple: The tag and CSS class.
"""
tag = "article"
tag = ""
css_class = ""

for num, param in enumerate(params):
Expand Down
32 changes: 16 additions & 16 deletions tests/test_templatetags_djpress_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -1546,9 +1546,9 @@ def test_get_recent_posts(settings, test_post1, test_post2, test_post3):


@pytest.mark.django_db
def test_blog_post_wrapper_single_post_no_tag(settings):
def test_post_wrapper_single_post_no_tag(settings):
"""Creates an article by default."""
template_text = "{% load djpress_tags %}{% blog_post %}<p>This is test post 1.</p>{% end_blog_post %}"
template_text = "{% load djpress_tags %}{% post_wrap %}<p>This is test post 1.</p>{% end_post_wrap %}"
context = Context({})

template = Template(template_text)
Expand All @@ -1564,9 +1564,9 @@ def test_blog_post_wrapper_single_post_no_tag(settings):


@pytest.mark.django_db
def test_blog_post_wrapper_single_post_with_valid_tag(settings):
def test_post_wrapper_single_post_with_valid_tag(settings):
"""Create a div instead."""
template_text = "{% load djpress_tags %}{% blog_post tag='div' %}<p>This is test post 1.</p>{% end_blog_post %}"
template_text = "{% load djpress_tags %}{% post_wrap tag='div' %}<p>This is test post 1.</p>{% end_post_wrap %}"
context = Context({})

template = Template(template_text)
Expand All @@ -1582,9 +1582,9 @@ def test_blog_post_wrapper_single_post_with_valid_tag(settings):


@pytest.mark.django_db
def test_blog_post_wrapper_single_post_with_valid_tag_double_quotes(settings):
def test_post_wrapper_single_post_with_valid_tag_double_quotes(settings):
"""Create a div instead."""
template_text = '{% load djpress_tags %}{% blog_post tag="div" %}<p>This is test post 1.</p>{% end_blog_post %}'
template_text = '{% load djpress_tags %}{% post_wrap tag="div" %}<p>This is test post 1.</p>{% end_post_wrap %}'
context = Context({})

template = Template(template_text)
Expand All @@ -1600,9 +1600,9 @@ def test_blog_post_wrapper_single_post_with_valid_tag_double_quotes(settings):


@pytest.mark.django_db
def test_blog_post_wrapper_single_post_with_invalid_tag():
def test_post_wrapper_single_post_with_invalid_tag():
"""Just returns the content."""
template_text = "{% load djpress_tags %}{% blog_post tag='foobar' %}<p>This is test post 1.</p>{% end_blog_post %}"
template_text = "{% load djpress_tags %}{% post_wrap tag='foobar' %}<p>This is test post 1.</p>{% end_post_wrap %}"
context = Context({})

template = Template(template_text)
Expand All @@ -1611,9 +1611,9 @@ def test_blog_post_wrapper_single_post_with_invalid_tag():


@pytest.mark.django_db
def test_blog_post_wrapper_single_post_with_valid_tag_arg_only():
def test_post_wrapper_single_post_with_valid_tag_arg_only():
"""Just returns the content."""
template_text = "{% load djpress_tags %}{% blog_post 'article' %}<p>This is test post 1.</p>{% end_blog_post %}"
template_text = "{% load djpress_tags %}{% post_wrap 'article' %}<p>This is test post 1.</p>{% end_post_wrap %}"
context = Context({})

template = Template(template_text)
Expand All @@ -1622,9 +1622,9 @@ def test_blog_post_wrapper_single_post_with_valid_tag_arg_only():


@pytest.mark.django_db
def test_blog_post_wrapper_single_post_with_invalid_tag_arg_only():
def test_post_wrapper_single_post_with_invalid_tag_arg_only():
"""Just returns the content."""
template_text = "{% load djpress_tags %}{% blog_post 'foobar' %}<p>This is test post 1.</p>{% end_blog_post %}"
template_text = "{% load djpress_tags %}{% post_wrap 'foobar' %}<p>This is test post 1.</p>{% end_post_wrap %}"
context = Context({})

template = Template(template_text)
Expand All @@ -1633,9 +1633,9 @@ def test_blog_post_wrapper_single_post_with_invalid_tag_arg_only():


@pytest.mark.django_db
def test_blog_post_wrapper_single_post_with_class(settings):
def test_post_wrapper_single_post_with_class(settings):
template_text = (
"{% load djpress_tags %}{% blog_post class='blog-post' %}<p>This is test post 1.</p>{% end_blog_post %}"
"{% load djpress_tags %}{% post_wrap class='blog-post' %}<p>This is test post 1.</p>{% end_post_wrap %}"
)
context = Context({})

Expand All @@ -1651,8 +1651,8 @@ def test_blog_post_wrapper_single_post_with_class(settings):


@pytest.mark.django_db
def test_blog_post_wrapper_single_post_with_tag_and_class(settings):
template_text = "{% load djpress_tags %}{% blog_post tag='div' class='blog-post' %}<p>This is test post 1.</p>{% end_blog_post %}"
def test_post_wrapper_single_post_with_tag_and_class(settings):
template_text = "{% load djpress_tags %}{% post_wrap tag='div' class='blog-post' %}<p>This is test post 1.</p>{% end_post_wrap %}"
context = Context({})

template = Template(template_text)
Expand Down
16 changes: 8 additions & 8 deletions tests/test_templatetags_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
categories_html,
category_link,
post_read_more_link,
parse_blog_post_wrapper_params,
parse_post_wrapper_params,
)


Expand Down Expand Up @@ -131,33 +131,33 @@ def test_post_read_more_link(test_post1):
assert post_read_more_link(test_post1, link_class, read_more_text) == expected_output


def test_parse_blog_post_wrapper_params():
def test_parse_post_wrapper_params():
# Test case 1
params = ['tag="div"', 'class="blog-post"']
expected_output = ("div", "blog-post")
assert parse_blog_post_wrapper_params(params) == expected_output
assert parse_post_wrapper_params(params) == expected_output

# Test case 2
params = ['class="blog-post"']
expected_output = ("article", "blog-post")
assert parse_blog_post_wrapper_params(params) == expected_output
assert parse_post_wrapper_params(params) == expected_output

# Test case 3
params = ['"div"', '"blog-post"']
expected_output = ("div", "blog-post")
assert parse_blog_post_wrapper_params(params) == expected_output
assert parse_post_wrapper_params(params) == expected_output

# Test case 4
params = ['"blog-post"']
expected_output = ("blog-post", "")
assert parse_blog_post_wrapper_params(params) == expected_output
assert parse_post_wrapper_params(params) == expected_output

# Test case 4
params = ['""', '"blog-post"']
expected_output = ("", "blog-post")
assert parse_blog_post_wrapper_params(params) == expected_output
assert parse_post_wrapper_params(params) == expected_output

# Test case 6
params = ['tag="div"', 'class="blog-post"', 'extra="extra"']
expected_output = ("div", "blog-post")
assert parse_blog_post_wrapper_params(params) == expected_output
assert parse_post_wrapper_params(params) == expected_output
2 changes: 1 addition & 1 deletion tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_index_view_multiple_posts(client, test_post1, test_post2, test_post3):
f'<h2 class="p-name"><a href="/test-posts/test-post1/" title="Test Post1">Test Post1</a></h2>'
in response.content.decode()
)
print(response.content.decode())
assert f'<article class="h-entry">' in response.content.decode()


@pytest.mark.django_db
Expand Down

0 comments on commit 54ce464

Please sign in to comment.