Recently
Igor Kononuchenko laid out a
version of a typographer written in Python . Igor thanks a lot. And I modestly decided to make a typographic filter for django from the library. Not that it is difficult - but for beginners, like me, it can be useful. What actually happened.
1. To start, downloaded through SVN library from Google code:
http://code.google.com/p/typo-py/ .
2. As a guide in writing filters turned to
official documentation .
')
3. I did tests on a previously written Portfolio model - hence, such paths turned out. The file
typographus.py put in
mysite / portfolio / templatetags (don’t forget to put an empty
__init__.py file there). At the end of the file I added the filter code itself:
from django import template
register=template.Library()
@register.filter
@stringfilter
def typographus(string):
"""Russian typorgify"""
if type(string) is not unicode:
string = unicode(string)
return Typographus().process(string)
4. In the
settings.py file in the
INSTALLED_APPS list, added a line,
mysite.portfolio.templatetags.typographus , which connects the handler.
5. In the html-template we got the following code:
{% extends "base.html" %}
{% load typographus %}
...
{% block content %}
{% autoescape off %}
<h1>{{ portfolio.title|typographus }}</h1>
{{ portfolio.text|linebreaks|typographus }}
{% endautoescape %}
{% endblock %}
That's all. In the process of writing, I managed to run into a little problem: the typographer himself requires a line in utf8, and if a filter chain is used (for paragraph
breaks :
{{portfolio.text | linebreaks | typographus}} ), an error is
generated . Therefore, in the filter itself there is a check for the type of the string and a conference in utf8, if necessary.
I learned a little crumpled - I just recently used Django - if where I was wrong, correct me. And thanks again to
Igor for the typographer.