From f3fcd25e29d1a8c4d355eb650091e2920def658e Mon Sep 17 00:00:00 2001 From: la-ninpre Date: Wed, 10 Dec 2025 22:48:14 +0300 Subject: init --- atom/LICENSE | 29 ++++++++++++++++++ atom/atom.go | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 atom/LICENSE create mode 100644 atom/atom.go (limited to 'atom') diff --git a/atom/LICENSE b/atom/LICENSE new file mode 100644 index 0000000..3aae2fb --- /dev/null +++ b/atom/LICENSE @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2022, +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +* Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/atom/atom.go b/atom/atom.go new file mode 100644 index 0000000..f0c49e8 --- /dev/null +++ b/atom/atom.go @@ -0,0 +1,96 @@ +// taken from https://git.sr.ht/~m15o/html-journal +// Copyright 2022 m15o +// Distributed under the terms of a BSD 3-Clause license, see LICENSE file. + +package atom + +import ( + "encoding/xml" + "git.sr.ht/~m15o/htmlj" + "time" +) + +type Feed struct { + XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"` + Title string `xml:"title"` + ID string `xml:"id"` + Link []Link `xml:"link"` + Updated TimeStr `xml:"updated"` + Author *Person `xml:"author"` + Icon string `xml:"icon,omitempty"` + Logo string `xml:"logo,omitempty"` + Subtitle string `xml:"subtitle,omitempty"` + Entry []*Entry `xml:"entry"` +} + +type Entry struct { + Title string `xml:"title"` + ID string `xml:"id"` + Link []Link `xml:"link"` + Published TimeStr `xml:"published"` + Updated TimeStr `xml:"updated"` + Author *Person `xml:"author"` + Summary *Text `xml:"summary"` + Content *Text `xml:"content"` +} + +type Link struct { + Rel string `xml:"rel,attr,omitempty"` + Href string `xml:"href,attr"` + Type string `xml:"type,attr,omitempty"` + HrefLang string `xml:"hreflang,attr,omitempty"` + Title string `xml:"title,attr,omitempty"` + Length uint `xml:"length,attr,omitempty"` +} + +type Person struct { + Name string `xml:"name"` + URI string `xml:"uri,omitempty"` + Email string `xml:"email,omitempty"` + InnerXML string `xml:",innerxml"` +} + +type Text struct { + Type string `xml:"type,attr"` + Body string `xml:",chardata"` +} + +type TimeStr string + +func Time(t time.Time) TimeStr { + return TimeStr(t.Format("2006-01-02T15:04:05-07:00")) +} + +func FeedFromJournal(u string, j *htmlj.Journal) *Feed { + f := &Feed{ + Title: j.Title, + ID: u, + Author: &Person{ + Name: j.Title, + URI: u, + }, + Updated: Time(j.Updated), + Link: []Link{ + { + Rel: "alternate", + Href: u, + }, + }, + } + + for i := 0; i < len(j.Entries); i++ { + p := j.Entries[i] + f.Entry = append(f.Entry, &Entry{ + Title: p.Title, + ID: u + "#" + p.Title, + Published: Time(p.Published), + Updated: Time(p.Published), + Content: &Text{ + Type: "html", + Body: p.Content, + }, + }) + } + + return f +} -- cgit v1.2.3