markup

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2024 License: MIT Imports: 6 Imported by: 3

Documentation

Overview

Package markup implements an API for escaping markup elements in raw text input.

Cleaner wraps a WriteCloser, and wraps common functions to write properly formatted markup to the Writer - these are the *Escaped variant functions. Cleaner also provides the generic methods "Write" and "WriteString", to write unmodified text to the underlying WriteCloser.

Altid-flavoured markup

Described in more detail in the official document https://altid.github.io/markdown.html Common markup elements are generally easier to insert by hand, but several helper types are provided for more complex elements: color, url, and image; which are described in greater detail in greater detail below.

Index

Examples

Constants

View Source
const (
	NormalText byte = iota
	ColorCode
	ColorText
	ColorTextBold
	ColorTextStrong
	ColorTextStrike
	ColorTextEmphasis
	URLLink
	URLText
	ImagePath
	ImageText
	ImageLink
	BoldText
	StrikeText
	EmphasisText
	StrongText
	ErrorText
	EOF
)

The various types of text tokens

View Source
const (
	White      = "white"
	Black      = "black"
	Blue       = "blue"
	Green      = "green"
	Red        = "red"
	Brown      = "brown"
	Purple     = "purple"
	Orange     = "orange"
	Yellow     = "yellow"
	LightGreen = "lightgreen"
	Cyan       = "cyan"
	LightCyan  = "lightcyan"
	LightBlue  = "lightblue"
	Pink       = "pink"
	Grey       = "grey"
	LightGrey  = "lightgrey"
)

Available markup colour codes, feel free to PR your favourite

Variables

This section is empty.

Functions

func EscapeString

func EscapeString(msg string) string

EscapeString returns a properly escaped Altid markup string

Types

type Cleaner

type Cleaner struct {
	// contains filtered or unexported fields
}

Cleaner represents a WriteCloser used to escape any altid markdown elements from a reader

func NewCleaner

func NewCleaner(w io.WriteCloser) *Cleaner

NewCleaner returns a Cleaner

func (*Cleaner) Close

func (c *Cleaner) Close()

Close wraps the underlying WriteCloser's Close method

func (*Cleaner) Write

func (c *Cleaner) Write(msg []byte) (n int, err error)

Write call the underlying WriteCloser's Write method Write does not modify the contents of msg

func (*Cleaner) WriteEscaped

func (c *Cleaner) WriteEscaped(msg []byte) (n int, err error)

WriteEscaped writes the properly escaped markdown to the underlying WriteCloser

func (*Cleaner) WriteHeader

func (c *Cleaner) WriteHeader(degree int, msg []byte) (n int, err error)

WriteHeader is a variant of WriteEscaped which writes an nth degree markdown header element to the underlying WriteCloser

func (*Cleaner) WriteList

func (c *Cleaner) WriteList(depth int, msg []byte) (n int, err error)

WriteList is a variant of WriteEscaped which adds an nth-nested markdown list element to the underlying WriteCloser

func (*Cleaner) WriteString

func (c *Cleaner) WriteString(msg string) (n int, err error)

WriteString is a variant of Write which accepts a string as input

func (*Cleaner) WriteStringEscaped

func (c *Cleaner) WriteStringEscaped(msg string) (n int, err error)

WriteStringEscaped is a variant of WriteEscaped which accepts a string as input

func (*Cleaner) Writef

func (c *Cleaner) Writef(format string, args ...any) (n int, err error)

Writef is a variant of Write which accepts a format specifier

func (*Cleaner) WritefEscaped

func (c *Cleaner) WritefEscaped(format string, args ...any) (n int, err error)

WritefEscaped is a variant of WriteEscaped which accepts a format specifier

func (*Cleaner) WritefHeader

func (c *Cleaner) WritefHeader(degree int, format string, args ...any) (n int, err error)

WritefHeader is a variant of WriteHeader which accepts a format specifier

func (*Cleaner) WritefList

func (c *Cleaner) WritefList(depth int, format string, args ...any) (n int, err error)

WritefList is a variant of WriteList which accepts a format specifier

type Color

type Color struct {
	// contains filtered or unexported fields
}

Color represents a color markdown element Valid values for code are any [markup constants], or color strings in hexadecimal form. #000000 to #FFFFFF, as well as #000 to #FFF. No alpha channel support currently exists.

func NewColor

func NewColor(code string, msg []byte) (*Color, error)

NewColor returns a Color Returns error if color code is invalid

func (*Color) String

func (c *Color) String() string

type Image

type Image struct {
	Src string
	Alt string
}

Image represents an image markdown element

func (*Image) String

func (i *Image) String() string

type Item

type Item struct {
	ItemType byte
	Data     []byte
}

Item is returned from a call to Next() ItemType will be an ItemType

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

Lexer allows tokenizing of altid-flavored markdown for client-side parsers

Example
package main

import (
	"github.com/altid/libs/markup"
)

func main() {
	l := markup.NewStringLexer("Text with **bold and _strong tags_**")
	for {
		n := l.Next()
		switch n.ItemType {
		case markup.URLText:
		case markup.BoldText:
		case markup.StrongText:
		case markup.EOF:
			break
		}
	}
}
Output:

func NewLexer

func NewLexer(src []byte) *Lexer

NewLexer takes in a byte array and returns a ready to run Lexer

func NewStringLexer

func NewStringLexer(src string) *Lexer

NewStringLexer takes in a string and returns a ready to run Lexer

func (*Lexer) Bytes

func (l *Lexer) Bytes() ([]byte, error)

Bytes wil return a parsed byte array from the input with markdown elements cleaned Any URL will be turned from `[some text](someurl)` to `some text (some url)` IMG will be turned from `![some text](someimage)` to `some text (some image)` color tags will be removed and the raw text will be output

func (*Lexer) Next

func (l *Lexer) Next() Item

Next returns the next Item from the tokenizer If ItemType is EOF, any subsequent calls to Next() will panic

func (*Lexer) String

func (l *Lexer) String() (string, error)

String is the same as Bytes, but returns a string

type Notifier

type Notifier struct {
	// contains filtered or unexported fields
}

Notifier is a type provided for use with fslib's Notification function

func NewNotifier

func NewNotifier(path, from, msg string) *Notifier

NewNotifier returns a notifier ready for parsing

func (*Notifier) Parse

func (n *Notifier) Parse() (string, string, string)

Parse will properly clean the markdown for the `from` and `msg` elements As well as format the lines to fit the notification idioms expected by clients

type URL

type URL struct {
	Link []byte
	Msg  []byte
}

URL represents a link markdown element

func NewURL

func NewURL(link, msg []byte) (*URL, error)

NewURL returns a URL, or an error if encountered If `msg` is empty, the contents of `link` will be used If `link` is empty, an error will be returned There are no assumptions about what link points to But before a beta release there may be a schema imposed

func (*URL) String

func (u *URL) String() string

The form will be "[msg](link)"

Jump to

Keyboard shortcuts

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