Documentation
¶
Overview ¶
Package bbcode implements a bbcode parser and converter.
Example ¶
package main
import (
"os"
"vimagination.zapto.org/bbcode"
"vimagination.zapto.org/bbcode/bbcodehtml"
)
func main() {
parser := bbcode.New(bbcodehtml.All...)
parser.ConvertString(os.Stdout, `This is [b]Bold[/b], this is [i]Italic[/i], and this is a [url=http://www.example.com]link[/url].`)
}
Output: This is <b>Bold</b>, this is <i>Italic</i>, and this is a <a href="http://www.example.com">link</a>.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 )
Functions ¶
This section is empty.
Types ¶
type AttrFilterFunc ¶
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 ¶
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 {
// contains filtered or unexported fields
}
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) Open ¶
func (a *AttributeTag) Open(p *Processor, attr string)
Open outputs the opening of the tag.
type BBCode ¶
type BBCode struct {
// contains filtered or unexported fields
}
BBCode is a fully configured parser.
func New ¶
New create a new bbCode parser with the default configuration. See NewWithConfig for more information.
func NewWithConfig ¶
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 ¶
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 ¶
ConvertReader functions like Convert, but takes a io.Reader.
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 asterisk.
ValidTagName string
}
Config changes how the syntax of the interpreted bbCode.
type FilterTag ¶
type FilterTag struct {
OpenClose
// contains filtered or unexported fields
}
FilterTag is a Handler that filters which child nodes are processed.
func NewFilterTag ¶
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.
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 ¶
OpenClose is an interface for the methods required by FilterTag. Both Tag and AttributeTag implement this interface.
type Processor ¶
type Processor struct {
// contains filtered or unexported fields
}
Processor contains methods necessary for creating custom Handlers.
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 ¶
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 ¶
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 ¶
ProcessTag will process the given tag as normal.
type Tag ¶
type Tag struct {
// contains filtered or unexported fields
}
Tag is a simple Handler that just outputs open and closing tags.
func NewTag ¶
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>"))