Compare commits
5 commits
cf03c723d0
...
09409762ea
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
09409762ea | ||
| da76681c29 | |||
| ef3dc28e7c | |||
| 73fdf8bb46 | |||
| 245d6e0fa0 |
4 changed files with 180 additions and 72 deletions
2
go.mod
2
go.mod
|
|
@ -1,3 +1,5 @@
|
||||||
module forge.arimelody.space/ari/indir
|
module forge.arimelody.space/ari/indir
|
||||||
|
|
||||||
go 1.24.3
|
go 1.24.3
|
||||||
|
|
||||||
|
require github.com/gomarkdown/markdown v0.0.0-20260217112301-37c66b85d6ab
|
||||||
|
|
|
||||||
2
go.sum
Normal file
2
go.sum
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
github.com/gomarkdown/markdown v0.0.0-20260217112301-37c66b85d6ab h1:VYNivV7P8IRHUam2swVUNkhIdp0LRRFKe4hXNnoZKTc=
|
||||||
|
github.com/gomarkdown/markdown v0.0.0-20260217112301-37c66b85d6ab/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
|
||||||
41
main.go
41
main.go
|
|
@ -8,11 +8,16 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
|
||||||
|
"github.com/gomarkdown/markdown"
|
||||||
|
"github.com/gomarkdown/markdown/html"
|
||||||
|
"github.com/gomarkdown/markdown/parser"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed templates/dir.html
|
//go:embed templates/dir.html
|
||||||
|
|
@ -23,6 +28,7 @@ type (
|
||||||
Name string
|
Name string
|
||||||
Root bool
|
Root bool
|
||||||
Files []*File
|
Files []*File
|
||||||
|
Readme template.HTML
|
||||||
}
|
}
|
||||||
|
|
||||||
File struct {
|
File struct {
|
||||||
|
|
@ -34,9 +40,6 @@ type (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed templates/dir.html
|
|
||||||
var dirHTML string
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) < 2 { printHelp() }
|
if len(os.Args) < 2 { printHelp() }
|
||||||
|
|
||||||
|
|
@ -121,8 +124,8 @@ func main() {
|
||||||
}
|
}
|
||||||
isRoot := r.URL.Path == root
|
isRoot := r.URL.Path == root
|
||||||
|
|
||||||
filepath := path.Join(filesDir, strings.TrimPrefix(r.URL.Path, root))
|
fpath := path.Join(filesDir, strings.TrimPrefix(r.URL.Path, root))
|
||||||
info, err := os.Stat(filepath)
|
info, err := os.Stat(fpath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
|
|
@ -135,7 +138,7 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := os.Open(filepath)
|
file, err := os.Open(fpath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
|
@ -144,7 +147,7 @@ func main() {
|
||||||
defer func() {
|
defer func() {
|
||||||
err := file.Close()
|
err := file.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "failed to close file %s: %v\n", filepath, err)
|
fmt.Fprintf(os.Stderr, "failed to close file %s: %v\n", fpath, err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
@ -159,7 +162,7 @@ func main() {
|
||||||
|
|
||||||
_, err = file.WriteTo(w)
|
_, err = file.WriteTo(w)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "failed to send file %s: %v\n", filepath, err)
|
fmt.Fprintf(os.Stderr, "failed to send file %s: %v\n", fpath, err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -169,13 +172,33 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// embeded readme
|
||||||
|
var readmeHTML template.HTML
|
||||||
|
entries, err := os.ReadDir(fpath)
|
||||||
|
if err == nil {
|
||||||
|
for _, entry := range entries {
|
||||||
|
if strings.EqualFold(entry.Name(), "readme.md") {
|
||||||
|
src, err := os.ReadFile(filepath.Join(fpath, entry.Name()))
|
||||||
|
if err == nil {
|
||||||
|
mdFlags := html.CommonFlags | html.HrefTargetBlank
|
||||||
|
mdRenderer := html.NewRenderer(html.RendererOptions{Flags: mdFlags})
|
||||||
|
mdParser := parser.NewWithExtensions(parser.CommonExtensions | parser.AutoHeadingIDs)
|
||||||
|
md := mdParser.Parse(src)
|
||||||
|
readmeHTML = template.HTML(markdown.Render(md, mdRenderer))
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
data := Directory{
|
data := Directory{
|
||||||
Root: isRoot,
|
Root: isRoot,
|
||||||
Name: r.URL.Path,
|
Name: r.URL.Path,
|
||||||
Files: []*File{},
|
Files: []*File{},
|
||||||
|
Readme: readmeHTML,
|
||||||
}
|
}
|
||||||
|
|
||||||
fsDir := os.DirFS(filepath)
|
fsDir := os.DirFS(fpath)
|
||||||
directories, err := fs.ReadDir(fsDir, ".")
|
directories, err := fs.ReadDir(fsDir, ".")
|
||||||
for _, dir := range directories {
|
for _, dir := range directories {
|
||||||
name := dir.Name()
|
name := dir.Name()
|
||||||
|
|
|
||||||
|
|
@ -1,82 +1,152 @@
|
||||||
<!DOCTYPE html>
|
<!doctype html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
<title>Files in {{.Name}}</title>
|
<title>Files in {{.Name}}</title>
|
||||||
<style>
|
<style>
|
||||||
html {
|
html {
|
||||||
background: #101010;
|
background: #101010;
|
||||||
color: #f0f0f0;
|
color: #f0f0f0;
|
||||||
font-family: 'Monaspace Argon', monospace;
|
font-family: "Monaspace Argon", monospace;
|
||||||
font-size: 16px;
|
font-size: 16px;
|
||||||
}
|
}
|
||||||
|
|
||||||
body {
|
body {
|
||||||
width: min(calc(100% - 1em), 1000px);
|
width: min(calc(100% - 1em), 1000px);
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
table {
|
table {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
border-collapse: collapse;
|
border-collapse: collapse;
|
||||||
}
|
}
|
||||||
|
|
||||||
tr:hover {
|
tr:hover {
|
||||||
background-color: #80808040;
|
background-color: #80808040;
|
||||||
}
|
}
|
||||||
|
|
||||||
th {
|
th {
|
||||||
text-align: left;
|
text-align: left;
|
||||||
}
|
}
|
||||||
|
|
||||||
td {
|
td {
|
||||||
width: 1%;
|
width: 1%;
|
||||||
max-width: 500px;
|
max-width: 500px;
|
||||||
padding: .2em 0;
|
padding: 0.2em 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
table a {
|
table a {
|
||||||
display: block;
|
display: block;
|
||||||
line-break: anywhere;
|
line-break: anywhere;
|
||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
}
|
}
|
||||||
|
|
||||||
a {
|
a {
|
||||||
color: #b7fd49;
|
color: #b7fd49;
|
||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
}
|
}
|
||||||
a:hover {
|
a:hover {
|
||||||
color: white;
|
color: white;
|
||||||
text-decoration: underline;
|
text-decoration: underline;
|
||||||
}
|
}
|
||||||
|
|
||||||
footer {
|
.readme {
|
||||||
padding: 1em 0;
|
margin-top: 2rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
@media screen and (max-width: 700px) {
|
.readme h1, .readme h2, .readme h3,
|
||||||
body {
|
.readme h4, .readme h5, .readme h6 {
|
||||||
font-size: 12px;
|
color: #f0f0f0;
|
||||||
}
|
margin-top: 1.2rem;
|
||||||
|
}
|
||||||
|
|
||||||
td {
|
.readme p, .readme li {
|
||||||
width: auto;
|
line-height: 1.7;
|
||||||
}
|
color: #d0d0d0;
|
||||||
|
}
|
||||||
|
|
||||||
td:last-of-type,
|
.readme a {
|
||||||
th:last-of-type {
|
color: #b7fd49;
|
||||||
display: none;
|
}
|
||||||
}
|
|
||||||
}
|
.readme a:hover {
|
||||||
|
color: white;
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
|
||||||
|
.readme code {
|
||||||
|
background: #1e1e1e;
|
||||||
|
border: 1px solid #333;
|
||||||
|
border-radius: 3px;
|
||||||
|
padding: .1em, .4em;
|
||||||
|
font-family: 'Monaspace Argon', monospace;
|
||||||
|
font-size: .9em;
|
||||||
|
color: #b7fd79;
|
||||||
|
}
|
||||||
|
|
||||||
|
.readme pre {
|
||||||
|
background: #1e1e1e;
|
||||||
|
border: 3px solid #333;
|
||||||
|
border-radius: 6px;
|
||||||
|
padding: 1em;
|
||||||
|
overflow-x: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.readme pre code {
|
||||||
|
border: none;
|
||||||
|
padding: 0;
|
||||||
|
background: transparent;
|
||||||
|
}
|
||||||
|
|
||||||
|
.readme blockquote {
|
||||||
|
border-left: 3px solid #b7fd49;
|
||||||
|
margin: 0;
|
||||||
|
padding-left: 1em;
|
||||||
|
color: #a0a0a0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.readme hr {
|
||||||
|
border-color: #333;
|
||||||
|
}
|
||||||
|
|
||||||
|
.readme table {
|
||||||
|
width: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.readme table td,
|
||||||
|
.readme table th {
|
||||||
|
border: 1px solid #333;
|
||||||
|
padding: .3em .6em;
|
||||||
|
width: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
footer {
|
||||||
|
padding: 1em 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media screen and (max-width: 700px) {
|
||||||
|
body {
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
td {
|
||||||
|
width: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
td:last-of-type,
|
||||||
|
th:last-of-type {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<main>
|
<main>
|
||||||
<h1>Files in {{.Name}}</h1>
|
<h1>Files in {{.Name}}</h1>
|
||||||
<hr>
|
<hr />
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Name</th>
|
<th>Name</th>
|
||||||
|
|
@ -89,8 +159,7 @@ footer {
|
||||||
<td>—</td>
|
<td>—</td>
|
||||||
<td>—</td>
|
<td>—</td>
|
||||||
</tr>
|
</tr>
|
||||||
{{end}}
|
{{end}} {{range .Files}}
|
||||||
{{range .Files}}
|
|
||||||
<tr>
|
<tr>
|
||||||
<td><a href="{{.URI}}">{{.Name}}</a></td>
|
<td><a href="{{.URI}}">{{.Name}}</a></td>
|
||||||
<td>{{if .IsDir}}—{{else}}{{.Size}}{{end}}</td>
|
<td>{{if .IsDir}}—{{else}}{{.Size}}{{end}}</td>
|
||||||
|
|
@ -98,10 +167,22 @@ footer {
|
||||||
</tr>
|
</tr>
|
||||||
{{end}}
|
{{end}}
|
||||||
</table>
|
</table>
|
||||||
<hr>
|
<hr />
|
||||||
|
{{if .Readme}}
|
||||||
|
<article class="readme">
|
||||||
|
{{.Readme}}
|
||||||
|
</article>
|
||||||
|
{{end}}
|
||||||
</main>
|
</main>
|
||||||
<footer>
|
<footer>
|
||||||
<em>made with <span aria-label="love">♥</span> by ari, 2025 <a href="https://forge.arimelody.space/ari/indir" target="_blank">[source]</a></em>
|
<em
|
||||||
|
>made with <span aria-label="love">♥</span> by ari, 2025
|
||||||
|
<a
|
||||||
|
href="https://forge.arimelody.space/ari/indir"
|
||||||
|
target="_blank"
|
||||||
|
>[source]</a
|
||||||
|
></em
|
||||||
|
>
|
||||||
</footer>
|
</footer>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue