Skip to content

Commit

Permalink
Merge pull request #480 from Pelican-Elegant/fix-default
Browse files Browse the repository at this point in the history
fix clean url and default issue
  • Loading branch information
talha131 authored Aug 10, 2019
2 parents a29f3ca + 6ae1b51 commit 54aa309
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 38 deletions.
61 changes: 61 additions & 0 deletions documentation/content/Advanced Features/clean-url.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
authors: Talha Mansoor
Title: Clean URL
Tags: unique
Date: 2019-08-10 20:44
Slug: clean-url
Category: Advanced Features
---

Out of the box, Pelican generated URLs are [unclean](https://en.wikipedia.org/wiki/Clean_URL), i.e. they have `.html` appended. Elegant lets you use clean URLs with minimal configuration.

!!! Important "Mandatory"

To use clean URLs, you server must support it.

## Categories, Tags and Archives

In your Pelican configuration set following values

```python
TAGS_URL = "tags"
CATEGORIES_URL = "categories"
ARCHIVES_URL = "archives"
```

With it set, your categories, tags and archives will be served from following links,

1. [`/categories`](/categories)
1. [`/tags`](/tags)
1. [`/archives`](/archives)

If you do not set the variables then the URLs will become

1. [`/categories.html`](/categories.html)
1. [`/tags.html`](/tags.html)
1. [`/archives.html`](/archives.html)

## Articles

For clean URLs for articles, just set

```python
ARTICLE_URL = "{slug}"
```

## Pages

For pages, set

```python
PAGE_URL = "{slug}"
PAGE_SAVE_AS = "{slug}.html"
```

## Search

For search page, set

```python
SEARCH_URL = "search"
```
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).
5 changes: 4 additions & 1 deletion documentation/pelicanconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# Regional Settings
TIMEZONE = "Asia/Karachi"
DATE_FORMATS = {"en": "%b %d, %Y"}
DEFAULT_LANG = "en"

# Plugins and extensions
MARKDOWN = {
Expand Down Expand Up @@ -56,6 +55,10 @@
ARTICLE_URL = "{slug}"
PAGE_URL = "{slug}"
PAGE_SAVE_AS = "{slug}.html"
TAGS_URL = "tags"
CATEGORIES_URL = "categories"
ARCHIVES_URL = "archives"
SEARCH_URL = "search"

# Feeds
AUTHOR_FEED_ATOM = None
Expand Down
38 changes: 26 additions & 12 deletions gulpfile.babel.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
import fs from "fs";
import path from "path";
import { watch, parallel } from "gulp";
import { exec } from "child_process";
import { create as browserSyncCreate } from "browser-sync";
const browserSync = browserSyncCreate();

const content_404 = fs.readFileSync(
path.join(__dirname, "documentation/output/404.html")
);

const buildAll = () => exec("cd documentation && invoke build");

const reload = cb => {
browserSync.init({
ui: {
port: 9002
},
server: {
baseDir: "documentation/output",
serveStaticOptions: {
extensions: ["html"]
}
browserSync.init(
{
ui: {
port: 9002
},
server: {
baseDir: "documentation/output",
serveStaticOptions: {
extensions: ["html"]
}
},
files: "documentation/output/*.html",
port: 9001
},
files: "documentation/output/*.html",
port: 9001
});
(_, bs) => {
bs.addMiddleware("*", (_, res) => {
res.write(content_404);
res.end();
});
}
);
cb();
};

Expand Down
3 changes: 2 additions & 1 deletion templates/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ <h1>That page doesn't exist!</h1>
<p>Sorry, but the page you are looking for cannot be found. It seems that the page you were trying to reach doesn't exist, or may be it has just moved, or it no longer exists.</p>
<p>The best thing to do is to use the search form or start again from the <a href="{{ SITEURL }}">home page</a>.</p>
<div class="input-append">
<form action="{{ SITEURL }}/search.html" onsubmit="return validateForm(this.elements['q'].value)">
{% from '_includes/_defaults.html' import SEARCH_URL with context %}
<form action="{{ SITEURL }}/{{ SEARCH_URL }}" onsubmit="return validateForm(this.elements['q'].value)">
<input type="text" class="span12" placeholder="Search" name="q" id="tipue_search_input_404">
<button class="btn" type="submit">Search</button>
</form>
Expand Down
24 changes: 24 additions & 0 deletions templates/_includes/_defaults.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,27 @@
{% 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 %}

{%if not SEARCH_URL %}
{% set SEARCH_URL = 'search.html' %}
{% else %}
{% set SEARCH_URL = SEARCH_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
11 changes: 6 additions & 5 deletions templates/base.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="{{ DEFAULT_LANG | default("en-US") }}">
<html lang="{{ DEFAULT_LANG }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
Expand Down Expand Up @@ -78,10 +78,11 @@
<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>
<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>
{% from '_includes/_defaults.html' import TAGS_URL, CATEGORIES_URL, ARCHIVES_URL, SEARCH_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_URL }}" 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>
</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
4 changes: 2 additions & 2 deletions templates/search.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, SEARCH_URL with context %}
<meta property="og:title" content="Search · {{ SITENAME|striptags|e }}"/>
<meta name="twitter:title" content="Search · {{ SITENAME|striptags|e }}">
<meta property="og:url" content="{{ SITEURL }}/search.html" />
<meta property="og:url" content="{{ SITEURL }}/{{ SEARCH_URL }}" />
<meta property="og:description" content="Search results for {{ SITENAME|striptags|e }} blog" />
<meta name="twitter:description" content="Search results for {{ 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 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

0 comments on commit 54aa309

Please sign in to comment.