def render_text(text, autocompletes=None, comment=None, unwrap_p=False): # Render comment text into HTML. import re # Put @-mentions in bold. if autocompletes: text, _ = match_autocompletes(text, autocompletes, lambda text: "**" + text + "**") # Rewrite attachment:### URLs. if comment is not None: def get_attachment_url(attachment_id): try: return Attachment.objects.get( id=attachment_id.group(1)).get_absolute_url() except: return "about:blank" text = re.sub("(?<=\()attachment:(\d+)(?=\))", get_attachment_url, text) # Render to HTML as if CommonMark. import CommonMark parsed = CommonMark.Parser().parse(text) text = CommonMark.HtmlRenderer({"safe": True}).render(parsed) if unwrap_p: # If it's a single paragraph, unwrap it. text = re.sub(r"^<p>(.*)</p>$", r"\1", text) return text
def to_json(self, user=None): parser = CommonMark.Parser() renderer = CommonMark.HtmlRenderer() ast = parser.parse(self.event.abstract) abstract = renderer.render(ast) data = { 'title': self.event.title, 'event_slug': self.event.slug, 'abstract': abstract, 'from': self.when.lower.isoformat(), 'to': self.when.upper.isoformat(), 'url': str(self.event.get_absolute_url()), 'id': self.id, 'speakers': [ { 'name': speaker.name , 'url': str(speaker.get_absolute_url()) } for speaker in self.event.speakers.all() ], 'bg-color': self.event.event_type.color, 'fg-color': '#fff' if self.event.event_type.light_text else '#000', 'event_type': self.event.event_type.slug, 'location': self.location.slug, 'location_icon': self.location.icon, 'timeslots': self.timeslots, } if user and user.is_authenticated: is_favorited = user.favorites.filter(event_instance=self).exists() data['is_favorited'] = is_favorited return data
def markdown(text, escape=True): parser = CommonMark.Parser() ast = parser.parse(text) renderer = CommonMark.HtmlRenderer() html = renderer.render(ast) return html
def neomarkdown(markdown_content): parser = CommonMark.Parser() renderer = CommonMark.HtmlRenderer() ast = parser.parse(markdown_content) html = renderer.render(ast) # json = CommonMark.dumpJSON(ast) # CommonMark.dumpAST(ast) return html
def test_smart_dashes(self): md = 'a - b -- c --- d ---- e ----- f' EM = '\u2014' EN = '\u2013' expected_html = ('<p>' + 'a - ' + 'b ' + EN + ' ' + 'c ' + EM + ' ' + 'd ' + EN + EN + ' ' + 'e ' + EM + EN + ' ' + 'f</p>\n') parser = CommonMark.Parser(options=dict(smart=True)) ast = parser.parse(md) renderer = CommonMark.HtmlRenderer() html = renderer.render(ast) self.assertEqual(html, expected_html)
def page(name='teemo'): exodus_dir = os.path.dirname(os.path.realpath(__file__)) page = 'pages/%s.md' % name path = os.path.join(exodus_dir, page) if path.startswith(exodus_dir) and os.path.exists(path): with open(str(path)) as p: c = p.read() parser = CommonMark.Parser() ast = parser.parse(unicode(c, 'utf-8')) renderer = CommonMark.HtmlRenderer() html = renderer.render(ast) return render_template('page.html', page=html) else: return render_template('404.html'), 404
def extract_last_changelog(path): with open(path, 'r') as fp: parser = CommonMark.Parser() content = fp.read() changelog = ast_to_changelog(parser.parse(content)) if len(changelog.releases) == 0: raise Exception('No changelog releases') if len(changelog.releases) > 1: current = changelog.releases[0] previous = changelog.releases[1] with open(path, 'r') as fp: content = fp.read() pattern = r'\#\# {}(.*\n)([\n\S\s]*)\#\# {}'.format(re.escape(current.name), re.escape(previous.name)) result = re.search(pattern, content, re.MULTILINE) return result.group(2).strip() elif len(changelog.releases) == 1: current = changelog.releases[0] with open(path, 'r') as fp: content = fp.read() pattern = r'\#\# {}(.*\n)([\n\S\s]*)'.format(re.escape(current.name)) result = re.search(pattern, content, re.MULTILINE) return result.group(2).strip()
def get(self): posts = glob("posts/*.md") output = '<html><head><link rel="stylesheet" href="tufte.css"/></head><body>' archive_content = "# Archive\n\nHere's an archive of all of my published posts:\n\n" metadata = {} ordered_posts = {} if len(posts) > 0: for post in posts: metadata[post] = extract_metadata(post) for post in posts: print(metadata[post]) if metadata[post] is not None: if 'post_number' in metadata[post].keys(): ordered_posts[metadata[post]['post_number']] = post[6:-3] for i in range(1,len(ordered_posts.keys())+1): post = ordered_posts[i] archive_content += "1. [" + post + "](" + post + ")\n" archive_content += "\n\n" print(ordered_posts) output += cm.commonmark(archive_content) output += '</body></html>' self.write(output)
def markdown(md, autoescape=True): if autoescape: esc = conditional_escape else: esc = lambda x: x html = CommonMark.commonmark(esc(md)) return mark_safe(html)
def toHTML(self,md,metadata,html,generateTitle=False): uri = self.weburi + metadata['published'][0] print('<article xmlns="http://www.w3.org/1999/xhtml" vocab="{}" typeof="{}" resource="{}">'.format(self.vocab,self.typeof,uri),file=html) print('<script type="application/json+ld">',file=html) print('{\n"@context" : "http://schema.org/",',file=html) print('"@id" : "{}",'.format(uri),file=html) print('"headline" : "{}",'.format(metadata['title'][0]),file=html) print('"datePublished" : "{}",'.format(metadata['published'][0]),file=html) print('"dateModified" : "{}",'.format(metadata['updated'][0]),file=html) if ("keywords" in metadata): print('"keywords" : [{}],'.format(','.join(['"' + m + '"' for m in metadata['keywords']])),file=html) html.write('"author" : [ ') for index,author in enumerate(metadata['author']): if (index>0): html.write(', ') html.write('{{ "@type" : "Person", "name" : "{}" }}'.format(author)) html.write(']\n') print('}',file=html) print('</script>',file=html) if generateTitle: print("<h1>{}</h1>".format(escape(metadata['title'][0],quote=False)),file=html) print(CommonMark.commonmark(md),file=html) print('</article>',file=html)
def read_wiki_page(path): # Turn the URL-style path into the canonical filesystem-aware path path = path.replace('/', os.path.sep).lower() full_path = os.path.join(wiki_bp.root_path, wiki_bp.static_folder, path) reg_path = _get_md(full_path) dir_path = _get_md(os.path.join(full_path, '_index')) if os.path.isfile(reg_path): full_path = reg_path elif os.path.isfile(dir_path): full_path = dir_path elif os.path.isdir(full_path): files = os.listdir(full_path) lines = ['<h1>{}</h1>'.format(fn2title(path) or 'Home')] for f in files: if f.startswith('.'): continue f = os.path.splitext(f)[0] wiki_path = '/'.join([path, f]) if path else f if os.path.isdir(os.path.join(full_path, f)): wiki_path += '/' lines.append('<a href="{}">{}</a>'.format(url_for('.wiki', wiki_path=wiki_path), fn2title(f))) return '\n'.join(lines) else: abort(404) with open(full_path) as fl: return commonmark.commonmark(fl.read().decode('utf-8'))
def parse(self, src): self.src = src self.recipe = Recipe(title=None) parser = CommonMark.Parser() ast = parser.parse(src) self.current = ast.first_child self._parse_title() self._parse_description() self._parse_tags_and_yields() if self.current is not None and self.current.t == 'thematic_break': self._next_node() else: # TODO this divider is required, but we might just continue anyways? raise RuntimeError("Invalid, expected divider before ingredients") self._parse_ingredients() if self.current is not None: if self.current.t == 'thematic_break': self._next_node() else: # TODO this divider is required, but we might just continue anyways? raise RuntimeError("Invalid, expected divider after ingredients") self.recipe.instructions = self._get_source_until() return self.recipe
def markdown(s): tainted_html = CommonMark.commonmark(s) safe_html = bleach.clean(tainted_html, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES, styles=ALLOWED_STYLES) return safe_html
def generate_email(self, data, tagline): email = u""" Hi {name}, {tagline} ## Outgoing Calls {outgoing} ## Incoming Calls {incoming} Thanks, The Call Stats Robot """.format( name=data['name'], outgoing=self.stats_markdown(data['outgoing']), incoming=self.stats_markdown(data['incoming']), tagline=tagline) html_email = CommonMark.commonmark(email) # Send HTML email html_part = MIMEText(html_email, 'html', 'utf-8') msg = MIMEMultipart('alternative') msg['Subject'] = 'Your Call Stats' msg['From'] = local_settings.SMTP_FROM msg['To'] = data['email'] msg.attach(html_part) self.smtp.sendmail( local_settings.SMTP_FROM, data['email'], msg.as_string())
def _render(self, text): '''Render CommonMark with ettings taken in account''' parser = CommonMark.Parser() ast = parser.parse(text) renderer = HtmlRenderer(self) html = renderer.render(ast) return html
def markdown(value): # Renders the string using CommonMark in safe mode, which blocks # raw HTML in the input and also some links using a blacklist, # plus a second pass filtering using a whitelist for allowed # tags and URL schemes. import CommonMark ast = CommonMark.Parser().parse(force_unicode(value)) html = CommonMark.HtmlRenderer({'safe': True}).render(ast) import html5lib, urlparse def filter_url(url): try: urlp = urlparse.urlparse(url) except Exception as e: # invalid URL return None if urlp.scheme not in ("http", "https"): return None return url valid_tags = set( 'strong em a code p h1 h2 h3 h4 h5 h6 pre br hr img ul ol li span blockquote' .split()) valid_tags = set('{http://www.w3.org/1999/xhtml}' + tag for tag in valid_tags) dom = html5lib.HTMLParser().parseFragment(html) for node in dom.iter(): if node.tag not in valid_tags and node.tag != 'DOCUMENT_FRAGMENT': node.tag = '{http://www.w3.org/1999/xhtml}span' for name, val in node.attrib.items(): if name.lower() in ("href", "src"): val = filter_url(val) if val is None: node.attrib.pop(name) else: node.set(name, val) else: # No other attributes are permitted. node.attrib.pop(name) html = html5lib.serialize(dom, quote_attr_values="always", omit_optional_tags=False, alphabetical_attributes=True) return safestring.mark_safe(html)
def test_unicode(self): s = CommonMark.commonmark('<div>\u2020</div>\n') self.assertEqual(s, '<div>\u2020</div>\n', 'Unicode works in an HTML block.') CommonMark.commonmark('* unicode: \u2020') CommonMark.commonmark('# unicode: \u2020') CommonMark.commonmark('```\n# unicode: \u2020\n```')
def create_post(request): print request.FILES.get('file') print type(request.FILES.get('file')) image = request.FILES.get('file') content = request.POST.get('post-input') published = datetime.now() is_markdown = json.loads(request.POST.get('is-markdown-post')) if is_markdown: contentType = "text/x-markdown" content = CommonMark.commonmark(content) else: contentType = "text/plain" visibility = request.POST.get('visibility') c_username = request.user.username user_object = User.objects.get(username=c_username) author_object = Author.objects.get(email=user_object) author_name = author_object.displayName post_id = uuid.uuid4() DITTO_HOST = 'http://' + request.get_host() + '/api/posts/' + str(post_id) title = request.POST.get('title') description = request.POST.get('description') categories = request.POST.get('categories') c = categories.split(' ') categories_json = json.dumps(c) new_post = Post(published=published, author=author_object, content=content, contentType=contentType, visibility=visibility, source=DITTO_HOST, origin=DITTO_HOST, categories=categories, title=title, description=description, id = post_id) new_post.save() if image: print image.content_type #image.name = str(uuid.uuid4()) print image.name print "before creating" new_image = Img(actual_image = image) new_image.parent_post = new_post print "before saving" new_image.save() print "after saving" new_post.content = new_post.content + ' <br> <img src="http://ditto-test.herokuapp.com/ditto/media/images/'+image.name+'" >' #new_post.content = new_post.content + ' <br> <img src="http://localhost:8000/ditto/media/images/'+image.name+'" >' new_post.save() return HttpResponse(request.POST.get('post_body'))
def get_markdown_ast(markdown_file): try: f = open(markdown_file, 'r') return CommonMark.DocParser().parse(f.read()) except: logging.error( "Error: Can't open {0} for reading".format(markdown_file)) sys.exit(1) finally: f.close()
def parse_markdown(text): if not text: return None text = reDisplayMath.sub(lambda m: f'<math src={quoteattr(m.group(1))} />', text) text = reInlineMath.sub(lambda m: f'<math src={quoteattr(m.group(1))} />', text) html = CommonMark.commonmark(text) ast = ElementTree.fromstring(f"<turingarena>{html}</turingarena>") return list(xml_children(ast))
def render(self, context): text = self.nodelist.render(context) if self.title: text = "### %s\n\n%s" % (self.title, text) #text = markdown(text) text = CommonMark.commonmark(text) link_anchor = ANCHOR_PATTERN % self.anchor text = bleach.linkify(text, callbacks=self.CALLBACKS, skip_pre=True) text = link_anchor + text + TOP_LINK return text
def main(): parser = argparse.ArgumentParser( description="Process Markdown according to " "the CommonMark specification.") if sys.version_info < (3, 0): reload(sys) # noqa sys.setdefaultencoding('utf-8') parser.add_argument( 'infile', nargs="?", type=argparse.FileType('r'), default=sys.stdin, help="Input Markdown file to parse, defaults to STDIN") parser.add_argument( '-o', nargs="?", type=argparse.FileType('w'), default=sys.stdout, help="Output HTML/JSON file, defaults to STDOUT") parser.add_argument('-a', action="store_true", help="Print formatted AST") parser.add_argument('-aj', action="store_true", help="Output JSON AST") args = parser.parse_args() parser = CommonMark.Parser() f = args.infile o = args.o lines = [] for line in f: lines.append(line) data = "".join(lines) ast = parser.parse(data) if not args.a and not args.aj: renderer = CommonMark.HtmlRenderer() o.write(renderer.render(ast)) exit() if args.a: # print ast CommonMark.dumpAST(ast) exit() # o.write(ast.to_JSON()) o.write(CommonMark.dumpJSON(ast)) exit()
def commonmark(value): """ Runs CommonMark over a given value. Syntax:: {{ value|commonmark }} :type value: str :rtype: str """ import CommonMark parser = CommonMark.DocParser() renderer = CommonMark.HTMLRenderer() ast = parser.parse(value) return mark_safe( force_text(renderer.render(ast)) )
def init_app(app): parser = CommonMark.DocParser() renderer = CommonMark.HTMLRenderer() app.extensions['markdown'] = UDataMarkdown(app, parser, renderer) @app.template_filter() def mdstrip(value, length=None): ''' Truncate and strip tags from a markdown source The markdown source is truncated at the excerpt if present and smaller than the required length. Then, all html tags are stripped. ''' if not value: return '' if EXCERPT_TOKEN in value: value = value.split(EXCERPT_TOKEN, 1)[0] if length > 0: value = do_truncate(value, length, end='…') rendered = md(value) return do_striptags(rendered)
def __init__(self, parser=None, style_name=None): if parser is None: parser = CommonMark.Parser() if style_name is None: style_name = 'native' self.parser = parser self.style_name = style_name self.list_level = -1 self.counters = {} self.footnotes = []
def markdown(text): """Filter for turning text into markdown. :param text: String to be transformed. :return: Transformed html string. Usage: {{ 'Some *markdown*'|markdown }} "Some <strong>markdown</strong>" """ return Markup(CommonMark.commonmark(text))
def main(): parser = argparse.ArgumentParser( description="Process Markdown according to " "the CommonMark specification.") if sys.version_info < (3, 0): reload(sys) # noqa sys.setdefaultencoding('utf-8') parser.add_argument('infile', nargs="?", type=argparse.FileType('r'), default=sys.stdin, help="Input Markdown file to parse, defaults to STDIN") parser.add_argument('-o', nargs="?", type=argparse.FileType('w'), default=sys.stdout, help="Output HTML/JSON file, defaults to STDOUT") parser.add_argument('-a', action="store_true", help="Print formatted AST") parser.add_argument('-aj', action="store_true", help="Output JSON AST") args = parser.parse_args() parser = CommonMark.Parser() f = args.infile o = args.o lines = [] for line in f: lines.append(line) data = "".join(lines) ast = parser.parse(data) if not args.a and not args.aj: renderer = CommonMark.HtmlRenderer() o.write(renderer.render(ast)) exit() if args.a: # print ast CommonMark.dumpAST(ast) exit() # o.write(ast.to_JSON()) o.write(CommonMark.dumpJSON(ast)) exit()
def markdown(s: str) -> str: commented_shortcodes = shortcodes.comment_shortcodes(s) tainted_html = CommonMark.commonmark(commented_shortcodes) # Create a Cleaner that supports parsing of bare links (see filters). cleaner = bleach.Cleaner(tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES, styles=ALLOWED_STYLES, strip_comments=False, filters=[bleach.linkifier.LinkifyFilter]) safe_html = cleaner.clean(tainted_html) return safe_html
def render_commonmark_template(template, context): # Render a CommonMark template to HTML. # Replace template tags with Unicode sentinels so that # the template tags do not impact CommonMark rendering. substitutions = [] import re def replace(m): # Record the substitution. index = len(substitutions) substitutions.append(m.group(0)) return "\uE000%d\uE001" % index # use Unicode private use area code points template = re.sub("{%.*?%}|{{.*?}}", replace, template) # Render the CommonMark. # Prevent CommonMark from mucking with our sentinels # in URLs though - it would otherwise add escaping. from CommonMark import inlines def urlencode_special(uri): import urllib.parse return "".join( urllib.parse.quote(c, safe="/@:+?=&()%#*,") # this is what CommonMark does if c not in "\uE000\uE001" else c # but keep our special codes for c in uri) inlines.normalize_uri = urlencode_special # Render. import CommonMark template = CommonMark.HtmlRenderer().render(CommonMark.Parser().parse(template)) # Put the template tags back that we removed prior to running # the CommonMark renderer. def replace(m): return substitutions[int(m.group(1))] template = re.sub("\uE000(\d+)\uE001", replace, template) # And finally render the Django template. return Template(template).render(Context(context))
def _convert_to_html(self, template_name: str = '') -> str: """ Convert from raw markdown text to html. :return: html formatted text """ html = "" if template_name == "": html = CommonMark.commonmark(self._raw_text) else: template = self._get_template(template_name) html_parts = self._get_html_parts() html = template.render(html_parts=html_parts) return html
def markdown_stuff(contentType, markdown): #checks for block content then escapes content to another #function before applying and returning markdowned content. #If not just replace the new line comment with a linebreak #to make it markdown clean = html.conditional_escape(contentType) if markdown: #markdown block quotes new_markdown = clean.replace('>', '>') new_markdown = CommonMark.commonmark(new_markdown) markdown_text = markdown_unescape(new_markdown) return markdown_text.replace('\n', '<br/>') return clean.replace('\n', '<br/>')
def _get_html_parts(self): # initialize html parts dictionary ret = {'title': '', 'body': ''} for l in self._raw_text.split('\n'): # First H1 size text set to page title if re.match(r'# .+$', l): ret['title'] = l.replace('#', '').strip() break # self html text set to body html ret['body'] = CommonMark.commonmark(self._raw_text) return ret
def format_summary(text): # Some summaries have double-newlines that are probably paragraph breaks. # Others have newlines at the ends of ~60-column lines that we don't care about. # Finally, some summaries have single linebreaks that seem to represent paragraphs. # Which are we dealing with? def avg(items): return sum(items)/len(items) avg_line_length = avg([len(line) for line in text.split("\n")+[""]]) if avg_line_length > 100: # Seems like newlines probably indicate paragraphs. Double them up so that we # can pass the rest through a renderer. text = text.replace("\n", "\n\n") # Turn the text into HTML. This is a fast way to do it that might work nicely. import CommonMark return CommonMark.commonmark(text)
def render_markdown(content): """ Return a html fragment from markdown text content Parameters ---------- content : str A markdown formatted text Returns ------- html : str """ return CommonMark.commonmark(content)
def get_thumbnail_url(doc, pagenumber, small): # Returns a URL to a thumbnail image for a particular page of the document. # 'small' is a boolean. # If the document is on DocumentCloud, get the URL to DocumentCloud's thumbnail image. documentcloud_id = get_documentcloud_document_id(doc) if documentcloud_id: # We can use the DocumentCloud API to get the URL to a thumbnail, but in the # interests of speed, construct the URL ourselves. #return query_documentcloud_api(documentcloud_id)["document"]["resources"]["page"]["image"].format( # page=pagenumber, # size="small" if small else "normal", #) return "https://assets.documentcloud.org/documents/%s/pages/%s-p%d-%s.gif" % ( documentcloud_id[0], documentcloud_id[1], pagenumber, "small" if small else "normal") # If it's a Markdown document, download it, convert it to HTML, then render it to # a PDF, and then to an image, and return that image as a data: URL. elif doc.get("format") == "markdown" and os.path.exists("/usr/bin/htmldoc") and os.path.exists("/usr/bin/pdftoppm"): # Download the Markdown file. md = get_document_text(doc, pagenumber) # If we got it... if md: import subprocess, base64 # Render the Markdown as HTML. html = CommonMark.commonmark(md) # Render the HTML as a PDF. # TODO: Possible security issue if the Markdown source can generate HTML that # causes htmldoc to perform network requests or possibly unsafe operations. pdf = subprocess.check_output(["/usr/bin/htmldoc", "--quiet", "--continuous", "--size", "4.5x5.8in", # smaller page magnifies the text "--top", "0", "--right", "1cm", "--bottom", "1cm", "--left", "1cm", # margins "-t", "pdf14", "-"], input=html.encode("utf8")) # Render the PDF and a PNG. png = subprocess.check_output(["/usr/bin/pdftoppm", "-singlefile", "-r", "60", "-png"], input=pdf) # Return a data: URL so we don't have to store/host the image anywhere, # but we can display it directly. return "data:image/png;base64," + base64.b64encode(png).decode("ascii") # No thumbnail image is available for this resource. return None
def md_escaped(value, inline=True): if not value: return "" formatted = value.strip() if inline: formatted = formatted.replace('\n', '<br>') formatted = CommonMark.commonmark(formatted).strip() # Remove wrapping <p> tags. if inline and formatted.startswith('<p>') and formatted.endswith('</p>'): formatted = formatted[3:-4] return formatted
def report(env, node_name, report_id): """Displays a single report including all the events associated with that report and their status. The report_id may be the puppetdb's report hash or the configuration_version. This allows for better integration into puppet-hipchat. :param env: Search for reports in this environment :type env: :obj:`string` :param node_name: Find the reports whose certname match this value :type node_name: :obj:`string` :param report_id: The hash or the configuration_version of the desired report :type report_id: :obj:`string` """ envs = environments() check_env(env, envs) query = AndOperator() report_id_query = OrOperator() report_id_query.add(EqualsOperator("hash", report_id)) report_id_query.add(EqualsOperator("configuration_version", report_id)) if env != '*': query.add(EqualsOperator("environment", env)) query.add(EqualsOperator("certname", node_name)) query.add(report_id_query) reports = puppetdb.reports(query=query) try: report = next(reports) except StopIteration: abort(404) report.version = CommonMark.commonmark(report.version) return render_template( 'report.html', report=report, events=yield_or_stop(report.events()), logs=report.logs, metrics=report.metrics, envs=envs, current_env=env)
def generate_html(self, mdfile=None): if mdfile is None: mdf = self.mdfile else: mdf = mdfile filepath = "posts/" + mdf if mdf[-3:] != ".md": filepath += ".md" with open(filepath) as open_md: content = open_md.readlines() header = [] markdown = self.__parse_yaml_frontmatter(content) markdown = ''.join(markdown) return self.preamble + cm.commonmark(markdown) + self.closing
def post_new(request): if request.method == "POST": form = PostForm(data=request.POST) #print("REQUEST.POST:%s"%request.POST) imageForm = ImageForm(request.POST, request.FILES) #print("FILES: %s"%request.FILES['imageFile']) #print(form) #print(form.errors) if form.is_valid(): post = form.save(commit=False) post.author = Author.objects.get(user=request.user.id) post.published = timezone.now() if post.contentType == 'text/x-markdown': post.content = CommonMark.commonmark(post.content) post.set_source() post.set_origin() post.save() if request.POST['image_url']: post.content = post.content + "<br><img src="+"'"+request.POST['image_url']+"'"+"/>" post.save() #return redirect('show_posts') #return render(request, 'authors/index.html', {'form':form}) if imageForm.is_valid(): image = Image(imageFile=request.FILES['imageFile']) image.post = post image.save() content = post.content print("image url: %s"%image.imageFile.url) imgUrl = "https://mighty-cliffs-82717.herokuapp.com/media/" + image.imageFile.url #this will work for mighty cliffs but not for local testing #imgUrl = post.source + "media/"+image.imageFile.url print("imgUrl: %s"%imgUrl) post.content = content + "<br><img src="+"'"+imgUrl+"'"+"/>" post.save() print("post.content: %s"%post.content) return HttpResponseRedirect('/') else: return HttpResponseRedirect('/') else: form = PostForm() return render(request, 'authors/index.html', {'form':form})
def compile(content_path, compile_path, stylesheet, html_template): (mds, other) = sep_filetype(content_path, ".md") # Prepare dir structure in compile_path if os.path.isdir(compile_path): shutil.move(compile_path, compile_path + ".old") os.mkdir(compile_path) new_dirs = [os.path.dirname(path.replace(content_path, compile_path)) for path in (mds + other)] for d in new_dirs: os.makedirs(d, exist_ok=True) # Copy all non md files for f in other: new_path = f.replace(content_path, compile_path) # Coply file to new path, but don't follow symlinks! shutil.copy2(f, new_path, follow_symlinks=False) for f in mds: with open(f, "r") as md_f: md = md_f.read() # Going to use simple str.format to build html to avoid deps with # templating languages with open(html_template, 'r') as template_f: template = template_f.read() compiled_html = template.format(body=CommonMark.commonmark(md), stylesheet=stylesheet) new_path = f.replace(content_path, compile_path) new_path = os.path.splitext(new_path)[0] + ".html" with open(new_path, "w") as html_f: html_f.write(compiled_html) if os.path.isdir(compile_path + ".old"): shutil.rmtree(compile_path + ".old")
def test_random_text(self, s): CommonMark.commonmark(s)
def test_dumpAST_orderedlist(self): md = '1.' ast = Parser().parse(md) CommonMark.dumpAST(ast)
def _on_notify_text(self, *args): html_text = CommonMark.commonmark(self.get_property('text')) self.web_view.load_html_string(html_text, '')
def comment_new(request): if request.method == "POST": try: form = CommentForm(data=request.POST) if form.is_valid(): comment = form.save(commit=False) #comment.post=post comment.author = Author.objects.get(user=request.user.id) postid = request.POST.get("post_id", "") #print("post_id: %s"%postid) post = Post.objects.get(post_id=postid) #print("post: %s"%post) comment.post = post comment.pub_date = timezone.now() if comment.contentType == 'text/x-markdown': comment.comment_text = CommonMark.commonmark(comment.comment_text) comment.save() return redirect('../../') except: form = CommentForm(data=request.POST) if form.is_valid(): # TODO: FIX THIS, CHECK FOR OR CREATE GLOBAL AUTHOR # MUST ALSO PASS IN THE HOST SO WE CAN RETRIEVE THE CORRECT NODE # Get the author of the comment #try: author = Author.objects.get(user=request.user.id) #except: # author, status = GlobalAuthor.objects.get_or_create(user=request.user.id) # Create the author dictionary object author_dict = { "id": author.author_id, "host": author.host, "displayName": author.user.username, # todo: get the real user name "url": author.url, "github": author.github } # Create the comment dictionary object if request.POST.get("contentType") == 'text/x-markdown': comment = CommonMark.commonmark(request.POST.get("comment_text")) else: comment = request.POST.get("comment_text") comment = { "author": author_dict, "comment": comment, "contentType": request.POST.get("contentType"), "id": uuid.uuid4().hex, "published": str(datetime.now()) } # Get the node url node_name = request.POST.get("node_name") try: node = Node.objects.get(node_name = node_name) except: node = Node.objects.get(node_name = "Team 6") node_url = node.node_url # Get the node's authentication token auth_token = "Basic " + str(node.basic_auth_token) # Get the post id post_id = request.POST.get("post_id") # Build the URL to post to url = node_url + "posts/" + post_id + "/comments/" #if node_name == 'Team 6': # url += '/' print 'URL: ' print url # Create the request object req = urllib2.Request(url) req.add_header('Content-Type', 'application/json') req.add_header('Authorization', auth_token) # Jsonify the request json_request = json.dumps(comment) #print json_request # Post to the node urllib2.urlopen(req, json_request) return redirect('../../') else: form = CommentForm() return render(request, 'authors/index.html', {'form': form})
def __init__(self, body, encoding='UTF-8'): self.plain_version = body import CommonMark html = CommonMark.commonmark(body) html = self._template.format(markup=html) super().__init__(html, encoding)
def get(self): parser = PageCreator('index.md') with open('index.md') as open_md: self.write(parser.preamble + cm.commonmark(open_md.read()) + parser.closing)
def create_comment(request): # text/x-markdown # text/plain comment = request.POST.get('comment-input') parent_id = request.POST.get('comment-parent-id') is_markdown = request.POST.get('comment-is-markdown',default='off') origin = request.POST.get('comment-parent-origin') if is_markdown == 'on': comment = CommonMark.commonmark(comment) is_markdown = True else: is_markdown = False #post_object = Post.objects.get(id=parent_id) c_username = request.user.username user_object = User.objects.get(username=c_username) author_object = Author.objects.get(email=user_object) #author_name = author_object.displayName # new_comment = Comment(author_id = author_object, id = post_object, comment = comment, is_markdown = is_markdown, published=published) # print("comment made") packet = {} packet['comment'] = comment if is_markdown: packet['contentType'] = 'text/x-markdown' else: packet['contentType'] = 'text/plain' packet['published'] = str(datetime.now()) packet['author'] = {} packet['author']['id'] = str(author_object.id) packet['author']['host'] = request.get_host() packet['author']['displayName'] = author_object.displayName packet['author']['url'] = author_object.url packet['author']['github'] = "http://github.com/" + author_object.github json_packet = json.dumps(packet) # WE COULD USE THIS IF THEY GAVE US ORIGIN INSTEAD OF AN EMPTY STRING # if "origin":"http://whereitcamefrom.com/api/posts/zzzzz", # url1 = origin + "/comments/" # janky stuff, we should just fix our api for the url if 'ditto-test' in origin: url1 = origin + "/comments/" elif 'mighty' not in origin: url1 = "http://project-c404.rhcloud.com/api/posts/" + parent_id + "/comments/" else: url1 = origin + "/comments/" # + "/api/posts/" + parent_id + "/comments" print url1 print json_packet # this works for posting a comment to ourselves #url1 = "http://" + request.get_host() + "/api/posts/" + parent_id + "/comments/" req = urllib2.Request(url1) req.add_header('Content-Type', 'application/json') foreign_hosts = ForeignHost.objects.filter() if 'ditto-test' in origin: base64string = base64.encodestring('%s:%s' % ("admin", "pass")).replace('\n', '') req.add_header("Authorization", "Basic %s" % base64string) else: for host in foreign_hosts: if host.url in origin: base64string = base64.encodestring('%s:%s' % (host.username, host.password)).replace('\n', '') req.add_header("Authorization", "Basic %s" % base64string) urllib2.urlopen(req, json_packet) # new_comment.save() return redirect('/feed')
def test_output(self): s = CommonMark.commonmark('*hello!*') self.assertEqual(s, '<p><em>hello!</em></p>\n')
def markdown(content): """ A Django template filter to render the given content as Markdown. """ return CommonMark.commonmark(content)
import CommonMark parser = CommonMark.Parser() renderer = CommonMark.HtmlRenderer() ast = parser.parse("Hello *World*") html = renderer.render(ast) # json = CommonMark.ASTtoJSON(ast) # CommonMark.dumpAST(ast) print(html) # with syntactic sugar myhtml = CommonMark.commonmark("Hello *goodbye* I see *I try*!") print(myhtml)
def markdown(src): return mark_safe(CommonMark.commonmark(force_text(src)))
help="Input Markdown file to parse, defaults to STDIN") parser.add_argument( '-o', nargs="?", type=argparse.FileType('w'), default=sys.stdout, help="Output HTML/JSON file, defaults to STDOUT") parser.add_argument('-a', action="store_true", help="Print formatted AST") parser.add_argument('-aj', action="store_true", help="Output JSON AST") args = parser.parse_args() parser = CommonMark.Parser() f = args.infile o = args.o lines = [] for line in f: lines.append(line) data = "".join(lines) ast = parser.parse(data) if not args.a and not args.aj: renderer = CommonMark.HtmlRenderer() o.write(renderer.render(ast)) exit() if args.a: # print ast CommonMark.dumpAST(ast) exit() # o.write(ast.to_JSON()) o.write(CommonMark.ASTtoJSON(ast)) exit()
def test_null_string_bug(self): s = CommonMark.commonmark('> sometext\n>\n\n') self.assertEqual( s, '<blockquote>\n<pre><code>sometext\n</code></pre>' '\n</blockquote>\n')
def _parse_node(self, node): children = node.children + node.inline_content if node.t in ("ATXHeader", "SetextHeader"): obj = Style(name="Heading {0}".format(node.level)) elif node.t == "Paragraph": obj = Paragraph() elif node.t == "Str": obj = Text(text=node.c) elif node.t in ("Softbreak", "Hardbreak"): obj = Text(text=" ") elif node.t == "Emph": obj = Italic() elif node.t == "Strong": obj = Bold() elif node.t == "Image": caption = node.label[0].c if len(node.label) else "" obj = Image(location=node.destination, caption=caption) elif node.t == "HtmlBlock": # Special case. Parse the HTML into instructions instructions = self.html_parser.parse(node.string_content) if not instructions: obj = IgnoredOperation() elif len(instructions) == 1: # Only contains one instruction, carry on as normal return instructions[0] else: # Lots of instructions. Return a group return Group(instructions) elif node.t == "List": obj = BulletList() if node.list_data["type"] == "Bullet" else NumberedList() elif node.t == "ListItem": obj = ListElement() elif node.t == "Link": obj = HyperLink(location=node.destination) children.extend(node.label) elif node.t in ("ReferenceDef", "HorizontalRule"): # ToDo: handle markdown references obj = IgnoredOperation() elif node.t == "Code": # Need an inline code object obj = InlineCode([Text(text=node.c)]) elif node.t == "IndentedCode": obj = CodeBlock([Text(text=node.string_content)]) else: CommonMark.dumpAST(node) raise ParseException("Cannot process node type {0}".format(node.t)) if isinstance(node.c, list): children = children + node.c for child in children: result = self._parse_node(child) if result is None: continue obj.add_child(result) return obj
def comments(request, uuid): if request.method == 'GET': # Does the post actually exist? try: p = Post.objects.get(post_id=uuid) except: return Response(status=status.HTTP_404_NOT_FOUND) # Default if not given page_num = request.GET.get('page', DEFAULT_PAGE_NUM) page_size = request.GET.get('size', DEFAULT_PAGE_SIZE) # Get all local and global comments, combine and paginate results local_comments = Comment.objects.filter(post=p) global_comments = GlobalComment.objects.filter(post=p) all_comments = sorted( chain(local_comments, global_comments), key=attrgetter('pub_date')) # Need this here or else the pagination bitches about it being unicode page_num = int(page_num) # Get the right page pages = Paginator(all_comments, page_size) page = pages.page(page_num+1) data = page.object_list response_obj = {} response_obj['query'] = 'comments' response_obj['count'] = len(all_comments) response_obj['size'] = page_size if page.has_next(): response_obj['next'] = settings.LOCAL_HOST + 'api/posts/' + uuid + '/comments?page=' + str(page_num + 1) + '&size=' + str(page_size) if page.has_previous(): response_obj['previous'] = settings.LOCAL_HOST + 'api/posts/' + uuid + '/comments?page=' + str(page_num - 1) + '&size=' + str(page_size) serializer = CommentSerializer(data, many=True) response_obj['comments'] = serializer.data return Response(response_obj, status=status.HTTP_200_OK) elif request.method == 'POST': try: try: # Get an existing global author author = GlobalAuthor.objects.get(global_author_id=request.data['author']['id']) print 'Found existing global author...' except: # Create a new global author global_author_name = request.data['author']['displayName'] print 'Creating new global author' id = request.data['author']['id'] url = request.data['author']['url'] host = request.data['author']['host'] author = GlobalAuthor(global_author_name = global_author_name, url = url, host = host) author.save(); print 'Successfully created author' post = Post.objects.get(post_id=uuid) print 'Found a post...' comment = GlobalComment(author=author, post=post) print 'Initialized a comment..' if request.data['contentType'] == 'text/x-markdown': markedOne= CommonMark.commonmark(request.data['comment']) comment.comment_text = markedOne else: comment.comment_text = request.data['comment'] print 'added text..' comment.contentType = request.data['contentType'] print 'Added comment type' comment.save() print 'Successfully created comment' return Response("Comment Successfully Added.", status=status.HTTP_201_CREATED) except: return Response("Comment not added, bad JSON format.", status=status.HTTP_400_BAD_REQUEST)