add [lb], [rb], and [@] tags

This commit is contained in:
2025-12-04 08:31:49 +03:00
parent 88f80c38cc
commit b812e01473
3 changed files with 58 additions and 0 deletions

View File

@@ -141,6 +141,12 @@ TAGS = {
"spoiler": tag_spoiler, "spoiler": tag_spoiler,
} }
VOID_TAGS = {
'lb': lambda attr: '[',
'rb': lambda attr: ']',
'@': lambda attr: '@',
}
# [img] is considered block for the purposes of collapsing whitespace, # [img] is considered block for the purposes of collapsing whitespace,
# despite being potentially inline (since the resulting <img> tag is inline, but creates a block container around itself and sibling images). # despite being potentially inline (since the resulting <img> tag is inline, but creates a block container around itself and sibling images).
# [code] has a special case in is_inline(). # [code] has a special case in is_inline().
@@ -260,6 +266,7 @@ def babycode_to_html(s, banned_tags=[]):
subj = sanitize(s) subj = sanitize(s)
parser = Parser(subj) parser = Parser(subj)
parser.valid_bbcode_tags = allowed_tags parser.valid_bbcode_tags = allowed_tags
parser.void_bbcode_tags = set(VOID_TAGS)
parser.bbcode_tags_only_text_children = TEXT_ONLY parser.bbcode_tags_only_text_children = TEXT_ONLY
parser.mentions_allowed = '@mention' not in banned_tags parser.mentions_allowed = '@mention' not in banned_tags
parser.valid_emotes = EMOJI.keys() parser.valid_emotes = EMOJI.keys()
@@ -296,6 +303,8 @@ def babycode_to_html(s, banned_tags=[]):
c = c + Markup(fold(child, _nobr, _surrounding)) c = c + Markup(fold(child, _nobr, _surrounding))
res = TAGS[element['name']](c, element['attr'], surrounding) res = TAGS[element['name']](c, element['attr'], surrounding)
return res return res
case "bbcode_void":
return VOID_TAGS[element['name']](element['attr'])
case "link": case "link":
return f"<a href=\"{element['url']}\">{element['url']}</a>" return f"<a href=\"{element['url']}\">{element['url']}</a>"
case 'emote': case 'emote':

View File

@@ -11,6 +11,7 @@ PAT_MENTION = r'[a-zA-Z0-9_-]'
class Parser: class Parser:
def __init__(self, src_str): def __init__(self, src_str):
self.valid_bbcode_tags = {} self.valid_bbcode_tags = {}
self.void_bbcode_tags = {}
self.valid_emotes = [] self.valid_emotes = []
self.bbcode_tags_only_text_children = [] self.bbcode_tags_only_text_children = []
self.mentions_allowed = True self.mentions_allowed = True
@@ -228,11 +229,46 @@ class Parser:
} }
def parse_bbcode_void(self):
self.save_position()
if not self.check_char("["):
self.restore_position()
return None
name = self.match_pattern(PAT_BBCODE_TAG)
if name == "":
self.restore_position()
return None
attr = None
if self.check_char("="):
attr = self.match_pattern(PAT_BBCODE_ATTR)
if not self.check_char("]"):
self.restore_position()
return None
if not name in self.void_bbcode_tags:
self.restore_position()
return None
self.forget_position()
return {
'type': 'bbcode_void',
'name': name,
'attr': attr,
}
def parse_element(self, siblings): def parse_element(self, siblings):
if self.is_end_of_source(): if self.is_end_of_source():
return None return None
element = self.parse_emote() \ element = self.parse_emote() \
or self.parse_bbcode_void() \
or self.parse_bbcode() \ or self.parse_bbcode() \
or self.parse_rule() \ or self.parse_rule() \
or self.parse_link() \ or self.parse_link() \

View File

@@ -173,6 +173,19 @@
<a class="mention display me" href="#mentions" title="@your-username">Your display name</a> <a class="mention display me" href="#mentions" title="@your-username">Your display name</a>
<p>Mentioning a user does not notify them. It is simply a way to link to their profile in your posts.</p> <p>Mentioning a user does not notify them. It is simply a way to link to their profile in your posts.</p>
</section> </section>
<section class="guide-section">
<h2 id="void-tags">Void tags</h2>
<p>The special void tags <code class="inline-code">[lb]</code>, <code class="inline-code">[rb]</code>, and <code class="inline-code">[@]</code> will appear as the literal characters <code class="inline-code">[</code>, <code class="inline-code">]</code>, and <code class="inline-code">@</code> respectively. Unlike other tags, they are self-contained and have no closing equivalent.</p>
<ul class="guide-list">
{% set lbrb = "[color=red]This text will be red[/color]\n\n[lb]color=red[rb]This text won't be red[lb]/color[rb]" %}
<li><code class="inline-code">[lb]</code> and <code class="inline-code">[rb]</code> allow you to use square brackets without them being interpreted as Babycode:
{{ ("[code]" + lbrb + "[/code]") | babycode | safe }}
Will result in:<br>
{{ lbrb | babycode | safe }}
</li>
<li>The <code class="inline-code">[@]</code> tag allows you to use the @ symbol without it being turned into a mention.</li>
</ul>
</section>
{% endset %} {% endset %}
{{ sections | safe }} {{ sections | safe }}
</div> </div>