Files in {{.Name}}
-+
| Name | @@ -89,8 +159,7 @@ footer {— | — |
|---|---|---|
| {{.Name}} | {{if .IsDir}}—{{else}}{{.Size}}{{end}} | @@ -98,10 +167,22 @@ footer {
+
+ {{if .Readme}} +
diff --git a/go.mod b/go.mod index c1104b3..0b80eac 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module forge.arimelody.space/ari/indir go 1.24.3 + +require github.com/gomarkdown/markdown v0.0.0-20260217112301-37c66b85d6ab diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..52d2bfd --- /dev/null +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index a00ecd0..267f123 100755 --- a/main.go +++ b/main.go @@ -8,11 +8,16 @@ import ( "net/http" "os" "path" + "path/filepath" "slices" "strconv" "strings" "time" _ "embed" + + "github.com/gomarkdown/markdown" + "github.com/gomarkdown/markdown/html" + "github.com/gomarkdown/markdown/parser" ) //go:embed templates/dir.html @@ -23,6 +28,7 @@ type ( Name string Root bool Files []*File + Readme template.HTML } File struct { @@ -34,9 +40,6 @@ type ( } ) -//go:embed templates/dir.html -var dirHTML string - func main() { if len(os.Args) < 2 { printHelp() } @@ -121,8 +124,8 @@ func main() { } isRoot := r.URL.Path == root - filepath := path.Join(filesDir, strings.TrimPrefix(r.URL.Path, root)) - info, err := os.Stat(filepath) + fpath := path.Join(filesDir, strings.TrimPrefix(r.URL.Path, root)) + info, err := os.Stat(fpath) if err != nil { http.NotFound(w, r) return @@ -135,7 +138,7 @@ func main() { return } - file, err := os.Open(filepath) + file, err := os.Open(fpath) if err != nil { http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return @@ -144,7 +147,7 @@ func main() { defer func() { err := file.Close() 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) 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 } @@ -169,13 +172,33 @@ func main() { 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{ Root: isRoot, Name: r.URL.Path, Files: []*File{}, + Readme: readmeHTML, } - fsDir := os.DirFS(filepath) + fsDir := os.DirFS(fpath) directories, err := fs.ReadDir(fsDir, ".") for _, dir := range directories { name := dir.Name() diff --git a/templates/dir.html b/templates/dir.html index 562cc95..b0b9ebf 100644 --- a/templates/dir.html +++ b/templates/dir.html @@ -1,82 +1,152 @@ - +
- - + +| Name | @@ -89,8 +159,7 @@ footer {— | — |
|---|---|---|
| {{.Name}} | {{if .IsDir}}—{{else}}{{.Size}}{{end}} | @@ -98,10 +167,22 @@ footer {