Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/vendor/github.com/frustra/bbcode/html.go
akuma06 ed61de6276 Add bbcode support (#1433)
* Add bbcode support

Closes #687
As the issue suggested, I added bbcodes support to the forms.
Now we support basic bbcodes, markdown and html tags

* Add new dependencies
2017-08-29 09:56:44 +10:00

94 lignes
2 Kio
Go

// Copyright 2015 Frustra. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package bbcode
import (
"html"
"net/url"
"strings"
)
// HTMLTag represents a DOM node.
type HTMLTag struct {
Name string
Value string
Attrs map[string]string
Children []*HTMLTag
}
// NewHTMLTag creates a new HTMLTag with string contents specified by value.
func NewHTMLTag(value string) *HTMLTag {
return &HTMLTag{
Value: value,
Attrs: make(map[string]string),
Children: make([]*HTMLTag, 0),
}
}
func (t *HTMLTag) String() string {
var value string
if len(t.Value) > 0 {
value = html.EscapeString(t.Value)
}
var attrString string
for key, value := range t.Attrs {
attrString += " " + key + `="` + strings.Replace(html.EscapeString(value), "\n", "", -1) + `"`
}
if len(t.Children) > 0 {
var childrenString string
for _, child := range t.Children {
childrenString += child.String()
}
if len(t.Name) > 0 {
return value + "<" + t.Name + attrString + ">" + childrenString + "</" + t.Name + ">"
} else {
return value + childrenString
}
} else if len(t.Name) > 0 {
return value + "<" + t.Name + attrString + ">"
} else {
return value
}
}
func (t *HTMLTag) AppendChild(child *HTMLTag) *HTMLTag {
if child == nil {
t.Children = append(t.Children, NewHTMLTag(""))
} else {
t.Children = append(t.Children, child)
}
return t
}
func InsertNewlines(out *HTMLTag) {
if strings.ContainsRune(out.Value, '\n') {
parts := strings.Split(out.Value, "\n")
for i, part := range parts {
if i == 0 {
out.Value = parts[i]
} else {
out.AppendChild(NewlineTag())
if len(part) > 0 {
out.AppendChild(NewHTMLTag(part))
}
}
}
}
}
// Returns a new HTMLTag representing a line break
func NewlineTag() *HTMLTag {
var out = NewHTMLTag("")
out.Name = "br"
return out
}
func ValidURL(raw string) string {
u, err := url.Parse(raw)
if err != nil {
return ""
}
return u.String()
}