2020-11-21 17:53:04 +01:00
|
|
|
package gmitohtml
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"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
|
|
|
|
|
2020-11-21 17:53:04 +01:00
|
|
|
func rewriteURL(u string, loc *url.URL) string {
|
|
|
|
if daemonAddress != "" {
|
|
|
|
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, "//") {
|
|
|
|
newPath := u
|
|
|
|
if u[0] != '/' {
|
|
|
|
newPath = path.Join(loc.Path, u)
|
|
|
|
}
|
|
|
|
return "http://" + daemonAddress + "/gemini/" + loc.Host + newPath
|
|
|
|
}
|
|
|
|
return "http://" + daemonAddress + "/gemini/" + u
|
|
|
|
}
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
result = append(result, line...)
|
|
|
|
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++
|
|
|
|
}
|
|
|
|
split := bytes.SplitN(line[splitStart:], []byte(" "), 2)
|
2020-11-23 02:54:44 +01:00
|
|
|
if len(split) != 2 {
|
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
|
|
|
|
|
|
|
linkURL := line[splitStart:]
|
|
|
|
linkLabel := line[splitStart:]
|
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-21 17:53:04 +01:00
|
|
|
}
|
2020-11-24 06:35:33 +01:00
|
|
|
link := append([]byte(`<a href="`), rewriteURL(string(linkURL), parsedURL)...)
|
|
|
|
link = append(link, []byte(`">`)...)
|
|
|
|
link = append(link, linkLabel...)
|
|
|
|
link = append(link, []byte(`</a>`)...)
|
|
|
|
result = append(result, link...)
|
|
|
|
result = append(result, []byte("<br>")...)
|
|
|
|
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-22 05:44:22 +01:00
|
|
|
result = append(result, []byte(fmt.Sprintf("<h%d>%s</h%d>", heading, line[heading:], heading))...)
|
2020-11-21 17:53:04 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, line...)
|
|
|
|
result = append(result, []byte("<br>")...)
|
|
|
|
}
|
|
|
|
|
2020-11-22 05:44:22 +01:00
|
|
|
if preformatted {
|
|
|
|
result = append(result, []byte("</pre>\n")...)
|
|
|
|
}
|
|
|
|
|
2020-11-23 03:32:29 +01:00
|
|
|
result = append([]byte(pageHeader), result...)
|
|
|
|
result = append(result, []byte(pageFooter)...)
|
2020-11-24 23:29:16 +01:00
|
|
|
return fillTemplateVariables(result, u, false)
|
2020-11-21 17:53:04 +01:00
|
|
|
}
|