Skip to content

Commit

Permalink
Adds a TERMS_ENABLED setting.
Browse files Browse the repository at this point in the history
  • Loading branch information
BertrandBordage committed Aug 29, 2014
1 parent 0f60da9 commit e4ed0c8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
9 changes: 9 additions & 0 deletions docs/settings.rst
Expand Up @@ -8,6 +8,14 @@ Settings
Common settings
---------------

.. _TERMS_ENABLED:

TERMS_ENABLED
.............

:Default: ``True``
:Definition: If set to ``False``, globally disabled django-terms.

.. _TERMS_DEBUG:

TERMS_DEBUG
Expand All @@ -21,6 +29,7 @@ TERMS_DEBUG

TERMS_ADDITIONAL_IGNORED_APPS
.............................

:Default: ``()``
:Definition: A list or tuple of ignored Django applications
(expressed as strings)
Expand Down
37 changes: 24 additions & 13 deletions terms/html.py
Expand Up @@ -2,9 +2,14 @@

import re
from bs4 import BeautifulSoup
try:
from django.utils.encoding import smart_text
except ImportError: # For Django < 1.4.2
from django.utils.encoding import smart_unicode as smart_text
from .models import Term
from .settings import TERMS_IGNORED_TAGS, TERMS_IGNORED_CLASSES, \
TERMS_IGNORED_IDS, TERMS_REPLACE_FIRST_ONLY
from .settings import (
TERMS_IGNORED_TAGS, TERMS_IGNORED_CLASSES, TERMS_IGNORED_IDS,
TERMS_REPLACE_FIRST_ONLY, TERMS_ENABLED)


def build_or_regexp(l):
Expand Down Expand Up @@ -60,15 +65,21 @@ def str_to_soup(html):
return BeautifulSoup(html, 'html.parser')


def replace_in_html(html):
variants_dict = Term.objects.variants_dict()
replace_dict = Term.objects.replace_dict()
replace_regexp = Term.objects.replace_regexp()
replace_regexp__sub = replace_regexp.sub
if TERMS_ENABLED:
def replace_in_html(html):
soup = str_to_soup(html)

soup = str_to_soup(html)
for content in get_interesting_contents(soup, replace_regexp):
new_content = str_to_soup(replace_terms(
replace_dict, variants_dict, replace_regexp__sub, content))
content.replace_with(new_content)
return soup
variants_dict = Term.objects.variants_dict()
replace_dict = Term.objects.replace_dict()
replace_regexp = Term.objects.replace_regexp()
replace_regexp__sub = replace_regexp.sub

for content in get_interesting_contents(soup, replace_regexp):
new_content = str_to_soup(replace_terms(
replace_dict, variants_dict, replace_regexp__sub, content))
content.replace_with(new_content)

return smart_text(soup)
else:
def replace_in_html(html):
return html
2 changes: 2 additions & 0 deletions terms/settings.py
Expand Up @@ -3,6 +3,8 @@
from django.conf import settings


TERMS_ENABLED = getattr(settings, 'TERMS_ENABLED', True)

TERMS_DEBUG = getattr(settings, 'TERMS_DEBUG', settings.DEBUG)

TERMS_IGNORED_APPS = getattr(settings, 'TERMS_IGNORED_APPS',
Expand Down
6 changes: 1 addition & 5 deletions terms/templatetags/terms.py
Expand Up @@ -2,10 +2,6 @@

from django.template import Library
from django.template.defaultfilters import stringfilter
try:
from django.utils.encoding import smart_text
except ImportError: # For Django < 1.4.2
from django.utils.encoding import smart_unicode as smart_text
from ..html import replace_in_html

register = Library()
Expand All @@ -14,4 +10,4 @@
@register.filter
@stringfilter
def replace_terms(html):
return smart_text(replace_in_html(html))
return replace_in_html(html)

0 comments on commit e4ed0c8

Please sign in to comment.