Skip to content
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

Merged
merged 13 commits into from Oct 9, 2016
Merged

Added simple "create 404 page" plugin. #175

merged 13 commits into from Oct 9, 2016

Conversation

@felixfontein
Copy link
Contributor

@felixfontein felixfontein commented Oct 2, 2016

Using the new feature in getnikola/nikola#2517.

@felixfontein
Copy link
Contributor Author

@felixfontein felixfontein commented Oct 2, 2016

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 :)

@felixfontein
Copy link
Contributor Author

@felixfontein felixfontein commented Oct 2, 2016

Ok, I think I'm now happy with the current state. Ready to merge from my side.

Copy link
Member

@Kwpolska Kwpolska left a comment

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]
Copy link
Member

@Kwpolska Kwpolska Oct 2, 2016

(minor wart) this line could use a few space characters, so could conf.py.sample

Copy link
Contributor Author

@felixfontein felixfontein Oct 2, 2016

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).
Copy link
Member

@Kwpolska Kwpolska Oct 2, 2016

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/).

Copy link
Contributor Author

@felixfontein felixfontein Oct 2, 2016

Done.

@@ -0,0 +1,57 @@
from nikola.plugin_categories import Task
Copy link
Member

@Kwpolska Kwpolska Oct 2, 2016

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.)

Copy link
Contributor Author

@felixfontein felixfontein Oct 2, 2016

Done.

def prepare_error_page(self, destination, lang, template):
context = {}

deps = self.site.template_system.template_deps(template)
Copy link
Member

@Kwpolska Kwpolska Oct 2, 2016

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?

Copy link
Contributor Author

@felixfontein felixfontein Oct 2, 2016

Let me take a look.

Copy link
Contributor Author

@felixfontein felixfontein Oct 2, 2016

This should be no problem with getnikola/nikola#2519.

<div class="title">
<h1 class="entry-title">{{ messages('Page not found', lang) }}</h1>
</div>
<div>
Copy link
Member

@Kwpolska Kwpolska Oct 2, 2016

(minor wart) do we really need a classless div here?

Copy link
Contributor Author

@felixfontein felixfontein Oct 2, 2016

No, that's not needed here. It's just a leftover from my personal theme.

@felixfontein
Copy link
Contributor Author

@felixfontein felixfontein commented Oct 2, 2016

I now renamed the plugin to errorpages.



class CreateErrorPages(Task):
name = "create_error_pages"
Copy link
Member

@Kwpolska Kwpolska Oct 2, 2016

Should also say errorpages.

Copy link
Contributor Author

@felixfontein felixfontein Oct 2, 2016

Done.

def set_site(self, site):
super(CreateErrorPages, self).set_site(site)

def prepare_error_page(self, destination, lang, template):
Copy link
Member

@Kwpolska Kwpolska Oct 2, 2016

So, no way not to copy-paste this?

Copy link
Contributor Author

@felixfontein felixfontein Oct 2, 2016

You mean what you mentioned above? I'm still looking at nikola.py...

Copy link
Member

@Kwpolska Kwpolska left a comment

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
Copy link
Member

@Kwpolska Kwpolska Oct 9, 2016

Still uses old variable name here.

Copy link
Contributor Author

@felixfontein felixfontein Oct 9, 2016

Thanks for noticing! Fixed it.

@Kwpolska
Copy link
Member

@Kwpolska Kwpolska commented Oct 9, 2016

Great work! Merging. Will be downloadable in a minute or two.

@Kwpolska Kwpolska merged commit 15a4462 into getnikola:master Oct 9, 2016
1 check was pending
@felixfontein
Copy link
Contributor Author

@felixfontein felixfontein commented Oct 9, 2016

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants