예제 #1
0
 def save(self, *args, **kwargs):
     if not self.slug:
         self.slug = slugify_uniquely(self.name, User, "username")
     return super(Team, self).save(*args, **kwargs)
예제 #2
0
파일: models.py 프로젝트: ScriptSB/snipt
    def save(self, *args, **kwargs):

        if not self.slug:
            self.slug = slugify_uniquely(self.title, Snipt)

        if not self.key:
            self.key = hashlib.md5((self.slug +
                                    str(datetime.datetime.now()) +
                                    str(random.random())).encode('utf-8')).hexdigest()

        if self.lexer == 'markdown':
            self.stylized = markdown(self.code, 'default')

            # Snipt embeds
            for match in re.findall('\[\[(\w{32})\]\]', self.stylized):
                self.stylized = self.stylized.replace('[[' + str(match) + ']]',
                                                      """
                        <script type="text/javascript"
                                   src="https://snipt.net/embed/{}/?snipt">
                           </script>
                       <div id="snipt-embed-{}"></div>""".format(match, match))

            # YouTube embeds
            for match in re.findall('\[\[youtube-(\w{11})\-(\d+)x(\d+)\]\]',
                                    self.stylized):
                self.stylized = self.stylized \
                    .replace('[[youtube-{}-{}x{}]]'.format(
                        str(match[0]),
                        str(match[1]),
                        str(match[2])),
                        """<iframe width="{}" height="{}"
                           src="https://www.youtube.com/embed/{}"
                           frameborder="0" allowfullscreen></iframe>"""
                        .format(match[1], match[2], match[0]))

            # Vimeo embeds
            for match in re.findall('\[\[vimeo-(\d+)\-(\d+)x(\d+)\]\]',
                                    self.stylized):
                self.stylized = self.stylized \
                    .replace('[[vimeo-{}-{}x{}]]'.format(
                        str(match[0]),
                        str(match[1]),
                        str(match[2])),
                        """<iframe src="https://player.vimeo.com/video/{}"
                           width="{}" height="{}" frameborder="0"
                           webkitAllowFullScreen mozallowfullscreen
                           allowFullScreen></iframe>"""
                        .format(match[0], match[1], match[2]))

            # Tweet embeds
            for match in re.findall('\[\[tweet-(\d+)\]\]', self.stylized):
                self.stylized = self.stylized \
                    .replace(
                        '[[tweet-{}]]'.format(str(match)),
                        '<div class="embedded-tweet" data-tweet-id="{}"></div>'
                        .format(str(match)))

            # Parse Snipt usernames
            for match in re.findall('@(\w+) ', self.stylized):

                # Try and get the Snipt user by username.
                user = get_object_or_None(User, username=match)

                if user:
                    url = user.profile.get_user_profile_url()
                    self.stylized = self.stylized \
                        .replace('@{} '.format(str(match)),
                                 '<a href="{}">@{}</a> '.format(url, match))

        else:
            self.stylized = highlight(self.code,
                                      get_lexer_by_name(self.lexer,
                                                        encoding='UTF-8'),
                                      HtmlFormatter(linenos='table',
                                                    anchorlinenos=True,
                                                    lineanchors='L',
                                                    linespans='L',
                                                    ))
        self.line_count = len(self.code.split('\n'))

        if self.lexer == 'markdown':
            lexer_for_embedded = 'text'
        else:
            lexer_for_embedded = self.lexer

        embedded = highlight(self.code,
                             get_lexer_by_name(lexer_for_embedded,
                                               encoding='UTF-8'),
                             HtmlFormatter(
                                 style='native',
                                 noclasses=True,
                                 prestyles="""
                                     background-color: #1C1C1C;
                                     border-radius: 5px;
                                     color: #D0D0D0;
                                     display: block;
                                     font: 11px Monaco, monospace;
                                     margin: 0;
                                     overflow: auto;
                                     padding: 15px;
                                     -webkit-border-radius: 5px;
                                     -moz-border-radius: 5px;
                                     """))
        embedded = (embedded.replace("\\\"", "\\\\\"")
                            .replace('\'', '\\\'')
                            .replace("\\", "\\\\")
                            .replace('background: #202020', ''))
        self.embedded = embedded

        snipt = super(Snipt, self).save(*args, **kwargs)

        diff = self._unidiff_output(self.original_code or '', self.code)

        if (diff != ''):
            log_entry = SniptLogEntry(user=self.last_user_saved,
                                      snipt=self,
                                      code=self.code,
                                      diff=diff)
            log_entry.save()

        return snipt
예제 #3
0
파일: models.py 프로젝트: MSylvia/snipt
    def save(self, *args, **kwargs):

        if not self.slug:
            self.slug = slugify_uniquely(self.title, Snipt)

        if not self.key:
            self.key = md5.new(self.slug + str(datetime.datetime.now()) + str(random.random())).hexdigest()

        if self.lexer == 'markdown':
            self.stylized = markdown(self.code, 'default')

            # Snipt embeds
            for match in re.findall('\[\[(\w{32})\]\]', self.stylized):
                self.stylized = self.stylized.replace('[[' + str(match) + ']]',
                    '<script type="text/javascript" src="https://snipt.net/embed/{}/?snipt"></script><div id="snipt-embed-{}"></div>'.format(match, match))

            # YouTube embeds
            for match in re.findall('\[\[youtube-(\w{11})\-(\d+)x(\d+)\]\]', self.stylized):
                self.stylized = self.stylized.replace('[[youtube-{}-{}x{}]]'.format(str(match[0]), str(match[1]), str(match[2])),
                    '<iframe width="{}" height="{}" src="https://www.youtube.com/embed/{}" frameborder="0" allowfullscreen></iframe>'.format(match[1], match[2], match[0]))

            # Vimeo embeds
            for match in re.findall('\[\[vimeo-(\d+)\-(\d+)x(\d+)\]\]', self.stylized):
                self.stylized = self.stylized.replace('[[vimeo-{}-{}x{}]]'.format(str(match[0]), str(match[1]), str(match[2])),
                    '<iframe src="https://player.vimeo.com/video/{}" width="{}" height="{}" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'.format(match[0], match[1], match[2]))

        else:
            self.stylized = highlight(self.code,
                                      get_lexer_by_name(self.lexer, encoding='UTF-8'),
                                      HtmlFormatter(linenos='table', linenospecial=1, lineanchors='line'))
        self.line_count = len(self.code.split('\n'))

        if self.lexer == 'markdown':
            lexer_for_embedded = 'text'
        else:
            lexer_for_embedded = self.lexer

        embedded = highlight(self.code,
                             get_lexer_by_name(lexer_for_embedded, encoding='UTF-8'),
                             HtmlFormatter(
                                 style='native',
                                 noclasses=True,
                                 prestyles="""
                                     background-color: #1C1C1C;
                                     border-radius: 5px;
                                     color: #D0D0D0;
                                     display: block;
                                     font: 11px Monaco, monospace;
                                     margin: 0;
                                     overflow: auto;
                                     padding: 15px;
                                     -webkit-border-radius: 5px;
                                     -moz-border-radius: 5px;
                                     """))
        embedded = (embedded.replace("\\\"","\\\\\"")
                            .replace('\'','\\\'')
                            .replace("\\", "\\\\")
                            .replace('background: #202020', ''))
        self.embedded = embedded

        return super(Snipt, self).save(*args, **kwargs)
