2020-11-21 17:53:04 +01:00
|
|
|
package gmitohtml
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-11-25 03:34:14 +01:00
|
|
|
"html"
|
2020-11-21 17:53:04 +01:00
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2020-11-22 05:44:22 +01:00
|
|
|
"sync"
|
2020-11-21 17:53:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ErrInvalidURL is the error returned when the URL is invalid.
|
|
|
|
var ErrInvalidURL = errors.New("invalid URL")
|
|
|
|
|
|
|
|
var daemonAddress string
|
|
|
|
|
2020-11-22 05:44:22 +01:00
|
|
|
var assetLock sync.Mutex
|
|
|
|
|
2021-06-09 01:19:26 +02:00
|
|
|
var imageExtensions = []string{"png", "jpg", "jpeg", "gif", "svg", "webp"}
|
|
|
|
|
2020-11-21 17:53:04 +01:00
|
|
|
func rewriteURL(u string, loc *url.URL) string {
|
2021-07-10 20:28:08 +02:00
|
|
|
if daemonAddress == "" {
|
|
|
|
return u
|
|
|
|
}
|
2020-11-25 03:34:14 +01:00
|
|
|
|
2021-07-10 20:28:08 +02:00
|
|
|
if loc.Path == "" {
|
|
|
|
loc.Path = "/"
|
|
|
|
}
|
2020-11-26 06:59:12 +01:00
|
|
|
|
2021-07-10 20:28:08 +02:00
|
|
|
scheme := "gemini"
|
|
|
|
if strings.HasPrefix(loc.Path, "/file/") {
|
|
|
|
scheme = "file"
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(u, "file://") {
|
|
|
|
if !allowFileAccess {
|
|
|
|
return "http://" + daemonAddress + "/?FileAccessNotAllowed"
|
2020-11-26 06:59:12 +01:00
|
|
|
}
|
2021-07-10 20:28:08 +02:00
|
|
|
return "http://" + daemonAddress + "/file/" + u[7:]
|
|
|
|
}
|
2020-11-26 06:59:12 +01:00
|
|
|
|
2021-07-10 20:28:08 +02:00
|
|
|
offset := 0
|
|
|
|
if strings.HasPrefix(u, "gemini://") {
|
|
|
|
offset = 9
|
|
|
|
}
|
|
|
|
firstSlash := strings.IndexRune(u[offset:], '/')
|
|
|
|
if firstSlash != -1 {
|
|
|
|
u = strings.ToLower(u[:firstSlash+offset]) + u[firstSlash+offset:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(u, "gemini://") {
|
|
|
|
return "http://" + daemonAddress + "/gemini/" + u[9:]
|
|
|
|
} else if strings.Contains(u, "://") {
|
|
|
|
return u
|
|
|
|
} else if loc != nil && len(u) > 0 && !strings.HasPrefix(u, "//") {
|
|
|
|
if u[0] != '/' {
|
|
|
|
if loc.Path[len(loc.Path)-1] == '/' {
|
|
|
|
u = path.Join("/", loc.Path, u)
|
|
|
|
} else {
|
|
|
|
u = path.Join("/", path.Dir(loc.Path), u)
|
2020-11-21 17:53:04 +01:00
|
|
|
}
|
|
|
|
}
|
2021-07-10 20:28:08 +02:00
|
|
|
return "http://" + daemonAddress + "/" + scheme + "/" + strings.ToLower(loc.Host) + u
|
2020-11-21 17:53:04 +01:00
|
|
|
}
|
2021-07-10 20:28:08 +02:00
|
|
|
return "http://" + daemonAddress + "/" + scheme + "/" + u
|
2020-11-21 17:53:04 +01:00
|
|
|
}
|
|
|
|
|
2020-12-03 19:42:39 +01:00
|
|
|
func newPage() []byte {
|
|
|
|
data := []byte(pageHeader)
|
|
|
|
if daemonAddress != "" {
|
|
|
|
data = append(data, navHeader...)
|
|
|
|
}
|
|
|
|
return append(data, contentHeader...)
|
|
|
|
}
|
|
|
|
|
2020-11-21 17:53:04 +01:00
|
|
|
// Convert converts text/gemini to text/html.
|
|
|
|
func Convert(page []byte, u string) []byte {
|
|
|
|
var result []byte
|
|
|
|
|
|
|
|
var preformatted bool
|
|
|
|
|
|
|
|
parsedURL, err := url.Parse(u)
|
|
|
|
if err != nil {
|
|
|
|
parsedURL = nil
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(bytes.NewReader(page))
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Bytes()
|
|
|
|
l := len(line)
|
|
|
|
if l >= 3 && string(line[0:3]) == "```" {
|
|
|
|
preformatted = !preformatted
|
|
|
|
if preformatted {
|
|
|
|
result = append(result, []byte("<pre>\n")...)
|
|
|
|
} else {
|
|
|
|
result = append(result, []byte("</pre>\n")...)
|
|
|
|
}
|
2020-11-22 05:44:22 +01:00
|
|
|
continue
|
2020-11-21 17:53:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if preformatted {
|
2020-11-25 03:34:14 +01:00
|
|
|
result = append(result, html.EscapeString(string(line))...)
|
2020-11-21 17:53:04 +01:00
|
|
|
result = append(result, []byte("\n")...)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-11-24 06:35:33 +01:00
|
|
|
if l >= 6 && bytes.HasPrefix(line, []byte("=>")) {
|
|
|
|
splitStart := 2
|
2020-11-24 23:31:14 +01:00
|
|
|
if line[splitStart] == ' ' || line[splitStart] == '\t' {
|
2020-11-24 06:35:33 +01:00
|
|
|
splitStart++
|
|
|
|
}
|
2020-11-25 17:52:51 +01:00
|
|
|
|
|
|
|
var split [][]byte
|
|
|
|
firstSpace := bytes.IndexRune(line[splitStart:], ' ')
|
|
|
|
firstTab := bytes.IndexRune(line[splitStart:], '\t')
|
|
|
|
if firstSpace != -1 && (firstTab == -1 || firstSpace < firstTab) {
|
|
|
|
split = bytes.SplitN(line[splitStart:], []byte(" "), 2)
|
|
|
|
} else if firstTab != -1 {
|
2020-11-24 06:35:33 +01:00
|
|
|
split = bytes.SplitN(line[splitStart:], []byte("\t"), 2)
|
2020-11-23 02:54:44 +01:00
|
|
|
}
|
2020-11-24 06:35:33 +01:00
|
|
|
|
2020-11-25 17:52:51 +01:00
|
|
|
var linkURL []byte
|
|
|
|
var linkLabel []byte
|
2020-11-21 17:53:04 +01:00
|
|
|
if len(split) == 2 {
|
2020-11-24 06:35:33 +01:00
|
|
|
linkURL = split[0]
|
|
|
|
linkLabel = split[1]
|
2020-11-25 17:52:51 +01:00
|
|
|
} else {
|
|
|
|
linkURL = line[splitStart:]
|
|
|
|
linkLabel = line[splitStart:]
|
2020-11-21 17:53:04 +01:00
|
|
|
}
|
2020-11-25 17:52:51 +01:00
|
|
|
|
2021-06-09 01:19:26 +02:00
|
|
|
parts := strings.Split(string(linkURL), ".")
|
|
|
|
extension := parts[len(parts)-1]
|
|
|
|
isImage := false
|
|
|
|
for _, ext := range imageExtensions {
|
|
|
|
if extension == ext {
|
|
|
|
isImage = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 22:45:52 +02:00
|
|
|
uri := html.EscapeString(rewriteURL(string(linkURL), parsedURL))
|
|
|
|
title := html.EscapeString(string(linkLabel))
|
|
|
|
|
|
|
|
// If link ends with gif/png/jpg, add a image instead of a link
|
2021-06-09 01:19:26 +02:00
|
|
|
if isImage && Config.ConvertImages {
|
2021-07-10 05:09:11 +02:00
|
|
|
result = append(result, []byte("<img src=\""+uri+"\" alt=\""+title+"\">")...)
|
2021-06-09 01:19:26 +02:00
|
|
|
} else {
|
2021-07-10 05:09:11 +02:00
|
|
|
result = append(result, []byte("<a href=\""+uri+"\">"+title+"</a><br>")...)
|
2021-06-09 01:19:26 +02:00
|
|
|
}
|
|
|
|
|
2020-11-24 06:35:33 +01:00
|
|
|
continue
|
2020-11-21 17:53:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
heading := 0
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
if line[i] == '#' {
|
|
|
|
heading++
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if heading > 0 {
|
2020-11-25 03:34:14 +01:00
|
|
|
result = append(result, []byte(fmt.Sprintf("<h%d>%s</h%d>", heading, html.EscapeString(string(line[heading:])), heading))...)
|
2020-11-21 17:53:04 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-11-25 03:34:14 +01:00
|
|
|
result = append(result, html.EscapeString(string(line))...)
|
2020-11-21 17:53:04 +01:00
|
|
|
result = append(result, []byte("<br>")...)
|
|
|
|
}
|
|
|
|
|
2020-11-22 05:44:22 +01:00
|
|
|
if preformatted {
|
|
|
|
result = append(result, []byte("</pre>\n")...)
|
|
|
|
}
|
|
|
|
|
2020-12-03 19:42:39 +01:00
|
|
|
data := newPage()
|
2020-11-27 05:43:03 +01:00
|
|
|
data = append(data, result...)
|
|
|
|
data = append(data, []byte(pageFooter)...)
|
|
|
|
return fillTemplateVariables(data, u, false)
|
2020-11-21 17:53:04 +01:00
|
|
|
}
|