Skip to main content

A django BBCode integration..

Project description

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

Documentation

Online browsable documentation is available at https://django-precise-bbcode.readthedocs.org.

Requirements

  • Python 3.5+

  • Django 2.2+

  • PIL or Pillow (required for smiley tags)

Installation

Just run:

pip install django-precise-bbcode

Once installed you can configure your project to use django-precise-bbcode with the following steps.

Add precise_bbcode to INSTALLED_APPS in your project’s settings module:

INSTALLED_APPS = (
    # other apps
    'precise_bbcode',
)

Then install the models:

python manage.py migrate

Usage

Rendering bbcodes

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 the get_parser shortcut and use the render 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 loading bbcode_tags:

{% load bbcode_tags %}
{% bbcode entry.bbcode_content %}
{{ "[b]Write some bbcodes![/b]"|bbcode }}

The BBCode content included in the entry.bbcode_content field will be converted to HTML and displayed. The last statement will output <strong>Write some bbcodes!</strong>.

Storing bbcodes

While you can use the Django built-in models.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 this django-precise-bbcode provides a BBCodeTextField.

from django.db import models
from precise_bbcode.fields import BBCodeTextField

class Post(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 its rendered attribute:

{{ post.content.rendered }}

And more…

Head over to the documentation for all the details on how to use the BBCode parser and how to define custom BBcode tags, placeholders and smilies.

Authors

Morgan Aubert (@ellmetha) and contributors

License

BSD. See LICENSE for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-precise-bbcode-1.2.11.tar.gz (27.8 kB view details)

Uploaded Source

Built Distribution

django_precise_bbcode-1.2.11-py3-none-any.whl (38.6 kB view details)

Uploaded Python 3

File details

Details for the file django-precise-bbcode-1.2.11.tar.gz.

File metadata

  • Download URL: django-precise-bbcode-1.2.11.tar.gz
  • Upload date:
  • Size: 27.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.2 CPython/3.8.1 Darwin/19.2.0

File hashes

Hashes for django-precise-bbcode-1.2.11.tar.gz
Algorithm Hash digest
SHA256 4857a6a85ff9ec016a13ad714cb0625467d6ef8c549fdedeb2850b0e14f18725
MD5 b893c7297c9162ef215e2ff5d7e3fe65
BLAKE2b-256 e6298c01e79a59aab79b56340b50233509e50ce5453fa9669401a98972c33166

See more details on using hashes here.

File details

Details for the file django_precise_bbcode-1.2.11-py3-none-any.whl.

File metadata

File hashes

Hashes for django_precise_bbcode-1.2.11-py3-none-any.whl
Algorithm Hash digest
SHA256 d9a1254c9c5fd967d4a34a1b5aeee1d51caca596208a58cbe00d8f4055f78092
MD5 84db50c0c97c2d7364a03d9ba1705a42
BLAKE2b-256 0c9c9711d07b9ea9832bb36e5ab5a7b8064a0e1072cf57e8b0f05cc53b8e411c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page