def get(self, page_title):
        """When we receive an HTTP Get request to the view pages, we pull that
       page from the datastore and render it.  If the page does not exist
       we pass empty arguments to the template and the template displays
       the option to the user to create the page
    """
        # Attempt to locate the page
        entry = WikiPage.gql('WHERE title = :1', page_title).get()

        # If it exists, render the body, if not, don't
        if entry and entry.body:
            wiki_body, count = _WIKI_WORD.subn(r'<a href="/view/\1" >\1</a>',
                                               entry.body)
            wiki_body = markdown.markdown(wiki_body)
            author = entry.author
        else:
            wiki_body = ""
            author = ""

        self.generate('view.html',
                      template_values={
                          'page_title': page_title,
                          'body': wiki_body,
                          'author': author
                      })
  def get_page_content(self, page_title):
    """When memcache lookup fails, we want to query the information from
       the datastore and return it.  If the data isn't in the data store,
       simply return empty strings
    """
    # Find the wiki entry
    entry = WikiContent.gql('WHERE title = :1', page_title).get()

    if entry:
      # Retrieve the current version
      current_version = WikiRevision.gql('WHERE wiki_page =  :1 '
                                         'ORDER BY version_number DESC', entry).get()
      # Define the body, version number, author email, author nickname
      # and revision date
      body = current_version.revision_body
      version = current_version.version_number
      author_email = urllib.quote(current_version.author.wiki_user.email())
      author_nickname = current_version.author.wiki_user.nickname()
      version_date = current_version.created
      # Replace all wiki words with links to those wiki pages
      wiki_body, count = _WIKI_WORD.subn(r'<a href="/view/\1">\1</a>',
                                         body)
      # Markdown the body to allow formatting of the wiki page
      wiki_body = markdown.markdown(wiki_body)
    else:
      # These things do not exist
      wiki_body = ''
      author_email = ''
      author_nickname = ''
      version = ''
      version_date = ''

    return [wiki_body, author_email, author_nickname, version, version_date]
 def parse(self, path: Path, source: Path, dest: Path):
     content = Content.load(self.read(path))
     html = markdown.markdown(content.body)
     self.write(path, dest, html)
     sys.stdout.write(
         "\x1b[1;32m{} converted to HTML. Metadata: {}\n".format(
             path.name, content))
def markdown_to_text(markdown_string):
    # md -> html -> text since BeautifulSoup can extract text cleanly
    html = markdown.markdown(markdown_string)

    # extract text
    soup = BeautifulSoup(html, "html.parser")
    text = ''.join(soup.findAll(text=True))

    return text
Esempio n. 5
0
def markdown2(value):
    try:
        import markdown
    except ImportError:
        if settings.DEBUG:
            raise template.TemplateSyntaxError("Error in 'markdonw' filter: The Python markdown2 library isn't install.")
        return force_text(value)
    else:
        return mark_safe(markdown.markdown(value,
                                       extensions=['markdown.extensions.fenced_code', 'markdown.extensions.codehilite'],
                                       safe_mode=True,
                                       enable_attributes=False))
Esempio n. 6
0
    def get(self, slug=None):
        # look up the article
        article = models.Article.get_by_slug(slug)
        if not article:
            return self.render_template('errors/default_error.html')

        # fetch the content from ourselves
        http = httplib2.Http(cache=None, timeout=None, proxy_info=None)
        uri = "http://%s/assets/blog/%s" % (self.request.host,
                                            article.filename)
        response, content = http.request(uri,
                                         method="GET",
                                         body=None,
                                         headers=None)

        # if the article wasn't found
        if response['status'] == '404':
            return self.render_template('errors/default_error.html')

        # fetch the user's info who wrote the article
        owner_info = models.User.get_by_id(article.owner.id())
        if not owner_info.name:
            article_owner = owner_info.username
        else:
            article_owner = owner_info.name

        # build gravatar URL
        gravatar_hash = md5.new(owner_info.email.lower().strip()).hexdigest()
        article_gravatar_url = "https://www.gravatar.com/avatar/%s?s=48" % gravatar_hash

        # date format
        date_format = "%A, %d %b %Y"
        article_created = article.created.strftime(date_format)

        # create markdown and sanitize
        article_html = markdown.markdown(content)
        article_html = bleach.clean(article_html, config.bleach_tags,
                                    config.bleach_attributes)

        # load page content into params
        params = {
            'article_created': article_created,
            'article_html': article_html,
            'article_slug': article.slug,
            'article_title': article.title,
            'article_type': article.article_type,
            'article_owner': article_owner,
            'article_gravatar_url': article_gravatar_url,
            'article_host': self.request.host,
        }
        return self.render_template('blog/detail.html', **params)
