~m15o/nightfall-server

ba89521b763a4209cd127101285607180b3b4342 — m15o 8 months ago 31e0fc8
Fix gemini links
2 files changed, 17 insertions(+), 12 deletions(-)

M gmi2html/convert.go
M main.go
M gmi2html/convert.go => gmi2html/convert.go +15 -10
@@ 2,6 2,7 @@ package gmi2html

import (
	"html"
	"net/url"
	"regexp"
	"strings"
)


@@ 32,7 33,7 @@ func sanitize(input string) string {
	return html.EscapeString(input)
}

func Convert(gmi string) string {
func Convert(gmi, baseurl string) string {
	var rv []string
	preMode := false
	ulMode := false


@@ 75,21 76,25 @@ func Convert(gmi string) string {
				if len(matches[2]) == 0 {
					matches[2] = matches[1]
				}
				url := matches[1]
				if strings.HasPrefix(matches[1], "gemini://") {
					url = strings.Replace(matches[1], "gemini://", "/gemini/",1)
				} else if strings.HasPrefix(matches[1], "/") {
					url = matches[1][1:]
				linkurl := matches[1]
				bu, _ := url.Parse("gemini://" + baseurl)
				lu, _ := url.Parse(linkurl)
				ru := linkurl
				if !strings.Contains(linkurl, "://") {
					ru = bu.ResolveReference(lu).String()
				}
				if strings.HasSuffix(url, ".png") || strings.HasSuffix(url, ".PNG") || strings.HasSuffix(url, ".jpg") || strings.HasSuffix(url, ".JPG") || strings.HasSuffix(url, ".jpeg") || strings.HasSuffix(url, ".gif") || strings.HasSuffix(url, ".GIF") {
					rv = append(rv, "<img src=\""+sanitize(url)+"\"/>")
				if strings.Contains(ru, "gemini://") {
					ru = strings.Replace(ru, "gemini://", "/gemini/",1)
				}
				if strings.HasSuffix(ru, ".png") || strings.HasSuffix(ru, ".PNG") || strings.HasSuffix(ru, ".jpg") || strings.HasSuffix(ru, ".JPG") || strings.HasSuffix(ru, ".jpeg") || strings.HasSuffix(ru, ".gif") || strings.HasSuffix(ru, ".GIF") {
					rv = append(rv, "<img src=\""+sanitize(ru)+"\"/>")
					continue
				}
				if linkMode {
					rv = append(rv, "<a href=\""+sanitize(url)+"\">"+sanitize(matches[2])+"</a><br/>")
					rv = append(rv, "<a href=\""+sanitize(ru)+"\">"+sanitize(matches[2])+"</a><br/>")
					continue
				}
				rv = append(rv, "<p><a href=\""+sanitize(url)+"\">"+sanitize(matches[2])+"</a><br/>")
				rv = append(rv, "<p><a href=\""+sanitize(ru)+"\">"+sanitize(matches[2])+"</a><br/>")
				linkMode = true
			case preRegexp.MatchString(l):
				clearUlMode(&ulMode, &rv)

M main.go => main.go +2 -2
@@ 99,7 99,7 @@ func startWebServer(conf *cfg.Config) {
			case gemini.StatusRedirect:
			case gemini.StatusPermanentRedirect:
				w.Header().Set("Content-Type", "text/html; charset=utf-8")
				if _, err := w.Write([]byte("<style>" + STYLE + "</style>" + gmi2html.Convert("=> " + resp.Meta))); err != nil {
				if _, err := w.Write([]byte("<style>" + STYLE + "</style>" + gmi2html.Convert("=> " + resp.Meta, geminiURL))); err != nil {
					return
				}
				break


@@ 125,7 125,7 @@ func startWebServer(conf *cfg.Config) {
								up = "<p><a href=\""+strings.Join(p[:len(p)-1], "/")+"/\">Go up</a></p>"
							}
						}
						if _, err = w.Write([]byte("<style>" + STYLE + "</style>" + up + gmi2html.Convert(string(b)))); err != nil {
						if _, err = w.Write([]byte("<style>" + STYLE + "</style>" + up + gmi2html.Convert(string(b), geminiURL))); err != nil {
							return
						}
					}