Skip to main content

A django BBCode integration..

Project description

https://badge.fury.io/py/django-precise-bbcode.png build-status https://coveralls.io/repos/ellmetha/django-precise-bbcode/badge.png?branch=master

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 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

Read more in the documentation (latest version).

Requirements

  • python >= 2.7 (tested with version 2.7, 3.2, 3.3)

  • django >= 1.4.2 (tested with version 1.4.2, 1.5, 1.6)

  • PIL or Pillow (required for smiley tags)

  • setuptools

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 syncdb

Or, if you are using South:

python manage.py migrate precise_bbcode

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.parser 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 and smilies.

Author

Morgan Aubert (@ellmetha) <morgan.aubert@zoho.com>

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-0.4.2.tar.gz (28.6 kB view details)

Uploaded Source

Built Distribution

django_precise_bbcode-0.4.2-py2.py3-none-any.whl (39.5 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for django-precise-bbcode-0.4.2.tar.gz
Algorithm Hash digest
SHA256 37621ef0917936ec5e9f6c800ef7dc4c26493974abe8e4958b648c4d4b072279
MD5 87677d6fd0c4bdca180b8bc0386ab70b
BLAKE2b-256 05ccb4fc2a4cde53eb85f27b9a7dceecfd50071c7843f5b123c067c2a2324b73

See more details on using hashes here.

File details

Details for the file django_precise_bbcode-0.4.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_precise_bbcode-0.4.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5be79b56c8b072b9bb66529a02456c1efc9541b01f844e28238289bf73182c0b
MD5 3cb87def0d828c170562ecce8472a001
BLAKE2b-256 202880d88f1ed17dfd9422b10997b03fccb81beb1ae4a128468d6da2e35d8d20

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