def get(self, artist): data = {'success': False, 'infos': None} params = { 'type': 'artist', 'q': artist, 'page': 1, 'per_page': 1, 'key': settings.DISCOGS_KEY, 'secret': settings.DISCOGS_SECRET } headers = {'User-Agent': 'DjangoZik - opensource web player - dev'} infos = requests.get(self.url, params=params, headers=headers, verify=False) if infos.status_code == 200: try: data['success'] = True json_infos = json.loads(infos.text) params = { 'key': settings.DISCOGS_KEY, 'secret': settings.DISCOGS_SECRET } details = requests.get(self.url_info % json_infos['results'][0]['id'], params=params, headers=headers) json_details = json.loads(details.text) data['infos'] = { 'image': json_infos['results'][0]['thumb'], 'text': bbcode.render_html(json_details['profile']) } except: # return data the next line pass return data
def _sanitize_post(inp, bbcode_uid: str): smls = re.compile(r'<!-- s.*? --><img src=\\?"\{SMILIES_PATH\}/.*?\\?" ' 'alt=\\?"(.*?)\\?" title=\\?".*?" /><!-- s.*? -->') inp = re.sub(smls, r"\1", inp) inp = html.unescape(inp) inp = inp.replace(":" + bbcode_uid, "") img_tags = re.compile(r"\[/?img\]") inp = re.sub(img_tags, "", inp) youtube_embeds = re.compile( r'\[html\]<iframe width="\d+" height="\d+" ' 'src="//www.youtube.com/embed/([^"]+)" frameborder=' r'"0" allowfullscreen></iframe>\[/html\]') inp = re.sub(youtube_embeds, r"https://www.youtube.com/watch?v=\1", inp) inp = bbcode.render_html(inp, drop_unrecognized=True, escape_html=False, replace_links=False) inp = inp.replace('rel="nofollow"', "") inp = HTMLSlacker(inp).get_output() return inp
def topic_view(request): posts_per_page = 20 # TODO Posts per page should probably be configurable page = int(request.matchdict['page']) start = (page - 1) * posts_per_page end = posts_per_page * page try: topic = Topic.objects.get(id=request.matchdict['topic_id']) posts = topic.posts[start:end] except DoesNotExist: request.session.flash("Invalid topic ID") return HTTPFound(location=request.route_url('forum')) last_page = math.ceil(len(topic.posts) / posts_per_page) if 'content' in request.POST and request.has_permission( 'forum_post_reply'): content = BeautifulSoup(request.POST['content'][:5000], 'html.parser').get_text() # Strip all HTML content = bbcode.render_html( content) # Convert remaining BBCode to HTML post = Post(user=request.get_user, content=content) topic.update(push__posts=post, last_post_date=post.post_date) request.session.flash('New reply posted.') return HTTPFound(location=request.route_url( 'forum_topic', page=page, topic_id=topic.id)) return { 'posts': posts, 'topic': topic, 'page': page, 'category': topic.category, 'last_page': last_page, 'start_post': start + 1, 'end_post': min(end, len(topic.posts)), 'total_posts': len(topic.posts) }
def post(): date = int(time.time()) nick = _un(form.getvalue("nick")) # Stip away all HTML one might have inserted into the message. Should # prevent one from doing nasty things. message = re.sub('<[^<]+?>', '', _un(form.getvalue("message"))) message = bbcode.render_html(message) # Render the BBcode to html # Get the highest ID currently owned by one of the messages in our database # and increase it by one, this will be the ID of our new message. _id = c.execute("SELECT MAX(id) FROM comments;").fetchone()[0] + 1 c.execute(insert, (_id, nick, date, message)) # Flush the changes to disk connection.commit() print "Content-type: text/html; charset=utf-8" print print """ <!DOCTYPE html> <html lang="fi"> <head> <META HTTP-EQUIV="refresh" content="0; URL=%(url)s/comments.html"> </head> """ % {'url': baseurl}
def post(update, context): if update.channel_post: if update.channel_post.chat.username == "rememberbox": text = update.channel_post.text entities = update.channel_post.parse_entities() first, *others = text.splitlines() length = len(first) + 2 if first.startswith('👉'): path = first[2:] first = path.split('/')[-1] # remove trigger emoji filename = path.lower().replace(' ', '-') + ".md" rest = parse_bbcode("\n".join(others), length, entities, urled=True) html = "<p>" + unescape(render_html(rest)) + "</p>" context.bot.send_message(chat_id=691609650, text=html) rest = Tomd(html).markdown[length + 1:].replace('<br />', '\n') content = "# " + first + "\n\n" + rest try: commit = repo.create_file( filename, "automated post from telegram channel", content, branch="master") sha = getattr(commit['commit'], 'sha') url = "https://github.com/Lulzx/til/commit/" + sha context.bot.send_message( chat_id=691609650, text="new addition in TIL repository: {}".format(url)) except: pass
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 recursively_add_comment_to_object(db, obj, comment, parent_id=None): if parent_id is None: print 'should be adding comment %d' % (comment.id) else: print 'should be adding comment %d with parent %d' % (comment.id, parent_id) by_author = True if obj.user_id == comment.user_id else False content = bbcode.render_html(comment.comment.decode('utf-8')) tc = ThreadedComment.objects.create(id=comment.id, user_id=comment.user_id, user_name=comment.user_name, submit_date=comment.submit_date, ip_address=comment.ip_address, by_author=by_author, replied_to_id=parent_id, content_object=obj, user_email=comment.user_email, user_url=comment.user_url, comment=content, site_id=1) obj.comments.add(tc) child_comments = get_children_for_comment(db, comment.id) for c in child_comments: recursively_add_comment_to_object(db, obj, c, comment.id)
def run(): error_count = 0 created_count = 0 db = sqlsoup.SQLSoup('mysql://root@localhost/rhizome') events = db.announce_event.all() tz = timezone.get_current_timezone() print "Found %s events" % (len(events), ) # with transaction.atomic(): for event in events: if event.is_spam == 1 or event.status == 0: print "Skipping event: %s" % event.title continue # print 'decoding description' description = bbcode.render_html(event.description.decode('utf-8')) def tz_aware(dt): return dt # return tz.localize(dt, is_dst=False) try: if event.start_date is None: event.start_date = event.end_date location = location_from_obj(event) e = Event.objects.create(title=event.title, start=tz_aware(event.start_date), end=tz_aware(event.end_date), content=description, user_id=event.user_id, location=location, url=event.url, legacy_id=event.id) e.created = event.created e.save() comments = get_top_level_comments(db, event.id, 84) for comment in comments: recursively_add_comment_to_object(db, e, comment) created_count += 1 except Exception, e: print e error_count += 1 if created_count % 50 == 0: print "Created %s events. Received %s errors." % (created_count, error_count, )
def text_to_html(text, markup): if markup == "bbcode": text = bbcode.render_html(text) elif markup == "markdown": text = markdown.markdown(text, safe_mode="escape") elif markup == "textile": text = textile.textile(text, safe_mode="escape") else: raise Exception("Invalid markup property: %s" % markup) return urlize(text)
def post(self, request, *args, **kwargs): form = self.form_class(request.POST) if form.is_valid(): mybbcodes = request.POST['bbcodes'] html = bbcode.render_html(mybbcodes) h = html2text.HTML2Text() h.body_width = 0 mymdcodes = h.handle(html) return render(request, self.template_name, {'converter_form': form, 'mdcodestring': mymdcodes })
def render_onmouseover(lastposts, boardid): result = "" i = len(lastposts) - 1 c = 0 lastPostUser = "" while i >= 0: if not lastposts[i]["content"]: i -= 1 continue # 跳过被删除的回复 else: if lastPostUser == "": lastPostUser = lastposts[i][ "userName"] if boardid != 182 else "匿名" content = noquote(lastposts[i]["content"]) if i == len(lastposts) - 1 and len(content) == 0: try: target = "在".join(lastposts[i]["content"].split( "以下是引用")[1].split("的发言:")[0].split("在")[:-1]).replace( "楼:用户", "L 用户 ") + " 的发言" if "L" not in target: content = "[i]引用[/i]" else: targetlc = int(target.split("L")[0].strip()) if targetlc < from_ + 1: content = lastposts[i]["content"] else: content = "[i]引用[/i]" except: #traceback.print_exc() i -= 1 continue if len(content): for left, right in [("[align=right]", "[/align]"), ("[right]", "[/right]")]: if left in content and content.strip().endswith(right): content = left.join(content.split(left)[0:-1]) content = content.replace("[/size]", "").replace("[/font]", "").strip() result += "<b>[" + str( lastposts[i] ["floor"]) + "L " + lastposts[i]["userName"] + "]</b> " if lastposts[i]["contentType"] == 0: result += bbcode.render_html(content) else: mdhtml = markdown.markdown(content, extensions=[ 'del_ins', 'markdown.extensions.extra', 'urlize' ]).strip() result += mdhtml.replace("<p>", "").replace("</p>", "").strip() result += "<br>" c += 1 i -= 1 return result, lastPostUser
def process_body(body, encoding): "Do some massaging of the raw body, depending on encoding" if encoding.lower() == "html": return body.decode("utf-8"), "text/html; charset=utf-8" if encoding.lower() == "plain": return body.decode("utf-8"), "text/plain; charset=utf-8" if encoding.lower() == "elcode": if bbcode: return (bbcode.render_html(body.decode("utf-8")), "text/html; charset=utf-8") else: return body.decode("utf-8"), "text/plain; charset=utf-8"
def new_topic(request): # TODO configurable max chars for title and content category = Category.objects.get(id=request.matchdict['category_id']) if 'name' in request.POST and 'content' in request.POST: content = BeautifulSoup(request.POST['content'][:5000], 'html.parser').get_text() # Strip all HTML content = bbcode.render_html(content) # Convert remaining BBCode to HTML topic = Topic(user=request.get_user, name=request.POST['name'][:100], category=category) post = Post(user=request.get_user, content=content) topic.last_post_date = post.post_date topic.posts.append(post) topic.save() return HTTPFound(location=request.route_url('forum_topic', topic_id=topic.id, page=1)) return {'category': category}
def poi_new(request, device_id): message = "" if not request.user.is_authenticated(): return redirect('user:login') device = get_object_or_404(Device, pk=device_id) if not device.owners.filter(id=request.user.id).exists(): return Http404 if request.method == 'POST': vPoi = Poi(); form = POIform(request.POST, request.FILES) if form.is_valid(): vPoi.uuid = form.cleaned_data['uuid'] vPoi.name = form.cleaned_data['name'] vPoi.parent_device = device vPoi.save(); vPoi.owners.add(request.user) vPoi.save() picture = form.cleaned_data['image'] w, h = get_image_dimensions(picture) if h > 600: message = "Wysokość załączonego pliku to " + str( h) + " pikseli. Jego wysokość nie może być większa niż 600 pikseli" else: path = "files/" + str(vPoi.parent_device.id) + "/" + vPoi.uuid checkpath(path) handlefile(picture, path) bbtext = form.cleaned_data['bb_text'] vPoi.content = bbtext vPoi.save() rendered = bbcode.render_html(bbtext) renderfile = open(path + "/index.html", "w") renderfile.write( '<html><head><meta charset="ISO-8859-2"></head>' '<body><img src="file.jpg" style="width: 100%; display: block; max-height: 600px;" /><p>') renderfile.write(rendered) renderfile.write( '</p></body></html>') renderfile.close() createzip(vPoi) return redirect('devices:dashboard') else: message = form.errors else: form = POIform() return render(request, 'devices/poi.new.html', {'form': form, 'message': message})
def migrate(entry): old_id = entry.get('thread-id') if not old_id or not old_id.startswith('mt_'): return print(entry.id, entry.title) comments = phpbb.cursor().execute( """ SELECT CASE p.poster_id WHEN -1 THEN p.post_username ELSE u.username END, u.user_email, pt.post_text, p.post_time, u.user_website, p.poster_ip FROM phpbb_integrate AS i LEFT JOIN phpbb_topics AS t ON i.topic = t.topic_id LEFT JOIN phpbb_posts AS p on p.topic_id = t.topic_id LEFT JOIN phpbb_posts_text AS pt ON pt.post_id = p.post_id LEFT JOIN phpbb_users AS u ON p.poster_id = u.user_id WHERE i.source = ? AND p.poster_id != 13 """, (old_id, )) for row in comments: thread = get_thread(entry, old_id) username, email, post_text, date, website, remote_addr = row if not username: username = '******' post_text = ('<p>' + bbcode.render_html(post_text) + '</p>').replace( '<br /><br />', '</p><p>') remote_addr = socket.inet_ntoa(struct.pack("!I", int(remote_addr, 16))) print( f'{username} ({email}, {website}), {remote_addr} @ {date}:\n{post_text}' ) isso.cursor().execute( """ INSERT INTO comments (tid, author, email, website, created, remote_addr, text, voters, mode) VALUES (?, ?, ?, ?, ?, ?, ?, '', 1) """, (thread, username, email, website, date, remote_addr, post_text))
def run(): error_count = 0 created_count = 0 db = sqlsoup.SQLSoup('mysql://root@localhost/rhizome') opportunities = db.announce_opportunity.all() # tz = timezone.get_current_timezone() print "Found %s events" % (len(opportunities), ) # with transaction.atomic(): for opportunity in opportunities: if opportunity.is_spam == 1 or opportunity.status == 0: print "Skipping opportunity: %s" % opportunity.title continue # print 'decoding description' description = bbcode.render_html(opportunity.description.decode('utf-8')) try: location = location_from_obj(opportunity) o = Opportunity.objects.create(title=opportunity.title, deadline=tz_aware(opportunity.deadline), content=description, user_id=opportunity.user_id, legacy_id=opportunity.id, location=location, url=opportunity.url) o.created = opportunity.created o.save() comments = get_top_level_comments(db, opportunity.id, 85) for comment in comments: recursively_add_comment_to_object(db, o, comment) created_count += 1 except Exception, e: print e error_count += 1 if created_count % 50 == 0: print "Created %s events. Received %s errors." % (created_count, error_count, )
def new_topic(request): # TODO configurable max chars for title and content category = Category.objects.get(id=request.matchdict['category_id']) if 'name' in request.POST and 'content' in request.POST: content = BeautifulSoup(request.POST['content'][:5000], 'html.parser').get_text() # Strip all HTML content = bbcode.render_html( content) # Convert remaining BBCode to HTML topic = Topic(user=request.get_user, name=request.POST['name'][:100], category=category) post = Post(user=request.get_user, content=content) topic.last_post_date = post.post_date topic.posts.append(post) topic.save() return HTTPFound(location=request.route_url( 'forum_topic', topic_id=topic.id, page=1)) return {'category': category}
def run(): error_count = 0 created_count = 0 db = sqlsoup.SQLSoup('mysql://root@localhost/rhizome') discussions = db.execute('select A.id, A.created, B.user_id, B.comment, C.title, B.id as comment_id from `discuss_discussionthread` A left join django_comments B on A.object_pk = B.id left join threadedcomments_comment C on B.id = C.comment_ptr_id where A.content_type_id = 112 and A.is_public = 1 and A.is_removed = 0 and B.is_public = 1 and B.is_removed = 0 and A.id <> 1523;') # top level discussions... for discussion in discussions.fetchall(): print discussion.id # print 'getting children for discussion thread %d (comment_id: %d)' % (discussion.id, discussion.comment_id) try: # d = Discussion.objects.create(legacy_id=discussion.id, # content=discussion.comment.decode('utf-8'), # title=discussion.title, # user_id=discussion.user_id) # # d.created = discussion.created # d.save() content = bbcode.render_html(discussion.comment.decode('utf-8')) # content = linebreaks() # content = bleach.linkify(content) Discussion.objects.update_or_create(legacy_id=discussion.id, defaults={ 'content': content }) # child_comments = get_children_for_comment(db, discussion.comment_id) # print 'found %d children' % len(child_comments) # for comment in child_comments: # try: # recursively_add_comment_to_object(db, d, comment) # except Exception, e: # print e except Exception, e: print e
def render_markup(markup, text): if text is None: return None if markup == BBCODE: if bbcode is not None: return bbcode.render_html(text) elif markup == MARKDOWN: if markdown is not None: return markdown.markdown(text) elif markup == TEXTILE: if textile is not None: return textile.textile(text) elif markup == RST: if publish_parts is not None: return publish_parts(text, writer_name='html')['fragment'] elif markup == PLAIN_HTML: if html_sanitizer is not None: return html_sanitizer.parse(text) return text
def topic_view(request): posts_per_page = 20 # TODO Posts per page should probably be configurable page = int(request.matchdict['page']) start = (page - 1) * posts_per_page end = posts_per_page * page try: topic = Topic.objects.get(id=request.matchdict['topic_id']) posts = topic.posts[start:end] except DoesNotExist: request.session.flash("Invalid topic ID") return HTTPFound(location=request.route_url('forum')) last_page = math.ceil(len(topic.posts) / posts_per_page) if 'content' in request.POST and request.has_permission('forum_post_reply'): content = BeautifulSoup(request.POST['content'][:5000], 'html.parser').get_text() # Strip all HTML content = bbcode.render_html(content) # Convert remaining BBCode to HTML post = Post(user=request.get_user, content=content) topic.update(push__posts=post, last_post_date=post.post_date) request.session.flash('New reply posted.') return HTTPFound(location=request.route_url('forum_topic', page=page, topic_id=topic.id)) return {'posts': posts, 'topic': topic, 'page': page, 'category': topic.category, 'last_page': last_page, 'start_post': start+1, 'end_post': min(end, len(topic.posts)), 'total_posts': len(topic.posts)}
import bbcode html = bbcode.render_html("test\ntest") print(html)
import time import cProfile import pstats import StringIO def word(length, allowed_chars='abcdefghijklmnopqrstuvwxyz'): return ''.join(random.choice(allowed_chars) for i in range(length)) def generate_lines(lines): fmt = '%d\t[%s]\t[%s]\t[%s]\t%s' for i in range(lines): level = word(random.randint(1, 10)) module = word(random.randint(1, 10)) sentence = ' '.join(word(random.randint(2, 7)) for i in range(10)) yield fmt % (i, datetime.datetime.now().isoformat(), level, module, sentence) if __name__ == '__main__': for num in (100, 1000, 10000): doc = '\n'.join(generate_lines(num)) pr = cProfile.Profile() start = time.time() pr.enable() fmt = bbcode.render_html(doc) pr.disable() elapsed = time.time() - start print '%d lines: %.6fs (%.2f kB)' % (num, elapsed, len(doc) / 1024.0) s = StringIO.StringIO() ps = pstats.Stats(pr, stream=s).sort_stats('cumulative') ps.print_stats() print s.getvalue()
def renderHtlm(): #region header = r''' <html> <head> <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <script type="text/javascript"> $(function(){ $('#example_tree').find('SPAN').click(function(e){ $(this).parent().children('UL').toggle(); }); }); </script> <link rel="stylesheet" type="text/css" href="https://static.f-list.net/css/common/default-0.0.10.css?trigger=1446" /> <link rel='stylesheet' type='text/css' href='https://static.f-list.net/css/common/character.css?trigger=1446' /> <link rel="stylesheet" type="text/css" href="https://static.f-list.net/css/dark.css?trigger=1446" /> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script> <script src='https://static.f-list.net/js/f-list.js?trigger=1446' type='text/javascript'></script> <script src='https://static.f-list.net/js/f-list.utils.js?trigger=1446' type='text/javascript'></script> <script type="text/javascript"> $("div.CollapseHeader").toggle(function(){ $(this).addClass("ExpandedHeader"); }, function () { $(this).removeClass("ExpandedHeader"); }); $(document).ready(function() { //Slide up and down on click $("div.CollapseHeader").click(function(){ $(this).toggleClass('inactive'); $(this).parent().next("div.CollapseBlock").slideToggle("slow"); }); }); </script> <link rel="stylesheet" href="https://www.f-list.net/static/images/icons/fa/css/font-awesome.min.css"> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <script src="https://www.google.com/jsapi?key=ABQIAAAA4tvKLKnKwB5W1nL_b_wJXRQMCsOcopUbve-Ru1_bsEbiyzNqWRTAzatggHo_c6fm8l70pM-fvoK3TQ"></script> </head> <body> <noscript><div id="JSWarning">You need to enable javascript to be able to use this website.</div></noscript> <td id="Content" style="min-width: 659px;"> <div id="tabs" class="ui-tabs ui-widget ui-widget-content ui-corner-all"> <div id="tabs-1" class="StyledForm ui-tabs-panel ui-widget-content ui-corner-bottom"> <div class="FormattedBlock" style="word-wrap:break-word;"> ''' footer = r''' </div> </div> </div> </td> </body> </html> ''' #endregion content = str(textEdit.toPlainText()) if cb_cbStatus.isChecked(): bbcode.cb_open = True else: bbcode.cb_open = False #print content formattedContent = bbcode.render_html(content) html = header+'\n'+formattedContent+'\n'+footer textPreview.setHtml(html)
def bbcode_render_html_b(c): print(bbcode.render_html("[b]convert b to strong[/b]"))
def bbcode_render_html_u(c): print(bbcode.render_html("[u]convert u to u[/u]"))
def detail_html(self): if not self.detail: return None return bbcode.render_html(self.detail)
def word(length, allowed_chars='abcdefghijklmnopqrstuvwxyz'): return ''.join(random.choice(allowed_chars) for i in range(length)) def generate_lines(lines): fmt = '%d\t[%s]\t[%s]\t[%s]\t%s' for i in range(lines): level = word(random.randint(1, 10)) module = word(random.randint(1, 10)) sentence = ' '.join(word(random.randint(2, 7)) for i in range(10)) yield fmt % (i, datetime.datetime.now().isoformat(), level, module, sentence) if __name__ == '__main__': for num in (100, 1000, 10000): doc = '\n'.join(generate_lines(num)) pr = cProfile.Profile() start = time.time() pr.enable() fmt = bbcode.render_html(doc) pr.disable() elapsed = time.time() - start print '%d lines: %.6fs (%.2f kB)' % (num, elapsed, len(doc) / 1024.0) s = StringIO.StringIO() ps = pstats.Stats(pr, stream=s).sort_stats('cumulative') ps.print_stats() print s.getvalue()
def parse(self, input_type, output_format, text): return bbcode.render_html(text)
def test_render_html(self): html = bbcode.render_html("[b]hello[/b] [i]world[/i]") self.assertEqual(html, "<strong>hello</strong> <em>world</em>")
reply_content += "New subject: %s\n\n" % reply_title subject_follow_ups.append((parent_title, reply_title)) reply_content += clean_text(bodies[reply['id']]['body']) comment = etree.Element(WP + "comment") etree.SubElement(comment, WP + "comment_id").text = str(reply['id']) etree.SubElement(comment, WP + "comment_approved").text = reply['approved'] == 'Y' and "1" or "0" etree.SubElement(comment, WP + "comment_author").text = etree.CDATA(reply['author']) etree.SubElement(comment, WP + "comment_author_email").text = user_email etree.SubElement(comment, WP + "comment_author_url").text = user_url etree.SubElement(comment, WP + "comment_author_IP").text = reply['host'] etree.SubElement(comment, WP + "comment_type").text = "" etree.SubElement(comment, WP + "comment_parent").text = str(reply['parent']) etree.SubElement(comment, WP + "comment_date").text = reply['datestamp'].isoformat(' ') etree.SubElement(comment, WP + "comment_date_gmt").text = reply['datestamp'].isoformat(' ') etree.SubElement(comment, WP + "comment_content").text = etree.CDATA(bbcode.render_html(reply_content).replace('<br />', '\n').replace('<br/>', '\n')) page.append(comment) items.append(page) # Save some topic information for later topic_list.append({ 'link': topic_link, 'title': topic['subject'], 'date': topic['datestamp'], }) # As content of top-level forum page, list all topics current_year = None forum_content = "" topic_list.sort(key=operator.itemgetter('date'))
def showOutput(text): #prints output bbcode in one window, and html formatted version in another. window.outbox.setText(text) window.fancyOutbox.setText(bbcode.render_html(text)) log("Output written.")
def bbcode_render_html(c): print(bbcode.render_html(sys.stdin.read()))
def run(): error_count = 0 created_count = 0 db = sqlsoup.SQLSoup('mysql://root@localhost/rhizome') user_join = db.join(db.auth_user, db.accounts_rhizomeuser, db.auth_user.id == db.accounts_rhizomeuser.user_id, isouter=True) # user_join = db.join(user_join, db.accounts_useraddress) users = user_join.filter(db.auth_user.id != 1059863) tz = timezone.get_current_timezone() def tz_aware(dt): return dt if dt is None: return None return tz.localize(dt, is_dst=False) created_users = [] user_profiles = [] for user in users: try: u = User(id=user.id, first_name=user.first_name, last_name=user.last_name, email=user.email, password=user.password, date_joined=tz_aware(user.date_joined), username=user.username, is_staff=user.is_staff, is_active=user.is_active, is_superuser=user.is_superuser, last_login=tz_aware(user.last_login)) if user.bio: bio = bbcode.render_html(user.bio.decode('utf-8')) else: bio = '' if user.show_email: show_email = user.show_email else: show_email = False if user.visible: visible = user.visible else: visible = False up = UserProfile(user_id=user.id, bio=bio, url=user.url, facebook=user.facebook, twitter=user.twitter, show_email=show_email, visible=visible, registration_code=user.registration_code) created_users.append(u) user_profiles.append(up) ''' todo - location, occupation, industry, education, icon. ''' # if user.icon and not user.icon.isspace() and user.icon != 'accounts/images/icons/rhizome_user_default.png': # try: # url = 'http://media.rhizome.org/%s' % user.icon # img_temp = NamedTemporaryFile(delete=True) # img_temp.write(urllib2.urlopen(url).read()) # img_filename = urlparse(url).path.split('/')[-1] # img_temp.flush() # # print profile.avatar # # profile.avatar.save(img_filename, File(img_temp)) # # except Exception, e: # print "trouble with image" # print e created_count += 1 except Exception, e: print e error_count += 1 if created_count % 50 == 0: print "Created %s users. Received %s errors." % (created_count, error_count, )
def render_dispbbs(topic, simplify, posts, userinfos): """ need: topic, simplify, posts, userinfos need modules: time, markdown, bbcode returns: floor2post, time2post, user2post, is_edited, is_deleted, posts, topic """ floor2post = {} user2post = {} time2post = {} is_deleted = False is_edited = False elapsed_days = int((time.time() - ftime(topic["time"])) // (24 * 3600)) if elapsed_days > 30 and "水楼" not in topic["title"]: extra_message = "这是 {elapsed_days} 天前发表的主题,如无必要请勿回复".format(**locals()) if simplify: i = 0 for post in posts: post["purequote"] = False post["quotedinfo"] = [] time2post[ftime(post["time"])] = post["floor"] floor2post[post["floor"]] = i u = post["userName"] if topic["boardId"] != 182 else "匿名" if u not in user2post: user2post[u] = [(i, ftime(post["time"]))] else: user2post[u].append((i, ftime(post["time"]))) i += 1 for post in posts: if post["content"] is None: is_deleted = True post["content"] = "===此回复已经被删===" if simplify: # 精简纯引用 c = post["content"].strip() is_purequote = check_purequote(c) is_lastfloor = post["floor"] == posts[-1]["floor"] if is_purequote and not is_lastfloor: try: try: quotetarget = int( post["content"].split("以下是引用")[1].split("楼")[0]) except: username_and_time = post["content"].split( "以下是引用[i]")[1].split("[/i]")[0] username = "******".join( username_and_time.split("在")[:-1]).strip() timestr1 = username_and_time.split("在")[-1] timestr2 = timestr1.replace(" AM", "") timestr3 = timestr1.replace(" PM", "") if timestr2 != timestr1: thetime = htime2(timestr2) elif timestr3 != timestr1: thetime = htime2( timestr3) + 43200 # stupid fix for PM, add 12h else: thetime = htime(timestr1) for diff in [0, -1, 1]: if thetime + diff in [ i[1] for i in user2post[username] ]: quotetarget = time2post[thetime + diff] break else: raise if quotetarget != 1: posts[floor2post[quotetarget]]["quotedinfo"].append( [post["floor"], post["userName"]]) post["purequote"] = True # 如果不能提取出引用目标就放弃 except: pass if post["awards"] is not None and len(post["awards"]): awardcontent = "" for award in post["awards"]: awardcontent += award["content"] + " reason:" + award[ "reason"] + " by:" + award["operatorName"] + "\n" if post["contentType"]: post[ "content"] += "\n----\n#### 评分记录\n\n" + awardcontent.replace( "\n", "\n\n") else: post[ "content"] += "\n[color=red][align=right]==========\n" + awardcontent + "[/align][/color]" post["content_orignal"] = post["content"] post["content"] = fixcontent_before(post["content"]) if post["contentType"]: content = post["content"] post["content"] = markdown.markdown( content, extensions=['del_ins', 'markdown.extensions.extra', 'urlize']) if post["content"].count("klzzwxh:") > 0: post["content"] = markdown.markdown( content, extensions=['del_ins', 'markdown.extensions.extra']) else: post["content"] = "<div class='ubbcode'>" + bbcode.render_html( post["content"]) + "</div>" post["content"] = fixcontent_after(post["content"]) oldtime = ftime(post["time"]) nowtime = int(time.time()) post["howlongago"] = calc_howlongago(nowtime, oldtime) post["portrait"] = getportrait(post["userId"], userinfos) if simplify: for post in posts: post["quoteddetail"] = ", ".join([ "{}L <b>{}</b>".format(item[0], item[1]) for item in post["quotedinfo"] ]) topic["participantCount"] = len(set([post["userName"] for post in posts])) is_edited = any([i["lastUpdateTime"] for i in posts]) return floor2post, time2post, user2post, is_edited, is_deleted, posts, topic
def parse_bbcode(text): return mark_safe(bbcode.render_html(text))
def showOutput(text): window.outbox.setText(text) window.fancyOutbox.setText(bbcode.render_html(text))
def bbcode_render_html_i(c): print(bbcode.render_html("[i]convert i to em[/i]"))
def bbcode(value): return mark_safe(bbcode_lib.render_html(value))
def rendered_body(self): return bbcode.render_html(self.body)