From ce19e19efce3139d2c7b4024274b6ed2683e015e Mon Sep 17 00:00:00 2001 From: Feuerfuchs Date: Tue, 4 Jun 2019 22:28:02 +0200 Subject: Style improvements --- Makefile | 3 ++- assets/IBMPlexMono-Regular.woff | Bin 50664 -> 0 bytes assets/IBMPlexMono-Regular.woff2 | Bin 35536 -> 0 bytes assets/style.css | 1 - css/main.scss | 29 +++++++++++++---------------- gopherproxy.go | 31 ++++++++++++++++++++++--------- style.go | 3 +++ template.go | 5 +++-- 8 files changed, 43 insertions(+), 29 deletions(-) delete mode 100644 assets/IBMPlexMono-Regular.woff delete mode 100644 assets/IBMPlexMono-Regular.woff2 delete mode 100644 assets/style.css create mode 100644 style.go diff --git a/Makefile b/Makefile index c2151ad..8377afe 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,8 @@ dev: build ./gopherproxy -bind 127.0.0.1:8000 build: clean - sassc -t compressed css/main.scss assets/style.css + sassc -t compressed css/main.scss style.go + sed -i "s/.*/package gopherproxy\n\nvar styletext = \`&\`/" style.go go build -o ./gopherproxy ./cmd/gopherproxy/main.go profile: diff --git a/assets/IBMPlexMono-Regular.woff b/assets/IBMPlexMono-Regular.woff deleted file mode 100644 index 7d63d89..0000000 Binary files a/assets/IBMPlexMono-Regular.woff and /dev/null differ diff --git a/assets/IBMPlexMono-Regular.woff2 b/assets/IBMPlexMono-Regular.woff2 deleted file mode 100644 index d0d7ded..0000000 Binary files a/assets/IBMPlexMono-Regular.woff2 and /dev/null differ diff --git a/assets/style.css b/assets/style.css deleted file mode 100644 index 70b1998..0000000 --- a/assets/style.css +++ /dev/null @@ -1 +0,0 @@ -@font-face{font-family:'IBM Plex Mono';font-style:normal;font-weight:normal;src:url("/assets/IBMPlexMono-Regular.woff2") format("woff2"),url("/assets/IBMPlexMono-Regular.woff") format("woff")}body{background-color:#14171a;color:#cad1d8}.wrap{text-align:center}.content{display:inline-block;min-width:50em;margin:0;padding:2em 1em;text-align:left;font-family:'IBM Plex Mono', 'Fira Code', 'Fira Mono', 'Roboto Mono', 'Droid Sans Mono', Monaco, Consolas, Courier, monospace;font-size:1em;line-height:1.5}::selection{color:#000;background-color:#cad1d8}:link{color:#fff}:visited{color:#cad1d8}:link:hover,:visited:hover{color:#fff}.link-type{color:#929ba3} diff --git a/css/main.scss b/css/main.scss index dfda503..8ae988e 100644 --- a/css/main.scss +++ b/css/main.scss @@ -1,20 +1,17 @@ +$accent: scale-color(#e8ad58, $lightness: 30%); $background: hsl(210, 14%, 9%); $text: mix(hsl(210, 60%, 95%), $background, 85%); $text-minus: mix(hsl(210, 100%, 95%), $background, 60%); $text-faint: mix(hsl(210, 100%, 95%), $background, 40%); $text-plus: #fff; -$sel-background: $text; +$link-idle: $text-plus; +$link-visited: $text; +$sel-background: $accent; $sel-text: #000; -@font-face { - font-family: 'IBM Plex Mono'; - font-style: normal; - font-weight: normal; - src: url('/assets/IBMPlexMono-Regular.woff2') format('woff2'), - url('/assets/IBMPlexMono-Regular.woff') format('woff'); -} - body { + margin: 0; + padding: 0; background-color: $background; color: $text; } @@ -40,18 +37,18 @@ body { } :link { - color: $text-plus; + color: $link-idle; } :visited { - color: $text; + color: $link-visited; } -:link, :visited { - &:hover { - color: $text-plus; - } -} +// :link, :visited { +// &:hover { +// color: $text-plus; +// } +// } .link-type { color: $text-minus; diff --git a/gopherproxy.go b/gopherproxy.go index 6f1e8ec..dc849d0 100644 --- a/gopherproxy.go +++ b/gopherproxy.go @@ -22,7 +22,7 @@ type Item struct { Text string } -func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport string, d gopher.Directory) error { +func renderDirectory(w http.ResponseWriter, tpl *template.Template, styletext string, hostport string, d gopher.Directory) error { var title string out := make([]Item, len(d.Items)) @@ -73,9 +73,10 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport str return tpl.Execute(w, struct { Title string + Style string Lines []Item RawText string - }{title, out, ""}) + }{title, styletext, out, ""}) } // GopherHandler returns a Handler that proxies requests @@ -83,7 +84,7 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport str // to the request path and renders the content using the provided template. // The optional robots parameters points to a robotstxt.RobotsData struct // to test user agents against a configurable robotst.txt file. -func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, uri string) http.HandlerFunc { +func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, styletext string, uri string) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { agent := req.UserAgent() path := strings.TrimPrefix(req.URL.Path, "/") @@ -127,20 +128,21 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, uri } if res.Body != nil { - if strings.HasSuffix(uri, ".txt") { + if strings.HasSuffix(uri, ".txt") || strings.HasSuffix(uri, ".md") { // handle .txt files buf := new(bytes.Buffer) buf.ReadFrom(res.Body) tpl.Execute(w, struct { Title string + Style string RawText string Lines []Item - }{uri, buf.String(), nil}) + }{uri, styletext, buf.String(), nil}) } else { io.Copy(w, res.Body) } } else { - if err := renderDirectory(w, tpl, hostport, res.Dir); err != nil { + if err := renderDirectory(w, tpl, styletext, hostport, res.Dir); err != nil { io.WriteString(w, fmt.Sprintf("Error:
%s
", err)) return } @@ -187,19 +189,30 @@ func ListenAndServe(bind, robotsfile, uri string) error { } } + styledata, err := ioutil.ReadFile(".style") + if err == nil { + styletext = string(styledata) + } + tpldata, err := ioutil.ReadFile(".template") if err == nil { tpltext = string(tpldata) } - tpl, err = template.New("gophermenu").Parse(tpltext) + funcMap := template.FuncMap{ + "safeCss": func(s string) template.CSS { + return template.CSS(s) + }, + } + + tpl, err = template.New("gophermenu").Funcs(funcMap).Parse(tpltext) if err != nil { log.Fatal(err) } - http.HandleFunc("/", GopherHandler(tpl, robotsdata, uri)) + http.HandleFunc("/", GopherHandler(tpl, robotsdata, styletext, uri)) http.HandleFunc("/robots.txt", RobotsTxtHandler(robotstxtdata)) - http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/")))) + // http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/")))) return http.ListenAndServe(bind, nil) } diff --git a/style.go b/style.go new file mode 100644 index 0000000..e6e301a --- /dev/null +++ b/style.go @@ -0,0 +1,3 @@ +package gopherproxy + +var styletext = `body{margin:0;padding:0;background-color:#14171a;color:#cad1d8}.wrap{text-align:center}.content{display:inline-block;min-width:50em;margin:0;padding:2em 1em;text-align:left;font-family:'IBM Plex Mono', 'Fira Code', 'Fira Mono', 'Roboto Mono', 'Droid Sans Mono', Monaco, Consolas, Courier, monospace;font-size:1em;line-height:1.5}::selection{color:#000;background-color:#efc68a}:link{color:#fff}:visited{color:#cad1d8}.link-type{color:#929ba3}` diff --git a/template.go b/template.go index b42f811..9582e3a 100644 --- a/template.go +++ b/template.go @@ -6,9 +6,10 @@ var tpltext = ` {{.Title}} - - +
-- cgit v1.2.3-70-g09d2