From 6db0708d10192a24760035e12bb7eee8c2b4e2d7 Mon Sep 17 00:00:00 2001 From: Bobby Date: Wed, 27 Mar 2024 22:36:48 -0400 Subject: Update: View Text Files in Repos --- dev_status/utils.py | 87 +++++++++++++++++++++++++++++++++++++++--- dev_status/views.py | 15 +++++++- static/css/fonts.css | 32 ++++++++-------- templates/dev_status/repo.html | 56 ++++++++++++++++++++++++++- 4 files changed, 167 insertions(+), 23 deletions(-) diff --git a/dev_status/utils.py b/dev_status/utils.py index 02876dc5..ffe7e029 100644 --- a/dev_status/utils.py +++ b/dev_status/utils.py @@ -1,4 +1,34 @@ from datetime import datetime +from pygments import highlight +from pygments.lexers import get_lexer_for_filename +from pygments.lexers.special import TextLexer +from pygments.formatters import HtmlFormatter + + +def text_lines(text): + # return the number of lines in a text + return len(text.split("\n")) - 1 + + +def text_loc(text): + text = text.strip() + + # return the number of lines of code in a text + return len([line for line in text.split("\n") if line.strip()]) + + +def size_format(size_bytes): + if size_bytes == 0: + return "0B" + + size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") + + i = int(size_bytes // 1024) + for size in size_name: + if i == 0: + return "{:.1f} {}".format(size_bytes, size) + size_bytes /= 1024 + i = int(size_bytes // 1024) def relative_date(entry): @@ -8,18 +38,63 @@ def relative_date(entry): now = datetime.now() diff = now - committedDate if diff.days > 365: - entry["commit"]["committedDate"] = str(diff.days // 365) + " year" + ("s" if diff.days // 365 > 1 else "") + " ago" + entry["commit"]["committedDate"] = ( + str(diff.days // 365) + + " year" + + ("s" if diff.days // 365 > 1 else "") + + " ago" + ) elif diff.days > 30: - entry["commit"]["committedDate"] = str(diff.days // 30) + " month" + ("s" if diff.days // 30 > 1 else "") + " ago" + entry["commit"]["committedDate"] = ( + str(diff.days // 30) + + " month" + + ("s" if diff.days // 30 > 1 else "") + + " ago" + ) elif diff.days > 7: - entry["commit"]["committedDate"] = str(diff.days // 7) + " week" + ("s" if diff.days // 7 > 1 else "") + " ago" + entry["commit"]["committedDate"] = ( + str(diff.days // 7) + " week" + ("s" if diff.days // 7 > 1 else "") + " ago" + ) elif diff.days > 0: - entry["commit"]["committedDate"] = str(diff.days) + " day" + ("s" if diff.days > 1 else "") + " ago" + entry["commit"]["committedDate"] = ( + str(diff.days) + " day" + ("s" if diff.days > 1 else "") + " ago" + ) elif diff.seconds > 3600: - entry["commit"]["committedDate"] = str(diff.seconds // 3600) + " hour" + ("s" if diff.seconds // 3600 > 1 else "") + " ago" + entry["commit"]["committedDate"] = ( + str(diff.seconds // 3600) + + " hour" + + ("s" if diff.seconds // 3600 > 1 else "") + + " ago" + ) elif diff.seconds > 60: - entry["commit"]["committedDate"] = str(diff.seconds // 60) + " minute" + ("s" if diff.seconds // 60 > 1 else "") + " ago" + entry["commit"]["committedDate"] = ( + str(diff.seconds // 60) + + " minute" + + ("s" if diff.seconds // 60 > 1 else "") + + " ago" + ) else: entry["commit"]["committedDate"] = "just now" return entry + + +def highlight_code(text, filename): + print(filename) + print(text) + try: + lexer = get_lexer_for_filename(filename, stripall=True) + except: + lexer = None + + formatter = HtmlFormatter( + noclasses=True, + style="native", + wrapcode=True, + linenos="inline", + nobackground=True, + ) + if lexer: + return highlight(text, lexer, formatter) + else: + return highlight(text, TextLexer(), formatter) diff --git a/dev_status/views.py b/dev_status/views.py index 678be122..96ad7d42 100644 --- a/dev_status/views.py +++ b/dev_status/views.py @@ -6,7 +6,13 @@ import requests from django.shortcuts import render from dotenv import load_dotenv from github import Github -from dev_status.utils import relative_date +from dev_status.utils import ( + relative_date, + text_lines, + text_loc, + size_format, + highlight_code, +) load_dotenv() g = Github(os.getenv("GH_TOKEN")) @@ -172,6 +178,13 @@ def get_repo(request, r=None, p=None): else: # if it is a blob, display the file tree = data["data"]["repository"]["object"] + tree["path"] = p + tree["name"] = p.split("/")[-1] + if not tree["isBinary"]: + tree["lines"] = text_lines(tree["text"]) + tree["loc"] = text_loc(tree["text"]) + tree["size"] = size_format(tree["byteSize"]) + tree["text"] = highlight_code(tree["text"], tree["name"]) # get commit information for each file or directory if viewMode == "tree": diff --git a/static/css/fonts.css b/static/css/fonts.css index e25f479a..aed92b83 100644 --- a/static/css/fonts.css +++ b/static/css/fonts.css @@ -1,28 +1,30 @@ -@import url('https://fonts.googleapis.com/css2?family=Mali:ital,wght@0,400;0,700;1,400;1,700&display=swap'); -@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+JP:wght@400;700&display=swap'); +@import url("https://fonts.googleapis.com/css2?family=Mali:ital,wght@0,400;0,700;1,400;1,700&display=swap"); +@import url("https://fonts.googleapis.com/css2?family=Noto+Sans+JP:wght@400;700&display=swap"); @font-face { - font-family: 'Comic Code Regular'; - src: url('../fonts/COCOLGR.otf') format('opentype'); - font-weight: normal; - font-style: normal; + font-family: "Comic Code Regular"; + src: url("../fonts/COCOLGR.otf") format("opentype"); + font-weight: normal; + font-style: normal; } @font-face { - font-family: 'Hatsukoi Friends Mini'; - src: url('../fonts/HAKOIMI.otf') format('opentype'); - font-weight: normal; - font-style: normal; + font-family: "Hatsukoi Friends Mini"; + src: url("../fonts/HAKOIMI.otf") format("opentype"); + font-weight: normal; + font-style: normal; } +/* pre, code { - font-family: 'Comic Code Regular', sans-serif; -} + font-family: "Comic Code Regular", sans-serif; +} */ .en { - font-family: 'Mali', sans-serif; + font-family: "Mali", sans-serif; } -body.ja, .ja { - font-family: 'Noto Sans JP', sans-serif; +body.ja, +.ja { + font-family: "Noto Sans JP", sans-serif; } diff --git a/templates/dev_status/repo.html b/templates/dev_status/repo.html index 59ca7b0d..b880517b 100644 --- a/templates/dev_status/repo.html +++ b/templates/dev_status/repo.html @@ -1,4 +1,13 @@ {% extends 'blog/partials/base.html' %} {% block content %} {% load static %} + -{% endif %} {% endblock %} +{% endif %} {% if "byteSize" in files and not files.isBinary %} +
+
+

+ Viewing + {{ files.name }} +
+{{ files.lines }} lines ({{ files.loc }} loc) • {{ files.size }}
+

+
+ {{files.text|safe}} +
+ + {% comment %} +

+ + File + + {{ files.name }} + - {{ files.size }} +

+ +
{{ files.text }}
+ {% endcomment %} {% endif %} {% endblock %} + -- cgit v1.2.3