flounder

A simple gemini site builder
Log | Files | Refs | README | LICENSE

commit c97be5a5ae4cbe60128edba800f1196967036bde
parent 08597a8cdf70f93bd5305c5c1ff3fe0cc2274c60
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sun, 27 Dec 2020 19:21:10 -0800

Add auto gemfeed parsing

Diffstat:
Mgemfeed.go | 36+++++++++++++++++++++++++-----------
Mgemini.go | 10+++++++++-
Mhttp.go | 3+--
Atemplates/gemfeed.gmi | 6++++++
4 files changed, 41 insertions(+), 14 deletions(-)

diff --git a/gemfeed.go b/gemfeed.go @@ -28,19 +28,22 @@ type FeedEntry struct { 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 @@ func generateFeedFromFolder(folder string) []*FeedEntry { } 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 @@ func ParseGemfeed(text io.Reader, baseUrl url.URL, limit int) (*Gemfeed, error) } 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)" } diff --git a/gemini.go b/gemini.go @@ -17,7 +17,15 @@ import ( 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 { diff --git a/http.go b/http.go @@ -553,8 +553,7 @@ func userFile(w http.ResponseWriter, r *http.Request) { _, 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) } diff --git a/templates/gemfeed.gmi b/templates/gemfeed.gmi @@ -0,0 +1,6 @@ +{{$host := .Host }} +# {{ .Title }} +{{ range .FeedEntries }} +=> {{.File}} {{.DateString}} {{.Title}} {{end}} + +=> //{{$host}} Home