New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added simple "create 404 page" plugin. #175
Conversation
I think I'd like to generalize this plugin a bit before it is merged. (I just noticed I'd also like to have a 403 error message ;) ) So please wait with merging :) |
Ok, I think I'm now happy with the current state. Ready to merge from my side. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, good. Just a few minor warts and things that could be simplified.
By the way, perhaps a shorter name like errorpages
would look better?
for a 404 error page, and | ||
~~~ | ||
# Create 404 and 403 error pages | ||
CREATE_ERROR_PAGES=[403,404] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(minor wart) this line could use a few space characters, so could conf.py.sample
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
~~~ | ||
for both 403 and 404 error pages. Note that you need to provide a `403.tmpl` template for the latter example. | ||
|
||
You also need to configure your web server accordingly. You can find documentation on how to do that for common webservers here: [Apache](https://httpd.apache.org/docs/2.4/custom-error.html), [Nginx](http://nginx.org/en/docs/http/ngx_http_core_module.html#error_page). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will work out of the box on [GitHub Pages](https://help.github.com/articles/creating-a-custom-404-page-for-your-github-pages-site/).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@@ -0,0 +1,57 @@ | |||
from nikola.plugin_categories import Task |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a copyright notice and license of your choice. (Steal the header from Nikola and change the names if you don’t have a favorite license.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
def prepare_error_page(self, destination, lang, template): | ||
context = {} | ||
|
||
deps = self.site.template_system.template_deps(template) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be done without copy-pasting heavy bits of nikola.py? Since you already depend on future versions of Nikola, perhaps you could modify the generic page renderer to do this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me take a look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be no problem with getnikola/nikola#2519.
<div class="title"> | ||
<h1 class="entry-title">{{ messages('Page not found', lang) }}</h1> | ||
</div> | ||
<div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(minor wart) do we really need a classless div here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, that's not needed here. It's just a leftover from my personal theme.
I now renamed the plugin to |
|
||
|
||
class CreateErrorPages(Task): | ||
name = "create_error_pages" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should also say errorpages
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
def set_site(self, site): | ||
super(CreateErrorPages, self).set_site(site) | ||
|
||
def prepare_error_page(self, destination, lang, template): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, no way not to copy-paste this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean what you mentioned above? I'm still looking at nikola.py
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that getnikola/nikola#2519 is merged, please use generic_renderer
here.
|
||
To use this, you need a template `XXX.tmpl` for every [HTTP status code](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes) `XXX`. The plugin provides simple templates `404.tmpl` both for Mako and Jinja, but you can easily adjust them to your needs. These default templates assume that the strings `'Page not found'` and `'The page you are trying to access does not exist. Please use your browser\'s "back" button to return to the previous page.'` are translated. If your blog uses another language than English, you need to translate these strings yourself and provide the translations in your theme's `messages_XX.py` files. Or (for simple one-language blogs) simply adjust the strings in `XXX.tmpl`. | ||
|
||
To tell the plugin which error pages to create, add `CREATE_ERROR_PAGES` to your `conf.py`. This must be a list of error codes, like |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still uses old variable name here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for noticing! Fixed it.
Great work! Merging. Will be downloadable in a minute or two. |
Thanks! |
Using the new feature in getnikola/nikola#2517.