Skip to content

Commit

Permalink
Update post_categories to use microformats
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartmaxwell committed Oct 23, 2024
1 parent e92e5ea commit e481d21
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 54 deletions.
8 changes: 4 additions & 4 deletions docs/templatetags.md
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ Outputs:
<h1 class="author-title">Posts that 'Sam Jones' wrote</h1>
```

## post_categories_link
## post_categories

Returns a list of links to the categories of the current post.

Expand All @@ -892,7 +892,7 @@ An HTML string containing a list of category links for the current post.
Just the tag with no arguments:

```django
{% post_categories_link %}
{% post_categories %}
```

Outputs:
Expand All @@ -908,7 +908,7 @@ Outputs:
The tag with all options provided as arguments and using a `div` for the outer tag:

```django
{% post_categories_link outer="div" outer_class="post-categories" link_class="category-link" %}
{% post_categories outer="div" outer_class="post-categories" link_class="category-link" %}
```

Outputs the following HTML:
Expand All @@ -922,7 +922,7 @@ Outputs the following HTML:
The tag with a `span` for the outer tag:

```django
{% post_categories_link outer="span" %}
{% post_categories outer="span" %}
```

Outputs the following HTML:
Expand Down
4 changes: 2 additions & 2 deletions src/djpress/templates/djpress/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h1>{% blog_title_link %}</h1>
{% post_content outer_tag="section" %}

<footer>
<p>Categories: {% post_categories_link "span" "badge" %}</p>
<p>Categories: {% post_categories "span" "badge" %}</p>
</footer>

{% end_post_wrap %}
Expand All @@ -54,7 +54,7 @@ <h1>Latest Posts</h1>
{% post_content outer_tag="section" %}

<footer>
<p>Categories: {% post_categories_link "span" "badge" %}</p>
<p>Categories: {% post_categories "span" "badge" %}</p>
</footer>

{% end_post_wrap %}
Expand Down
2 changes: 1 addition & 1 deletion src/djpress/templatetags/djpress_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ def author_name(


@register.simple_tag(takes_context=True)
def post_categories_link(
def post_categories(
context: Context,
outer: str = "ul",
outer_class: str = "",
Expand Down
14 changes: 13 additions & 1 deletion src/djpress/templatetags/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,19 @@ def category_link(category: Category, link_class: str = "") -> str:
"""
category_url = category.url

link_class_html = f' class="{link_class}"' if link_class else ""
link_classes = ""

# Add p-category if microformats are enabled
if djpress_settings.MICROFORMATS_ENABLED:
link_classes += "p-category "

# Add the user-defined link class
link_classes += link_class

# Trim any trailing spaces
link_classes = link_classes.strip()

link_class_html = f' class="{link_classes}"' if link_classes else ""

return (
f'<a href="{category_url}" title="View all posts in the {category.title} '
Expand Down
62 changes: 36 additions & 26 deletions tests/test_templatetags_djpress_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,13 @@ def test_post_category_link_with_category_path(settings, category1):
assert settings.DJPRESS_SETTINGS["CATEGORY_ENABLED"] is True
assert settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"] == "test-url-category"

expected_output = f'<a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category">{category1.title}</a>'
expected_output = f'<a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="p-category">{category1.title}</a>'
assert djpress_tags.post_category_link(category1) == expected_output

# disable microformats
settings.DJPRESS_SETTINGS["MICROFORMATS_ENABLED"] = False

expected_output = f'<a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category">{category1.title}</a>'
assert djpress_tags.post_category_link(category1) == expected_output


Expand All @@ -316,8 +321,13 @@ def test_post_category_link_with_category_path_with_one_link_class(settings, cat
assert settings.DJPRESS_SETTINGS["CATEGORY_ENABLED"] is True
assert settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"] == "test-url-category"

expected_output = f'<a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="class1">{category1.title}</a>'
expected_output = f'<a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="p-category class1">{category1.title}</a>'
assert djpress_tags.post_category_link(category1, "class1") == expected_output

# disable microformats
settings.DJPRESS_SETTINGS["MICROFORMATS_ENABLED"] = False

expected_output = f'<a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="class1">{category1.title}</a>'
assert djpress_tags.post_category_link(category1, "class1") == expected_output


Expand All @@ -327,7 +337,7 @@ def test_post_category_link_with_category_path_with_two_link_classes(settings, c
assert settings.DJPRESS_SETTINGS["CATEGORY_ENABLED"] is True
assert settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"] == "test-url-category"

expected_output = f'<a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="class1 class2">{category1.title}</a>'
expected_output = f'<a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="p-category class1 class2">{category1.title}</a>'

assert djpress_tags.post_category_link(category1, "class1 class2") == expected_output

Expand Down Expand Up @@ -596,23 +606,23 @@ def test_post_categories(settings, test_post1):
# Confirm settings are set according to settings_testing.py
assert settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"] == "test-url-category"

expected_output = f'<ul><li><a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category">Test Category1</a></li></ul>'
expected_output = f'<ul><li><a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="p-category">Test Category1</a></li></ul>'

assert djpress_tags.post_categories_link(context) == expected_output
assert djpress_tags.post_categories(context) == expected_output


def test_post_categories_none_post_context():
context = Context({"post": None})

expected_output = ""
assert djpress_tags.post_categories_link(context) == expected_output
assert djpress_tags.post_categories(context) == expected_output


def test_post_categories_no_post_context():
context = Context({"foo": None})

expected_output = ""
assert djpress_tags.post_categories_link(context) == expected_output
assert djpress_tags.post_categories(context) == expected_output


@pytest.mark.django_db
Expand All @@ -621,7 +631,7 @@ def test_post_categories_no_categories_context(test_post1):
context = Context({"post": test_post1})

expected_output = ""
assert djpress_tags.post_categories_link(context) == expected_output
assert djpress_tags.post_categories(context) == expected_output


@pytest.mark.django_db
Expand All @@ -631,9 +641,9 @@ def test_post_categories_ul(settings, test_post1):
# Confirm settings are set according to settings_testing.py
assert settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"] == "test-url-category"

expected_output = f'<ul><li><a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category">Test Category1</a></li></ul>'
expected_output = f'<ul><li><a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="p-category">Test Category1</a></li></ul>'

assert djpress_tags.post_categories_link(context, "ul") == expected_output
assert djpress_tags.post_categories(context, "ul") == expected_output


@pytest.mark.django_db
Expand All @@ -643,9 +653,9 @@ def test_post_categories_ul_class1(settings, test_post1):
# Confirm settings are set according to settings_testing.py
assert settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"] == "test-url-category"

expected_output = f'<ul><li><a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="class1">Test Category1</a></li></ul>'
expected_output = f'<ul><li><a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="p-category class1">Test Category1</a></li></ul>'

assert djpress_tags.post_categories_link(context, outer="ul", link_class="class1") == expected_output
assert djpress_tags.post_categories(context, outer="ul", link_class="class1") == expected_output


@pytest.mark.django_db
Expand All @@ -655,9 +665,9 @@ def test_post_categories_ul_class1_class2(settings, test_post1):
# Confirm settings are set according to settings_testing.py
assert settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"] == "test-url-category"

expected_output = f'<ul><li><a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="class1 class2">Test Category1</a></li></ul>'
expected_output = f'<ul><li><a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="p-category class1 class2">Test Category1</a></li></ul>'

assert djpress_tags.post_categories_link(context, outer="ul", link_class="class1 class2") == expected_output
assert djpress_tags.post_categories(context, outer="ul", link_class="class1 class2") == expected_output


@pytest.mark.django_db
Expand All @@ -667,9 +677,9 @@ def test_post_categories_div(settings, test_post1):
# Confirm settings are set according to settings_testing.py
assert settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"] == "test-url-category"

expected_output = f'<div><a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category">Test Category1</a></div>'
expected_output = f'<div><a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="p-category">Test Category1</a></div>'

assert djpress_tags.post_categories_link(context, outer="div") == expected_output
assert djpress_tags.post_categories(context, outer="div") == expected_output


@pytest.mark.django_db
Expand All @@ -679,9 +689,9 @@ def test_post_categories_div_class1(settings, test_post1):
# Confirm settings are set according to settings_testing.py
assert settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"] == "test-url-category"

expected_output = f'<div><a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="class1">Test Category1</a></div>'
expected_output = f'<div><a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="p-category class1">Test Category1</a></div>'

assert djpress_tags.post_categories_link(context, outer="div", link_class="class1") == expected_output
assert djpress_tags.post_categories(context, outer="div", link_class="class1") == expected_output


@pytest.mark.django_db
Expand All @@ -691,9 +701,9 @@ def test_post_categories_div_class1_class2(settings, test_post1):
# Confirm settings are set according to settings_testing.py
assert settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"] == "test-url-category"

expected_output = f'<div><a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="class1 class2">Test Category1</a></div>'
expected_output = f'<div><a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="p-category class1 class2">Test Category1</a></div>'

assert djpress_tags.post_categories_link(context, outer="div", link_class="class1 class2") == expected_output
assert djpress_tags.post_categories(context, outer="div", link_class="class1 class2") == expected_output


@pytest.mark.django_db
Expand All @@ -703,9 +713,9 @@ def test_post_categories_span(settings, test_post1):
# Confirm settings are set according to settings_testing.py
assert settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"] == "test-url-category"

expected_output = f'<span><a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category">Test Category1</a></span>'
expected_output = f'<span><a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="p-category">Test Category1</a></span>'

assert djpress_tags.post_categories_link(context, outer="span") == expected_output
assert djpress_tags.post_categories(context, outer="span") == expected_output


@pytest.mark.django_db
Expand All @@ -715,9 +725,9 @@ def test_post_categories_span_class1(settings, test_post1):
# Confirm settings are set according to settings_testing.py
assert settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"] == "test-url-category"

expected_output = f'<span><a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="class1">Test Category1</a></span>'
expected_output = f'<span><a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="p-category class1">Test Category1</a></span>'

assert djpress_tags.post_categories_link(context, outer="span", link_class="class1") == expected_output
assert djpress_tags.post_categories(context, outer="span", link_class="class1") == expected_output


@pytest.mark.django_db
Expand All @@ -727,9 +737,9 @@ def test_post_categories_span_class1_class2(settings, test_post1):
# Confirm settings are set according to settings_testing.py
assert settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"] == "test-url-category"

expected_output = f'<span><a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="class1 class2">Test Category1</a></span>'
expected_output = f'<span><a href="/{settings.DJPRESS_SETTINGS["CATEGORY_PREFIX"]}/test-category1/" title="View all posts in the Test Category1 category" class="p-category class1 class2">Test Category1</a></span>'

assert djpress_tags.post_categories_link(context, outer="span", link_class="class1 class2") == expected_output
assert djpress_tags.post_categories(context, outer="span", link_class="class1 class2") == expected_output


@pytest.mark.django_db
Expand Down
Loading

0 comments on commit e481d21

Please sign in to comment.