An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# bbgo
[![Build Status](https://travis-ci.org/namreg/bbgo.svg?branch=master)](https://travis-ci.org/namreg/bbgo)
[![Go Report Card](https://goreportcard.com/badge/github.com/namreg/bbgo)](https://goreportcard.com/report/github.com/namreg/bbgo)
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](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]` --> `alt`
* `[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`: