def markdown_to_html(text, codesyntax): def matcher(match): found = match.group() try: codesyntax = _codesyntax_regex.findall(found)[0] except IndexError: codesyntax = None found = _codesyntax_regex.sub('```', found) if codesyntax: def highlighter(m): lexer = _get_lexer(codesyntax) code = m.group().replace('```', '') return highlight(code, lexer, HtmlFormatter()) found = _markdown_pre_regex.sub(highlighter, found) found = found.replace('```', '<pre>', 1) found = found.replace('```', '</pre>') return found text = _markdown_pre_regex.sub(matcher, text) html = markdown.markdown( gfm(text), extensions=['markdown.extensions.tables'] ) html = html.replace('<table>', '<table class="ui celled table">') return html
def router(request, path=None): if path: paths = path.split('/') values = None if len(paths) == 2: # if paths[1] == 'index.html': # return HttpResponseRedirect('/%s/'%paths[0]) # if paths[1] == '': # paths[1] = 'index.html' minisite = get_object_or_404(Minisite, slug=paths[0]) page = get_object_or_404(Page, slug=paths[1], minisite=minisite) if page.format == '0': page.text_html = linebreaks(urlize(imgize(page.text))) if page.format == '2': html = gfm(page.text) html = page.text page.text_html = markdown.markdown( html, ['extra', 'codehilite', 'toc', 'nl2br'], safe_mode=False, html_replacement_text='--RAW HTML NOT ALLOWED--', output_format='html5') values = {'minisite': minisite, 'page': page} else: return HttpResponseRedirect('/%s/' % path) return render_to_response('home/minisite.html', values, context_instance=RequestContext(request)) return HttpResponseNotFound()
def edit(post_id): try: oid = ObjectId(post_id) except: return redirect(url_for("newpost")) post = db.posts.find_one({"_id": oid}) if request.method == 'GET': form = EditPostForm() form.post_id.data = post_id form.title.data = post['title'] form.content.data = post['body'] return render_template("write.html", form=form, post_id=post_id) else: form = NewPostForm(request.form) if form.validate(): title = form.title.data body = form.content.data md = Markdown() html = md.convert(gfm(body)) db.posts.update( {"_id": oid}, {"$set": { "title": title, "body": body, "html": html }}) return redirect(url_for("home")) else: return render_template("write.html", form=form, post_id=post_id)
def markdownToSoup(markdownstring): """Takes a string of markdown, and returns a rendered bs4 element""" renderedMarkdown = markdown(gfm(markdownstring)) newsoup = BeautifulSoup(renderedMarkdown, bs4parser) container = newsoup.body container.name = 'span' container['class'] = 'mdrender' return container
def format_comment_body(comment): body_text_gfm = comment['body'] body_text_md = gfm.gfm(body_text_gfm) body_text_html = markdown.markdown(body_text_md) body_text_lines = (BeautifulSoup(body_text_html).findAll(text=True)) body_text = u"\n".join(body_text_lines) return body_text
def save(self, *args, **kwargs): if self.format == 'markdown': if self.raw_content.strip(): self.compiled_content = markdown2.markdown(gfm.gfm(force_unicode((self.raw_content)))).strip() else: self.compiled_content = '' else: self.compiled_content = self.raw_content super(Block, self).save(*args, **kwargs)
def save(self, *args, **kwargs): if self.format == 'markdown': if self.raw_content.strip(): self.compiled_content = markdown2.markdown( gfm.gfm(force_unicode((self.raw_content)))).strip() else: self.compiled_content = '' else: self.compiled_content = self.raw_content super(Block, self).save(*args, **kwargs)
def get(self,extID): ext = Extension.gql('WHERE extID = :1',extID).get() if not ext: path = os.path.join(os.path.dirname(__file__), 'templates/head.html') self.response.out.write(template.render(path, {'title':'Extension Not Found'})) path = os.path.join(os.path.dirname(__file__), 'templates/ext404.html') self.response.out.write(template.render(path, {})) self.response.set_status(404); else: # Convert the Markdown in the description to HTML, # but escape any HTML added by the user. ext.description = markdown.markdown(text=gfm.gfm(ext.description), safe_mode='escape') # Add the Extension to the template vars dictionary and # set other template vars to their default values. templateVars = { 'ext':ext, 'upvotePercent':0, 'downvotePercent':0, 'ratingCount':0, 'userRating':None, 'starred':False, 'userIsDev':False } if ext.developer: templateVars['devname'] = ext.developer.nickname() templateVars['ratingCount'],templateVars['upvotePercent'],templateVars['downvotePercent'] = getRatingInfo(extID) user = users.get_current_user() if user: userEntry = User.gql('WHERE user = :1',user).get() if userEntry: if extID in userEntry.starred: templateVars['starred'] = True userRating = Rating.gql('WHERE user = :1 AND extID = :2',user,extID).get() if userRating: templateVars['userRating'] = userRating.value else: templateVars['userRating'] = 0 # Every user's default vote is zero if user == ext.developer: templateVars['userIsDev'] = True path = os.path.join(os.path.dirname(__file__), 'templates/head.html') self.response.out.write(template.render(path, {'title':ext.title,'stylesheet':'gallery'})) path = os.path.join(os.path.dirname(__file__), 'templates/info.html') self.response.out.write(template.render(path, templateVars)) path = os.path.join(os.path.dirname(__file__), 'templates/foot.html') self.response.out.write(template.render(path, {}))
def newpost(): if request.method == 'GET': return render_template("write.html", form=NewPostForm(), mode="new") else: form = NewPostForm(request.form) if form.validate(): title = form.title.data body = form.content.data md = Markdown() html = md.convert(gfm(body)) db.posts.insert({"title":title, "body":body, "html":html, "created_at":datetime.now()}) return redirect(url_for("home")) else: return render_template("write.html", form=form, mode="new")
def newpost(): if request.method == 'GET': return render_template("write.html", form=NewPostForm(), mode="new") else: form = NewPostForm(request.form) if form.validate(): title = form.title.data body = form.content.data md = Markdown() html = md.convert(gfm(body)) db.posts.insert({ "title": title, "body": body, "html": html, "created_at": datetime.now() }) return redirect(url_for("home")) else: return render_template("write.html", form=form, mode="new")
def markdown_to_html(text, codesyntax): def matcher(match): found = match.group() try: codesyntax = _codesyntax_regex.findall(found)[0] except IndexError: codesyntax = None found = _codesyntax_regex.sub('```', found) if codesyntax: def highlighter(m): lexer = _get_lexer(codesyntax) code = m.group().replace('```', '') return highlight(code, lexer, HtmlFormatter()) found = _markdown_pre_regex.sub(highlighter, found) found = found.replace('```', '<pre>', 1) found = found.replace('```', '</pre>') return found text = _markdown_pre_regex.sub(matcher, text) html = markdown.markdown(gfm(text)) return html
def markdown_to_html(text, codesyntax): # print "PRE" def matcher(match): found = match.group() try: codesyntax = _codesyntax_regex.findall(found)[0] except IndexError: codesyntax = None #def codesyntax_finder(m): # codesyntax = m.group() # print "\tCODESYNTAX", repr(codesyntax) # return '```' found = _codesyntax_regex.sub('```', found) # print "FOUND" # print repr(found) ##print repr(codesyntax) # print repr(found) if codesyntax: def highlighter(m): # print "IN HERE" # print "\t", repr(codesyntax) # print "\t", repr(m.group()) # print "\t", repr('\n%s\n' % _highlight_it(m.group().strip(), codesyntax)) lexer = _get_lexer(codesyntax) code = m.group().replace('```', '') return highlight(code, lexer, HtmlFormatter()) found = _markdown_pre_regex.sub(highlighter, found) found = found.replace('```', '<pre>', 1) found = found.replace('```', '</pre>') print "" return found text = _markdown_pre_regex.sub(matcher, text) #print repr(text) #print gfm(text) #print "\n" html = markdown.markdown(gfm(text)) return html
def edit(post_id): try: oid = ObjectId(post_id) except: return redirect(url_for("newpost")) post = db.posts.find_one({"_id":oid}) if request.method == 'GET': form = EditPostForm() form.post_id.data = post_id form.title.data = post['title'] form.content.data = post['body'] return render_template("write.html", form=form, post_id=post_id) else: form = NewPostForm(request.form) if form.validate(): title = form.title.data body = form.content.data md = Markdown() html = md.convert(gfm(body)) db.posts.update({"_id":oid}, {"$set" :{"title":title, "body":body, "html":html}}) return redirect(url_for("home")) else: return render_template("write.html", form=form, post_id=post_id)
def router(request,path=None): if path: paths = path.split('/') values = None if len(paths) == 2: # if paths[1] == 'index.html': # return HttpResponseRedirect('/%s/'%paths[0]) # if paths[1] == '': # paths[1] = 'index.html' minisite = get_object_or_404(Minisite,slug=paths[0]) page = get_object_or_404(Page,slug=paths[1],minisite=minisite) if page.format == '0': page.text_html = linebreaks(urlize(imgize(page.text))) if page.format == '2': html = gfm(page.text) html = page.text page.text_html = markdown.markdown(html, ['extra','codehilite','toc','nl2br'],safe_mode=False, html_replacement_text='--RAW HTML NOT ALLOWED--',output_format='html5') values = {'minisite':minisite,'page':page} else: return HttpResponseRedirect('/%s/'%path) return render_to_response('home/minisite.html',values,context_instance=RequestContext(request)) return HttpResponseNotFound()
import os import sys import gfm if len(sys.argv) == 1: sys.exit(-1) with open(sys.argv[1], "r") as fin: markdown_content = fin.read() markdown_content = gfm.gfm(markdown_content) print gfm.markdown(markdown_content)
def getHTMLDescription(self): # Convert the Markdown in the description to HTML, # but escape any HTML added by the user. return markdown.markdown(text=gfm.gfm(self.description),safe_mode='escape')
def format_gfm(value): return markdown(gfm(value))
def get_html_text(self): return markdown(text=gfm(self.text), safe_mode='escape').replace( '<a href="', '<a target="_blank" href="')
def get_html_description(self): # Convert the description from Markdown to HTML. return markdown(text=gfm(self.description), safe_mode='escape').replace( '<a href="', '<a target="_blank" href="')