2020-06-18 16:54:48 -04:00
|
|
|
// Package renderer provides functions to convert various data into a cview primitive.
|
|
|
|
// Example objects include a Gemini response, and an error.
|
|
|
|
//
|
|
|
|
// Rendered lines always end with \r\n, in an effort to be Window compatible.
|
|
|
|
package renderer
|
|
|
|
|
|
|
|
import (
|
2020-07-28 16:58:32 -04:00
|
|
|
"fmt"
|
2020-06-18 16:54:48 -04:00
|
|
|
urlPkg "net/url"
|
2020-09-04 12:42:01 -04:00
|
|
|
"regexp"
|
2020-06-18 16:54:48 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2021-04-07 12:56:32 -04:00
|
|
|
"code.rocketnine.space/tslocum/cview"
|
2020-07-28 16:58:32 -04:00
|
|
|
"github.com/makeworld-the-better-one/amfora/config"
|
2020-06-18 16:54:48 -04:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2020-09-04 12:42:01 -04:00
|
|
|
// Regex for identifying ANSI color codes
|
|
|
|
var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*m`)
|
|
|
|
|
2020-07-10 17:45:14 -04:00
|
|
|
// RenderANSI renders plain text pages containing ANSI codes.
|
|
|
|
// Practically, it is used for the text/x-ansi.
|
2021-02-17 14:17:13 -05:00
|
|
|
func RenderANSI(s string) string {
|
2020-07-10 17:45:14 -04:00
|
|
|
s = cview.Escape(s)
|
2020-09-04 12:42:01 -04:00
|
|
|
if viper.GetBool("a-general.color") && viper.GetBool("a-general.ansi") {
|
2020-07-10 17:45:14 -04:00
|
|
|
s = cview.TranslateANSI(s)
|
2020-09-04 12:42:01 -04:00
|
|
|
} else {
|
|
|
|
s = ansiRegex.ReplaceAllString(s, "")
|
2020-07-10 17:45:14 -04:00
|
|
|
}
|
2021-02-17 14:17:13 -05:00
|
|
|
return s
|
2020-07-10 17:45:14 -04:00
|
|
|
}
|
|
|
|
|
2020-07-03 17:28:56 -04:00
|
|
|
// RenderPlainText should be used to format plain text pages.
|
2021-02-17 14:17:13 -05:00
|
|
|
func RenderPlainText(s string) string {
|
|
|
|
// It used to add a left margin, now this is done elsewhere.
|
|
|
|
// The function is kept for convenience and in case rendering
|
|
|
|
// is needed in the future.
|
2021-03-01 16:07:50 -05:00
|
|
|
return cview.Escape(s)
|
2020-07-03 17:28:56 -04:00
|
|
|
}
|
|
|
|
|
2020-07-03 14:02:33 -04:00
|
|
|
// wrapLine wraps a line to the provided width, and adds the provided prefix and suffix to each wrapped line.
|
|
|
|
// It recovers from wrapping panics and should never cause a panic.
|
|
|
|
// It returns a slice of lines, without newlines at the end.
|
|
|
|
//
|
|
|
|
// Set includeFirst to true if the prefix and suffix should be applied to the first wrapped line as well
|
|
|
|
func wrapLine(line string, width int, prefix, suffix string, includeFirst bool) []string {
|
|
|
|
// Anonymous function to allow recovery from potential WordWrap panic
|
|
|
|
var ret []string
|
|
|
|
func() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
// Use unwrapped line instead
|
|
|
|
if includeFirst {
|
|
|
|
ret = []string{prefix + line + suffix}
|
|
|
|
} else {
|
|
|
|
ret = []string{line}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
wrapped := cview.WordWrap(line, width)
|
|
|
|
for i := range wrapped {
|
|
|
|
if !includeFirst && i == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wrapped[i] = prefix + wrapped[i] + suffix
|
|
|
|
}
|
|
|
|
ret = wrapped
|
|
|
|
}()
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2020-06-18 16:54:48 -04:00
|
|
|
// convertRegularGemini converts non-preformatted blocks of text/gemini
|
|
|
|
// into a cview-compatible format.
|
2020-09-01 16:41:30 -04:00
|
|
|
// Since this only works on non-preformatted blocks, RenderGemini
|
|
|
|
// should always be used instead.
|
|
|
|
//
|
2020-06-18 16:54:48 -04:00
|
|
|
// It also returns a slice of link URLs.
|
|
|
|
// numLinks is the number of links that exist so far.
|
2020-06-21 16:53:12 -04:00
|
|
|
// width is the number of columns to wrap to.
|
2020-06-18 16:54:48 -04:00
|
|
|
//
|
2020-09-01 16:41:30 -04:00
|
|
|
//
|
|
|
|
// proxied is whether the request is through the gemini:// scheme.
|
|
|
|
// If it's not a gemini:// page, set this to true.
|
|
|
|
func convertRegularGemini(s string, numLinks, width int, proxied bool) (string, []string) {
|
2020-06-18 16:54:48 -04:00
|
|
|
links := make([]string, 0)
|
|
|
|
lines := strings.Split(s, "\n")
|
|
|
|
wrappedLines := make([]string, 0) // Final result
|
|
|
|
|
|
|
|
for i := range lines {
|
|
|
|
lines[i] = strings.TrimRight(lines[i], " \r\t\n")
|
|
|
|
|
2020-08-28 12:22:49 -04:00
|
|
|
if strings.HasPrefix(lines[i], "#") {
|
2020-06-18 16:54:48 -04:00
|
|
|
// Headings
|
2020-07-28 16:58:32 -04:00
|
|
|
var tag string
|
2020-06-24 12:01:09 -04:00
|
|
|
if viper.GetBool("a-general.color") {
|
2020-08-28 12:22:49 -04:00
|
|
|
if strings.HasPrefix(lines[i], "###") {
|
2020-07-28 16:58:32 -04:00
|
|
|
tag = fmt.Sprintf("[%s::b]", config.GetColorString("hdg_3"))
|
2020-07-03 14:02:33 -04:00
|
|
|
} else if strings.HasPrefix(lines[i], "##") {
|
2020-07-28 16:58:32 -04:00
|
|
|
tag = fmt.Sprintf("[%s::b]", config.GetColorString("hdg_2"))
|
2020-07-03 14:02:33 -04:00
|
|
|
} else if strings.HasPrefix(lines[i], "#") {
|
2020-07-28 16:58:32 -04:00
|
|
|
tag = fmt.Sprintf("[%s::b]", config.GetColorString("hdg_1"))
|
2020-06-24 12:01:09 -04:00
|
|
|
}
|
2020-07-28 16:58:32 -04:00
|
|
|
wrappedLines = append(wrappedLines, wrapLine(lines[i], width, tag, "[-::-]", true)...)
|
2020-06-24 12:01:09 -04:00
|
|
|
} else {
|
|
|
|
// Just bold, no colors
|
2020-07-03 14:02:33 -04:00
|
|
|
wrappedLines = append(wrappedLines, wrapLine(lines[i], width, "[::b]", "[-::-]", true)...)
|
2020-06-18 16:54:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Links
|
|
|
|
} else if strings.HasPrefix(lines[i], "=>") && len([]rune(lines[i])) >= 3 {
|
|
|
|
// Trim whitespace and separate link from link text
|
|
|
|
|
|
|
|
lines[i] = strings.Trim(lines[i][2:], " \t") // Remove `=>` part too
|
|
|
|
delim := strings.IndexAny(lines[i], " \t") // Whitespace between link and link text
|
|
|
|
|
|
|
|
var url string
|
|
|
|
var linkText string
|
|
|
|
if delim == -1 {
|
|
|
|
// No link text
|
|
|
|
url = lines[i]
|
|
|
|
linkText = url
|
|
|
|
} else {
|
|
|
|
// There is link text
|
|
|
|
url = lines[i][:delim]
|
|
|
|
linkText = strings.Trim(lines[i][delim:], " \t")
|
2020-12-01 14:00:01 -05:00
|
|
|
if viper.GetBool("a-general.show_link") {
|
|
|
|
linkText += " (" + url + ")"
|
|
|
|
}
|
2020-06-18 16:54:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if strings.TrimSpace(lines[i]) == "" || strings.TrimSpace(url) == "" {
|
|
|
|
// Link was just whitespace, reset it and move on
|
|
|
|
lines[i] = "=>"
|
|
|
|
wrappedLines = append(wrappedLines, lines[i])
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
links = append(links, url)
|
2020-08-04 20:44:43 -04:00
|
|
|
num := numLinks + len(links) // Visible link number, one-indexed
|
|
|
|
|
|
|
|
var indent int
|
|
|
|
if num > 99 {
|
|
|
|
// Indent link text by 3 or more spaces
|
|
|
|
indent = len(strconv.Itoa(num)) + 4 // +4 indent for spaces and brackets
|
|
|
|
} else {
|
|
|
|
// One digit and two digit links have the same spacing - see #60
|
|
|
|
indent = 5 // +4 indent for spaces and brackets, and 1 for link number
|
|
|
|
}
|
|
|
|
|
|
|
|
// Spacing after link number: 1 or 2 spaces?
|
|
|
|
var spacing string
|
|
|
|
if num > 9 {
|
|
|
|
// One space to keep it in line with other links - see #60
|
|
|
|
spacing = " "
|
|
|
|
} else {
|
|
|
|
// One digit numbers use two spaces
|
|
|
|
spacing = " "
|
|
|
|
}
|
2020-06-18 16:54:48 -04:00
|
|
|
|
2021-12-03 11:45:36 -05:00
|
|
|
// Underline non-gemini links if enabled
|
|
|
|
var linkTag string
|
|
|
|
if viper.GetBool("a-general.underline") {
|
2021-12-03 20:54:53 -05:00
|
|
|
linkTag = `[` + config.GetColorString("foreign_link") + `::u]`
|
|
|
|
} else {
|
|
|
|
linkTag = `[` + config.GetColorString("foreign_link") + `]`
|
2021-12-03 11:45:36 -05:00
|
|
|
}
|
|
|
|
|
2020-07-03 14:02:33 -04:00
|
|
|
// Wrap and add link text
|
|
|
|
// Wrap the link text, but add some spaces to indent the wrapped lines past the link number
|
|
|
|
// Set the style tags
|
|
|
|
// Add them to the first line
|
|
|
|
|
2020-07-09 15:21:09 -04:00
|
|
|
var wrappedLink []string
|
|
|
|
|
2021-12-03 11:45:36 -05:00
|
|
|
pU, err := urlPkg.Parse(url)
|
|
|
|
if !proxied && err == nil &&
|
|
|
|
(pU.Scheme == "" || pU.Scheme == "gemini" || pU.Scheme == "about") {
|
|
|
|
// A gemini link
|
|
|
|
|
|
|
|
if viper.GetBool("a-general.color") {
|
2020-06-18 16:54:48 -04:00
|
|
|
// Add the link text in blue (in a region), and a gray link number to the left of it
|
2020-07-28 16:58:32 -04:00
|
|
|
// Those are the default colors, anyway
|
2020-07-09 15:21:09 -04:00
|
|
|
|
|
|
|
wrappedLink = wrapLine(linkText, width,
|
2020-08-04 20:44:43 -04:00
|
|
|
strings.Repeat(" ", indent)+
|
|
|
|
`["`+strconv.Itoa(num-1)+`"][`+config.GetColorString("amfora_link")+`]`,
|
2020-07-09 15:21:09 -04:00
|
|
|
`[-][""]`,
|
|
|
|
false, // Don't indent the first line, it's the one with link number
|
|
|
|
)
|
|
|
|
|
|
|
|
// Add special stuff to first line, like the link number
|
2020-07-28 16:58:32 -04:00
|
|
|
wrappedLink[0] = fmt.Sprintf(`[%s::b][`, config.GetColorString("link_number")) +
|
2020-08-04 20:44:43 -04:00
|
|
|
strconv.Itoa(num) + "[]" + "[-::-]" + spacing +
|
|
|
|
`["` + strconv.Itoa(num-1) + `"][` + config.GetColorString("amfora_link") + `]` +
|
2020-07-09 15:21:09 -04:00
|
|
|
wrappedLink[0] + `[-][""]`
|
2020-06-18 16:54:48 -04:00
|
|
|
} else {
|
2021-12-03 11:45:36 -05:00
|
|
|
// No color
|
|
|
|
|
|
|
|
wrappedLink = wrapLine(linkText, width,
|
|
|
|
strings.Repeat(" ", len(strconv.Itoa(num))+4)+ // +4 for spaces and brackets
|
|
|
|
`["`+strconv.Itoa(num-1)+`"]`,
|
|
|
|
`[""]`,
|
|
|
|
false, // Don't indent the first line, it's the one with link number
|
|
|
|
)
|
|
|
|
|
|
|
|
wrappedLink[0] = `[::b][` + strconv.Itoa(num) + "[][::-] " +
|
|
|
|
`["` + strconv.Itoa(num-1) + `"]` +
|
|
|
|
wrappedLink[0] + `[""]`
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Not a gemini link
|
|
|
|
|
|
|
|
if viper.GetBool("a-general.color") {
|
|
|
|
// Color
|
2020-07-09 15:21:09 -04:00
|
|
|
|
|
|
|
wrappedLink = wrapLine(linkText, width,
|
2020-08-04 20:44:43 -04:00
|
|
|
strings.Repeat(" ", indent)+
|
2021-12-03 20:52:30 -05:00
|
|
|
`["`+strconv.Itoa(num-1)+`"]`+linkTag,
|
2021-12-03 11:45:36 -05:00
|
|
|
`[-::-][""]`,
|
2020-07-09 15:21:09 -04:00
|
|
|
false, // Don't indent the first line, it's the one with link number
|
|
|
|
)
|
|
|
|
|
2020-07-28 16:58:32 -04:00
|
|
|
wrappedLink[0] = fmt.Sprintf(`[%s::b][`, config.GetColorString("link_number")) +
|
2021-12-03 20:52:30 -05:00
|
|
|
strconv.Itoa(num) + "[][-::-]" + spacing +
|
|
|
|
`["` + strconv.Itoa(num-1) + `"]` + linkTag +
|
2021-12-03 11:45:36 -05:00
|
|
|
wrappedLink[0] + `[-::-][""]`
|
|
|
|
} else {
|
|
|
|
// No color
|
|
|
|
|
|
|
|
wrappedLink = wrapLine(linkText, width,
|
2021-12-03 20:52:30 -05:00
|
|
|
strings.Repeat(" ", indent)+
|
2021-12-10 11:52:15 -05:00
|
|
|
`["`+strconv.Itoa(num-1)+`"]`,
|
2021-12-03 11:45:36 -05:00
|
|
|
`[::-][""]`,
|
|
|
|
false, // Don't indent the first line, it's the one with link number
|
|
|
|
)
|
|
|
|
|
2021-12-03 20:52:30 -05:00
|
|
|
wrappedLink[0] = `[::b][` + strconv.Itoa(num) + "[][::-]" + spacing +
|
2021-12-10 11:52:15 -05:00
|
|
|
`["` + strconv.Itoa(num-1) + `"]` +
|
2021-12-03 11:45:36 -05:00
|
|
|
wrappedLink[0] + `[::-][""]`
|
2020-06-18 16:54:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 14:02:33 -04:00
|
|
|
wrappedLines = append(wrappedLines, wrappedLink...)
|
|
|
|
|
2020-06-18 16:54:48 -04:00
|
|
|
// Lists
|
|
|
|
} else if strings.HasPrefix(lines[i], "* ") {
|
|
|
|
if viper.GetBool("a-general.bullets") {
|
2020-07-03 14:02:33 -04:00
|
|
|
// Wrap list item, and indent wrapped lines past the bullet
|
2020-07-28 16:58:32 -04:00
|
|
|
wrappedItem := wrapLine(lines[i][1:], width,
|
|
|
|
fmt.Sprintf(" [%s]", config.GetColorString("list_text")),
|
|
|
|
"[-]", false)
|
2020-07-03 14:02:33 -04:00
|
|
|
// Add bullet
|
2020-07-28 16:58:32 -04:00
|
|
|
wrappedItem[0] = fmt.Sprintf(" [%s]\u2022", config.GetColorString("list_text")) +
|
|
|
|
wrappedItem[0] + "[-]"
|
2020-07-03 14:02:33 -04:00
|
|
|
wrappedLines = append(wrappedLines, wrappedItem...)
|
2021-05-06 22:26:34 -04:00
|
|
|
} else {
|
|
|
|
wrappedItem := wrapLine(lines[i][1:], width,
|
|
|
|
fmt.Sprintf(" [%s]", config.GetColorString("list_text")),
|
|
|
|
"[-]", false)
|
|
|
|
// Add "*"
|
|
|
|
wrappedItem[0] = fmt.Sprintf(" [%s]*", config.GetColorString("list_text")) +
|
|
|
|
wrappedItem[0] + "[-]"
|
|
|
|
wrappedLines = append(wrappedLines, wrappedItem...)
|
|
|
|
|
2020-06-18 16:54:48 -04:00
|
|
|
}
|
|
|
|
// Optionally list lines could be colored here too, if color is enabled
|
2020-07-03 14:02:33 -04:00
|
|
|
} else if strings.HasPrefix(lines[i], ">") {
|
|
|
|
// It's a quote line, add extra quote symbols and italics to the start of each wrapped line
|
2020-06-18 16:54:48 -04:00
|
|
|
|
2020-12-20 17:52:06 -05:00
|
|
|
if len(lines[i]) == 1 {
|
|
|
|
// Just an empty quote line
|
|
|
|
wrappedLines = append(wrappedLines, fmt.Sprintf("[%s::i]>[-::-]", config.GetColorString("quote_text")))
|
|
|
|
} else {
|
|
|
|
// Remove beginning quote and maybe space
|
|
|
|
lines[i] = strings.TrimPrefix(lines[i], ">")
|
|
|
|
lines[i] = strings.TrimPrefix(lines[i], " ")
|
|
|
|
wrappedLines = append(wrappedLines,
|
|
|
|
wrapLine(lines[i], width, fmt.Sprintf("[%s::i]> ", config.GetColorString("quote_text")),
|
|
|
|
"[-::-]", true)...,
|
|
|
|
)
|
|
|
|
}
|
2020-06-18 16:54:48 -04:00
|
|
|
|
2020-07-03 14:02:33 -04:00
|
|
|
} else if strings.TrimSpace(lines[i]) == "" {
|
2020-06-18 16:54:48 -04:00
|
|
|
// Just add empty line without processing
|
|
|
|
wrappedLines = append(wrappedLines, "")
|
|
|
|
} else {
|
2020-07-03 14:02:33 -04:00
|
|
|
// Regular line, just wrap it
|
2020-07-28 16:58:32 -04:00
|
|
|
wrappedLines = append(wrappedLines, wrapLine(lines[i], width,
|
|
|
|
fmt.Sprintf("[%s]", config.GetColorString("regular_text")),
|
|
|
|
"[-]", true)...)
|
2020-06-18 16:54:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(wrappedLines, "\r\n"), links
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:53:12 -04:00
|
|
|
// RenderGemini converts text/gemini into a cview displayable format.
|
2021-02-27 21:18:03 -05:00
|
|
|
// It also returns a slice of link URLs.
|
2020-07-02 23:55:24 -04:00
|
|
|
//
|
|
|
|
// width is the number of columns to wrap to.
|
|
|
|
// leftMargin is the number of blank spaces to prepend to each line.
|
2020-09-01 16:41:30 -04:00
|
|
|
//
|
|
|
|
// proxied is whether the request is through the gemini:// scheme.
|
|
|
|
// If it's not a gemini:// page, set this to true.
|
2021-02-27 21:18:03 -05:00
|
|
|
func RenderGemini(s string, width int, proxied bool) (string, []string) {
|
2020-06-18 16:54:48 -04:00
|
|
|
s = cview.Escape(s)
|
2020-09-04 12:42:01 -04:00
|
|
|
|
2020-06-18 16:54:48 -04:00
|
|
|
lines := strings.Split(s, "\n")
|
|
|
|
links := make([]string, 0)
|
|
|
|
|
|
|
|
// Process and wrap non preformatted lines
|
|
|
|
rendered := "" // Final result
|
|
|
|
pre := false
|
|
|
|
buf := "" // Block of regular or preformatted lines
|
2021-02-17 14:17:13 -05:00
|
|
|
|
|
|
|
// processPre is for rendering preformatted blocks
|
|
|
|
processPre := func() {
|
2021-02-27 18:17:49 -05:00
|
|
|
|
2021-02-17 14:17:13 -05:00
|
|
|
// Support ANSI color codes in preformatted blocks - see #59
|
|
|
|
if viper.GetBool("a-general.color") && viper.GetBool("a-general.ansi") {
|
|
|
|
buf = cview.TranslateANSI(buf)
|
2021-05-29 16:02:16 -04:00
|
|
|
// The TranslateANSI function will reset the colors when it encounters
|
|
|
|
// an ANSI reset code, injecting a full reset tag: [-:-:-]
|
|
|
|
// This uses the default foreground and background colors of the
|
|
|
|
// application, but in this case we want it to use the preformatted text
|
|
|
|
// color as the foreground, as we're still in a preformat block.
|
|
|
|
buf = strings.ReplaceAll(
|
|
|
|
buf, "[-:-:-]",
|
|
|
|
fmt.Sprintf("[%s:-:-]", config.GetColorString("preformatted_text")),
|
|
|
|
)
|
2021-02-17 14:17:13 -05:00
|
|
|
} else {
|
|
|
|
buf = ansiRegex.ReplaceAllString(buf, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The final newline is removed (and re-added) to prevent background glitches
|
|
|
|
// where the terminal background color slips through. This only happens on
|
|
|
|
// preformatted blocks with ANSI characters.
|
|
|
|
//
|
|
|
|
// Lines are modified below to always end with \r\n
|
|
|
|
buf = strings.TrimSuffix(buf, "\r\n")
|
|
|
|
|
2021-12-10 11:52:15 -05:00
|
|
|
if viper.GetBool("a-general.color") {
|
|
|
|
rendered += fmt.Sprintf("[%s]", config.GetColorString("preformatted_text")) +
|
|
|
|
buf + fmt.Sprintf("[%s:%s:-]\r\n", config.GetColorString("regular_text"), config.GetColorString("bg"))
|
|
|
|
} else {
|
|
|
|
rendered += buf + "\r\n"
|
|
|
|
}
|
2021-02-17 14:17:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// processRegular processes non-preformatted sections
|
|
|
|
processRegular := func() {
|
|
|
|
// ANSI not allowed in regular text - see #59
|
|
|
|
buf = ansiRegex.ReplaceAllString(buf, "")
|
|
|
|
|
|
|
|
ren, lks := convertRegularGemini(buf, len(links), width, proxied)
|
|
|
|
links = append(links, lks...)
|
|
|
|
rendered += ren
|
|
|
|
}
|
|
|
|
|
2020-06-18 16:54:48 -04:00
|
|
|
for i := range lines {
|
|
|
|
if strings.HasPrefix(lines[i], "```") {
|
|
|
|
if pre {
|
|
|
|
// In a preformatted block, so add the text as is
|
|
|
|
// Don't add the current line with backticks
|
2021-02-17 14:17:13 -05:00
|
|
|
processPre()
|
2020-11-04 20:31:04 -05:00
|
|
|
|
2020-06-18 16:54:48 -04:00
|
|
|
} else {
|
|
|
|
// Not preformatted, regular text
|
2021-02-17 14:17:13 -05:00
|
|
|
processRegular()
|
2020-06-18 16:54:48 -04:00
|
|
|
}
|
|
|
|
buf = "" // Clear buffer for next block
|
|
|
|
pre = !pre
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Lines always end with \r\n for Windows compatibility
|
|
|
|
buf += strings.TrimSuffix(lines[i], "\r") + "\r\n"
|
|
|
|
}
|
|
|
|
// Gone through all the lines, but there still is likely a block in the buffer
|
|
|
|
if pre {
|
|
|
|
// File ended without closing the preformatted block
|
2021-02-17 14:17:13 -05:00
|
|
|
processPre()
|
2020-06-18 16:54:48 -04:00
|
|
|
} else {
|
|
|
|
// Not preformatted, regular text
|
2021-02-17 14:17:13 -05:00
|
|
|
processRegular()
|
2020-07-02 23:55:24 -04:00
|
|
|
}
|
|
|
|
|
2021-02-27 21:18:03 -05:00
|
|
|
return rendered, links
|
2020-06-18 16:54:48 -04:00
|
|
|
}
|