markup

package
v0.24.8 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2024 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package markup converts a custom html-like markup into markdown-like text.

Using an interpretation of English-like text as a series of characters in left-to-right columns and top-to-bottom rows, then:

  • Newlines: move to the start of the next row.
  • Paragraphs: act as a newline followed by an empty row.
  • Soft-newlines: at the start of a row, these do nothing. Otherwise they act like a newline.
  • Soft-paragraphs: after an empty row, these do nothing. Otherwise they act like a paragraph.
  • Lists: a set of uniformly indented rows which may contain nested lists of deeper indentation.
  • Ordered lists : each row in the list starts with a number, starting with 1. increasing by 1.
  • Unordered lists: each row in the list starts with some uniform marker, ex. a bullet.

The custom html elements include:

<b>   - bold text
<i>   - italic text
<s>   - strikethrough
<u>   - underlined text
<hr>  - horizontal divider
<br>  - new line
<p>   - soft-paragraph ( there is no "hard" paragraph marker )
<wbr> - soft-newline
<a="ref"></a> - link text
<ol></ol> - ordered list
<ul></ul> - unordered list
<li></li> - line item in a list

Nested tags are okay; interleaved tags are not supported. For example:

<b><i></i></b>

is fine. While this:

<b><i></b></i>

will cause trouble.

Unknown tags are left as is.

Generalized tags attributes ( `<b class="...">` ) are not supported. Links using `<a>` were added for use with menus. Their interpretation depends on the host and context.

Index

Constants

View Source
const (
	// Starts a new line of text.
	Newline = '\n'
	// Conditionally prints a single line of blank text;
	// doesn't write a blank line if there already is one.
	Paragraph = '\v'
	// Starts a new line only if the output isnt already at the start of a newline.
	Softline = '\r'
	Space    = ' '
)
View Source
const (
	TagLink          = "a"
	TagBold          = "b"
	TagDivider       = "hr"
	TagItalic        = "i"
	TagItem          = "li"
	TagStrike        = "s"
	TagUnderline     = "u"
	TagParagraph     = "p"
	TagNewline       = "br"
	TagSoftline      = "wbr"
	TagOrderedList   = "ol"
	TagUnorderedList = "ul"
)

Variables

View Source
var Formats = Formatting{
	TagLink:      FormatFunc(linkFormat),
	TagBold:      Format{Sub: "**"},
	TagDivider:   Format{Sub: "\r-----------------------------\n"},
	TagItalic:    Format{Sub: "*"},
	TagStrike:    Format{Sub: "~~"},
	TagUnderline: Format{Sub: "__"},

	TagParagraph: Format{Rune: Paragraph},
	TagNewline:   Format{Rune: Newline},
	TagSoftline:  Format{Rune: Softline},

	TagItem:          FormatFunc(listItem),
	TagOrderedList:   listFormat(true),
	TagUnorderedList: listFormat(false),
}

Formats - the default set of replacement string values.

View Source
var Tabwidth = 2

Tabwidth - how many spaces should list indentation generate

Functions

func ToText

func ToText(w io.Writer) io.Writer

listen for html-like markup written to the returned stream and write its plain text equivalent into the passed stream. ex. writing <b>bold</b> will output **bold** to w.

Types

type Format

type Format struct {
	Sub   string // replaces the opening tag
	Close string // optionally, replaces the closing tag ( but only if sub is specified )
	Rune  rune   // when sub is empty
}

Format - generic implementation of FormatWriter If a substitution string exists, it replaces the opening and closing tags; otherwise the specified rune replaces the opening tag, and Close is ignored.

func (Format) WriteClose added in v0.24.8

func (g Format) WriteClose(c *converter) (err error)

func (Format) WriteOpen added in v0.24.8

func (g Format) WriteOpen(c *converter, attr string) (_ error)

type FormatFunc added in v0.24.8

type FormatFunc func(c *converter, attr string, open bool) error

function based implementation of FormatWriter interface

func (FormatFunc) WriteClose added in v0.24.8

func (fn FormatFunc) WriteClose(c *converter) error

func (FormatFunc) WriteOpen added in v0.24.8

func (fn FormatFunc) WriteOpen(c *converter, attr string) error

type FormatWriter added in v0.24.8

type FormatWriter interface {
	WriteOpen(c *converter, attr string) error
	WriteClose(c *converter) error
}

FormatWriter - generate replacement text for certain html-like tags.

type Formatting

type Formatting map[string]FormatWriter

Formatting - all of the unique replacement strings. Which html tags map to which format is hardcoded.

func (*Formatting) WriteTag

func (fs *Formatting) WriteTag(c *converter, tag, attr string) (okay bool, err error)

returns true if it recognized the tag

Jump to

Keyboard shortcuts

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