5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-14 21:10:28 +00:00
matterbridge/bridge/telegram/html.go

65 lines
1.5 KiB
Go
Raw Normal View History

package btelegram
import (
"bytes"
"github.com/russross/blackfriday"
"html"
)
2018-02-25 22:54:20 +00:00
type customHTML struct {
blackfriday.Renderer
}
2018-02-25 22:54:20 +00:00
func (options *customHTML) Paragraph(out *bytes.Buffer, text func() bool) {
marker := out.Len()
if !text() {
out.Truncate(marker)
return
}
out.WriteString("\n")
}
2018-02-25 22:54:20 +00:00
func (options *customHTML) BlockCode(out *bytes.Buffer, text []byte, lang string) {
out.WriteString("<pre>")
out.WriteString(html.EscapeString(string(text)))
out.WriteString("</pre>\n")
}
2018-02-25 22:54:20 +00:00
func (options *customHTML) Header(out *bytes.Buffer, text func() bool, level int, id string) {
options.Paragraph(out, text)
}
2018-02-25 22:54:20 +00:00
func (options *customHTML) HRule(out *bytes.Buffer) {
out.WriteByte('\n')
}
2018-02-25 22:54:20 +00:00
func (options *customHTML) BlockQuote(out *bytes.Buffer, text []byte) {
out.WriteString("> ")
out.Write(text)
out.WriteByte('\n')
}
2018-02-25 22:54:20 +00:00
func (options *customHTML) List(out *bytes.Buffer, text func() bool, flags int) {
options.Paragraph(out, text)
}
2018-02-25 22:54:20 +00:00
func (options *customHTML) ListItem(out *bytes.Buffer, text []byte, flags int) {
out.WriteString("- ")
out.Write(text)
out.WriteByte('\n')
}
func makeHTML(input string) string {
return string(blackfriday.Markdown([]byte(input),
2018-02-25 22:54:20 +00:00
&customHTML{blackfriday.HtmlRenderer(blackfriday.HTML_USE_XHTML|blackfriday.HTML_SKIP_IMAGES, "", "")},
blackfriday.EXTENSION_NO_INTRA_EMPHASIS|
blackfriday.EXTENSION_FENCED_CODE|
blackfriday.EXTENSION_AUTOLINK|
blackfriday.EXTENSION_SPACE_HEADERS|
blackfriday.EXTENSION_HEADER_IDS|
blackfriday.EXTENSION_BACKSLASH_LINE_BREAK|
blackfriday.EXTENSION_DEFINITION_LISTS))
}