Skip to content

Commit

Permalink
fix(clean URL): default URL of categories, tags and archives fails on…
Browse files Browse the repository at this point in the history
… some servers

In the first version of the theme, we had hard coded ".html" to URLs
like "tags.html".

Later it was made configurable via ARCHIVES_URL, CATEGORIES_URL, and
TAGS_URL. But the default values of these variables were incorrectly
set to "tags", "categories" and "archives".

This broke on several servers that didn't append ".html" to URLs
automatically.

With this patch, the default value of these variables is "*.html".
This theme should work out of the box on all servers.

BREAKING CHANGE: To enable clean URLs for tags, categories and archives,
first configure your server to support clean URLs. Then set `TAGS_URL`,
`CATEGORIES_URL` and `ARCHIVES_URL` to `"tags"`, `"categories"` and
`"archives"` respectively.

fix #280
fix #276
  • Loading branch information
talha131 committed Aug 10, 2019
1 parent a29f3ca commit 3c7df6a
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ Elegant is best maintained when you treat it as a separate resource in your
project. Do not edit template files, JavaScript code and style sheets. This
practice will make upgrading Elegant fuss free process.

Instead see the guides in ["Extra Customization" category](categories.html#extra-customization-ref).
Instead see the guides in ["Extra Customization" category](categories#extra-customization-ref).
18 changes: 18 additions & 0 deletions templates/_includes/_defaults.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,21 @@
{% else %}
{% set PHOTOS_LIGHTBOX = PHOTOS_LIGHTBOX %}
{% endif %}

{%if not TAGS_URL %}
{% set TAGS_URL = 'tags.html' %}
{% else %}
{% set TAGS_URL = TAGS_URL %}
{% endif %}

{%if not CATEGORIES_URL %}
{% set CATEGORIES_URL = 'categories.html' %}
{% else %}
{% set CATEGORIES_URL = CATEGORIES_URL %}
{% endif %}

{%if not ARCHIVES_URL %}
{% set ARCHIVES_URL = 'archives.html' %}
{% else %}
{% set ARCHIVES_URL = ARCHIVES_URL %}
{% endif %}
8 changes: 4 additions & 4 deletions templates/archives.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@

{% block meta_tags_in_head %}
{{ super() }}
{% from '_includes/_defaults.html' import FEATURED_IMAGE, ARCHIVES_URL with context %}
<meta property="og:title" content="All Posts · {{ SITENAME|striptags|e }}"/>
<meta name="twitter:title" content="All Posts · {{ SITENAME|striptags|e }}">
<meta property="og:url" content="{{ SITEURL }}/{{ ARCHIVES_URL|default('archives') }}" />
<meta property="og:url" content="{{ SITEURL }}/{{ ARCHIVES_URL }}" />
<meta property="og:description" content="Full archives of {{ SITENAME|striptags|e }} blog" />
<meta name="twitter:description" content="Full archives of {{ SITENAME|striptags|e }} blog">
<meta property="og:site_name" content="{{ SITENAME|striptags|e }}" />
<meta property="og:article:author" content="{{ AUTHOR }}" />
{% from '_includes/_defaults.html' import FEATURED_IMAGE with context %}
{% if FEATURED_IMAGE %}
<meta property="og:image" content="{{FEATURED_IMAGE}}" />
<meta name="twitter:image" content="{{FEATURED_IMAGE}}" >
{% endif %}
{% endblock meta_tags_in_head %}

{% block content %}

{% from '_includes/_defaults.html' import ARCHIVES_URL with context %}
<div class="row-fluid">
<header class="page-header span10 offset2">
<h1><a href="{{ SITEURL }}/{{ ARCHIVES_URL|default('archives') }}">All Posts</a></h1>
<h1><a href="{{ SITEURL }}/{{ ARCHIVES_URL }}">All Posts</a></h1>
</header>
</div>
<div class="row-fluid">
Expand Down
6 changes: 4 additions & 2 deletions templates/article.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,15 @@ <h4>Published</h4>
{% include '_includes/series.html' %}
{% if article.category|trim|count > 0 %}
<h4>Category</h4>
<a class="category-link" href="{{ SITEURL }}/categories.html#{{ category.slug }}-ref">{{ article.category }}</a>
{% from '_includes/_defaults.html' import CATEGORIES_URL with context %}
<a class="category-link" href="{{ SITEURL }}/{{ CATEGORIES_URL }}#{{ category.slug }}-ref">{{ article.category }}</a>
{% endif %}
{% if article.tags and article.tags[0]|trim|count > 0 %}
{% from '_includes/_defaults.html' import TAGS_URL with context %}
<h4>Tags</h4>
<ul class="list-of-tags tags-in-article">
{% for tag in article.tags|sort %}
<li><a href="{{ SITEURL }}/{{ TAGS_URL|default('tags') }}#{{ tag.slug }}-ref">{{ tag }}
<li><a href="{{ SITEURL }}/{{ TAGS_URL }}#{{ tag.slug }}-ref">{{ tag }}
{% for aTag, tagged_articles in tags if aTag == tag %}
<span>{{ tagged_articles|count }}</span>
{% endfor %}</a></li>
Expand Down
7 changes: 4 additions & 3 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@
<li {% if p == page %} class="active"{% endif %}><a href="{{ SITEURL }}/{{ p.url }}">{{ p.title }}</a></li>
{% endfor %}
{% endif %}
<li {% if page_name == 'categories' %} class="active"{% endif %}><a href="{{ SITEURL }}/{{ CATEGORIES_URL|default('categories') }}">Categories</a></li>
<li {% if page_name == 'tags' %} class="active"{% endif %}><a href="{{ SITEURL }}/{{ TAGS_URL|default('tags') }}">Tags</a></li>
<li {% if page_name == 'archives' %} class="active"{% endif %}><a href="{{ SITEURL }}/{{ ARCHIVES_URL|default('archives') }}">Archives</a></li>
{% from '_includes/_defaults.html' import TAGS_URL, CATEGORIES_URL, ARCHIVES_URL with context %}
<li {% if page_name == 'categories' %} class="active"{% endif %}><a href="{{ SITEURL }}/{{ CATEGORIES_URL }}">Categories</a></li>
<li {% if page_name == 'tags' %} class="active"{% endif %}><a href="{{ SITEURL }}/{{ TAGS_URL }}">Tags</a></li>
<li {% if page_name == 'archives' %} class="active"{% endif %}><a href="{{ SITEURL }}/{{ ARCHIVES_URL }}">Archives</a></li>
<li><form class="navbar-search" action="{{ SITEURL }}/search.html" onsubmit="return validateForm(this.elements['q'].value);"> <input type="text" class="search-query" placeholder="Search" name="q" id="tipue_search_input"></form></li>
</ul>
</div>
Expand Down
7 changes: 4 additions & 3 deletions templates/categories.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

{% block meta_tags_in_head %}
{{ super() }}
{% from '_includes/_defaults.html' import FEATURED_IMAGE, CATEGORIES_URL with context %}
<meta property="og:title" content="All Categories · {{ SITENAME|striptags|e }}"/>
<meta name="twitter:title" content="All Categories · {{ SITENAME|striptags|e }}">
<meta property="og:url" content="{{ SITEURL }}/{{ CATEGORIES_URL|default('categories') }}" />
<meta property="og:url" content="{{ SITEURL }}/{{ CATEGORIES_URL }}" />
<meta property="og:description" content="All categories of the {{ SITENAME|striptags|e }} blog" />
<meta name="twitter:description" content="All categories of the {{ SITENAME|striptags|e }} blog">
<meta property="og:site_name" content="{{ SITENAME|striptags|e }}" />
<meta property="og:article:author" content="{{ AUTHOR }}" />
{% from '_includes/_defaults.html' import FEATURED_IMAGE with context %}
{% if FEATURED_IMAGE %}
<meta property="og:image" content="{{FEATURED_IMAGE}}" />
<meta name="twitter:image" content="{{FEATURED_IMAGE}}" >
Expand All @@ -30,9 +30,10 @@
{% endblock feed_links %}

{% block content %}
{% from '_includes/_defaults.html' import CATEGORIES_URL with context %}
<div class="row-fluid">
<header class="page-header span10 offset2">
<h1><a href="{{ SITEURL }}/{{ CATEGORIES_URL|default('categories') }}">All Categories</a></h1>
<h1><a href="{{ SITEURL }}/{{ CATEGORIES_URL }}">All Categories</a></h1>
</header>
</div>

Expand Down
6 changes: 3 additions & 3 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ <h1 id="my-projects">{{ PROJECTS_TITLE }}</h1>
{% if articles and PROJECTS and LANDING_PAGE_TITLE %}
{% set css_class = 'span12' %}
{% endif %}
{% from '_includes/_defaults.html' import RECENT_ARTICLES_COUNT, ARCHIVES_URL, CATEGORIES_URL with context %}
<div class="row-fluid">
<div class="{{css_class}}">
<header>
<h1 id="recent-posts">Recent Posts <a id="allposts" href="{{ SITEURL }}/{{ ARCHIVES_URL|default('archives') }}">all posts</a></h1>
<h1 id="recent-posts">Recent Posts <a id="allposts" href="{{ SITEURL }}/{{ ARCHIVES_URL }}">all posts</a></h1>
</header>
<div class="recent-posts">
{% for article in articles %}
{% from '_includes/_defaults.html' import RECENT_ARTICLES_COUNT with context %}
{% if loop.index0 < RECENT_ARTICLES_COUNT %}
<article itemscope>
<a href="{{ SITEURL }}/{{ article.url }}">
Expand All @@ -111,7 +111,7 @@ <h1 id="recent-posts">Recent Posts <a id="allposts" href="{{ SITEURL }}/{{ ARCHI
</a>
<section>
posted in
<a href="{{ SITEURL }}/{{ CATEGORIES_URL|default('categories') }}#{{ article.category.slug }}-ref">{{ article.category }}</a>
<a href="{{ SITEURL }}/{{ CATEGORIES_URL }}#{{ article.category.slug }}-ref">{{ article.category }}</a>
<div class="recent-posts-time">
<time itemprop="dateCreated" datetime="{{ article.date.isoformat() }}">{{ article.locale_date }}</time>
</div>
Expand Down
8 changes: 4 additions & 4 deletions templates/tags.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
{{ super() }}
<meta property="og:title" content="All Tags · {{ SITENAME|striptags|e }}"/>
<meta name="twitter:title" content="All Tags · {{ SITENAME|striptags|e }}">
<meta property="og:url" content="{{ SITEURL }}/{{ TAGS_URL|default('tags') }}" />
{% from '_includes/_defaults.html' import FEATURED_IMAGE, TAGS_URL with context %}
<meta property="og:url" content="{{ SITEURL }}/{{ TAGS_URL }}" />
<meta property="og:description" content="All tags used in the {{ SITENAME|striptags|e }} blog" />
<meta name="twitter:description" content="All tags used in the {{ SITENAME|striptags|e }} blog">
<meta property="og:site_name" content="{{ SITENAME|striptags|e }}" />
<meta property="og:article:author" content="{{ AUTHOR }}" />
{% from '_includes/_defaults.html' import FEATURED_IMAGE with context %}
{% if FEATURED_IMAGE %}
<meta property="og:image" content="{{FEATURED_IMAGE}}" />
<meta name="twitter:image" content="{{FEATURED_IMAGE}}" >
Expand All @@ -30,10 +30,10 @@
{% endblock feed_links %}

{% block content %}

{% from '_includes/_defaults.html' import TAGS_URL with context %}
<div class="row-fluid">
<header class="page-header span10 offset2">
<h1><a href="{{ SITEURL }}/{{ TAGS_URL|default('tags') }}">All Tags</a></h1>
<h1><a href="{{ SITEURL }}/{{ TAGS_URL }}">All Tags</a></h1>
</header>
</div>
<div class="row-fluid">
Expand Down

3 comments on commit 3c7df6a

@astraindev
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your idea of putting the defaults in the _defaults.html file. Obviously, these changes will fix the issue. You got all of the relevant files.

@astraindev
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost forgot...for the documentation, use the following diff as well (for the same reason):

diff --git a/documentation/pelicanconf.py b/documentation/pelicanconf.py
index eda3f02..e1efb51 100644
--- a/documentation/pelicanconf.py
+++ b/documentation/pelicanconf.py
@@ -54,8 +54,9 @@ DEFAULT_PAGINATION = False
 DEFAULT_CATEGORY = "Miscellaneous"
 USE_FOLDER_AS_CATEGORY = False
 ARTICLE_URL = "{slug}"
+ARTICLE_SAVE_AS = "{slug}/index.html"
 PAGE_URL = "{slug}"
-PAGE_SAVE_AS = "{slug}.html"
+PAGE_SAVE_AS = "{slug}/index.html"

 # Feeds
 AUTHOR_FEED_ATOM = None

@talha131
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your idea of putting the defaults in the _defaults.html file. Obviously, these changes will fix the issue. You got all of the relevant files.

Thanks a lot @andrewdstrain for reviewing the patch.


Almost forgot...for the documentation, use the following diff as well (for the same reason):

I just tried the documentation site locally and noticed that all pages and articles are working without making any change to the configuration

Please let me know if I am missing something here.

Please sign in to comment.