Git Product home page Git Product logo

bbcode's Introduction

bbcode

-- import "vimagination.zapto.org/bbcode"

Package bbcode implements a bbcode parser and converter

Usage

var (
	// HTMLText is a text processor that will HTML encode all text output
	HTMLText htmlText
	// PlainText is a text processor that just outputs all text with no change
	PlainText plainText
)

type AttrFilterFunc

type AttrFilterFunc func(string) []byte

AttrFilterFunc is a wrapper for a func so that it satisfies the AttrFilterer interface

func (AttrFilterFunc) AttrFilter

func (a AttrFilterFunc) AttrFilter(attr string) []byte

AttrFilter satisfies the AttrFilterer interface

type AttrFilterer

type AttrFilterer interface {
	AttrFilter(string) []byte
}

AttrFilterer is used with AttributeTag to provide an attribute filter for the AttributeTag. It is used to process the parsed attribute for writing.

type AttributeTag

type AttributeTag struct {
}

AttributeTag is a simple Handler that outputs and open tag, with an attribute, and a close tag.

func NewAttributeTag

func NewAttributeTag(name string, open, openClose, attrOpen, attrClose, close []byte, filter AttrFilterer) *AttributeTag

NewAttributeTag creates a new Attribute Tag. The open byte slice is used to start the open tag and the openClose is used to close the open tag. The attrOpen and attrClose byte slices are used to surround the filtered attribute, within the open tag. Lastly, the close byte slice is used for the closing tag. The filter is used to modify the attribute, whether to correct formatting errors, or to validate. If a nil slice is returned, then no attribute is outputted. For example the following would create a colour tag for handling font colour:

NewAttributeTag("colour",
	[]byte("<span"),
	[]byte(">"),
	[]byte(" style=\"color: "),
	[]byte("\""),
	[]byte("</span>"),
	colourChecker)

A nil filter means that the attr will be written to the output with HTML encoding

func (*AttributeTag) Close

func (a *AttributeTag) Close(p *Processor, attr string)

Close outputs the closing of the tag

func (*AttributeTag) Handle

func (a *AttributeTag) Handle(p *Processor, attr string)

Handle processes the tag

func (*AttributeTag) Name

func (a *AttributeTag) Name() string

Name returns the name of the tag

func (*AttributeTag) Open

func (a *AttributeTag) Open(p *Processor, attr string)

Open outputs the opening of the tag

type BBCode

type BBCode struct {
}

BBCode is a fully configured parser

func New

func New(tags ...Handler) *BBCode

New create a new bbCode parser with the default configuration. See NewWithConfig for more information

func NewWithConfig

func NewWithConfig(c Config, tags ...Handler) *BBCode

NewWithConfig creates a bbCode parser with a custom bbCode configuration. The tags are a list of Handlers that will be used to process the tag tokens that are generated by the parser. The first Handler with an empty string for a name with be used to process the text. By default, this is the HTMLText handler.

func (*BBCode) Convert

func (b *BBCode) Convert(w io.Writer, input []byte) error

Convert parses the byte slice and writes the output to the writer. Any error will be from the writing process and not from the parser

func (*BBCode) ConvertReader

func (b *BBCode) ConvertReader(w io.Writer, input io.Reader) error

ConvertReader functions like Convert, but takes a io.Reader

func (*BBCode) ConvertString

func (b *BBCode) ConvertString(w io.Writer, input string) error

ConvertString functions like Convert, but takes a string

type CloseTag

type CloseTag struct {
	Name string
}

CloseTag is a token containing the name of the tag

type Config

type Config struct {
	// TagOpen is the character used to open the tags.
	// In the default configuration this is the open bracket '['
	TagOpen rune

	// TagClose is the character used to close the tags.
	// In the default configuration this is the close bracket ']'
	TagClose rune

	// ClosingTag is the character used to define a closing tag, as opposed
	// to an opening tag.
	// In the default configuration, this is the slash '/'
	ClosingTag rune

	// AttributeSep is the character used to separate the tag name from the
	// attribute.
	// In the default configuration this is the equals sign '='
	AttributeSep rune

	// ValidTagName lists the characters that are allowed in the tag names.
	// Neither of the TagClose or AttributeSep characters should be in this
	// list.
	// In the default configuration this is A-Z a-z 0-9 and the asterix
	ValidTagName string
}

Config changes how the syntax of the inteperated bbCode

type FilterTag

type FilterTag struct {
	OpenClose
}

FilterTag is a Handler that filters which child nodes are processed

func NewFilterTag

func NewFilterTag(o OpenClose, filter func(string) bool) *FilterTag

NewFilterTag creates a Handler that filters which child nodes are processed. The filter takes the name of the tag and returns a bool determining whether the tag will be processed as a tag or as text. An empty tag name in the filter is used to determine is text is processed.

func (*FilterTag) Handle

func (f *FilterTag) Handle(p *Processor, attr string)

Handle processes the tag, using its filter to determine which children are also processed

type Handler

type Handler interface {
	// Name returns the name of the bbCode tag that this will be used for.
	// Returning an empty string indicates that this Handler should be used
	// for text handling.
	Name() string
	// Handle takes a pointer to the Processor and the attribute to the tag.
	Handle(*Processor, string)
}

Handler is an interface that represents the text and tag processors

type OpenClose

type OpenClose interface {
	Name() string
	Open(*Processor, string)
	Close(*Processor, string)
}

OpenClose is an interface for the methods required by FilterTag. Both Tag and AttributeTag implement this interface

type OpenTag

type OpenTag struct {
	Name string
	Attr *string
}

OpenTag is a token containing the name of the tag and a possible attribute.

type Processor

type Processor struct {
}

Processor contains methods necessary for creating custom Handler's

func (*Processor) Get

func (p *Processor) Get() interface{}

Get returns the next token. It will be either a Text, OpenTag or a CloseTag

func (*Processor) GetContents

func (p *Processor) GetContents(untilTag string) string

GetContents grabs the raw contents of a tag and returns it as a string

func (*Processor) Print

func (p *Processor) Print(t interface{})

Print writes the textual representation of the given token to the output, using the text Handler

func (*Processor) Process

func (p *Processor) Process(untilTag string) bool

Process will continue processing the bbCode until it gets to an end tag which matches the tag name given, or until it reaches the end of the input. It returns true if the end tag was found, or false otherwise.

func (*Processor) ProcessTag

func (p *Processor) ProcessTag(t OpenTag)

ProcessTag will process the given tag as normal

func (*Processor) Write

func (p *Processor) Write(b []byte) (int, error)

Write writes to the underlying writer. The error is stored and does not need to be handled

type Tag

type Tag struct {
}

Tag is a simple Handler that just outputs open and closing tags

func NewTag

func NewTag(name string, open, close []byte) *Tag

NewTag creates a simple Handler that outputs an open and close tag. For example, the following would be used to create a tag for handling bold:

NewTag("b", []byte("<b>"), []("</b>"))

func (*Tag) Close

func (t *Tag) Close(p *Processor, attr string)

Close outputs the closing of the tag

func (*Tag) Handle

func (t *Tag) Handle(p *Processor, attr string)

Handle processes the tag

func (*Tag) Name

func (t *Tag) Name() string

Name returns the name of the tag

func (*Tag) Open

func (t *Tag) Open(p *Processor, attr string)

Open outputs the opening of the tag

type Text

type Text []string

Text is a token containing simple textual data

bbcode's People

Contributors

mjkwoolnough avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.