Add auto gemfeed parsing
alex wennerberg alex@alexwennerberg.com
Sun, 27 Dec 2020 19:21:10 -0800
4 files changed,
41 insertions(+),
14 deletions(-)
M
gemfeed.go
→
gemfeed.go
@@ -28,19 +28,22 @@ Url *url.URL
Date time.Time DateString string Feed *Gemfeed + File string // TODO refactor } // Non-standard extension // Requires yyyy-mm-dd formatted files -func generateFeedFromFolder(folder string) []*FeedEntry { - user := getCreator(folder) +func generateFeedFromUser(user string) []FeedEntry { + gemlogFolder := "gemlog" // TODO make configurable + gemlogFolderPath := path.Join(c.FilesDirectory, user, gemlogFolder) + // NOTE: assumes sanitized input feed := Gemfeed{ Title: user + "'s Gemfeed", Creator: user, - // URL? + // URL? etc? } - var feedEntries []*FeedEntry - err := filepath.Walk(folder, func(thepath string, info os.FileInfo, err error) error { + var feedEntries []FeedEntry + err := filepath.Walk(gemlogFolderPath, func(thepath string, info os.FileInfo, err error) error { base := path.Base(thepath) if len(base) >= 10 { entry := FeedEntry{}@@ -57,25 +60,36 @@ return nil
} defer f.Close() scanner := bufio.NewScanner(f) - i := 0 for scanner.Scan() { - if i > 5 { // To be more efficient, only scan the top 5 lines - break + // skip blank lines + if scanner.Text() == "" { + continue } line := scanner.Text() if strings.HasPrefix(line, "#") { entry.Title = strings.Trim(line, "# \t") break + } else { + var title string + if len(line) > 50 { + title = line[:50] + } else { + title = line + } + entry.Title = "[" + title + "...]" } - i += 1 } - // get title from first header + entry.File = base + feedEntries = append(feedEntries, entry) } return nil }) if err != nil { return nil } + sort.Slice(feedEntries, func(i, j int) bool { + return feedEntries[i].Date.After(feedEntries[j].Date) + }) return feedEntries }@@ -156,7 +170,7 @@ parsedUrl.Path = path.Join(path.Dir(baseUrl.Path), parsedUrl.Path)
} parsedUrl.Scheme = "" if time.Now().After(date) { - fe := FeedEntry{title, parsedUrl, date, matches[2], &gf} + fe := FeedEntry{title, parsedUrl, date, matches[2], &gf, ""} if fe.Title == "" { fe.Title = "(Untitled)" }
M
gemini.go
→
gemini.go
@@ -17,7 +17,15 @@
var gt *template.Template func generateGemfeedPage(user string) string { - return "" + feedItems := generateFeedFromUser(user) + data := struct { + Host string + Title string + FeedEntries []FeedEntry + }{c.Host, user + "'s Gemlog", feedItems} + var buff bytes.Buffer + gt.ExecuteTemplate(&buff, "gemfeed.gmi", data) + return buff.String() } func generateFolderPage(fullpath string) string {
M
http.go
→
http.go
@@ -553,8 +553,7 @@ if p == "/" || isDir {
_, err := os.Stat(path.Join(fullPath, "index.gmi")) if os.IsNotExist(err) { if p == "/gemlog" { - // geminiContent = generateGemfeedPage(fullPath) - geminiContent = generateFolderPage(fullPath) + geminiContent = generateGemfeedPage(userName) } else { geminiContent = generateFolderPage(fullPath) }
A
templates/gemfeed.gmi
@@ -0,0 +1,6 @@
+{{$host := .Host }} +# {{ .Title }} +{{ range .FeedEntries }} +=> {{.File}} {{.DateString}} {{.Title}} {{end}} + +=> //{{$host}} Home