util

package
v0.0.0-...-186a780 Latest Latest
Warning

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

Go to latest
Published: May 20, 2021 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package util contains general utilities that are used in multiple places throughout gemdown.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyRewriteSpecs

func ApplyRewriteSpecs(specs []*RewriteSpec, link string) (string, error)

ApplyRewriteSpecs applies multiple rewrite specs to a single link, returning the final link.

Types

type RewriteSpec

type RewriteSpec struct {

	// Pattern is a regular expression. Any links which match this pattern
	// are re-written according to this spec.
	Pattern string

	// Scheme is the protocol to which the link should be re-written.
	// I.e. the link's existing protocol is replaced with this.  For
	// example, 'http' or 'https'. If empty, the link protocol is not
	// changed.
	Scheme string

	// Extension is the extension with which the link should be re-written.
	// In other words, the link's extension is replaced with this. If
	// empty, the link extension is not changed.
	Extension string

	// Proxy is a format string into which the link is substituted after
	// the protcol and extension have been re-written (if the pertinent
	// fields are non-empty). The format string should contain exactly one
	// "%s". The intended use case of this field is to allow Gemini or
	// Gopher links to be re-written to have appropriate proxies when
	// converting to HTTP.
	Proxy string

	// Encode if true will cause the link to be URL-encoded before
	// being inserted into the Proxy format string. Only has an effect
	// if Proxy is non-empty.
	Encode bool
}

RewriteSpec specifies how some class of links is to be rewritten.

func UnmarshalRewriteSpec

func UnmarshalRewriteSpec(text []byte) ([]*RewriteSpec, error)

UnmarshalRewriteSpec loads a JSON string given as the text argument and returna list of RewriteSpec objects.

func (r *RewriteSpec) RewriteLink(link string) (string, error)

RewriteLink implements the link rewriting behavior for gemdown. This can fail if the RewriteSpec has an invalid regular expression, or if link is not an IEEE RFC3986 compliant URI. If the link does not match the pattern for the RewriteSpec, it is returned unchanged. If an error occurs, the link is also returned unchanged.

Jump to

Keyboard shortcuts

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