예제 #4
0
    def save(self, *args, **kwargs):

        if not self.slug:
            self.slug = slugify_uniquely(self.title, Snipt)

        if not self.key:
            self.key = md5.new(self.slug + str(datetime.datetime.now()) +
                               str(random.random())).hexdigest()

        if self.lexer == 'markdown':
            self.stylized = markdown(self.code, 'default')

            # Snipt embeds
            for match in re.findall('\[\[(\w{32})\]\]', self.stylized):
                self.stylized = self.stylized.replace(
                    '[[' + str(match) + ']]',
                    '<script type="text/javascript" src="https://snipt.net/embed/{}/?snipt"></script><div id="snipt-embed-{}"></div>'
                    .format(match, match))

            # YouTube embeds
            for match in re.findall('\[\[youtube-(\w{11})\-(\d+)x(\d+)\]\]',
                                    self.stylized):
                self.stylized = self.stylized.replace(
                    '[[youtube-{}-{}x{}]]'.format(str(match[0]), str(match[1]),
                                                  str(match[2])),
                    '<iframe width="{}" height="{}" src="https://www.youtube.com/embed/{}" frameborder="0" allowfullscreen></iframe>'
                    .format(match[1], match[2], match[0]))

            # Vimeo embeds
            for match in re.findall('\[\[vimeo-(\d+)\-(\d+)x(\d+)\]\]',
                                    self.stylized):
                self.stylized = self.stylized.replace(
                    '[[vimeo-{}-{}x{}]]'.format(str(match[0]), str(match[1]),
                                                str(match[2])),
                    '<iframe src="https://player.vimeo.com/video/{}" width="{}" height="{}" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'
                    .format(match[0], match[1], match[2]))

            # Tweet embeds
            for match in re.findall('\[\[tweet-(\d+)\]\]', self.stylized):
                self.stylized = self.stylized.replace(
                    '[[tweet-{}]]'.format(str(match)),
                    '<div class="embedded-tweet" data-tweet-id="{}"></div>'.
                    format(str(match)))

            # Parse Snipt usernames
            for match in re.findall('@(\w+) ', self.stylized):

                # Try and get the Snipt user by username.
                user = get_object_or_None(User, username=match)

                if user:
                    url = user.profile.get_user_profile_url()
                    self.stylized = self.stylized.replace(
                        '@{} '.format(str(match)),
                        '<a href="{}">@{}</a> '.format(url, match))

        else:
            self.stylized = highlight(
                self.code, get_lexer_by_name(self.lexer, encoding='UTF-8'),
                HtmlFormatter(
                    linenos='table',
                    anchorlinenos=True,
                    lineanchors='L',
                    linespans='L',
                ))
        self.line_count = len(self.code.split('\n'))

        if self.lexer == 'markdown':
            lexer_for_embedded = 'text'
        else:
            lexer_for_embedded = self.lexer

        embedded = highlight(
            self.code, get_lexer_by_name(lexer_for_embedded, encoding='UTF-8'),
            HtmlFormatter(style='native',
                          noclasses=True,
                          prestyles="""
                                     background-color: #1C1C1C;
                                     border-radius: 5px;
                                     color: #D0D0D0;
                                     display: block;
                                     font: 11px Monaco, monospace;
                                     margin: 0;
                                     overflow: auto;
                                     padding: 15px;
                                     -webkit-border-radius: 5px;
                                     -moz-border-radius: 5px;
                                     """))
        embedded = (embedded.replace("\\\"", "\\\\\"").replace(
            '\'', '\\\'').replace("\\",
                                  "\\\\").replace('background: #202020', ''))
        self.embedded = embedded

        return super(Snipt, self).save(*args, **kwargs)
