~sotirisp/kindleto

5214175fec8dc9f4e2e7888a4799e4dc055074a8 — Sotiris Papatheodorou 1 year, 11 months ago 8726217
Fix link lines with trailing spaces appearing without descriptions

While the Gemini specification v0.16.1 allows link lines without
descriptions they are impossible to follow in Kindleto. Took the
opportunity to restructure link line parsing to a separate function.
1 files changed, 22 insertions(+), 15 deletions(-)

M gemini/gemtext.go
M gemini/gemtext.go => gemini/gemtext.go +22 -15
@@ 48,6 48,25 @@ func isGemtextQuote(line string) bool {
	return strings.HasPrefix(line, ">")
}

func parseLinkLine(pageURL *url.URL, line string) (*url.URL, string) {
	line = strings.TrimLeft(strings.TrimPrefix(line, "=>"), "\t ")
	whitespaceIdx := strings.IndexAny(line, "\t ")
	var linkURLString string
	var linkDesc string
	if whitespaceIdx == -1 {
		linkURLString = line
		linkDesc = linkURLString
	} else {
		linkURLString = line[:whitespaceIdx]
		linkDesc = strings.TrimLeft(line[whitespaceIdx+1:], "\t ")
		if linkDesc == "" {
			linkDesc = linkURLString
		}
	}
	linkURL, _ := util.AbsoluteURL(pageURL, linkURLString)
	return linkURL, html.EscapeString(linkDesc)
}

// GemtextToHTML reads Gemini text from rd, and writes its HTML equivalent to w.
// The source URL is stored in u.
func GemtextToHTML(w http.ResponseWriter, u *url.URL, rd *bufio.Reader, td templates.TemplateData) error {


@@ 86,25 105,13 @@ func GemtextToHTML(w http.ResponseWriter, u *url.URL, rd *bufio.Reader, td templ
		case in_pre:
			io.WriteString(w, html.EscapeString(line)+"\n")
		case isGemtextLink(line):
			line = strings.TrimLeft(strings.TrimPrefix(line, "=>"), "\t ")
			spaceIdx := strings.IndexAny(line, "\t ")
			var linkURLString string
			var linkDesc string
			if spaceIdx == -1 {
				linkURLString = line
				linkDesc = html.EscapeString(linkURLString)
			} else {
				linkURLString = line[:spaceIdx]
				linkDesc = html.EscapeString(line[spaceIdx+1:])
			}
			linkURL, _ := util.AbsoluteURL(u, linkURLString)
			linkURLString = linkURL.String()
			linkURL, linkDesc := parseLinkLine(u, line)
			io.WriteString(w, `<a href="`)
			switch linkURL.Scheme {
			case "file", "finger", "gemini":
				io.WriteString(w, `/?url=`+gmi.QueryEscape(linkURLString))
				io.WriteString(w, "/?url="+gmi.QueryEscape(linkURL.String()))
			default:
				io.WriteString(w, linkURLString)
				io.WriteString(w, linkURL.String())
			}
			io.WriteString(w, `">`+linkDesc+
				`</a> <span class="scheme"> `+