Esempio n. 7
0
    def post(self, page_title):
        # Again, only accept saves from a signed in user
        current_user = users.get_current_user()

        if not current_user:
            self.redirect(users.create_login_url('/edit/' + page_title))

        # See if this user has a profile
        wiki_user = WikiUser.gql('WHERE wiki_user = :1', current_user).get()

        # If not, create the profile
        if not wiki_user:
            wiki_user = WikiUser(wiki_user=current_user)
            wiki_user.put()

        # get the user entered content in the form
        body = self.request.get('body')

        # Find the entry, if it exists
        entry = WikiContent.gql('WHERE title = :1', page_title).get()

        # Generate the version number based on the entries previous existence
        if entry:
            latest_version = WikiRevision.gql(
                'WHERE wiki_page = :content'
                ' ORDER BY version_number DESC',
                content=entry).get()
            version_number = latest_version.version_number + 1
        else:
            version_number = 1
            entry = WikiContent(title=page_title)
            entry.put()

        # Create a version for this entry
        version = WikiRevision(version_number=version_number,
                               revision_body=body,
                               author=wiki_user,
                               wiki_page=entry)
        version.put()

        # above, memcache sets the following:
        # return [wiki_body, author_email, author_nickname, version, version_date]
        content = [
            markdown.markdown(body),
            current_user.email(),
            current_user.nickname(), version_number, version.created
        ]
        memcache.set(page_title, content, 600)
        # After the entry has been saved, direct the user back to view the page
        self.redirect('/view/' + page_title)
    def get(self, page_title):
        """When we receive an HTTP Get request to the view pages, we pull that
       page from the datastore and render it.  If the page does not exist
       we pass empty arguments to the template and the template displays
       the option to the user to create the page
    """
        # Find the wiki entry
        entry = WikiContent.gql('WHERE title = :1', page_title).get()

        if entry:
            # Retrieve the current version
            current_version = WikiRevision.gql(
                'WHERE wiki_page =  :1 '
                'ORDER BY version_number DESC', entry).get()
            # Define the body, version number, author email, author nickname
            # and revision date
            body = current_version.revision_body
            version = current_version.version_number
            author_email = urllib.quote(
                current_version.author.wiki_user.email())
            author_nickname = current_version.author.wiki_user.nickname()
            version_date = current_version.created
            # Replace all wiki words with links to those wiki pages
            wiki_body, count = _WIKI_WORD.subn(r'<a href="/view/\1">\1</a>',
                                               body)
            # Markdown the body to allow formatting of the wiki page
            wiki_body = markdown.markdown(wiki_body)

        else:
            # These things do not exist
            wiki_body = ''
            author_email = ''
            author_nickname = ''
            version = ''
            version_date = ''

        # Render the template view.html, which extends base.html
        self.generate('view.html',
                      template_values={
                          'page_title': page_title,
                          'body': wiki_body,
                          'author': author_nickname,
                          'author_email': author_email,
                          'version': version,
                          'version_date': version_date
                      })
  def post(self, page_title):
    # Again, only accept saves from a signed in user
    current_user = users.get_current_user()

    if not current_user:
      self.redirect(users.create_login_url('/edit/' + page_title))

    # See if this user has a profile
    wiki_user = WikiUser.gql('WHERE wiki_user = :1', current_user).get()

    # If not, create the profile
    if not wiki_user:
      wiki_user = WikiUser(wiki_user=current_user)
      wiki_user.put()

    # get the user entered content in the form
    body = self.request.get('body')

    # Find the entry, if it exists
    entry = WikiContent.gql('WHERE title = :1', page_title).get()

    # Generate the version number based on the entries previous existence
    if entry:
      latest_version = WikiRevision.gql('WHERE wiki_page = :content'
                                        ' ORDER BY version_number DESC', content=entry).get()
      version_number = latest_version.version_number + 1
    else:
      version_number = 1
      entry = WikiContent(title=page_title)
      entry.put()

    # Create a version for this entry
    version = WikiRevision(version_number=version_number,
                           revision_body=body, author=wiki_user,
                           wiki_page=entry)
    version.put()

    # above, memcache sets the following:
    # return [wiki_body, author_email, author_nickname, version, version_date]
    content = [markdown.markdown(body), current_user.email(), 
               current_user.nickname(), version_number, version.created]
    memcache.set(page_title, content, 600)
    # After the entry has been saved, direct the user back to view the page
    self.redirect('/view/' + page_title)
