Example #1
0
def embed_snippet(request, snippetid):
    """
        Get a javascript code for pasting snippet anywhere on the web.

        Processes GET and POST requests.

        Snippet id is specified as url path (part between the host name and params), i.e.:
            GET xsnippet.org/1/embed will return js code for pasting snippet on your page
    """
    snippet = Snippet.get_by_id(int(snippetid))

    if snippet is not None:
        languagehl = Snippet.languages[snippet.language]
        if languagehl:
            lexer = get_lexer_by_name(languagehl, stripall=True)
        else:
            lexer = guess_lexer(snippet.content)

        formatter = HtmlFormatter(linenos="table")
        snippet.content = highlight(snippet.content, lexer, formatter)

        html = """
          <link rel="stylesheet" href="http://www.xsnippet.org/static/pygments/styles/colorful.css">
          <link rel="stylesheet" href="http://www.xsnippet.org/static/styles/embed.css">
          {0}
        """.format(
            snippet.content
        )

        js = "document.write('{0}');".format(r"\n".join(html.splitlines()))
        return create_response({"Content-Type": "text/html; charset=utf-8"}, js)
    else:
        return webapp2.abort(404, "Snippet with id {0} not found".format(snippetid))
Example #2
0
def png_snippet(request, snippetid):
    """
        Show image (png) with highlighted code of snippet.

        Processes GET and POST requests.

        Snippet id is specified as url path (part between the host name and params), i.e.:
            GET xsnippet.org/1/png will return image for snippet with id 1
    """
    snippet = Snippet.get_by_id(int(snippetid))

    if snippet is not None:
        # pygments highlighting
        languagehl = Snippet.languages[snippet.language]

        if languagehl:
            lexer = get_lexer_by_name(languagehl, stripall=True)
        else:
            lexer = guess_lexer(snippet.content)

        formatter = ImageFormatter(font_name="Ubuntu Mono")
        png = highlight(snippet.content, lexer, formatter)

        return create_response({"Content-Type": "image/png"}, png)
    else:
        return webapp2.abort(404, "Snippet with id {0} not found".format(snippetid))
Example #3
0
def show_snippet(request, snippetid):
    """
        Show the highlighted code of snippet and additional information

        Processes GET and POST requests.

        Snippet id is specified as url path (part between the host name and params), i.e.:
            GET xsnippet.org/1 will return page for snippet with id 1
    """

    snippet = Snippet.get_by_id(int(snippetid))

    if snippet is not None:
        # pygments highlighting
        languagehl = Snippet.languages.get(snippet.language, "text")

        if languagehl:
            lexer = get_lexer_by_name(languagehl, stripall=True)
        else:
            lexer = guess_lexer(snippet.content)

        formatter = HtmlFormatter(linenos="table")
        snippet.content = highlight(snippet.content, lexer, formatter)
        return render_to_response("show.html", snippet=snippet)
    else:
        return webapp2.abort(404, "Snippet with id {0} not found".format(snippetid))
Example #4
0
def raw_snippet(request, snippetid):
    """
        Get a raw text representation of snippet content

        Processes GET and POST requests.

        Snippet id is specified as url path (part between the host name and params), i.e.:
            GET xsnippet.org/1/raw will return text content of snippet with id 1
    """
    snippet = Snippet.get_by_id(int(snippetid))

    if snippet is not None:
        return create_response({"Content-Type": "text/plain; charset=utf-8"}, snippet.content)
    else:
        return webapp2.abort(404, "Snippet with id {0} not found".format(snippetid))
Example #5
0
def download_snippet(request, snippetid):
    """
        Download snippet content as a file

        Processes GET and POST requests.

        Snippet id is specified as url path (part between the host name and params), i.e.:
            GET xsnippet.org/1/download will return content of snippet with id 1 as a file
    """
    snippet = Snippet.get_by_id(int(snippetid))

    if snippet is not None:
        filename = snippetid
        extension = snippet.extensions[snippet.language] if snippet.language in snippet.extensions else ".txt"
        attachment = 'attachment; filename="{0}{1}"'.format(filename, extension)

        return create_response(
            {"Content-Type": "text/plain; charset=utf-8", "Content-Disposition": attachment}, snippet.content
        )
    else:
        return webapp2.abort(404, "Snippet with id {0} not found".format(snippetid))