diff options
| author | Bobby <[email protected]> | 2024-03-27 22:36:48 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2024-03-27 22:36:48 -0400 |
| commit | 6db0708d10192a24760035e12bb7eee8c2b4e2d7 (patch) | |
| tree | 5f546437218095256df57f32aadf77c40c7de543 | |
| parent | 5c0b62ad493b629142f722b2f84c91071153350a (diff) | |
| download | thatcomputerscientist-6db0708d10192a24760035e12bb7eee8c2b4e2d7.tar.xz thatcomputerscientist-6db0708d10192a24760035e12bb7eee8c2b4e2d7.zip | |
Update: View Text Files in Repos
| -rw-r--r-- | dev_status/utils.py | 87 | ||||
| -rw-r--r-- | dev_status/views.py | 15 | ||||
| -rw-r--r-- | static/css/fonts.css | 32 | ||||
| -rw-r--r-- | 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 %} +<style> + .highlight { + max-width: 680px; + } + + table td.linenos { + padding: 0; + } +</style> <img style="width: 730px; display: block; margin: 40px auto 20px auto" src="https://socialify.thatcomputerscientist.com/luciferreeves/{{repo}}/png?font=KoHo&forks=1&issues=1&language=1&language2=1&pattern=Circuit%20Board&pulls=1&stargazers=1&theme=Dark" @@ -59,4 +68,49 @@ {% endfor %} </tbody> </table> -{% endif %} {% endblock %} +{% endif %} {% if "byteSize" in files and not files.isBinary %} +<div + style=" + background: #272727a3; + border: 1px solid #434343; + border-radius: 8px 8px 2px 2px; + " +> + <div + style=" + background: #353535; + padding: 0px 10px; + border-radius: 8px 8px 0px 0px; + " + > + <h4 style="margin: 0"> + Viewing + <a href="{% url 'dev_status:repo-path' repo files.path %}" + >{{ files.name }}</a + > + <pre style="display: inline-block; margin-left: 10px"> +{{ files.lines }} lines ({{ files.loc }} loc) • {{ files.size }}</pre + > + </h4> + </div> + {{files.text|safe}} +</div> + + {% comment %} + <p> + <span title="File" style="margin-right: 5px"> + <img + src="{% static 'images/icons/notepad_file-2.png' %}" + alt="File" + style="display: inline-block; vertical-align: middle; height: 16px" + /> + </span> + <a href="{% url 'dev_status:repo-path' repo files.path %}" + >{{ files.name }}</a + > + - {{ files.size }} + </p> + + <pre>{{ files.text }}</pre> + {% endcomment %} {% endif %} {% endblock %} +</div> |
