- Notifications
You must be signed in to change notification settings - Fork11
A Django application for parsing, displaying and editing BBCodes-based text contents.
License
ellmetha/django-precise-bbcode
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Django-precise-bbcode is a Django application providing a way to create textual contents based on BBCodes.
BBCode is a special implementation of HTML. BBCode itself is similar in style to HTML, tags are enclosed in square brackets [ and ] rather than < and > and it offers greater control over what and how something is displayed.
This application includes a BBCode compiler aimed to render any BBCode content to HTML and allows the use of BBCodes tags in models, forms and admin forms. The BBCode parser comes with built-in tags (the default ones ;b,u, etc) and allows the use of smilies, custom BBCode placeholders and custom BBCode tags. These can be added in two different ways:
- Custom tags can be defined in the Django administration panel and stored into the database ; doing this allows any non-technical admin to add BBCode tags by defining the HTML replacement string associated with each tag
- Tags can also be manually registered to be used by the parser by defining a tag class aimed to render a given bbcode tag and its content to the corresponding HTML markup
Table of Contents
Online browsable documentation is available athttps://django-precise-bbcode.readthedocs.org.
- Python 3.6+
- Django 3.2+
- PIL or Pillow (required for smiley tags)
Just run:
pip install django-precise-bbcode
Once installed you can configure your project to usedjango-precise-bbcode with the following steps.
Addprecise_bbcode toINSTALLED_APPS in your project's settings module:
INSTALLED_APPS= (# other apps'precise_bbcode',)
Then install the models:
python manage.py migrate
Django-precise-bbcode comes with a BBCode parser that allows you to transform a textual content containing BBCode tags to the corresponding HTML markup. To do this, simply import theget_parser shortcut and use therender method of the BBCode parser:
>>> from precise_bbcode.bbcode import get_parser>>> parser = get_parser()>>> parser.render('[b]Hello [u]world![/u][/b]')'<strong>Hello <u>world!</u></strong>'It's that easy!
As you may need to render bbcodes inside one of your Django template, this parser can be used as a template filter or as a template tag after loadingbbcode_tags:
{% load bbcode_tags %}{% bbcode entry.bbcode_content %}{{ "[b]Write some bbcodes![/b]"|bbcode }}The BBCode content included in theentry.bbcode_content field will be converted to HTML and displayed. The last statement will output<strong>Write some bbcodes!</strong>.
While you can use the Django built-inmodels.TextField to add your BBCode contents to your models, a common need is to store both the BBCode content and the corresponding HTML markup in the database. To address thisdjango-precise-bbcode provides aBBCodeTextField.
fromdjango.dbimportmodelsfromprecise_bbcode.fieldsimportBBCodeTextFieldclassPost(models.Model):content=BBCodeTextField()
This field will store both the BBCode content and the correspondign HTML markup. The HTML content of such a field can then be displayed in any template by using itsrendered attribute:
{{ post.content.rendered }}Head over to thedocumentation for all the details on how to use the BBCode parser and how to define custom BBcode tags, placeholders and smilies.
Morgan Aubert (@ellmetha) andcontributors
BSD. SeeLICENSE for more details.
About
A Django application for parsing, displaying and editing BBCodes-based text contents.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.
Contributors6
Uh oh!
There was an error while loading.Please reload this page.