Browse Source

Fix link parsing

pull/5/head v1.0.1
Trevor Slocum 2 years ago
parent
commit
8c9f7852bb
  1. 3
      CHANGELOG
  2. 16
      pkg/gmitohtml/assets.go
  3. 23
      pkg/gmitohtml/convert.go

3
CHANGELOG

@ -1,6 +1,7 @@
1.0.1:
- Display navigation bar in pages
- Add option to allow local file access
- Display navigation bar in pages
- Support dark themed pages via prefers-color-scheme
1.0.0:
- Initial release

16
pkg/gmitohtml/assets.go

@ -399,15 +399,25 @@ template {
color: white;
background-color: black;
}
h1, h2, h3, h4, h5, h6 {
color: white;
}
a {
color: #8d94ff;
color: rgb(26, 168, 245);
text-decoration: none;
}
a:hover {
color: white;
a:hover,
a:focus,
a:active {
color: rgb(24, 151, 219);
text-decoration: underline;
}
a:visited {
color: rgb(200, 118, 255);
}
input {
background-color: black;
color: white;

23
pkg/gmitohtml/convert.go

@ -38,7 +38,11 @@ func rewriteURL(u string, loc *url.URL) string {
} else if loc != nil && len(u) > 0 && !strings.HasPrefix(u, "//") {
newPath := u
if u[0] != '/' {
newPath = path.Join(loc.Path, u)
if loc.Path[len(loc.Path)-1] == '/' {
newPath = path.Join("/", loc.Path, u)
} else {
newPath = path.Join("/", path.Dir(loc.Path), u)
}
}
return "http://" + daemonAddress + "/" + scheme + "/" + loc.Host + newPath
}
@ -84,17 +88,26 @@ func Convert(page []byte, u string) []byte {
if line[splitStart] == ' ' || line[splitStart] == '\t' {
splitStart++
}
split := bytes.SplitN(line[splitStart:], []byte(" "), 2)
if len(split) != 2 {
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 {
split = bytes.SplitN(line[splitStart:], []byte("\t"), 2)
}
linkURL := line[splitStart:]
linkLabel := line[splitStart:]
var linkURL []byte
var linkLabel []byte
if len(split) == 2 {
linkURL = split[0]
linkLabel = split[1]
} else {
linkURL = line[splitStart:]
linkLabel = line[splitStart:]
}
link := append([]byte(`<a href="`), html.EscapeString(rewriteURL(string(linkURL), parsedURL))...)
link = append(link, []byte(`">`)...)
link = append(link, html.EscapeString(string(linkLabel))...)

Loading…
Cancel
Save