diff --git a/app.py b/app.py index 3f702c1..efcbac6 100644 --- a/app.py +++ b/app.py @@ -9,6 +9,11 @@ import threading import time import shutil from datetime import timedelta +from pygments import highlight +from pygments.lexers import get_lexer_by_name, guess_lexer +from pygments.formatters import HtmlFormatter +from pygments.util import ClassNotFound +import json app = Flask(__name__) UPLOAD_FOLDER = './uploads' @@ -86,7 +91,23 @@ def content(vanity): if target: content_type, content_data = target[1], target[2] if content_type == 'pastebin': - return render_template('content.html', content=content_data, created_at=target[3]) + try: + lexer = guess_lexer(content_data) + language = lexer.aliases[0] + except ClassNotFound: + language = 'text' + lexer = get_lexer_by_name(language) + + formatter = HtmlFormatter(style='monokai', linenos=True, cssclass="source") + highlighted_code = highlight(content_data, lexer, formatter) + css = formatter.get_style_defs('.source') + return render_template('content.html', + highlighted_content=highlighted_code, + css=css, + raw_content=content_data, + created_at=target[3], + vanity=vanity, + language=language) elif content_type == 'file': file_path = os.path.join(app.config['UPLOAD_FOLDER'], f'{vanity}_{content_data}') file_info = { @@ -206,7 +227,23 @@ def redirect_vanity(vanity): if target: content_type, content_data = target[1], target[2] if content_type == 'pastebin': - return render_template('content.html', content=content_data, created_at=target[3]) + try: + lexer = guess_lexer(content_data) + language = lexer.aliases[0] + except ClassNotFound: + language = 'text' + lexer = get_lexer_by_name(language) + + formatter = HtmlFormatter(style='monokai', linenos=True, cssclass="source") + highlighted_code = highlight(content_data, lexer, formatter) + css = formatter.get_style_defs('.source') + return render_template('content.html', + highlighted_content=highlighted_code, + css=css, + raw_content=content_data, + created_at=target[3], + vanity=vanity, + language=language) elif content_type == 'file': file_path = os.path.join(app.config['UPLOAD_FOLDER'], f'{vanity}_{content_data}') file_info = { @@ -219,7 +256,7 @@ def redirect_vanity(vanity): elif content_type == 'folder': return redirect(url_for('folder_content', vanity=vanity)) elif content_type == 'url': - return render_template('content.html', url=content_data) + return render_template('content.html', content=content_data, url=content_data) return render_template('404.html'), 404 @app.route('//raw', methods=['GET']) diff --git a/templates/content.html b/templates/content.html index ce24cf5..fdd94eb 100644 --- a/templates/content.html +++ b/templates/content.html @@ -1,152 +1,211 @@ - + - Text + Content
-

Text

-
- {% if content %} - {{ content }} - {% elif url %} -

Redirecting to: {{ url }}

+

Content

+ {% if created_at %} +

Created at: {{ created_at }}

+ {% endif %} + {% if language %} +

Detected Language: {{ language }}

+ {% endif %} +
+ + +
+
+ {% if highlighted_content %} + {{ highlighted_content|safe }} {% else %} -

Content not found.

+
{{ content }}
{% endif %}
- {% if created_at %} -
-

Created at: {{ created_at }}

-
- {% endif %}
+ + + + - - - - diff --git a/templates/file.html b/templates/file.html index e05dde1..58b1a77 100644 --- a/templates/file.html +++ b/templates/file.html @@ -1,143 +1,29 @@ - + - File + File Content
-

File

-
-

File Name: {{ name }}

-

File Size: {{ size }} bytes

-

Last Modified: {{ modified_at }}

-

Download the file

+

File: {{ name }}

+

Size: {{ size }} bytes

+

Modified at: {{ modified_at }}

+
+ + + + - - - diff --git a/templates/folder.html b/templates/folder.html index f516f6d..8916260 100644 --- a/templates/folder.html +++ b/templates/folder.html @@ -1,103 +1,47 @@ - + - Folder Contents + Folder Content
-

Folder Contents

- -
- -

Current folder: /{{ current_folder }}

+

Folder: {{ current_folder }}

+ - - - Download All - - + + + + diff --git a/templates/pastebin.html b/templates/pastebin.html new file mode 100644 index 0000000..cf1720c --- /dev/null +++ b/templates/pastebin.html @@ -0,0 +1,54 @@ + + + + + + Pastebin Content + + + +

Pastebin Content

+

Created at: {{ created_at }}

+

Detected Language: {{ language }}

+
+ + +
+
+ {{ content|safe }} +
+ + + + \ No newline at end of file