flounder

A simple gemini site builder
Log | Files | Refs | README | LICENSE

commit a8444973aece69a635505a6f022743ce0bf84d68
parent 1d6c815b88efb0faae7dfbdcda6167e593e8c3cf
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Mon, 26 Oct 2020 12:56:22 -0700

Fix go-gemini dep, tls cert

Diffstat:
Mgemini.go | 18++++++++++++------
Agmi2html.go | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mgo.mod | 2+-
Mgo.sum | 4++--
Mhttp.go | 4++--
5 files changed, 92 insertions(+), 11 deletions(-)

diff --git a/gemini.go b/gemini.go @@ -4,7 +4,7 @@ import ( "crypto/tls" "strings" // "fmt" - "git.sr.ht/~adnano/gmi" + gmi "git.sr.ht/~adnano/go-gemini" "io/ioutil" "log" "path" @@ -57,16 +57,22 @@ func runGeminiServer() { hostname := strings.SplitN(c.Host, ":", 1)[0] // is this necc? server.GetCertificate = func(hostname string, store *gmi.CertificateStore) *tls.Certificate { - cert, err := tls.LoadX509KeyPair(c.TLSCertFile, c.TLSKeyFile) + cert, err := store.Lookup(hostname) if err != nil { - log.Fatal("Invalid TLS cert") + cert, err := tls.LoadX509KeyPair(c.TLSCertFile, c.TLSKeyFile) + if err != nil { + log.Fatal("Invalid TLS cert") + } + store.Add(hostname, cert) + return &cert } - return &cert + return cert } + var mux gmi.ServeMux // replace with wildcard cert - server.HandleFunc(hostname, gmiIndex) - server.HandleFunc("*."+hostname, gmiPage) + mux.HandleFunc(hostname, gmiIndex) + mux.HandleFunc("*."+hostname, gmiPage) server.ListenAndServe() } diff --git a/gmi2html.go b/gmi2html.go @@ -0,0 +1,75 @@ +package main + +import ( + "fmt" + "html" + "strings" + + "git.sr.ht/~adnano/go-gemini" +) + +func textToHTML(text gemini.Text) string { + var b strings.Builder + var pre bool + var list bool + for _, l := range text { + if _, ok := l.(gemini.LineListItem); ok { + if !list { + list = true + fmt.Fprint(&b, "<ul>\n") + } + } else if list { + list = false + fmt.Fprint(&b, "</ul>\n") + } + switch l.(type) { + case gemini.LineLink: + link := l.(gemini.LineLink) + url := html.EscapeString(link.URL) + name := html.EscapeString(link.Name) + if name == "" { + name = url + } + fmt.Fprintf(&b, "<p><a href='%s'>%s</a></p>\n", url, name) + case gemini.LinePreformattingToggle: + pre = !pre + if pre { + fmt.Fprint(&b, "<pre>\n") + } else { + fmt.Fprint(&b, "</pre>\n") + } + case gemini.LinePreformattedText: + text := string(l.(gemini.LinePreformattedText)) + fmt.Fprintf(&b, "%s\n", html.EscapeString(text)) + case gemini.LineHeading1: + text := string(l.(gemini.LineHeading1)) + fmt.Fprintf(&b, "<h1>%s</h1>\n", html.EscapeString(text)) + case gemini.LineHeading2: + text := string(l.(gemini.LineHeading2)) + fmt.Fprintf(&b, "<h2>%s</h2>\n", html.EscapeString(text)) + case gemini.LineHeading3: + text := string(l.(gemini.LineHeading3)) + fmt.Fprintf(&b, "<h3>%s</h3>\n", html.EscapeString(text)) + case gemini.LineListItem: + text := string(l.(gemini.LineListItem)) + fmt.Fprintf(&b, "<li>%s</li>\n", html.EscapeString(text)) + case gemini.LineQuote: + text := string(l.(gemini.LineQuote)) + fmt.Fprintf(&b, "<blockquote>%s</blockquote>\n", html.EscapeString(text)) + case gemini.LineText: + text := string(l.(gemini.LineText)) + if text == "" { + fmt.Fprint(&b, "<br>\n") + } else { + fmt.Fprintf(&b, "<p>%s</p>\n", html.EscapeString(text)) + } + } + } + if pre { + fmt.Fprint(&b, "</pre>\n") + } + if list { + fmt.Fprint(&b, "</ul>\n") + } + return b.String() +} diff --git a/go.mod b/go.mod @@ -3,7 +3,7 @@ module git.sr.ht/flounder go 1.15 require ( - git.sr.ht/~adnano/gmi v0.1.0-alpha.2 + git.sr.ht/~adnano/go-gemini v0.1.1 github.com/BurntSushi/toml v0.3.1 github.com/gorilla/handlers v1.5.1 github.com/gorilla/sessions v1.2.1 diff --git a/go.sum b/go.sum @@ -1,5 +1,5 @@ -git.sr.ht/~adnano/gmi v0.1.0-alpha.2 h1:5/wzImYT3mJmZ27lazJ8YAdpiVN3QNJruLX7PXOITeo= -git.sr.ht/~adnano/gmi v0.1.0-alpha.2/go.mod h1:t/m2KtH+7lXIF7jjVN+bNvwPbE0nxHOpvlA/WZ/KeLQ= +git.sr.ht/~adnano/go-gemini v0.1.1 h1:g6OwUxLviy6dkPiuW2eRQP5Fow412vUsKmKYbCr2H9U= +git.sr.ht/~adnano/go-gemini v0.1.1/go.mod h1:If1VxEWcZDrRt5FeAFnGTcM2Ud1E3BXs3VJ5rnZWKq0= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ= diff --git a/http.go b/http.go @@ -3,7 +3,7 @@ package main import ( "bytes" "database/sql" - "git.sr.ht/~adnano/gmi" + gmi "git.sr.ht/~adnano/go-gemini" "github.com/gorilla/handlers" "github.com/gorilla/sessions" _ "github.com/mattn/go-sqlite3" @@ -355,7 +355,7 @@ func userFile(w http.ResponseWriter, r *http.Request) { } file, _ := os.Open(fileName) - htmlString := gmi.Parse(file).HTML() + htmlString := textToHTML(gmi.Parse(file)) data := struct { SiteBody template.HTML PageTitle string