Esempio n. 10
0
    def get_page_content(self, page_title, revision_number=1):
        """When memcache lookup fails, we want to query the information from
       the datastore and return it.  If the data isn't in the data store,
       simply return empty strings
    """
        # Find the wiki entry
        entry = WikiContent.gql('WHERE title = :1', page_title).get()

        if entry:
            # Retrieve the current version
            if revision_number is not None:
                requested_version = WikiRevision.gql(
                    'WHERE wiki_page =  :1 '
                    'AND version_number = :2', entry,
                    int(revision_number)).get()
            else:
                requested_version = WikiRevision.gql(
                    'WHERE wiki_page =  :1 '
                    'ORDER BY version_number DESC', entry).get()
            # Define the body, version number, author email, author nickname
            # and revision date
            body = requested_version.revision_body
            version = requested_version.version_number
            author_email = urllib.quote(
                requested_version.author.wiki_user.email())
            author_nickname = requested_version.author.wiki_user.nickname()
            version_date = requested_version.created
            # Replace all wiki words with links to those wiki pages
            wiki_body, count = _WIKI_WORD.subn(r'<a href="/view/\1">\1</a>',
                                               body)
            # Markdown the body to allow formatting of the wiki page
            wiki_body = markdown.markdown(wiki_body)
        else:
            # These things do not exist
            wiki_body = ''
            author_email = ''
            author_nickname = ''
            version = ''
            version_date = ''

        return [
            wiki_body, author_email, author_nickname, version, version_date
        ]
  def get(self, page_title):
    """When we receive an HTTP Get request to the view pages, we pull that
       page from the datastore and render it.  If the page does not exist
       we pass empty arguments to the template and the template displays
       the option to the user to create the page
    """
    # Find the wiki entry
    entry = WikiContent.gql('WHERE title = :1', page_title).get()

    if entry:
      # Retrieve the current version
      current_version = WikiRevision.gql('WHERE wiki_page =  :1 '
                                         'ORDER BY version_number DESC', entry).get()
      # Define the body, version number, author email, author nickname
      # and revision date
      body = current_version.revision_body
      version = current_version.version_number
      author_email = urllib.quote(current_version.author.wiki_user.email())
      author_nickname = current_version.author.wiki_user.nickname()
      version_date = current_version.created
      # Replace all wiki words with links to those wiki pages
      wiki_body, count = _WIKI_WORD.subn(r'<a href="/view/\1">\1</a>',
                                         body)
      # Markdown the body to allow formatting of the wiki page
      wiki_body = markdown.markdown(wiki_body)

    else:
      # These things do not exist
      wiki_body = ''
      author_email = ''
      author_nickname = ''
      version = ''
      version_date = ''

    # Render the template view.html, which extends base.html
    self.generate('view.html', template_values={'page_title': page_title,
                                                'body': wiki_body,
                                                'author': author_nickname,
                                                'author_email': author_email,
                                                'version': version,
                                                'version_date': version_date})
 def get(self, page_title):
   """When we receive an HTTP Get request to the view pages, we pull that
      page from the datastore and render it.  If the page does not exist
      we pass empty arguments to the template and the template displays
      the option to the user to create the page
   """
   # Attempt to locate the page
   entry = WikiPage.gql('WHERE title = :1', page_title).get()
   
   # If it exists, render the body, if not, don't
   if entry and entry.body:
       wiki_body, count = _WIKI_WORD.subn(r'<a href="/view/\1" >\1</a>', entry.body)
       wiki_body = markdown.markdown(wiki_body)
       author = entry.author
   else:
       wiki_body = ""
       author = ""
   
   self.generate('view.html', template_values={'page_title': page_title,
                                               'body': wiki_body,
                                               'author': author})
