Invenio-Formatter¶
Jinja utilities for Invenio.
Further documentation is available on https://invenio-formatter.readthedocs.io/
User’s Guide¶
This part of the documentation will show you how to get started in using Invenio-Formatter.
Installation¶
Invenio-Formatter is on PyPI so all you need is:
$ pip install invenio-formatter
If you want to use the badge generation feature you need explicit enable it:
$ pip install invenio-formatter[badges]
This will install Pillow and CairoSVG library. For these libraries to work you must have the following system libraries installed with development headers:
- FreeType
- Cairo
- DejaVu Sans font
Linux¶
Install the dependencies with your package manager. For Ubuntu or Debian:
$ sudo apt-get install libcairo2-dev
OS X¶
Install the dependencies above with Homebrew:
$ brew install cairo
Download the [DejaVu Sans](https://dejavu-fonts.github.io) font and install it on your system.
Configuration¶
Configuration for Invenio-Formatter.
-
invenio_formatter.config.
FORMATTER_BADGES_ALLOWED_TITLES
= ['DOI']¶ List of allowed titles in badges.
-
invenio_formatter.config.
FORMATTER_BADGES_MAX_CACHE_AGE
= 0¶ The maximum amount of time a badge will be considered fresh.
-
invenio_formatter.config.
FORMATTER_BADGES_TITLE_MAPPING
= {}¶ Mapping of titles.
Usage¶
Jinja utilities for Invenio applications.
Quick start¶
This section presents an example of the Invenio-Formatter features. In this section we will create a simple page with a time formatter and a shields.io badge.
First, let us create a new template page on which we will render some text,
and save it to some location (e.g.: /home/myuser/templates/index.html
)
Today is {{ mydate|to_arrow|format_arrow('YYYY-MM-DD') }}
Your book: {{ badge_svg('isbn','9780399547331')|safe }}
Create a new Flask application and set some configuration for badge generation:
>>> from flask import Flask
>>> app = Flask('TestApp', template_folder='/home/myuser/templates')
>>> app.config.update(FORMATTER_BADGES_ALLOWED_TITLES=('isbn', 'ISBN', ))
>>> app.config.update(FORMATTER_BADGES_TITLE_MAPPING={'isbn': 'ISBN',})
Note
By default the template_folder
is the directory
templates
at the root of your flask application.
Load the Invenio-Formatter extension and run the application:
>>> from invenio_formatter import InvenioFormatter
>>> ext_fmt = InvenioFormatter(app)
>>> app.run() # doctest: +SKIP
You should now be able to access the index page with formatted date and badge examples at http://localhost:5000.
Badges endpoints¶
In addition of generating an image of a shield badge, a badge-rendering endpoint is also available for easy embedding on other websites.
You can modify the template as follows:
{% set mydate = mydate|from_isodate -%}
Today is {{ mydate.strftime('%Y-%m-%d') }}
Your book (with badge URL: ):
<img src="{{ url_for('invenio_formatter_badges.badge',
title='isbn', value='9780399547331', ext='svg') }}"></img>
Your badge will be visible on the page as before, and also directly accessible at http://localhost:5000/badge/ISBN/9780399547331.svg.
Example application¶
First install Invenio-Formatter by running:
$ pip install -e .[all]
$ cd examples
Next, start the development server:
$ export FLASK_APP=app.py FLASK_DEBUG=1
$ flask run
and open the example application in your browser:
$ open http://127.0.0.1:5000/
API Reference¶
If you are looking for information on a specific function, class or method, this part of the documentation is for you.
API Docs¶
Jinja utilities for Invenio.
-
class
invenio_formatter.ext.
InvenioFormatter
(app=None)[source]¶ Invenio-Formatter extension.
Extension initialization.
Parameters: app – The Flask application. (Default: None
)
Context preprocessors¶
Badges context processor.
-
invenio_formatter.context_processors.badges.
badges_processor
()[source]¶ Context processor for badges.
-
invenio_formatter.context_processors.badges.
generate_badge_png
(title, value, color='#007ec6')[source]¶ Generate the badge in PNG format.
Filters¶
Datetime Jinja filters.
-
invenio_formatter.filters.datetime.
format_arrow
(value, format_string)[source]¶ Format an arrow datetime object.
Parameters: - value – The arrow datetime object.
- format_string – The date format string
Returns: Returns a string representation of the given arrow datetime object, formatted according to the given format string.
Note
Do not use this filter to format date/times presented to an end user. Instead use
datetimeformat
ordateformat
from Invenio-I18N.
-
invenio_formatter.filters.datetime.
from_isodate
(value, strict=False)[source]¶ Convert an ISO formatted date into a Date object.
Parameters: - value – The ISO formatted date.
- strict – If value is
None
, then if strict isTrue
it returns the Date object of today, otherwise it returnsNone
. (Default:False
)
Returns: The Date object or
None
.
-
invenio_formatter.filters.datetime.
from_isodatetime
(value, strict=False)[source]¶ Convert an ISO formatted datetime into a Date object.
Parameters: - value – The ISO formatted datetime.
- strict – If value is
None
, then if strict isTrue
it returns the Date object of today, otherwise it returnsNone
. (Default:False
)
Returns: The Date object or
None
.
-
invenio_formatter.filters.datetime.
to_arrow
(value)[source]¶ Convert a Date object to an arrow datetime object.
HTML sanitisation Jinja filters.
-
invenio_formatter.filters.html.
sanitize_html
(value, tags=None, attributes=None)[source]¶ Sanitize HTML.
Parameters: - tags – Allowed HTML
tags
. Configuration set by Invenio-Config. - attributes – Allowed HTML
attributes
. Configuration set by Invenio-Config.
Use this function when you need to include unescaped HTML that contain user provided data.
- tags – Allowed HTML
Additional Notes¶
Notes on how to contribute, legal information and changes are here for the interested.
Contributing¶
Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.
Types of Contributions¶
Report Bugs¶
Report bugs at https://github.com/inveniosoftware/invenio-formatter/issues.
If you are reporting a bug, please include:
- Your operating system name and version.
- Any details about your local setup that might be helpful in troubleshooting.
- Detailed steps to reproduce the bug.
Fix Bugs¶
Look through the GitHub issues for bugs. Anything tagged with “bug” is open to whoever wants to implement it.
Implement Features¶
Look through the GitHub issues for features. Anything tagged with “feature” is open to whoever wants to implement it.
Write Documentation¶
Invenio-Formatter could always use more documentation, whether as part of the official Invenio-Formatter docs, in docstrings, or even on the web in blog posts, articles, and such.
Submit Feedback¶
The best way to send feedback is to file an issue at https://github.com/inveniosoftware/invenio-formatter/issues.
If you are proposing a feature:
- Explain in detail how it would work.
- Keep the scope as narrow as possible, to make it easier to implement.
- Remember that this is a volunteer-driven project, and that contributions are welcome :)
Get Started!¶
Ready to contribute? Here’s how to set up invenio-formatter for local development.
Fork the inveniosoftware/invenio-formatter repo on GitHub.
Clone your fork locally:
$ git clone git@github.com:your_name_here/invenio-formatter.git
Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:
$ mkvirtualenv invenio-formatter $ cd invenio-formatter/ $ pip install -e .[all]
Create a branch for local development:
$ git checkout -b name-of-your-bugfix-or-feature
Now you can make your changes locally.
When you’re done making changes, check that your changes pass tests:
$ ./run-tests.sh
The tests will provide you with test coverage and also check PEP8 (code style), PEP257 (documentation), flake8 as well as build the Sphinx documentation and run doctests.
Commit your changes and push your branch to GitHub:
$ git add . $ git commit -s -m "component: title without verbs" -m "* NEW Adds your new feature." -m "* FIX Fixes an existing issue." -m "* BETTER Improves and existing feature." -m "* Changes something that should not be visible in release notes." $ git push origin name-of-your-bugfix-or-feature
Submit a pull request through the GitHub website.
Pull Request Guidelines¶
Before you submit a pull request, check that it meets these guidelines:
- The pull request should include tests and must not decrease test coverage.
- If the pull request adds functionality, the docs should be updated. Put your new functionality into a function with a docstring.
- The pull request should work for Python 2.7, 3.3, 3.4 and 3.5. Check https://travis-ci.org/inveniosoftware/invenio-formatter/pull_requests and make sure that the tests pass for all supported Python versions.
Changes¶
Version 1.1.0a1 (released 2020-05-15)
- Integrates Semantic-UI templates
Version 1.0.3 (released 2020-05-06)
- Deprecated Python versions lower than 3.6.0. Now supporting 3.6.0 and 3.7.0
Version 1.0.2 (released 2019-07-29)
- Adds HTML sanitization Jinja filter.
Version 1.0.1 (released 2018-05-25)
- Fixes issue with badges being cached.
Version 1.0.0 (released 2018-03-23)
- Initial public release.
License¶
MIT License
Copyright (C) 2015-2018 CERN.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Note
In applying this license, CERN does not waive the privileges and immunities granted to it by virtue of its status as an Intergovernmental Organization or submit itself to any jurisdiction.
Contributors¶
- Alexander Ioannidis
- Alizee Pace
- Eamonn Maguire
- Harri Hirvonsalo
- Javier Delgado
- Jiri Kuncar
- Krzysztof Nowak
- Lars Holm Nielsen
- Leonardo Rossi
- Orestis Melkonian
- Sami Hiltunen
- Sebastian Witowski
- Tibor Simko