https://github.com/namreg/bbgo
Fast and customizable bbcode parser written in go
https://github.com/namreg/bbgo
bbcode bbcode-parser go
Last synced: 3 months ago
JSON representation
Fast and customizable bbcode parser written in go
- Host: GitHub
- URL: https://github.com/namreg/bbgo
- Owner: namreg
- License: mit
- Created: 2019-01-30T21:16:21.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-02-19T08:05:51.000Z (about 7 years ago)
- Last Synced: 2024-06-20T17:48:06.765Z (almost 2 years ago)
- Topics: bbcode, bbcode-parser, go
- Language: Go
- Homepage:
- Size: 1.55 MB
- Stars: 4
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bbgo
[](https://travis-ci.org/namreg/bbgo)
[](https://goreportcard.com/report/github.com/namreg/bbgo)
[](https://github.com/namreg/bbgo/blob/master/LICENSE)
BBGO is a fast [bbcode](https://en.wikipedia.org/wiki/BBCode) compiler for Go with supporting custom tags.
## Usage
```go
bbg := bbgo.New()
fmt.Println(bbg.Parse("[b]Hello World[/b]"))
// Output:
// Hello World
```
## Supported BBCode Syntax
```
[tag]basic tag[/tag]
[tag1][tag2]nested tags[/tag2][/tag1]
[tag=value]tag with value[/tag]
[tag arg=value]tag with named argument[/tag]
[tag="quote value"]tag with quoted value[/tag]
[tag=value foo="hello world" bar=baz]multiple tag arguments[/tag]
```
## Default Tags
* `[b]text[/b]` --> `text` (b, i, u, and s all map the same)
* `[url]link[/url]` --> `link`
* `[url=link]text[/url]` --> `text`
* `[img]link[/img]` --> `
`
* `[img=link]alt[/img]` --> `
`
* `[color=red]text[/color]` --> `text`
* `[quote]text[/quote]` --> `
Quotetext
`
* `[quote=Somebody]text[/quote]` --> `
Somebody said:text
`
* `[quote name=Somebody]text[/quote]` --> `
Somebody said:text
`
* `[code][b]anything[/b][/code]` --> `[b]anything[/b]
`
* `[list][*] item 1[*] item 2[*] item 3[/list]` --> `- item 1
- item 2
- item 3
## Adding Custom Tags
```go
bbg.RegisterTag("color", bbgo.Processor(func(ctx *context.Context, tag node.Tag, w io.Writer) {
switch t := tag.(type) {
case *node.OpeningTag:
// write here logic for opening tag
case *node.ClosingTag:
// write here logic for opening tag
case *node.SelfClosingTag:
// write here logic for self-closing tag
}
}))
```
The default tags can also be modified by calling `bbg.RegisterTag`: