atomconv

package
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2023 License: Unlicense Imports: 15 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Auto = types.Middleware(func(h types.Handler) types.Handler {
	return types.HandlerFunc(func(ctx context.Context, request *types.Request) *types.Response {
		if !(request.Scheme == "gemini" || request.Scheme == "spartan") || !strings.HasSuffix(request.Path, ".atom") {
			return h.Handle(ctx, request)
		}

		r := *request
		u := *request.URL
		u.Path = u.Path[:len(u.Path)-5]
		r.URL = &u

		response := h.Handle(ctx, &r)

		var (
			success      sliderule.Status
			buildSuccess func(string, io.Reader) *sliderule.Response
			buildFailure func(error) *sliderule.Response
		)
		switch request.Scheme {
		case "gemini":
			success = gemini.StatusSuccess
			buildSuccess = gemini.Success
			buildFailure = gemini.Failure
		case "spartan":
			success = spartan.StatusSuccess
			buildSuccess = spartan.Success
			buildFailure = spartan.ServerError
		}

		if response.Status != success {
			return response
		}

		mtype, _, err := mime.ParseMediaType(response.Meta.(string))
		if err != nil || mtype != "text/gemini" {
			return response
		}

		defer func() {
			_ = response.Close()
		}()

		doc, err := gemtext.Parse(response.Body)
		if err != nil {
			return buildFailure(err)
		}

		buf := &bytes.Buffer{}
		if err := Convert(buf, doc, request.URL); err != nil {
			return buildFailure(err)
		}
		return buildSuccess("application/atom+xml; charset=utf-8", buf)
	})
})

Auto is a middleware which builds atom feeds for any gemtext pages.

It looks for requests ending with the '.atom' extension, passes through the request with the extension clipped off, then if the response is in gemtext it converts it to an Atom feed according to the gmisub spec at gemini://gemini.circumlunar.space/docs/companion/subscription.gmi

Functions

func Convert

func Convert(wr io.Writer, doc gemtext.Document, location *url.URL) error

Convert turns 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

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL