feat: gists
This commit is contained in:
parent
890a2a633f
commit
fd2a128d40
4 changed files with 65 additions and 0 deletions
30
app.py
30
app.py
|
|
@ -6,13 +6,18 @@ from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from flask import Flask, jsonify, render_template, send_from_directory
|
from flask import Flask, jsonify, render_template, send_from_directory
|
||||||
|
from pygments import highlight
|
||||||
|
from pygments.formatters import HtmlFormatter
|
||||||
|
from pygments.lexers import TextLexer, get_lexer_for_filename
|
||||||
|
|
||||||
app = Flask(__name__, static_folder=None)
|
app = Flask(__name__, static_folder=None)
|
||||||
|
|
||||||
GIT_REPO_PATH = str(Path.home() / "dev")
|
GIT_REPO_PATH = str(Path.home() / "dev")
|
||||||
|
GIST_PATH = str(Path.home() / "gists")
|
||||||
EXPORT_MARKER = "readme.md"
|
EXPORT_MARKER = "readme.md"
|
||||||
if getpass.getuser() == "ec2-user":
|
if getpass.getuser() == "ec2-user":
|
||||||
GIT_REPO_PATH = "/srv/git"
|
GIT_REPO_PATH = "/srv/git"
|
||||||
|
GIST_PATH = "/srv/gists"
|
||||||
EXPORT_MARKER = "git-daemon-export-ok"
|
EXPORT_MARKER = "git-daemon-export-ok"
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -92,6 +97,31 @@ def serve_scripts(filename):
|
||||||
return send_from_directory("scripts", filename)
|
return send_from_directory("scripts", filename)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/gist/<path:filename>")
|
||||||
|
def serve_gist(filename):
|
||||||
|
file_path = os.path.join(GIST_PATH, filename)
|
||||||
|
|
||||||
|
if not os.path.exists(file_path) or not os.path.isfile(file_path):
|
||||||
|
return "File not found", 404
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(file_path, "r", encoding="utf-8") as f:
|
||||||
|
content = f.read()
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
return "Binary file cannot be displayed", 400
|
||||||
|
|
||||||
|
try:
|
||||||
|
lexer = get_lexer_for_filename(filename)
|
||||||
|
except:
|
||||||
|
lexer = TextLexer()
|
||||||
|
|
||||||
|
formatter = HtmlFormatter(style="default", cssclass="highlight", linenos=True, noclasses=True, cssstyles="padding: 20px; font-size: 18px; background-color: #f8f8f8;")
|
||||||
|
highlighted = highlight(content, lexer, formatter)
|
||||||
|
highlighted = highlighted.replace('<td class="code">', '<td class="code" style="padding-left: 20px;">')
|
||||||
|
|
||||||
|
return render_template("gist.html", filename=filename, highlighted_code=highlighted)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/repo/<int:repo_id>")
|
@app.route("/api/repo/<int:repo_id>")
|
||||||
def get_repo(repo_id):
|
def get_repo(repo_id):
|
||||||
repositories = [repo for repo in get_repositories() if repo.exported]
|
repositories = [repo for repo in get_repositories() if repo.exported]
|
||||||
|
|
|
||||||
|
|
@ -7,4 +7,5 @@ requires-python = ">=3.11"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"flask>=3.1.1",
|
"flask>=3.1.1",
|
||||||
"gunicorn>=23.0.0",
|
"gunicorn>=23.0.0",
|
||||||
|
"pygments>=2.19.1",
|
||||||
]
|
]
|
||||||
|
|
|
||||||
23
templates/gist.html
Normal file
23
templates/gist.html
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
|
<link rel="stylesheet" href="/styles/common.css" />
|
||||||
|
<link rel="stylesheet" href="/styles/index.css" />
|
||||||
|
<link rel="icon" type="image/webp" href="/public/logo.webp" />
|
||||||
|
<title>{{ filename }}</title>
|
||||||
|
</head>
|
||||||
|
<body class="graph-background">
|
||||||
|
<site-header></site-header>
|
||||||
|
<main class="main" style="flex-direction: column; align-items: center;">
|
||||||
|
<h2 style="font-weight: normal; font-size: 3em; align-self: flex-start; margin-left: 40px;">{{ filename }}</h2>
|
||||||
|
<div style="padding: 40px;">
|
||||||
|
{{ highlighted_code|safe }}
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
<site-footer></site-footer>
|
||||||
|
<script src="/scripts/common.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
11
uv.lock
generated
11
uv.lock
generated
|
|
@ -56,12 +56,14 @@ source = { virtual = "." }
|
||||||
dependencies = [
|
dependencies = [
|
||||||
{ name = "flask" },
|
{ name = "flask" },
|
||||||
{ name = "gunicorn" },
|
{ name = "gunicorn" },
|
||||||
|
{ name = "pygments" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.metadata]
|
[package.metadata]
|
||||||
requires-dist = [
|
requires-dist = [
|
||||||
{ name = "flask", specifier = ">=3.1.1" },
|
{ name = "flask", specifier = ">=3.1.1" },
|
||||||
{ name = "gunicorn", specifier = ">=23.0.0" },
|
{ name = "gunicorn", specifier = ">=23.0.0" },
|
||||||
|
{ name = "pygments", specifier = ">=2.19.1" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -154,6 +156,15 @@ wheels = [
|
||||||
{ url = "https://files.pythonhosted.org/packages/20/12/38679034af332785aac8774540895e234f4d07f7545804097de4b666afd8/packaging-25.0-py3-none-any.whl", hash = "sha256:29572ef2b1f17581046b3a2227d5c611fb25ec70ca1ba8554b24b0e69331a484", size = 66469, upload-time = "2025-04-19T11:48:57.875Z" },
|
{ url = "https://files.pythonhosted.org/packages/20/12/38679034af332785aac8774540895e234f4d07f7545804097de4b666afd8/packaging-25.0-py3-none-any.whl", hash = "sha256:29572ef2b1f17581046b3a2227d5c611fb25ec70ca1ba8554b24b0e69331a484", size = 66469, upload-time = "2025-04-19T11:48:57.875Z" },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pygments"
|
||||||
|
version = "2.19.1"
|
||||||
|
source = { registry = "https://pypi.org/simple" }
|
||||||
|
sdist = { url = "https://files.pythonhosted.org/packages/7c/2d/c3338d48ea6cc0feb8446d8e6937e1408088a72a39937982cc6111d17f84/pygments-2.19.1.tar.gz", hash = "sha256:61c16d2a8576dc0649d9f39e089b5f02bcd27fba10d8fb4dcc28173f7a45151f", size = 4968581, upload-time = "2025-01-06T17:26:30.443Z" }
|
||||||
|
wheels = [
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/8a/0b/9fcc47d19c48b59121088dd6da2488a49d5f72dacf8262e2790a1d2c7d15/pygments-2.19.1-py3-none-any.whl", hash = "sha256:9ea1544ad55cecf4b8242fab6dd35a93bbce657034b0611ee383099054ab6d8c", size = 1225293, upload-time = "2025-01-06T17:26:25.553Z" },
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "werkzeug"
|
name = "werkzeug"
|
||||||
version = "3.1.3"
|
version = "3.1.3"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue