feat: line nrs

This commit is contained in:
Barrett Ruth 2025-10-04 23:20:54 -04:00
parent 244d261374
commit 7fd5c307e0
6 changed files with 88 additions and 72 deletions

82
app.py
View file

@ -5,21 +5,17 @@ import os
from dataclasses import dataclass
from pathlib import Path
from flask import Flask, jsonify, render_template, send_from_directory, abort, request
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import TextLexer, get_lexer_for_filename
from pygments.util import ClassNotFound
from flask import Flask, abort, jsonify, render_template, request, send_from_directory
app = Flask(__name__, static_folder=None)
GIT_REPO_PATH = str(Path.home() / "dev")
GIST_PATH = str(Path.home() / "gists")
EXPORT_MARKER = "readme.md"
git_repo_path = str(Path.home() / "dev")
gist_path = str(Path.home() / "gists")
export_marker = "readme.md"
if getpass.getuser() == "apache":
GIT_REPO_PATH = "/srv/git"
GIST_PATH = "/srv/gists"
EXPORT_MARKER = "git-daemon-export-ok"
git_repo_path = "/srv/git"
gist_path = "/srv/gists"
export_marker = "git-daemon-export-ok"
@dataclass
@ -33,11 +29,11 @@ class Repository:
def get_repositories():
repositories = []
if not os.path.exists(GIT_REPO_PATH):
if not os.path.exists(git_repo_path):
return repositories
for item in os.listdir(GIT_REPO_PATH):
repo_path = os.path.join(GIT_REPO_PATH, item)
for item in os.listdir(git_repo_path):
repo_path = os.path.join(git_repo_path, item)
if not os.path.isdir(repo_path):
continue
@ -48,7 +44,7 @@ def get_repositories():
):
continue
exported = os.path.exists(os.path.join(repo_path, EXPORT_MARKER))
exported = os.path.exists(os.path.join(repo_path, export_marker))
description = "No description available"
description_file = os.path.join(repo_path, "description")
@ -82,6 +78,24 @@ def get_repositories():
return repositories
@app.route("/gist/<path:filename>")
def serve_gist(filename):
base = Path(gist_path).resolve()
target = (base / filename).resolve()
if base not in target.parents and base != target:
abort(404)
if not target.exists() or not target.is_file():
abort(404)
try:
content = target.read_text(encoding="utf-8")
except UnicodeDecodeError:
return "Binary file cannot be displayed", 400
ext = target.suffix.lstrip(".") or "plaintext"
return render_template("gist.html", filename=filename, code=content, lang=ext)
@app.route("/")
def index():
repositories = [repo for repo in get_repositories() if repo.exported]
@ -98,43 +112,6 @@ def serve_scripts(filename):
return send_from_directory("scripts", filename)
@app.route("/gist/<path:filename>")
def serve_gist(filename):
base = Path(GIST_PATH).resolve()
target = (base / filename).resolve()
if base not in target.parents and base != target:
abort(404)
if not target.exists() or not target.is_file():
abort(404)
try:
content = target.read_text(encoding="utf-8")
except UnicodeDecodeError:
return "Binary file cannot be displayed", 400
except OSError:
abort(404)
try:
lexer = get_lexer_for_filename(target.name)
except ClassNotFound:
lexer = TextLexer()
formatter = HtmlFormatter(
style="default",
cssclass="highlight",
linenos=False,
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("/<path:filename>")
def serve_gist_root(filename):
head = filename.split("/", 1)[0]
@ -166,4 +143,3 @@ def get_repo(repo_id):
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)