Skip to content

Commit

Permalink
Merge pull request #123 from zerasul/develop
Browse files Browse the repository at this point in the history
v 0.1.4
  • Loading branch information
zerasul committed Feb 21, 2020
2 parents 78af429 + 5a9bcb8 commit 104e9b9
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 116 deletions.
19 changes: 17 additions & 2 deletions Blask/blaskapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

from flask import Flask, render_template, request
from flask import Flask, render_template, request, abort
from Blask.blasksettings import BlaskSettings
from Blask.blogrenderer import BlogRenderer
from Blask.errors import PageNotExistError
Expand Down Expand Up @@ -51,6 +51,11 @@ def __init__(self, **kwargs):
self.app.add_url_rule("/search", view_func=self.searchpages, methods=["POST"])
self.app.add_url_rule("/category/<category>", view_func=self._getcategory, methods=["GET"])
self.app.add_url_rule("/author/<author>", view_func=self._getauthor, methods=["GET"])
## Register the error handler for each setting
print(self.settings)
for error in self.settings["errors"].keys():
self.app.register_error_handler(error, f=self._handle_http_errors)


def _index(self):
"""
Expand All @@ -72,7 +77,7 @@ def _getpage(self, filename):
try:
entry = self.blogrenderer.renderfile(filename)
except PageNotExistError:
entry = self.blogrenderer.renderfile("404")
abort(404)
content = entry.content
date = entry.date
template = entry.template
Expand Down Expand Up @@ -138,6 +143,16 @@ def _getauthor(self, author):
self.settings["defaultLayout"], title=self.settings["title"], content=content
)

def _handle_http_errors(self, errorMessage):
"""
Handle the custom http error code; getting the custom url name.
:param errorMessage: Message error.
:return: rendered custom error page.
"""
page = self.settings['errors'][errorMessage.code]
return self._getpage(page)


def run(self, **kwargs):
"""
Run the current instance of Blask
Expand Down
5 changes: 4 additions & 1 deletion Blask/blaskcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class CLIController:
Wanna help us?
Check [this project at GitHub](https://github.com/zerasul/blask)
Check [this project at GitHub](https://github.com/zerasul/blask) Or see the documentation in the [project web page](https://getblask.com/docs).
"""

settings = """
Expand All @@ -82,6 +82,9 @@ class CLIController:
# Title of the blog
title='Blask | A Simple Blog Engine Based on Flask'
# error handler configuration
errors = { 404 : '404' }
"""

not_found = "# 404\n Page not found"
Expand Down
2 changes: 2 additions & 0 deletions Blask/blasksettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"defaultLayout": str("template.html"),
"staticDir": str(BASE_DIR / "static"),
"title": "Blask | A Simple Blog Engine Based on Flask",
"errors": {
404 : "404"} # Dictionary with errors handler
}


Expand Down
6 changes: 4 additions & 2 deletions Blask/blogrenderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,10 @@ def __init__(self, name, md, content):
self.name = name
meta = md.Meta
if meta:
self.date = datetime.strptime(meta["date"][0], "%Y-%m-%d")
self.tags = meta["tags"][0].split(",")
if "date" in meta.keys():
self.date = datetime.strptime(meta["date"][0], "%Y-%m-%d")
if "tags" in meta.keys():
self.tags = meta["tags"][0].split(",")
if "template" in meta.keys():
self.template = meta["template"][0]
if "category" in meta.keys():
Expand Down
208 changes: 103 additions & 105 deletions Pipfile.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ postDir = "posts"
defaultLayout = "template.html"
staticDir = "static"
title = "Blask | A Simple Blog Engine Based on Flask"
errors= { 404: "404"}
```

For last, to Run Blask, use the next Code:
Expand All @@ -35,7 +36,7 @@ For last, to Run Blask, use the next Code:
if __name__ == '__main__':
b = BlaskApp(templateDir=settings.templateDir, postDir=settings.postDir
, defaultLayout=settings.defaultLayout,
staticDir=settings.staticDir, title=settings.title)
staticDir=settings.staticDir, title=settings.title, errors={404:'404'})
b.run()
```

Expand Down
6 changes: 6 additions & 0 deletions posts/0.1.4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Version 0.1.4

* [Fixed a Bug when the metadata date variable is not setted](https://github.com/zerasul/blask/issues/122)
* [Adding custom Error pages](https://github.com/zerasul/blask/issues/19)
* [Adding more info to default index webpage](https://github.com/zerasul/blask/issues/62)
* [Updating Dependencies](https://github.com/zerasul/blask/pulls?q=is%3Apr+is%3Aclosed+author%3Aapp%2Fdependabot-preview)
16 changes: 16 additions & 0 deletions posts/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ In this page we can see all the documentation about the Blask Project:
* [Post Metadata](#post-metadata)
* [Create a Template](#create-template)
* [Special Pages](#special-pages)
* [Custom Error Pages](#custom-error-pages)
* [Tag Search](#tag-search)
* [Category Search](#category-search)
* [Author Search](#author-search)
Expand Down Expand Up @@ -181,6 +182,21 @@ With Blask you can edit the content of 2 Special Pages:
* **index page**: This is the main page. Its markdown contents reside in the _index.md_ file in the posts folder.
* **404**: This is the _Page Not Found_ response. Its markdown contents reside in the _404.md_ file in the posts folder.

## <a id="custom-error-pages">Custom Error Pages</a>

(**Since 0.1.4**)

With Blask you can set custom error pages for the Http error codes. In the ```settings``` file, you can set the ```errors``` dictionary for link each http error code to the name of the markdown file (without extension), in the post directory.

:::python
# settings.py
...
# errors handle dictionary
errors = { 404 : '404',
500 : 'mycustom500'
}


## <a id="tag-search"></a>Tag Search

With Blask you can search posts by their tags. To see the posts with one particular tag, browse `http://< url >/tag/< tag-name >`.
Expand Down
2 changes: 2 additions & 0 deletions posts/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ First if you need to run Blask, need to see the `settings.py` file and set the p

# Website title
title = 'Blask | A Simple Blog Engine Based on Flask'
# custom Error handlers
errors = { 404 : "404"}


Next you need to run the next code:
Expand Down
1 change: 1 addition & 0 deletions posts/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ We also have some [examples](/examples) available.

Here are the release notes of each Blask version:

* [0.1.4](/0.1.4)
* [0.1.3](/0.1.3)
* [0.1.2](/0.1.2)
* [0.1.1](/0.1.1)
Expand Down
3 changes: 2 additions & 1 deletion posts/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ After that, you need to configure Blask, create a file called _settings.py_ file
defaultLayout = "template.html"
staticDir = path.join(BASE_DIR, "static")
title = "Blask"
errors = { 404:"404"}

You can export a environment variable that point to this file:

Expand All @@ -38,7 +39,7 @@ You can run the application using this code:

if __name__ == '__main__':
b = BlaskApp(templateDir=settings.templateDir, postDir=settings.postDir
, defaultLayout=settings.defaultLayout, staticDir=settings.staticDir, title=settings.title)
, defaultLayout=settings.defaultLayout, staticDir=settings.staticDir, title=settings.title, errors={404 : "404"})
b.run()
If you use the environment Variable you can run Blask without arguments:
Expand Down
4 changes: 4 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@

# Website title
title = 'Blask | A Simple Blog Engine Based on Flask'

errors= { 500 : "500",
404 : "404"
}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

setup(
name='Blask',
version='0.1.3',
version='0.1.4b1',
packages=find_packages(exclude=['tests']),
url='https://getblask.com',
license='GPL 3.0',
Expand Down
2 changes: 1 addition & 1 deletion templates/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ <h3>Table of Contents</h3>
</div>
</div>

<footer>&copy; 2019 - All rights reserved - v0.1.3 - Made with Blask.</footer>
<footer>&copy; 2020 - All rights reserved - v0.1.4 - Made with Blask.</footer>
<script async defer src="https://buttons.github.io/buttons.js"></script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
Expand Down
4 changes: 4 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@

# Website title
title = 'The mantis revenge!'

errors= { 500 : "500",
404 : "404"
}
4 changes: 2 additions & 2 deletions tests/settings_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os

from pathlib import Path
from Blask import blasksettings
from Blask.blasksettings import BlaskSettings
from pytest import fixture, raises
Expand All @@ -17,7 +17,7 @@ def test_from_environ(self):
settings = BlaskSettings()
for kw in blasksettings.DEFAULT_SETTINGS.keys():
if kw == 'postDir':
assert settings[kw] == os.path.join(os.getcwd(), 'posts2')
assert settings[kw] == os.path.join(Path('.').resolve(), 'posts2')
elif kw == 'title':
assert settings[kw] == 'The mantis revenge!'
else:
Expand Down
3 changes: 3 additions & 0 deletions tests/testsettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@

# Website title
title = 'The mantis revenge!'

errors= { 404 : "404"
}

0 comments on commit 104e9b9

Please sign in to comment.