Esempio n. 13
0
    def get(self):
        # load articles in from db and then stuff them in an array
        date_format = "%A, %d %b %Y"
        articles = models.Article.get_all()

        # http connection start
        http = httplib2.Http(cache=None, timeout=None, proxy_info=None)

        blogposts = []

        # loop through articles
        for article in articles:
            # fetch the content from ourselves
            uri = "http://%s/assets/blog/%s" % (self.request.host,
                                                article.filename)
            response, content = http.request(uri,
                                             method="GET",
                                             body=None,
                                             headers=None)

            if content:
                # create markdown and sanitize
                article_html = bleach.clean(markdown.markdown(content),
                                            config.bleach_tags,
                                            config.bleach_attributes)
                article_title = bleach.clean(article.title)

                # created when and by whom
                article_created = article.created.strftime(date_format)
                owner_info = models.User.get_by_id(article.owner.id())

                # load name
                try:
                    if not owner_info.name:
                        article_owner = owner_info.username
                    else:
                        article_owner = owner_info.name
                except:
                    article_owner = "StackMonkey"

                # build gravatar URL
                try:
                    gravatar_hash = md5.new(
                        owner_info.email.lower().strip()).hexdigest()
                except:
                    gravatar_hash = md5.new(
                        config.app_email.strip()).hexdigest()
                article_gravatar_url = "https://www.gravatar.com/avatar/%s?s=48" % gravatar_hash

                # build entry
                entry = {
                    'article_created': article_created,
                    'article_id': article.key.id(),
                    'article_title': article_title,
                    'article_type': article.article_type,
                    'article_html': article_html,
                    'article_slug': article.slug,
                    'article_owner': article_owner,
                    'article_gravatar_url': article_gravatar_url,
                    'article_host': self.request.host,
                }

                # append article if it's a post and not a draft
                if article.article_type == 'post' and not article.draft:
                    blogposts.append(entry)

        # pack and stuff into template
        params = {'blogposts': blogposts}
        return self.render_template('blog/blog.html', **params)
Esempio n. 14
0
 def save(self, *args, **kwargs):
     if not self.slug:
         self.slug = slugify(self.firstname + self.lastname + self.resort)
     import markdown
     self.description = markdown.markdown(self.markdown_description)
     super(Profile, self).save(*args, **kwargs)
Esempio n. 15
0
                newzip.write(f, arcname=f.relative_to(probdir))

        tmp.seek(0)

        data = tmp.read()
        m = hashlib.sha256()
        m.update(data)
        datahash = m.hexdigest()

        # convert task
        statement = ''
        with open(probdir / 'task.md') as f:
            statement = markdown.markdown(
                f.read(),
                extensions=[
                    'markdown.extensions.fenced_code',
                    'markdown.extensions.tables',
                    ExampleExtension(base_path=str(probdir))
                ],
            )

        with conn.cursor() as cursor:
            cursor.execute(
                '''
                insert into problems (name, title, statement, timelimit, testhash, testzip)
                values (%s, %s, %s, %s, %s, %s)
                on conflict(name) do update
                set (title, statement, timelimit, testhash, testzip)
                = (EXCLUDED.title, EXCLUDED.statement, EXCLUDED.timelimit,
                   EXCLUDED.testhash, EXCLUDED.testzip) 
                ''', (name, title, statement, int(
                    timelimit * 1000), datahash, data))