Commits

Sergey Maranchuk committed 3d2dabc

added nested param, fixed bug with nested quotes

  • Participants
  • Parent commits 38da88d

Comments (0)

Files changed (1)

File bbmarkup/__init__.py

 BBcode rule format:
     'pattern' and 'repl'' - params for re.sub(); 'repl' can be function
     'sortkey' - used to sort rules from highest to lowest; default value: 0
+    'nested' - show how many time tag can be nested to itself; only for [quote] now
 """
 
 BBCODE_RULES = [
         {'pattern': r'\[i\](.*?)\[/i\]', 'repl': r'<em>\1</em>'},
         {'pattern': r'\[u\](.*?)\[/u\]', 'repl': r'<u>\1</u>'},
         {'pattern': r'\[s\](.*?)\[/s\]', 'repl': r'<strike>\1</strike>'},
-        {'pattern': r'\[quote\](.*?)\[/quote\]', 'repl': r'<blockquote>\1</blockquote>'},
-        {'pattern': r'\[quote=(.*?)\](.*?)\[/quote\]', 'repl': r'<blockquote><em>\1</em> <br /> \2</blockquote>'},
+        {'pattern': r'\[quote\](.*?)\[/quote\]', 'repl': r'<blockquote>\1</blockquote>', 'nested': 5},
+        {'pattern': r'\[quote=(.*?)\](.*?)\[/quote\]', 'repl': r'<blockquote><em>\1</em> <br /> \2</blockquote>', 'nested': 5},
         {'pattern': r'\[center\](.*?)\[/center\]', 'repl': r'<div style="text-align: center;">\1</div>'},
         {'pattern': r'\[big\](.*?)\[/big\]', 'repl': r'<big>\1</big>'},
         {'pattern': r'\[small\](.*?)\[/small\]', 'repl': r'<small>\1</small>'},
 BBCODE_RULES_COMPILED = []
 for bbset in (getattr(settings, 'BBMARKUP_CUSTOM_RULES', []) or BBCODE_RULES):
     bbset['pattern'] = re.compile(bbset['pattern'], re.DOTALL)
+    bbset.setdefault('sortkey', 0)
+    bbset.setdefault('nested', 0)
     BBCODE_RULES_COMPILED.append(bbset)
 
 def bbcode(value, code_parser=code_parser):
     u'<pre><code>print 123\\nprint &#39;&lt;br/&gt;&#39;</code></pre>'
     >>> bbcode('[quote=test user]Test quote text[/quote]')
     u'<blockquote><em>test user</em> <br /> Test quote text</blockquote>'
+    >>> bbcode("[quote]Lorem [quote=sl]imsum[/quote] blabla [/quote]")
+    u'<blockquote>Lorem <blockquote><em>sl</em> <br /> imsum</blockquote> blabla </blockquote>'
     >>> bbcode('[color=red]Lorem[/color]')
     u'<span style="color:red">Lorem</span>'
     >>> bbcode('[color=#FAaF12]Lorem[/color]')
     value = escape(value)
     value = linebreaksbr(value)
     for bbset in BBCODE_RULES_COMPILED:
-        value = bbset['pattern'].sub(bbset['repl'], value)
+        for _ in xrange(bbset['nested'] + 1):
+            value = bbset['pattern'].sub(bbset['repl'], value)
 
     return mark_safe(value)