templates: repo and log

This commit is contained in:
Anirudh Oppiliappan 2022-12-17 21:33:04 +05:30
parent 4eaaf45129
commit 5091695e75
No known key found for this signature in database
GPG Key ID: 8A93F96F78C5D4C4
8 changed files with 168 additions and 61 deletions

View File

@ -45,13 +45,7 @@ func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
return return
} }
var desc string desc := getDescription(path)
db, err := os.ReadFile(filepath.Join(path, "description"))
if err == nil {
desc = string(db)
} else {
desc = ""
}
infos = append(infos, info{ infos = append(infos, info{
Name: dir.Name(), Name: dir.Name(),
@ -83,7 +77,7 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
return return
} }
files, err := gr.FileTree("") commits, err := gr.Commits()
if err != nil { if err != nil {
d.Write500(w) d.Write500(w)
log.Println(err) log.Println(err)
@ -109,12 +103,25 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
return return
} }
tpath := filepath.Join(d.c.Dirs.Templates, "*")
t := template.Must(template.ParseGlob(tpath))
if len(commits) >= 5 {
commits = commits[:5]
}
data := make(map[string]any) data := make(map[string]any)
data["name"] = name data["name"] = name
data["ref"] = mainBranch data["ref"] = mainBranch
data["readme"] = readmeContent data["readme"] = readmeContent
data["commits"] = commits
data["desc"] = getDescription(path)
if err := t.ExecuteTemplate(w, "repo", data); err != nil {
log.Println(err)
return
}
d.listFiles(files, data, w)
return return
} }
@ -283,3 +290,13 @@ func (d *deps) ServeStatic(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, f) http.ServeFile(w, r, f)
} }
func getDescription(path string) (desc string) {
db, err := os.ReadFile(filepath.Join(path, "description"))
if err == nil {
desc = string(db)
} else {
desc = ""
}
return
}

View File

