def test_linker(self): def _contextual_link(url, context): return '<a href="%s" target="_blank">%s</a>' % ( url, context["substitution"]) def _link(url): return _contextual_link(url, {"substitution": url}) # Test noncontextual linker p = bbcode.Parser(linker=_link) s = p.format('hello www.apple.com world') self.assertEqual( s, 'hello <a href="www.apple.com" target="_blank">www.apple.com</a> world' ) # Test contextual linker p = bbcode.Parser(linker=_contextual_link, linker_takes_context=True) s = p.format('hello www.apple.com world', substitution="oh hai") self.assertEqual( s, 'hello <a href="www.apple.com" target="_blank">oh hai</a> world') # Test default context in linker p = bbcode.Parser(linker=_contextual_link, linker_takes_context=True, default_context={'substitution': 'arf'}) s = p.format('hello www.apple.com world') self.assertEqual( s, 'hello <a href="www.apple.com" target="_blank">arf</a> world')
def test_max_depth(self): limit_one_parser = bbcode.Parser(max_tag_depth=1) limit_two_parser = bbcode.Parser(max_tag_depth=2) unlimited_parser = bbcode.Parser() src = '[quote][quote][quote]foo[/quote][/quote][/quote]' limit_one_expected = '<blockquote>[quote][quote]foo[/quote][/quote]</blockquote>' limit_two_expected = '<blockquote><blockquote>[quote]foo[/quote]</blockquote></blockquote>' unlimited_expected = '<blockquote><blockquote><blockquote>foo</blockquote></blockquote></blockquote>' self.assertEqual(limit_one_parser.format(src), limit_one_expected) self.assertEqual(limit_two_parser.format(src), limit_two_expected) self.assertEqual(unlimited_parser.format(src), unlimited_expected)
def setUp(self): # noqa extensions = [ C2CWikiLinkExtension(), ] self.markdown_parser = markdown.Markdown(output_format='xhtml5', extensions=extensions) self.bbcode_parser = bbcode.Parser(escape_html=False, newline='\n')
def build_bbcode_parser(self, spoiler=False): parser = bbcode.Parser(newline='\n', install_defaults=False, escape_html=False, replace_links=False, replace_cosmetic=False, drop_unrecognized=True) def true_formatter(tag, value, options, parent, context): if tag == 'url': return f'**{value}**' elif tag == 'spoiler': if spoiler: return f'*{value}*' return '~~spoiler~~' elif tag == 'raw': return f'`{value}`' elif tag in ['quote', 'code']: return f'```{value}```' return '' for tag in ['url', 'spoiler', 'quote', 'raw', 'code']: parser.add_formatter(tag, true_formatter, escape_html=False, replace_links=False, replace_cosmetic=False) return parser
def __init__(self): self.item_regex = re.compile( r"\[(item|itemname|itemicon)( nolink)?\](\d+)\[\/(item|itemname|itemicon)\]", re.IGNORECASE) self.guide_id_regex = re.compile(r"pid=(\d+)", re.IGNORECASE) # initialize bbcode parser self.parser = bbcode.Parser(install_defaults=False, newline="\n", replace_links=False, replace_cosmetic=False, drop_unrecognized=True) self.parser.add_simple_formatter("i", "<i>%(value)s</i>") self.parser.add_simple_formatter("b", "<highlight>%(value)s<end>") self.parser.add_simple_formatter("ts_ts", " + ", standalone=True) self.parser.add_simple_formatter("ts_ts2", " = ", standalone=True) self.parser.add_simple_formatter("ct", " | ", standalone=True) self.parser.add_simple_formatter("cttd", " | ", standalone=True) self.parser.add_simple_formatter("cttr", "\n | ", standalone=True) self.parser.add_simple_formatter("br", "\n", standalone=True) self.parser.add_formatter("img", self.bbcode_render_image) self.parser.add_formatter("url", self.bbcode_render_url) self.parser.add_formatter("item", self.bbcode_render_item) self.parser.add_formatter("itemname", self.bbcode_render_item) self.parser.add_formatter("itemicon", self.bbcode_render_item) self.parser.add_formatter("waypoint", self.bbcode_render_waypoint)
def clean_description(description): # Format spoilers description = re.sub('\[/?spoiler]', '||', description, flags=re.IGNORECASE) # Remove image links description = re.sub('\[url=.*?\](NSFW\s)?Example\s?\d*\[\/url\]', '', description, flags=re.IGNORECASE) # Strip bbcode description = bbcode.Parser().strip(description) # Remove sources description = re.sub('\[.*?from.*]', '', description, flags=re.IGNORECASE) # Strip trailing newlines/spaces description = description.rstrip() # Trim text to 1000 characters description = description[:1000] + (description[1000:] and '...') # Restore closing spoiler tag if trimmed description += '||' if description.count('||') % 2 else '' # Remove extraneous newlines description = re.sub(r'\n\s*\n', '\n\n', description) return description
def test_default_context(self): def _render_context(tag_name, value, options, parent, context): return context["hello"] parser = bbcode.Parser(default_context={"hello": "world"}) parser.add_formatter("c", _render_context) self.assertEqual(parser.format("[c]test[/c]"), "world")
def convertBBCodeToHTML(text): bb = bbcode.Parser() for tag in ('strike', 'table', 'tr', 'th', 'td', 'h1', 'h2', 'h3'): bb.add_simple_formatter(tag, '<{0}>%(value)s</{0}>'.format(tag)) #bb.add_simple_formatter('img', '<img style="display: inline-block; max-width: 100%%;" src="%(value)s"></img>', strip=True, replace_links=False) bb.add_formatter('img', render_img, strip=True, replace_links=False) bb.add_formatter('previewyoutube', render_yt, strip=True, replace_links=True) # The extra settings here are roughly based on the default formatters seen in the bbcode module source bb.add_simple_formatter('noparse', '%(value)s', render_embedded=False, replace_cosmetic=False) # see 'code' bb.add_simple_formatter('olist', '<ol>%(value)s</ol>', transform_newlines=False, strip=True, swallow_trailing_newline=True) # see 'list' bb.add_simple_formatter( 'spoiler', '<span style="color: #000000;background-color: #000000;padding: 0px 8px;">%(value)s</span>' ) # see 's' & above css return bb.format(text)
def end_element(tag): if tag == "text": if buffers["text"]: widget = widgets[-1] if widgets else None if isinstance(widget, TextViewer): text = textwrap.dedent("".join(buffers["text"])).strip() # default bbcode parser change \n with <br> we force it to \n to avoid changes # 'raplace_cosmetic' change symbols into ascii code for html and we don't want that too bbcode.g_parser = bbcode.Parser(newline='\n', replace_cosmetic=False) code = bbcode.render_html(text) html = mistune.Markdown(QTextEditRenderer())(code) widget.setHtml(html) buffers["text"] = [] elif tag == "label": if buffers["label"]: widget = widgets[-1] if widgets else None if isinstance(widget, QLabel): text = "".join(buffers["label"]).strip() text = mistune.markdown(text) widget.setText(text.strip()) buffers["label"] = [] if tag == "report": pass elif tag in ("col", "row"): layouts.pop() else: if tag == "section": layouts.pop() widgets.pop() # print("%s</%s>" % (" "*len(tags), tag)) popped_name = tags.pop() assert popped_name == tag, (popped_name, tag)
def wiki2html(data = ''): '''html_escape_table = { '&': '&', '"': '"', "'": ''', '>', '>', '<', '<', }''' #data = data.replace('[', '<') #data = data.replace(']', '>') #p = re.compile('\[{1}(/?\w*{1})[\]]') #result = re.sub('\[(/?\w*[^\])]+)', '<\1\0\2>', data.rstrip().lstrip()) parser = bbcode.Parser() parser.add_simple_formatter('h', '<h2 name="%(value)s">%(value)s</h2>') parser.add_simple_formatter('h2', '<h3>%(value)s</h3>') #parser.add_simple_formatter('img', '<img src="%(value)s">') result = parser.format(data) #result = result.replace('{{', '[') #result = result.replace('}}', ']') #result = bbcode.render_html(data) '''result = result.replace('>', '>') result = result.replace('<', '<') result = result.replace('[h2]', '[h3]'); result = result.replace('[/h2]', '[/h3]'); result = result.replace('[h]', '[h2]'); result = result.replace('[/h]', '[/h2]'); result = result.replace('[', '<') result = result.replace(']', '>') result = result.replace('{{', '[') result = result.replace('}}', ']')''' #result = re.sub('\[{1}(/?\w*)[\]]', '\1\0\2', data)#p.sub('<\1>', data) return result
def test_default_context(self): parser = bbcode.Parser(default_context={'hello': 'world'}) def _render_context(tag_name, value, options, parent, context): return context['hello'] parser.add_formatter('c', _render_context) self.assertEqual(parser.format('[c]test[/c]'), 'world')
def _get_bbcode_parser(): global _bbcode_parser if not _bbcode_parser: # prevent that BBCode parser escapes again (the Markdown parser does # this already) bbcode.Parser.REPLACE_ESCAPE = () _bbcode_parser = bbcode.Parser( escape_html=False, newline='\n', replace_links=False) return _bbcode_parser
def bodybbcode(self): parser = bbcode.Parser() """ Seems like the following line could be done in a module where I define more overrides? Sloppy but I'm a python n00bie """ parser.add_simple_formatter('img', '<img class="img-responsive" src=''%(value)s'' />', replace_links=False) parser.add_formatter('youtube', render_youtube, replace_links=False) parser.add_formatter('bandcamp', render_bandcamp, replace_links=False) parser.add_formatter('vimeo', render_vimeo, replace_links=False) parser.add_formatter('soundcloud', render_soundcloud, replace_links=False) return parser.format(self.body)
def description_html(self): parser = bbcode.Parser() def render_size(name, value, options, parent, context): return f'<span style="font-size: 4;">{value}</span>' parser.add_formatter('size', render_size) cleaned = self.description.replace('<br />', '') return html.unescape(parser.format(cleaned))
def oprawWpis(tresc): def prosty(znacznik, wnetrze, argumenty, rodzic, otoczenie): if wnetrze: return '<{znacznik}>{wnetrze}</{znacznik}>'.format(**locals()) else: return '' def barwa(znacznik, wnetrze, argumenty, rodzic, otoczenie): if znacznik in argumenty and wnetrze: return '<span style="color:{argumenty[znacznik]};">{wnetrze}</span>'.format( **locals()) else: return wnetrze def lacze(znacznik, wnetrze, argumenty, rodzic, otoczenie): if znacznik in argumenty: adres = argumenty[znacznik] wnetrze = wnetrze or adres return '<a href="{adres}">{wnetrze}</a>'.format(**locals()) else: return wnetrze def obraz(znacznik, wnetrze, argumenty, rodzic, otoczenie): if znacznik in argumenty and wnetrze: adres = argumenty[znacznik] return '<img src="{adres}" alt="{wnetrze}" />'.format(**locals()) else: return wnetrze def cytat(znacznik, wnetrze, argumenty, rodzic, otoczenie): if znacznik in argumenty: autor = argumenty[znacznik] wynik = '<p>{autor} napisaĆ(a):</p><blockquote>{wnetrze}</blockquote>' elif wnetrze: wynik = '<blockquote>{wnetrze}</blockquote>' else: wynik = '' return wynik.format(**locals()) def kod(znacznik, wnetrze, argumenty, rodzic, otoczenie): if wnetrze: return '<pre>{wnetrze}</pre>'.format(**locals()) else: return '' a = bbcode.Parser(install_defaults=False, replace_cosmetic=False) a.add_formatter('b', prosty) a.add_formatter('i', prosty) a.add_formatter('u', prosty) a.add_formatter('s', prosty) a.add_formatter('color', barwa) a.add_formatter('url', lacze) a.add_formatter('img', obraz) a.add_formatter('quote', cytat) a.add_formatter('code', kod) return (a.format(tresc))
def removebbcode(incomingString): try: import bbcode parser = bbcode.Parser() code = incomingString plain_txt = parser.strip(code) return plain_txt except: return incomingString
def __init__(self, username, accountname, apikey): self.username = username self.accountName = accountname self.apiKey = apikey self.bbParser = bbcode.Parser() Markdown.output_formats['plain'] = markdown_unmark_element self.mdParser = Markdown(output_format='plain') self.mdParser.stripTopLevelTags = False if self.username == 'DISABLED': self.username = ''
def save(self, force_insert=False, force_update=False, using=None, update_fields=None): parser = bbcode.Parser() parser.add_simple_formatter('code', '<pre>%(value)s</pre>') self.bbcode_content = self.bbcode_content.strip() self.content = parser.format(self.bbcode_content) super(Comment, self).save(force_insert, force_update, using, update_fields)
def get_parser(): parser = bbcode.Parser(linker=my_linker, linker_takes_context=True) parser.add_formatter('post', render_post) parser.add_formatter('thread', render_thread) parser.add_formatter('dialog', render_dialog) parser.add_formatter('arhibash', render_arhibash) parser.add_formatter('img', render_img, replace_links=False) # parser.add_formatter('color', render_color) return parser
def test_strip(self): result = self.parser.strip('[b]hello \n[i]world[/i][/b] -- []', strip_newlines=True) self.assertEqual(result, 'hello world -- []') html_parser = bbcode.Parser(tag_opener='<', tag_closer='>', drop_unrecognized=True) result = html_parser.strip( '<div class="test"><b>hello</b> <i>world</i><img src="test.jpg" /></div>' ) self.assertEqual(result, 'hello world')
def test_options_case(self): def _render_author(tag_name, value, options, parent, context): self.assertIn("dan", options) self.assertEqual(options["wAtSoN"], "1") return " ".join(key for key, value in options.items()) parser = bbcode.Parser() parser.add_formatter("author", _render_author) self.assertEqual( parser.format("[author Dan Watson=1]whatever[/author]"), "Dan Watson")
def __init__(self, pod, config): super(ShortcodesExtension, self).__init__(pod, config) self.parser = bbcode.Parser( newline='\n', install_defaults=False, escape_html=False, replace_cosmetic=False, replace_links=False) self.shortcodes = [] self.load_shortcodes()
def get_standard_bbc_parser(embed_images=True, escape_html=True): parser = bbcode.Parser(escape_html=escape_html, replace_links=False) if embed_images: parser.add_simple_formatter( 'img', '<a class="img-embed" href="%(value)s"><img src="%(value)s"></a>') else: parser.add_simple_formatter( 'img', '<a class="img-link" href="%(value)s">embedded image</a>') return parser
def __init__(self, config, masterconfig): self.username = config['WAUsername'] self.accountName = config['WAAccountName'] self.apiKey = config['WAAPIKey'] self.retailTOC = masterconfig['RetailTOC'] self.classicTOC = masterconfig['ClassicTOC'] self.bbParser = bbcode.Parser() Markdown.output_formats['plain'] = markdown_unmark_element self.mdParser = Markdown(output_format='plain') self.mdParser.stripTopLevelTags = False if self.username == 'DISABLED': self.username = ''
def __init__(self): def _bb_dummy(tagname, value, options, parent, context): return self.bbcode_parser = bbcode.Parser() self.bbcode_parser.add_formatter('font', _bb_dummy, standalone=True) self.bbcode_parser.add_formatter('bell', _bb_dummy, standalone=True) self.bbcode_parser.add_formatter('color', _bb_dummy, standalone=True) self.bbcode_parser.add_formatter('graphic', _bb_dummy, standalone=True) self.bbcode_parser.add_formatter('char', _bb_dummy, standalone=True) self.bbcode_parser.add_formatter('column', _bb_dummy, standalone=True) self.bbcode_parser.add_formatter('date', _bb_dummy, standalone=True) self.bbcode_parser.add_formatter('time', _bb_dummy, standalone=True)
def comments_bbcode_to_html_convert(): def render_table_tags(tag_name, value, options, parent, context): return f'<{tag_name}>{value}</{tag_name}>' parser = bbcode.Parser() parser.newline = '' parser.add_formatter('table', render_table_tags) parser.add_formatter('tr', render_table_tags) parser.add_formatter('td', render_table_tags) parser.drop_unrecognized = False comments = Comment.objects.all() for comment in comments: comment.text = parser.format(comment.text) Comment.objects.bulk_update(comments, ['text'])
def bbcode_parser_formatter(data: str) -> str: parser = bbcode.Parser() # parser.add_simple_formatter('u', '<strong>%(value)s</strong>') parser.add_simple_formatter('u', '<em>%(value)s</em>') parser.add_simple_formatter('color', '%(value)s') parser.REPLACE_ESCAPE = ( ("&", "&"), ("<", "<"), (">", ">"), ('"', """), ("'", "'"), (" ", " "), ) return parser.format(data)
def show_post(page_name): p = Post.query.filter_by(link_address=page_name).first() if p is None: abort(404) if p.is_draft and current_user.is_authenticated is False: abort(403) parser = bbcode.Parser() parser.add_simple_formatter( 'video', '<iframe width="420" height="315" src="https://www.youtube.com/embed/%(value)s"></iframe>', standalone=False) #p.content = bbcode.render_html(p.content) p.content = parser.format(p.content) return render_template('posts/view_post.html', p=p)
def get_closure_bbc_parser(): c_parser = bbcode.Parser( newline='\n', install_defaults=False, escape_html=False, replace_links=False, replace_cosmetic=False) def depyramiding_quote_render(tag_name, value, options, parent, context): if tag_name == 'quote': return '' return value c_parser.add_formatter('quote', depyramiding_quote_render) c_parser.add_simple_formatter( 'byusingthistagIaffirmlannyissupercool', '%(value)s') return c_parser
def rich_fmt(text): global parser if parser is None: parser = bbcode.Parser() for cmd in codes.keys(): parser.add_formatter(cmd, '') fmt = '' tokens = parser.tokenize(text) keep = False for token in tokens: token_type, tag_name, tag_options, token_text = token # print('--', token_type, tag_name, tag_options, token_text) if token_type == parser.TOKEN_TAG_START: if tag_name in codes.keys(): fmt += codes[tag_name] elif tag_name == 'color' and 'color' in tag_options: if tag_options['color'] in codes.keys(): fmt += codes[tag_options['color']] elif token_type == parser.TOKEN_TAG_END: if tag_name == 'blink': fmt += clear_codes['blink_off'] elif tag_name in ['b', 'bold']: fmt += clear_codes['bold_off'] elif tag_name in ['i', 'italicized']: fmt += clear_codes['italicized_off'] elif tag_name in ['u', 'underline']: fmt += clear_codes['underline_off'] elif tag_name in ['inv', 'inverse']: fmt += clear_codes['inverse_off'] else: fmt += clear_codes['reset'] elif token_type == parser.TOKEN_NEWLINE: fmt += '\n' else: fmt += token_text return fmt