2017-02-24 17:49:52 +00:00
|
|
|
package btelegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-06-09 10:47:40 +00:00
|
|
|
|
|
|
|
"github.com/russross/blackfriday"
|
2017-02-24 17:49:52 +00:00
|
|
|
)
|
|
|
|
|
2018-02-25 22:54:20 +00:00
|
|
|
type customHTML struct {
|
2017-02-24 17:49:52 +00:00
|
|
|
blackfriday.Renderer
|
|
|
|
}
|
|
|
|
|
2018-02-25 22:54:20 +00:00
|
|
|
func (options *customHTML) Paragraph(out *bytes.Buffer, text func() bool) {
|
2017-02-24 17:49:52 +00:00
|
|
|
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) {
|
2017-02-24 17:49:52 +00:00
|
|
|
out.WriteString("<pre>")
|
|
|
|
|
2022-07-06 22:47:50 +00:00
|
|
|
out.WriteString(string(text))
|
2017-02-24 17:49:52 +00:00
|
|
|
out.WriteString("</pre>\n")
|
|
|
|
}
|
|
|
|
|
2022-07-06 22:47:50 +00:00
|
|
|
func (options *customHTML) CodeSpan(out *bytes.Buffer, text []byte) {
|
|
|
|
out.WriteString("<code>")
|
|
|
|
out.WriteString(string(text))
|
|
|
|
out.WriteString("</code>")
|
|
|
|
}
|
|
|
|
|
2018-02-25 22:54:20 +00:00
|
|
|
func (options *customHTML) Header(out *bytes.Buffer, text func() bool, level int, id string) {
|
2017-02-24 17:49:52 +00:00
|
|
|
options.Paragraph(out, text)
|
|
|
|
}
|
|
|
|
|
2019-02-24 14:13:56 +00:00
|
|
|
func (options *customHTML) HRule(out *bytes.Buffer) {
|
2018-11-28 09:57:59 +00:00
|
|
|
out.WriteByte('\n') //nolint:errcheck
|
2017-02-24 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
2018-02-25 22:54:20 +00:00
|
|
|
func (options *customHTML) BlockQuote(out *bytes.Buffer, text []byte) {
|
2017-02-24 17:49:52 +00:00
|
|
|
out.WriteString("> ")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteByte('\n')
|
|
|
|
}
|
|
|
|
|
2022-07-06 22:47:50 +00:00
|
|
|
func (options *customHTML) LineBreak(out *bytes.Buffer) {
|
|
|
|
out.WriteByte('\n')
|
|
|
|
}
|
|
|
|
|
2018-02-25 22:54:20 +00:00
|
|
|
func (options *customHTML) List(out *bytes.Buffer, text func() bool, flags int) {
|
2017-02-24 17:49:52 +00:00
|
|
|
options.Paragraph(out, text)
|
|
|
|
}
|
|
|
|
|
2018-02-25 22:54:20 +00:00
|
|
|
func (options *customHTML) ListItem(out *bytes.Buffer, text []byte, flags int) {
|
2017-02-24 17:49:52 +00:00
|
|
|
out.WriteString("- ")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteByte('\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeHTML(input string) string {
|
2019-02-24 14:13:56 +00:00
|
|
|
return string(blackfriday.Markdown([]byte(input),
|
|
|
|
&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))
|
2017-02-24 17:49:52 +00:00
|
|
|
}
|