@ -37,7 +37,7 @@ func (d *deps) listFiles(files []git.NiceTree, data map[string]any, w http.Respo
data["files"] = files data["files"] = files
data["meta"] = d.c.Meta data["meta"] = d.c.Meta
if err := t.ExecuteTemplate(w, "repo", data); err != nil { if err := t.ExecuteTemplate(w, "tree", data); err != nil {
log.Println(err) log.Println(err)
return return
} }

View File

@ -6,12 +6,16 @@
--gray: #6a6a6a; --gray: #6a6a6a;
--dark: #444; --dark: #444;
--darker: #222; --darker: #222;
--sans-font: "InterVar", -apple-system, BlinkMacSystemFont, "Roboto", "Segoe UI", sans-serif;
--display-font: "InterDisplay", -apple-system, BlinkMacSystemFont, "Roboto", "Segoe UI", sans-serif;
--mono-font: monospace;
} }
html { html {
background: var(--light); background: var(--light);
-webkit-text-size-adjust: none; -webkit-text-size-adjust: none;
font-family: "InterVar", -apple-system, BlinkMacSystemFont, "Roboto", "Segoe UI", sans-serif; font-family: var(--sans-font);
} }
::selection { ::selection {
@ -38,7 +42,7 @@ main, footer {
} }
main h1, h2, h3, .small-heading { main h1, h2, h3, .small-heading {
font-family: "InterDisplay", -apple-system, BlinkMacSystemFont, "Roboto", "Segoe UI", sans-serif; font-family: var(--display-font);
font-weight: 500; font-weight: 500;
} }
@ -94,19 +98,65 @@ a:hover {
.index { .index {
display: grid; display: grid;
grid-template-columns: 6em 1fr 7em; grid-template-columns: 6em 1fr minmax(0, 7em);
grid-row-gap: 0.5em; grid-row-gap: 0.5em;
min-width: 0;
} }
.index-headings { .index-headings {
display: grid; display: grid;
grid-template-columns: 6em 1fr 7em; grid-template-columns: 6em 1fr minmax(0, 7em);
padding-bottom: 1.2em; padding-bottom: 1.2em;
padding-top: 1.2em; padding-top: 1.2em;
min-width: 0;
} }
@media (max-width: 385px) { .desc {
color: var(--gray);
font-style: italic;
}
.tree {
display: grid;
grid-template-columns: 8em minmax(0, 1fr);
grid-row-gap: 0.5em;
grid-column-gap: 1em;
min-width: 0;
}
.log {
display: grid;
grid-template-columns: 20rem minmax(0, 1fr);
grid-row-gap: 0.8em;
grid-column-gap: 8rem;
margin-bottom: 2em;
padding-bottom: 1em;
border-bottom: 1.5px solid var(--medium-gray);
}
.log pre {
white-space: pre-wrap;
}
.mode {
font-family: var(--mono-font);
}
.readme pre {
white-space: pre-wrap;
}
@media (max-width: 600px) {
.index { .index {
grid-row-gap: 0.8em; grid-row-gap: 0.8em;
} }
.log {
grid-template-columns: 1fr;
grid-row-gap: 0em;
}
.commit-info:not(:last-child) {
padding-bottom: 1.5rem;
}
} }

View File

@ -16,7 +16,7 @@
<div class="index"> <div class="index">
{{ range .info }} {{ range .info }}
<div><a href="/{{ .Name }}">{{ .Name }}</a></div> <div><a href="/{{ .Name }}">{{ .Name }}</a></div>
<div>{{ .Desc }}</div> <div class="desc">{{ .Desc }}</div>
<div>{{ .Idle }}</div> <div>{{ .Idle }}</div>
{{ end }} {{ end }}
</div> </div>

View File

@ -9,13 +9,17 @@
<body> <body>
{{ template "nav" . }} {{ template "nav" . }}
<main> <main>
{{ $repo := .name }} {{ $repo := .repo }}
<div class="log">
{{ range .commits }} {{ range .commits }}
<p><a href="/{{ $repo }}/commit/{{ .Hash.String }}">{{ slice .Hash.String 0 8 }}<a> <div>
&mdash; {{ .Author.Name }} <div><a href="/{{ $repo }}/commit/{{ .Hash.String }}">{{ slice .Hash.String 0 8 }}</a></div>
<span title="{{ .Author.When }}">{{ .Author.When.Format "Mon, 02 Jan 2006 15:04:05 -0700" }}</span></p> <div>{{ .Author.When.Format "Mon, 02 Jan 2006 15:04:05 -0700" }}</div>
<p><pre>{{ .Message }}</pre></p> <pre>{{ .Message }}</pre>
</div>
<div class="commit-info">{{ .Author.Name }} <span style="color: var(--gray);">{{ .Author.Email }}</span></div>
{{ end }} {{ end }}
</div>
</main> </main>
</body> </body>
</html> </html>

View File

@ -1,9 +1,8 @@
{{ define "nav" }} {{ define "nav" }}
<nav> <nav>
<ul> <ul>
<li><a href="/">all repos</a>
{{ if .name }} {{ if .name }}
<li><a href="/{{ .name }}">{{ .name }}</a> <li><a href="/{{ .name }}">summary</a>
<li><a href="/{{ .name }}/refs">refs</a> <li><a href="/{{ .name }}/refs">refs</a>
{{ if .ref }} {{ if .ref }}
<li><a href="/{{ .name }}/tree/{{ .ref }}/">tree</a> <li><a href="/{{ .name }}/tree/{{ .ref }}/">tree</a>

View File

@ -1,50 +1,34 @@
{{ define "repo" }} {{ define "repo" }}
<html> <html>
<title>{{ .name }}
{{ if .parent }}
&mdash; {{ .parent }}
{{ end }}
</title>
{{ template "head" . }} {{ template "head" . }}
<header> <header>
<h1>{{ .meta.Title }}</h1> <h2>
<h2>{{ .meta.Description }}</h2> <a href="/">all repos</a>
&mdash; {{ .name }}
</h2>
<h3 class="desc">{{ .desc }}</h3>
</header> </header>
<body> <body>
{{ template "nav" . }} {{ template "nav" . }}
<main> <main>
{{ $repo := .name }} {{ $repo := .name }}
{{ $ref := .ref }} <div class="log">
{{ $parent := .parent }} {{ range .commits }}
<div>
<table> <div><a href="/{{ $repo }}/commit/{{ .Hash.String }}">{{ slice .Hash.String 0 8 }}</a></div>
<tr> <div>{{ .Author.When.Format "Mon, 02 Jan 2006 15:04:05 -0700" }}</div>
<td></td> <pre>{{ .Message }}</pre>
<td><a href="../">..</a> </div>
</tr> <div class="commit-info">{{ .Author.Name }} <span style="color: var(--gray);">{{ .Author.Email }}</span></div>
{{ range .files }}
{{ if .IsFile }}
<tr>
<td><code>{{ .Mode }}</code></td>
<td>
{{ if $parent }}
<a href="/{{ $repo }}/blob/{{ $ref }}/{{ $parent }}/{{ .Name }}">{{ .Name }}</a>
{{ else }}
<a href="/{{ $repo }}/blob/{{ $ref }}/{{ .Name }}">{{ .Name }}</a>
{{ end }} {{ end }}
</td> </div>
</tr> <article class="readme">
{{ else }}
<tr>
<td><code>{{ .Mode }}</code></td>
<td>
{{ if $parent }}
<a href="/{{ $repo }}/tree/{{ $ref }}/{{ $parent }}/{{ .Name }}">{{ .Name }}/</a>
{{ else }}
<a href="/{{ $repo }}/tree/{{ $ref }}/{{ .Name }}">{{ .Name }}/</a>
{{ end }}
</td>
</tr>
{{ end }}
{{ end }}
</table>
<article>
<pre> <pre>
{{- if .readme }}{{ .readme }}{{- end -}} {{- if .readme }}{{ .readme }}{{- end -}}
</pre> </pre>

53
templates/tree.html Normal file
View File

@ -0,0 +1,53 @@
{{ define "tree" }}
<html>
<title>{{ .name }}
{{ if .parent }}
&mdash; {{ .parent }}
{{ end }}
</title>
{{ template "head" . }}
<header>
<h1>{{ .meta.Title }}</h1>
<h2>{{ .meta.Description }}</h2>
</header>
<body>
{{ template "nav" . }}
<main>
{{ $repo := .name }}
{{ $ref := .ref }}
{{ $parent := .parent }}
<div class="tree">
{{ if $parent }}
<div></div>
<div><a href="../">..</a></div>
{{ end }}
{{ range .files }}
<div class="mode">{{ .Mode }}</div>
<div>
{{ if .IsFile }}
{{ if $parent }}
<a href="/{{ $repo }}/blob/{{ $ref }}/{{ $parent }}/{{ .Name }}">{{ .Name }}</a>
{{ else }}
<a href="/{{ $repo }}/blob/{{ $ref }}/{{ .Name }}">{{ .Name }}</a>
{{ end }}
{{ else }}
{{ if $parent }}
<a href="/{{ $repo }}/tree/{{ $ref }}/{{ $parent }}/{{ .Name }}">{{ .Name }}/</a>
{{ else }}
<a href="/{{ $repo }}/tree/{{ $ref }}/{{ .Name }}">{{ .Name }}/</a>
{{ end }}
{{ end }}
</div>
{{ end }}
</div>
<article>
<pre>
{{- if .readme }}{{ .readme }}{{- end -}}
</pre>
</article>
</main>
</body>
</html>
{{ end }}