Typography Filter

    Recently, Igor Kononuchenko posted a version of a typograph written in Python . Thank you very much human Igor. And I modestly decided to make a typographic filter for django out of the library. Not that it's complicated - but for beginners like me it might come in handy. What actually happened.

    1. To get started, I downloaded through the SVN library with Google code: http://code.google.com/p/typo-py/ .

    2. As a guide in writing filters, I turned to official documentation .

    3. Did tests on the previously written Portfolio model - from here and such ways turned out. I put the typographus.py file in mysite / portfolio / templatetags (do not forget to put an empty file there__init__.py ). I added the filter code itself to the end of the file: 4. I added the line mysite.portfolio.templatetags.typographus to the settings.py file in the INSTALLED_APPS list , which connects the handler. 5. The following code turned out in the html-template: That's all. In the process of writing I managed to run into a small problem: the typographer itself requires a line in utf8, and if a filter chain is used (for paragraph transfers: {{portfolio.text | linebreaks | typographus}} ), an error is generated . Therefore, in the filter itself there is a check for the string type and a conversion in utf8, if necessary.

    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)






    {% extends "base.html" %}
    {% load typographus %}
    ...
    {% block content %}

    {% autoescape off %}

    {{ portfolio.title|typographus }}


    {{ portfolio.text|linebreaks|typographus }}
    {% endautoescape %}

    {% endblock %}




    I learned a little crumpled - I recently use Django - if I made a mistake somewhere - correct me. And thanks again to Igor for the typographer.

    Also popular now: