| 1 | package btelegram |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | |
| 6 | "github.com/russross/blackfriday" |
| 7 | ) |
| 8 | |
| 9 | type customHTML struct { |
| 10 | blackfriday.Renderer |
| 11 | } |
| 12 | |
| 13 | func (options *customHTML) Paragraph(out *bytes.Buffer, text func() bool) { |
| 14 | marker := out.Len() |
| 15 | |
| 16 | if !text() { |
| 17 | out.Truncate(marker) |
| 18 | return |
| 19 | } |
| 20 | out.WriteString("\n") |
| 21 | } |
| 22 | |
| 23 | func (options *customHTML) BlockCode(out *bytes.Buffer, text []byte, lang string) { |
| 24 | out.WriteString("<pre>") |
| 25 | |
| 26 | out.WriteString(string(text)) |
| 27 | out.WriteString("</pre>\n") |
| 28 | } |
| 29 | |
| 30 | func (options *customHTML) CodeSpan(out *bytes.Buffer, text []byte) { |
| 31 | out.WriteString("<code>") |
| 32 | out.WriteString(string(text)) |
| 33 | out.WriteString("</code>") |
| 34 | } |
| 35 | |
| 36 | func (options *customHTML) Header(out *bytes.Buffer, text func() bool, level int, id string) { |
| 37 | options.Paragraph(out, text) |
| 38 | } |
| 39 | |
| 40 | func (options *customHTML) HRule(out *bytes.Buffer) { |
| 41 | out.WriteByte('\n') //nolint:errcheck |
| 42 | } |
| 43 | |
| 44 | func (options *customHTML) BlockQuote(out *bytes.Buffer, text []byte) { |
| 45 | out.WriteString("> ") |
| 46 | out.Write(text) |
| 47 | out.WriteByte('\n') |
| 48 | } |
| 49 | |
| 50 | func (options *customHTML) LineBreak(out *bytes.Buffer) { |
| 51 | out.WriteByte('\n') |
| 52 | } |
| 53 | |
| 54 | func (options *customHTML) List(out *bytes.Buffer, text func() bool, flags int) { |
| 55 | options.Paragraph(out, text) |
| 56 | } |
| 57 | |
| 58 | func (options *customHTML) ListItem(out *bytes.Buffer, text []byte, flags int) { |
| 59 | out.WriteString("- ") |
| 60 | out.Write(text) |
| 61 | out.WriteByte('\n') |
| 62 | } |
| 63 | |
| 64 | func makeHTML(input string) string { |
| 65 | return string(blackfriday.Markdown([]byte(input), |
| 66 | &customHTML{blackfriday.HtmlRenderer(blackfriday.HTML_USE_XHTML|blackfriday.HTML_SKIP_IMAGES, "", "")}, |
| 67 | blackfriday.EXTENSION_NO_INTRA_EMPHASIS| |
| 68 | blackfriday.EXTENSION_FENCED_CODE| |
| 69 | blackfriday.EXTENSION_AUTOLINK| |
| 70 | blackfriday.EXTENSION_SPACE_HEADERS| |
| 71 | blackfriday.EXTENSION_HEADER_IDS| |
| 72 | blackfriday.EXTENSION_BACKSLASH_LINE_BREAK| |
| 73 | blackfriday.EXTENSION_DEFINITION_LISTS)) |
| 74 | } |
| 75 | |