diff --git a/go.mod b/go.mod index 0b80eac..c1104b3 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,3 @@ 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 deleted file mode 100644 index 52d2bfd..0000000 --- a/go.sum +++ /dev/null @@ -1,2 +0,0 @@ -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 267f123..a00ecd0 100755 --- a/main.go +++ b/main.go @@ -8,16 +8,11 @@ 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 @@ -28,7 +23,6 @@ type ( Name string Root bool Files []*File - Readme template.HTML } File struct { @@ -40,6 +34,9 @@ type ( } ) +//go:embed templates/dir.html +var dirHTML string + func main() { if len(os.Args) < 2 { printHelp() } @@ -124,8 +121,8 @@ func main() { } isRoot := r.URL.Path == root - fpath := path.Join(filesDir, strings.TrimPrefix(r.URL.Path, root)) - info, err := os.Stat(fpath) + filepath := path.Join(filesDir, strings.TrimPrefix(r.URL.Path, root)) + info, err := os.Stat(filepath) if err != nil { http.NotFound(w, r) return @@ -138,7 +135,7 @@ func main() { return } - file, err := os.Open(fpath) + file, err := os.Open(filepath) if err != nil { http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return @@ -147,7 +144,7 @@ func main() { defer func() { err := file.Close() if err != nil { - fmt.Fprintf(os.Stderr, "failed to close file %s: %v\n", fpath, err) + fmt.Fprintf(os.Stderr, "failed to close file %s: %v\n", filepath, err) } }() @@ -162,7 +159,7 @@ func main() { _, err = file.WriteTo(w) if err != nil { - fmt.Fprintf(os.Stderr, "failed to send file %s: %v\n", fpath, err) + fmt.Fprintf(os.Stderr, "failed to send file %s: %v\n", filepath, err) } return } @@ -172,33 +169,13 @@ 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(fpath) + fsDir := os.DirFS(filepath) directories, err := fs.ReadDir(fsDir, ".") for _, dir := range directories { name := dir.Name() diff --git a/templates/dir.html b/templates/dir.html index b0b9ebf..562cc95 100644 --- a/templates/dir.html +++ b/templates/dir.html @@ -1,152 +1,82 @@ - + - - + + Files in {{.Name}}

Files in {{.Name}}

-
+
@@ -159,7 +89,8 @@ - {{end}} {{range .Files}} + {{end}} + {{range .Files}} @@ -167,22 +98,10 @@ {{end}}
Name
{{.Name}} {{if .IsDir}}—{{else}}{{.Size}}{{end}}
-
- {{if .Readme}} -
- {{.Readme}} -
- {{end}} +