forked from solderpunk/molly-brown
Use headings in .gmi files instead of filenames in directory listings.
This commit is contained in:
parent
3f98a9edf1
commit
7066bad570
|
@ -93,4 +93,5 @@ The following options can be set in `/etc/molly.conf`:
|
||||||
all intermediate directories must exist, Molly Brown won't create
|
all intermediate directories must exist, Molly Brown won't create
|
||||||
them for you.
|
them for you.
|
||||||
* `DirectorySort`: A string specifying how to sort files in automatically generated directory listings. Must be one of "Name", "Size" or "Time" (default value "Name").
|
* `DirectorySort`: A string specifying how to sort files in automatically generated directory listings. Must be one of "Name", "Size" or "Time" (default value "Name").
|
||||||
* `DirectoryReverse`: Boolean, if true automatically generated directory listings will list files in descending order of whatever `DirectorySort` is set to (default value false).
|
* `DirectoryReverse` (boolean): if true, automatically generated directory listings will list files in descending order of whatever `DirectorySort` is set to (default value false).
|
||||||
|
* `DirectoryTitles` (boolean): if true, automatically generated directory listings will use the first top-level heading (i.e. line beginning with "# ") in files with an extension of `GeminiExt` instead of the filename (default value false).
|
||||||
|
|
|
@ -21,6 +21,7 @@ type Config struct {
|
||||||
SCGIPaths map[string]string
|
SCGIPaths map[string]string
|
||||||
DirectorySort string
|
DirectorySort string
|
||||||
DirectoryReverse bool
|
DirectoryReverse bool
|
||||||
|
DirectoryTitles bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type MollyFile struct {
|
type MollyFile struct {
|
||||||
|
|
31
handler.go
31
handler.go
|
@ -312,12 +312,12 @@ func generateDirectoryListing(URL *url.URL, path string, config Config) string {
|
||||||
if uint64(file.Mode().Perm())&0444 != 0444 {
|
if uint64(file.Mode().Perm())&0444 != 0444 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
listing += fmt.Sprintf("=> %s %s\n", url.PathEscape(file.Name()), generatePrettyFileLabel(file))
|
listing += fmt.Sprintf("=> %s %s\n", url.PathEscape(file.Name()), generatePrettyFileLabel(file, path, config))
|
||||||
}
|
}
|
||||||
return listing
|
return listing
|
||||||
}
|
}
|
||||||
|
|
||||||
func generatePrettyFileLabel(info os.FileInfo) string {
|
func generatePrettyFileLabel(info os.FileInfo, path string, config Config) string {
|
||||||
var size string
|
var size string
|
||||||
if info.IsDir() {
|
if info.IsDir() {
|
||||||
size = " "
|
size = " "
|
||||||
|
@ -335,11 +335,12 @@ func generatePrettyFileLabel(info os.FileInfo) string {
|
||||||
size = "GIGANTIC"
|
size = "GIGANTIC"
|
||||||
}
|
}
|
||||||
|
|
||||||
var name string
|
name := info.Name()
|
||||||
if len(info.Name()) > 40 {
|
if config.DirectoryTitles && filepath.Ext(name) == "."+config.GeminiExt {
|
||||||
|
name = readHeading(path, info)
|
||||||
|
}
|
||||||
|
if len(name) > 40 {
|
||||||
name = info.Name()[:36] + "..."
|
name = info.Name()[:36] + "..."
|
||||||
} else {
|
|
||||||
name = info.Name()
|
|
||||||
}
|
}
|
||||||
if info.IsDir() {
|
if info.IsDir() {
|
||||||
name += "/"
|
name += "/"
|
||||||
|
@ -347,6 +348,24 @@ func generatePrettyFileLabel(info os.FileInfo) string {
|
||||||
return fmt.Sprintf("%-40s %s %v", name, size, info.ModTime().Format("Jan _2 2006"))
|
return fmt.Sprintf("%-40s %s %v", name, size, info.ModTime().Format("Jan _2 2006"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readHeading(path string, info os.FileInfo) string {
|
||||||
|
filePath := filepath.Join(path, info.Name())
|
||||||
|
file, err := os.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return info.Name()
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
if strings.HasPrefix(line, "# ") {
|
||||||
|
return strings.TrimSpace(line[1:])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return info.Name()
|
||||||
|
}
|
||||||
|
|
||||||
func serveFile(path string, log *LogEntry, conn net.Conn, config Config) {
|
func serveFile(path string, log *LogEntry, conn net.Conn, config Config) {
|
||||||
// Get MIME type of files
|
// Get MIME type of files
|
||||||
ext := filepath.Ext(path)
|
ext := filepath.Ext(path)
|
||||||
|
|
Loading…
Reference in New Issue