Browse Source

Display navigation bar in pages

pull/5/head
Trevor Slocum 2 years ago
parent
commit
f8ae52eb7d
  1. 21
      pkg/gmitohtml/assets.go
  2. 2
      pkg/gmitohtml/convert.go
  3. 40
      pkg/gmitohtml/daemon.go

21
pkg/gmitohtml/assets.go

@ -9,7 +9,11 @@ const pageHeader = `
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="/assets/style.css">
</head>
<body>`
<body>
<form method="post" action="/" novalidate>
<input type="url" name="address" placeholder="Address" size="40" value="~GEMINICURRENTURL~" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" css="width: 100%;" ~GEMINIAUTOFOCUS~>
</form>
<br>`
const pageFooter = `
</body>
@ -17,10 +21,6 @@ const pageFooter = `
`
const indexPage = pageHeader + `
<form method="post" action="/" novalidate>
<input type="url" name="address" placeholder="Address" size="50" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" autofocus> <input type="submit" value="Go">
</form>
<br>
<ul>
<li><a href="/gemini/gus.guru/">GUS - Gemini Universal Search</a></li>
<li><a href="/gemini/gemini.circumlunar.space/">Gemini protocol</a></li>
@ -28,10 +28,13 @@ const indexPage = pageHeader + `
` + pageFooter
const inputPage = pageHeader + `
<form method="post" action="GEMINICURRENTURL">
<b>GEMINICURRENTURL</b> requests input.<br><br><br>
<b>GEMINIINPUTPROMPT</b><br><br>
<input type="GEMINIINPUTTYPE" name="input" placeholder="Input" size="50" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" autofocus> <input type="submit" value="Go">
<form method="post" action="~GEMINIINPUTFORM~">
<div style="padding-top: 25px;">
<span style="font-size: 1.5em;">~GEMINIINPUTPROMPT~</span><br><br>
<div>
<input type="~GEMINIINPUTTYPE~" name="input" placeholder="Input" size="40" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" autofocus>
</div>
</div>
</form>
` + pageFooter

2
pkg/gmitohtml/convert.go

@ -116,5 +116,5 @@ func Convert(page []byte, u string) []byte {
result = append([]byte(pageHeader), result...)
result = append(result, []byte(pageFooter)...)
return result
return fillTemplateVariables(result, u, false)
}

40
pkg/gmitohtml/daemon.go

@ -6,6 +6,7 @@ import (
"crypto/x509"
"errors"
"fmt"
"html"
"io/ioutil"
"log"
"net/http"
@ -91,31 +92,28 @@ func fetch(u string) ([]byte, []byte, error) {
data = []byte(inputPage)
data = bytes.Replace(data, []byte("GEMINICURRENTURL"), []byte(rewriteURL(u, requestURL)), 1)
currentURL := u
if strings.HasPrefix(currentURL, "gemini://") {
currentURL = currentURL[9:]
}
data = bytes.Replace(data, []byte("GEMINICURRENTURL"), []byte(currentURL), 1)
data = bytes.Replace(data, []byte("~GEMINIINPUTFORM~"), []byte(html.EscapeString(rewriteURL(u, requestURL))), 1)
prompt := "(No input prompt)"
if len(header) > 3 {
prompt = string(header[3:])
}
data = bytes.Replace(data, []byte("GEMINIINPUTPROMPT"), []byte(prompt), 1)
data = bytes.Replace(data, []byte("~GEMINIINPUTPROMPT~"), []byte(prompt), 1)
inputType := "text"
if requestSensitiveInput {
inputType = "password"
}
data = bytes.Replace(data, []byte("GEMINIINPUTTYPE"), []byte(inputType), 1)
data = bytes.Replace(data, []byte("~GEMINIINPUTTYPE~"), []byte(inputType), 1)
return header, data, nil
return header, fillTemplateVariables(data, u, false), nil
}
if !bytes.HasPrefix(header, []byte("2")) {
return header, []byte(fmt.Sprintf(pageHeader+"Server sent unexpected header:<br><br><b>%s</b>", header) + pageFooter), nil
errorPage := []byte(pageHeader)
errorPage = append(errorPage, []byte(fmt.Sprintf("Server sent unexpected header:<br><br><b>%s</b>", header))...)
errorPage = append(errorPage, []byte(pageFooter)...)
return header, errorPage, nil
}
if bytes.HasPrefix(header, []byte("20 text/html")) {
@ -131,7 +129,25 @@ func handleIndex(writer http.ResponseWriter, request *http.Request) {
return
}
writer.Write([]byte(indexPage))
writer.Write(fillTemplateVariables([]byte(indexPage), request.URL.String(), true))
}
func fillTemplateVariables(data []byte, currentURL string, autofocus bool) []byte {
if strings.HasPrefix(currentURL, "gemini://") {
currentURL = currentURL[9:]
}
if currentURL == "/" {
currentURL = ""
}
data = bytes.ReplaceAll(data, []byte("~GEMINICURRENTURL~"), []byte(currentURL))
autofocusValue := ""
if autofocus {
autofocusValue = "autofocus"
}
data = bytes.ReplaceAll(data, []byte("~GEMINIAUTOFOCUS~"), []byte(autofocusValue))
return data
}
func handleRequest(writer http.ResponseWriter, request *http.Request) {

Loading…
Cancel
Save