2017-02-24 18:49:52 +01:00
|
|
|
package btelegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"html"
|
2018-11-08 00:35:30 +01:00
|
|
|
"io"
|
2018-06-09 12:47:40 +02:00
|
|
|
|
|
|
|
"github.com/russross/blackfriday"
|
2017-02-24 18:49:52 +01:00
|
|
|
)
|
|
|
|
|
2018-02-25 23:54:20 +01:00
|
|
|
type customHTML struct {
|
2017-02-24 18:49:52 +01:00
|
|
|
blackfriday.Renderer
|
|
|
|
}
|
|
|
|
|
2018-02-25 23:54:20 +01:00
|
|
|
func (options *customHTML) Paragraph(out *bytes.Buffer, text func() bool) {
|
2017-02-24 18:49:52 +01:00
|
|
|
marker := out.Len()
|
|
|
|
|
|
|
|
if !text() {
|
|
|
|
out.Truncate(marker)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
out.WriteString("\n")
|
|
|
|
}
|
|
|
|
|
2018-02-25 23:54:20 +01:00
|
|
|
func (options *customHTML) BlockCode(out *bytes.Buffer, text []byte, lang string) {
|
2017-02-24 18:49:52 +01:00
|
|
|
out.WriteString("<pre>")
|
|
|
|
|
|
|
|
out.WriteString(html.EscapeString(string(text)))
|
|
|
|
out.WriteString("</pre>\n")
|
|
|
|
}
|
|
|
|
|
2018-02-25 23:54:20 +01:00
|
|
|
func (options *customHTML) Header(out *bytes.Buffer, text func() bool, level int, id string) {
|
2017-02-24 18:49:52 +01:00
|
|
|
options.Paragraph(out, text)
|
|
|
|
}
|
|
|
|
|
2018-11-08 00:35:30 +01:00
|
|
|
func (options *customHTML) HRule(out io.ByteWriter) {
|
2018-11-28 10:57:59 +01:00
|
|
|
out.WriteByte('\n') //nolint:errcheck
|
2017-02-24 18:49:52 +01:00
|
|
|
}
|
|
|
|
|
2018-02-25 23:54:20 +01:00
|
|
|
func (options *customHTML) BlockQuote(out *bytes.Buffer, text []byte) {
|
2017-02-24 18:49:52 +01:00
|
|
|
out.WriteString("> ")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteByte('\n')
|
|
|
|
}
|
|
|
|
|
2018-02-25 23:54:20 +01:00
|
|
|
func (options *customHTML) List(out *bytes.Buffer, text func() bool, flags int) {
|
2017-02-24 18:49:52 +01:00
|
|
|
options.Paragraph(out, text)
|
|
|
|
}
|
|
|
|
|
2018-02-25 23:54:20 +01:00
|
|
|
func (options *customHTML) ListItem(out *bytes.Buffer, text []byte, flags int) {
|
2017-02-24 18:49:52 +01:00
|
|
|
out.WriteString("- ")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteByte('\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeHTML(input string) string {
|
2018-10-22 19:48:29 +02:00
|
|
|
extensions := blackfriday.NoIntraEmphasis |
|
|
|
|
blackfriday.FencedCode |
|
|
|
|
blackfriday.Autolink |
|
|
|
|
blackfriday.SpaceHeadings |
|
|
|
|
blackfriday.HeadingIDs |
|
|
|
|
blackfriday.BackslashLineBreak |
|
|
|
|
blackfriday.DefinitionLists
|
|
|
|
|
|
|
|
renderer := &customHTML{blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{
|
|
|
|
Flags: blackfriday.UseXHTML | blackfriday.SkipImages,
|
|
|
|
})}
|
|
|
|
return string(blackfriday.Run([]byte(input), blackfriday.WithExtensions(extensions), blackfriday.WithRenderer(renderer)))
|
2017-02-24 18:49:52 +01:00
|
|
|
}
|