Package: django-textformat
django-textformatdjango-textformatmakes it easy to configure and compose text formats.
A text format is simply a set of text filters that are applied in a defined
order. A text filter is a function that takes a markup string and returns a the
formatted string.Built in filters are:html_escapeIt’s the same as Django’sescapetemplate filter, available as text
filter.linebreaks_and_paragraphsIt’s the same as Django’slinebreakstemplate filter, available as text
filter.linebreaksIt’s the same as Django’slinebreaksbrtemplate filter, available as text
filter.striptagsIt’s the same as Django’sstriptagstemplate filter, available as text
filter.urlizeIt’s the same as Django’surlizetemplate filter, available as text
filter.django-textformat does not provide any text formats by default, but to give you
an idea, a text format might consist of the builtin filtershtml_escape,urlize,linebreaks_and_paragraphs.That would allow you to group those filters into a format and use it
consistently throuhgout your project.UsageThe default use case for django-textformat might look like this in your model:fromdjango.dbimportmodelsfromdjango_textformatimportTextFormatFieldclassArticle(models.Model):title=models.CharField(max_length=50)content=models.TextField()content_format=TextFormatField()Then you can use the selected format for the article in your template like
this:{%loadtextformat%}{{article.content|apply_format:article.content_format}}Initially creating text formatsIn order to use a model likeArticleabove, you already need to have adjango_textformat.models.TextFormatinstance defined. You can either create
the format by hand or use a data migration. We suggest using a data migration
which will make sure that all instances of your project (e.g. for all devs)
have the same formats available.To do so, create an empty migration in one of your websites apps, like:python manage.py makemigrations blog --emptyNow make the newly created migration look something like this:# -*- coding: utf-8 -*-from__future__importunicode_literalsfromdjango.dbimportmigrations,modelsdefadd_format(apps,schema_editor):TextFormat=apps.get_model('django_textformat','TextFormat')markdown_format=TextFormat.objects.create(slug='article',name='Article Format')markdown_format.filters.create(name='html_escape',sort=1)markdown_format.filters.create(name='urlize',sort=2)markdown_format.filters.create(name='linebreaks_and_paragraphs',sort=3)defremove_format(apps,schema_editor):TextFormat=apps.get_model('django_textformat','TextFormat')format=TextFormat.objects.get(slug='article')format.delete()classMigration(migrations.Migration):dependencies=[('django_textformat','0001_initial'),]operations=[migrations.RunPython(add_format,remove_format,),]Adding custom text filtersIt’s easy to add custom text filters. In order to add one, you need atext_fitlers.pyfile in your app. Then add a function that takes a string
and returns the formatted string.Here is an example:# in your_app/text_filters.pyfromdjango_textformat.registryimportregistryimportmarkdown@registry.registerdefmarkdown(value):returnmarkdown.markdown(value,extensions=['extra'])Now you have a text filter called'markdown'available for use in your text
formats.DevelopmentInstall the dependencies (including the test dependencies) with:pip install -r requirements.txtThen you can run all tests with:toxChangelog0.1.0Initial release.