예제 #5
0
    def save(self, *args, **kwargs):

        if not self.slug:
            self.slug = slugify_uniquely(self.title, Snipt)

        if not self.key:
            self.key = hashlib.md5(
                (self.slug + str(datetime.datetime.now()) +
                 str(random.random())).encode("utf-8")).hexdigest()

        if self.lexer == "markdown":
            self.stylized = markdown(self.code, "default")

            # Snipt embeds
            for match in re.findall('\[\[(\w{32})\]\]', self.stylized):
                self.stylized = self.stylized.replace(
                    '[[' + str(match) + ']]',
                    """
                        <script type="text/javascript"
                                   src="https://snipt.net/embed/{}/?snipt">
                           </script>
                       <div id="snipt-embed-{}"></div>""".format(match, match),
                )

            # YouTube embeds
            for match in re.findall("\[\[youtube-(\w{11})\-(\d+)x(\d+)\]\]",
                                    self.stylized):
                self.stylized = self.stylized.replace(
                    "[[youtube-{}-{}x{}]]".format(str(match[0]), str(match[1]),
                                                  str(match[2])),
                    """<iframe width="{}" height="{}"
                           src="https://www.youtube.com/embed/{}"
                           frameborder="0" allowfullscreen></iframe>""".format(
                        match[1], match[2], match[0]),
                )

            # Vimeo embeds
            for match in re.findall("\[\[vimeo-(\d+)\-(\d+)x(\d+)\]\]",
                                    self.stylized):
                self.stylized = self.stylized.replace(
                    "[[vimeo-{}-{}x{}]]".format(str(match[0]), str(match[1]),
                                                str(match[2])),
                    """<iframe src="https://player.vimeo.com/video/{}"
                           width="{}" height="{}" frameborder="0"
                           webkitAllowFullScreen mozallowfullscreen
                           allowFullScreen></iframe>""".format(
                        match[0], match[1], match[2]),
                )

            # Tweet embeds
            for match in re.findall("\[\[tweet-(\d+)\]\]", self.stylized):
                self.stylized = self.stylized.replace(
                    "[[tweet-{}]]".format(str(match)),
                    '<div class="embedded-tweet" data-tweet-id="{}"></div>'.
                    format(str(match)),
                )

            # Parse Snipt usernames
            for match in re.findall('@(\w+) ', self.stylized):

                # Try and get the Snipt user by username.
                user = get_object_or_None(User, username=match)

                if user:
                    url = user.profile.get_user_profile_url()
                    self.stylized = self.stylized.replace(
                        "@{} ".format(str(match)),
                        '<a href="{}">@{}</a> '.format(url, match),
                    )

        else:
            self.stylized = highlight(
                self.code,
                get_lexer_by_name(self.lexer, encoding="UTF-8"),
                HtmlFormatter(linenos="table",
                              anchorlinenos=True,
                              lineanchors="L",
                              linespans="L"),
            )
        self.line_count = len(self.code.split("\n"))

        if self.lexer == "markdown":
            lexer_for_embedded = "text"
        else:
            lexer_for_embedded = self.lexer

        embedded = highlight(
            self.code,
            get_lexer_by_name(lexer_for_embedded, encoding="UTF-8"),
            HtmlFormatter(
                style="native",
                noclasses=True,
                prestyles="""
                                     background-color: #1C1C1C;
                                     border-radius: 5px;
                                     color: #D0D0D0;
                                     display: block;
                                     font: 11px Monaco, monospace;
                                     margin: 0;
                                     overflow: auto;
                                     padding: 15px;
                                     -webkit-border-radius: 5px;
                                     -moz-border-radius: 5px;
                                     """,
            ),
        )
        embedded = (embedded.replace('\\"', '\\\\"').replace(
            "'", "\\'").replace("\\", "\\\\").replace("background: #202020",
                                                      ""))
        self.embedded = embedded

        snipt = super(Snipt, self).save(*args, **kwargs)

        diff = self._unidiff_output(self.original_code or "", self.code)

        if diff != "":
            log_entry = SniptLogEntry(user=self.last_user_saved,
                                      snipt=self,
                                      code=self.code,
                                      diff=diff)
            log_entry.save()

        return snipt
예제 #6
0
 def save(self, *args, **kwargs):
     if not self.slug:
         self.slug = slugify_uniquely(self.name, User, "username")
     return super(Team, self).save(*args, **kwargs)