git.alexw.nyc home about git garden
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
// Parses Gemfeed according to the companion spec:
// gemini://gemini.circumlunar.space/docs/companion/subscription.gmi
package main

import (
	"bufio"
	"io/ioutil"
	"net/url"
	"os"
	"path"
	"path/filepath"
	"sort"
	"strings"
	"time"

	feeds "git.sr.ht/~aw/gorilla-feeds"
)

type Gemfeed struct {
	Title   string
	Creator string
	Url     *url.URL
	Entries []FeedEntry
}

func (gf *Gemfeed) toAtomFeed() string {
	feed := feeds.Feed{
		Title:  gf.Title,
		Author: &feeds.Author{Name: gf.Creator},
		Link:   &feeds.Link{Href: gf.Url.String()},
	}
	feed.Items = []*feeds.Item{}
	for _, fe := range gf.Entries {
		feed.Items = append(feed.Items, &feeds.Item{
			Title:   fe.Title,
			Link:    &feeds.Link{Href: fe.Url.String()}, // Rel=alternate?
			Created: fe.Date,                            // Updated not created?
			Content: fe.Content,
		})
	}
	res, _ := feed.ToAtom()
	return res
}

type FeedEntry struct {
	Title      string
	Url        *url.URL
	Date       time.Time
	DateString string
	Feed       *Gemfeed
	File       string // TODO refactor
	Content    string
}

func urlFromPath(fullPath string) url.URL {
	creator := getCreator(fullPath)
	baseUrl := url.URL{}
	baseUrl.Host = creator + "." + c.Host
	baseUrl.Path = getLocalPath(fullPath)
	return baseUrl
}

// Non-standard extension
// Requires yyyy-mm-dd formatted files
func generateFeedFromUser(user string) *Gemfeed {
	gemlogFolderPath := path.Join(c.FilesDirectory, user, GemlogFolder)
	// NOTE: assumes sanitized input
	u := urlFromPath(gemlogFolderPath)
	feed := Gemfeed{
		Title:   strings.Title(user) + "'s Gemlog",
		Creator: user,
		Url:     &u,
	}
	err := filepath.Walk(gemlogFolderPath, func(thepath string, info os.FileInfo, err error) error {
		base := path.Base(thepath)
		if len(base) >= 10 {
			entry := FeedEntry{}
			date, err := time.Parse("2006-01-02", base[:10])
			if err != nil {
				return nil
			}
			entry.Date = date
			entry.DateString = base[:10]
			entry.Feed = &feed
			f, err := os.Open(thepath)
			if err != nil {
				return nil
			}
			defer f.Close()
			scanner := bufio.NewScanner(f)
			for scanner.Scan() {
				// skip blank lines
				if scanner.Text() == "" {
					continue
				}
				line := scanner.Text()
				if strings.HasPrefix(line, "#") {
					entry.Title = strings.Trim(line, "# \t")
				} else {
					var title string
					if len(line) > 50 {
						title = line[:50]
					} else {
						title = line
					}
					entry.Title = "[" + title + "...]"
				}
				break
			}
			content, err := ioutil.ReadFile(thepath)
			if err != nil {
				return nil
			}
			entry.Content = string(content)
			entry.File = getLocalPath(thepath)
			u := urlFromPath(thepath)
			entry.Url = &u
			feed.Entries = append(feed.Entries, entry)
		}
		return nil
	})
	if err != nil {
		return nil
	}
	// Reverse chronological sort
	sort.Slice(feed.Entries, func(i, j int) bool {
		return feed.Entries[i].Date.After(feed.Entries[j].Date)
	})
	return &feed
}