From 4b42fce53b80c8eb1796c4fb4c3966fb4c759677 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Fri, 25 Aug 2023 20:46:28 -0600 Subject: GmisubToAtom: update docs and names, add tests --- gemini/gemtext/sub.go | 34 ++++++++++++++++++---------------- gemini/gemtext/sub_test.go | 2 +- 2 files changed, 19 insertions(+), 17 deletions(-) (limited to 'gemini/gemtext') diff --git a/gemini/gemtext/sub.go b/gemini/gemtext/sub.go index a99bed2..1247643 100644 --- a/gemini/gemtext/sub.go +++ b/gemini/gemtext/sub.go @@ -10,16 +10,28 @@ import ( "time" ) -type gemSub struct { +// GmisubToAtom converts a gemini document to Atom format. +// +// It identifies feed fields and entries according to the specification at +// gemini://gemini.circumlunar.space/docs/companion/subscription.gmi +func GmisubToAtom(doc Document, location url.URL) string { + buf := &bytes.Buffer{} + if err := atomTmpl.Execute(buf, parseGemSub(doc, &location)); err != nil { + panic(err) + } + return `` + "\n" + buf.String() +} + +type gmiSub struct { ID template.URL Title string Subtitle string Updated string - Entries []gemSubEntry + Entries []gmiSubEntry } -type gemSubEntry struct { +type gmiSubEntry struct { ID template.URL Updated string Title string @@ -27,8 +39,8 @@ type gemSubEntry struct { var linkElemRE = regexp.MustCompile(`(\d{4})-([0-1]\d)-([0-3]\d)`) -func parseGemSub(doc Document, location *url.URL) *gemSub { - sub := &gemSub{ID: template.URL(location.String())} +func parseGemSub(doc Document, location *url.URL) *gmiSub { + sub := &gmiSub{ID: template.URL(location.String())} updated := time.Time{} for i, line := range doc { @@ -75,7 +87,7 @@ func parseGemSub(doc Document, location *url.URL) *gemSub { entryUpdated := time.Date(year, time.Month(month), day, 12, 0, 0, 0, time.UTC) entryTitle := strings.TrimLeft(strings.TrimPrefix(strings.TrimLeft(label[10:], " \t"), "-"), " \t") - sub.Entries = append(sub.Entries, gemSubEntry{ + sub.Entries = append(sub.Entries, gmiSubEntry{ ID: template.URL(line.(LinkLine).URL()), Updated: entryUpdated.Format(time.RFC3339), Title: entryTitle, @@ -91,16 +103,6 @@ func parseGemSub(doc Document, location *url.URL) *gemSub { return sub } -func GemsubToAtom(doc Document, location url.URL) string { - buf := &bytes.Buffer{} - if err := atomTmpl.Execute(buf, parseGemSub(doc, &location)); err != nil { - panic(err) - } - return `` + "\n" + buf.String() -} - - - var atomTmpl = template.Must(template.New("atom").Parse(` {{.ID}} diff --git a/gemini/gemtext/sub_test.go b/gemini/gemtext/sub_test.go index 8bba682..26f98ed 100644 --- a/gemini/gemtext/sub_test.go +++ b/gemini/gemtext/sub_test.go @@ -51,7 +51,7 @@ func TestGemsubToAtom(t *testing.T) { if err != nil { t.Fatal(err) } - xml := GemsubToAtom(doc, *loc) + xml := GmisubToAtom(doc, *loc) if xml != test.output { t.Fatal("mismatched output") } -- cgit v1.2.3