summaryrefslogtreecommitdiff
path: root/gopher/gophermap/listdir.go
diff options
context:
space:
mode:
Diffstat (limited to 'gopher/gophermap/listdir.go')
-rw-r--r--gopher/gophermap/listdir.go91
1 files changed, 39 insertions, 52 deletions
diff --git a/gopher/gophermap/listdir.go b/gopher/gophermap/listdir.go
index 8d66277..a2c5214 100644
--- a/gopher/gophermap/listdir.go
+++ b/gopher/gophermap/listdir.go
@@ -6,6 +6,7 @@ import (
"os"
"path"
"path/filepath"
+ "slices"
"strings"
"tildegit.org/tjp/sliderule/gopher"
@@ -13,11 +14,11 @@ import (
)
// ListDir builds a gopher menu representing the contents of a directory.
-func ListDir(dir string, location *url.URL) (gopher.MapDocument, error) {
- return listDir(dir, location, nil, nil)
+func ListDir(dir string, location *url.URL, settings FileSystemSettings) (gopher.MapDocument, error) {
+ return listDir(dir, location, settings, nil, nil)
}
-func listDir(dir string, location *url.URL, hidden map[string]struct{}, extensions map[string]types.Status) (gopher.MapDocument, error) {
+func listDir(dir string, location *url.URL, settings FileSystemSettings, hidden map[string]struct{}, extensions map[string]types.Status) (gopher.MapDocument, error) {
contents, err := os.ReadDir(dir)
if err != nil {
return nil, err
@@ -28,55 +29,36 @@ func listDir(dir string, location *url.URL, hidden map[string]struct{}, extensio
for _, entry := range contents {
name := entry.Name()
- if _, ok := hidden[name]; ok || name == "gophermap" {
+ inf, err := entry.Info()
+ if err != nil {
+ return nil, err
+ }
+ if inf.Mode()&4 == 0 {
+ continue
+ }
+
+ if _, ok := hidden[name]; ok || slices.Contains(settings.DirMaps, name) {
continue
}
var code types.Status
- ext := strings.TrimPrefix(filepath.Ext(name), ".")
if entry.IsDir() {
code = gopher.MenuType
- } else if c, ok := extensions[ext]; ok && ext != "" {
- code = c
} else {
- switch ext {
- case "gophermap":
- code = gopher.MenuType
- case "exe", "bin", "out":
- code = gopher.BinaryFileType
- case "gif":
- code = gopher.GifFileType
- case "jpg", "jpeg", "tif", "tiff":
- code = gopher.ImageFileType
- case "bmp":
- code = gopher.BitmapType
- case "mp4", "mov", "avi", "wmv", "webm":
- code = gopher.MovieFileType
- case "pcm", "aiff", "mp3", "aac", "ogg", "wma", "flac", "alac":
- code = gopher.SoundFileType
- case "doc", "docx", "odt", "fodt":
- code = gopher.DocumentType
- case "html":
- code = gopher.HTMLType
- case "png":
- code = gopher.PngImageFileType
- case "rtf":
- code = gopher.RtfDocumentType
- case "wav":
- code = gopher.WavSoundFileType
- case "pdf":
- code = gopher.PdfDocumentType
- case "xml", "atom":
- code = gopher.XmlDocumentType
- default:
- code = gopher.TextFileType
+ ext := strings.TrimPrefix(filepath.Ext(name), ".")
+ if c, ok := extensions[ext]; ok {
+ code = c
+ } else if c, ok := extensions[name]; ok {
+ code = c
+ } else {
+ code = gopher.GuessItemType(name)
}
}
doc = append(doc, gopher.MapItem{
Type: code,
- Display: displayName(dir, entry),
+ Display: displayName(dir, entry, settings),
Selector: path.Join(path.Dir(location.Path), name),
Hostname: location.Hostname(),
Port: location.Port(),
@@ -86,32 +68,37 @@ func listDir(dir string, location *url.URL, hidden map[string]struct{}, extensio
return doc, nil
}
-func displayName(dir string, entry os.DirEntry) string {
+func displayName(dir string, entry os.DirEntry, settings FileSystemSettings) string {
fname := entry.Name()
+ fullpath := filepath.Join(dir, fname)
- // if is a gophermap, use !title or filename
- if strings.HasSuffix(fname, ".gophermap") {
- if title := gophermapTitle(dir, fname); title != "" {
+ if entry.Type().IsRegular() && settings.ParseExtended && (strings.HasSuffix(fname, ".gophermap") || slices.Contains(settings.DirMaps, fname)) {
+ if title := gophermapTitle(fullpath); title != "" {
return title
}
- return fname
}
if entry.IsDir() {
- if tag := tagTitle(dir, fname); tag != "" {
- return tag
+ if settings.DirTag != "" {
+ if tag := tagTitle(filepath.Join(fullpath, settings.DirTag)); tag != "" {
+ return tag
+ }
}
- if title := gophermapTitle(dir, filepath.Join(fname, "gophermap")); title != "" {
- return title
+ if settings.ParseExtended {
+ for _, mapname := range settings.DirMaps {
+ if title := gophermapTitle(filepath.Join(fullpath, mapname)); title != "" {
+ return title
+ }
+ }
}
}
return fname
}
-func gophermapTitle(dir, name string) string {
- file, err := os.Open(filepath.Join(dir, name))
+func gophermapTitle(path string) string {
+ file, err := os.Open(path)
if err != nil {
return ""
}
@@ -131,8 +118,8 @@ func gophermapTitle(dir, name string) string {
return strings.TrimRight(line[1:], "\r\n")
}
-func tagTitle(parent, name string) string {
- file, err := os.Open(filepath.Join(parent, name, "gophertag"))
+func tagTitle(path string) string {
+ file, err := os.Open(path)
if err != nil {
return ""
}