From e58fabd89f9708aaeb10e52f731759712034833f Mon Sep 17 00:00:00 2001 From: Feuerfuchs Date: Mon, 18 May 2020 18:02:20 +0200 Subject: WIP: Refactoring --- internal/port/gemini.go | 38 ++++++++++++++++---------- internal/port/main.go | 56 +++++++++++++++++++------------------- internal/port/tpl/_fonts.html | 12 ++++---- internal/port/tpl/_header.html | 48 -------------------------------- internal/port/tpl/_modals.html | 2 +- internal/port/tpl/gemini.html | 62 ++++++++++++++++++++++++++++-------------- internal/port/tpl/gopher.html | 19 ++++--------- 7 files changed, 105 insertions(+), 132 deletions(-) delete mode 100644 internal/port/tpl/_header.html (limited to 'internal/port') diff --git a/internal/port/gemini.go b/internal/port/gemini.go index 0d8292c..740fccd 100644 --- a/internal/port/gemini.go +++ b/internal/port/gemini.go @@ -25,6 +25,7 @@ type GeminiTemplateVariables struct { Assets AssetList Sections []GeminiSection Nav []GeminiNavItem + IsPlain bool } type GeminiNavItem struct { @@ -33,7 +34,7 @@ type GeminiNavItem struct { } type GeminiSection struct { - Type libgemini.GeminiDocSectionType + Type string Text string URL template.URL Items []string @@ -83,19 +84,19 @@ func parseGeminiDocument(body *bytes.Buffer, uri string, hostport string) (secti for _, section := range unpreppedSections { if section.Type != libgemini.LINK { sections = append(sections, GeminiSection{ - Type: section.Type, + Type: section.Type.String(), Text: section.Text, URL: template.URL(section.URL), Items: section.Items, }) + } else { + sections = append(sections, GeminiSection{ + Type: section.Type.String(), + Text: section.Text, + URL: template.URL(resolveURL(section.URL, baseURL)), + Items: section.Items, + }) } - - sections = append(sections, GeminiSection{ - Type: section.Type, - Text: section.Text, - URL: template.URL(resolveURL(section.URL, baseURL)), - Items: section.Items, - }) } return @@ -133,9 +134,10 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass URL: hostport, Assets: assetList, Sections: []GeminiSection{{ - Type: libgemini.RAW_TEXT, + Type: libgemini.RAW_TEXT.String(), Text: fmt.Sprintf("Error: %s", err), }}, + IsPlain: true, }); e != nil { log.Println("Template error: " + e.Error()) log.Println(err.Error()) @@ -162,9 +164,10 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass URL: fmt.Sprintf("%s/%s", hostport, uri), Assets: assetList, Sections: []GeminiSection{{ - Type: libgemini.RAW_TEXT, + Type: libgemini.RAW_TEXT.String(), Text: fmt.Sprintf("Error: %s", err), }}, + IsPlain: true, }); e != nil { log.Println("Template error: " + e.Error()) log.Println(err.Error()) @@ -184,9 +187,10 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass URL: fmt.Sprintf("%s/%s", hostport, uri), Assets: assetList, Sections: []GeminiSection{{ - Type: libgemini.RAW_TEXT, + Type: libgemini.RAW_TEXT.String(), Text: fmt.Sprintf("Error: %s", err), }}, + IsPlain: true, }); e != nil { log.Println("Template error: " + e.Error()) log.Println(err.Error()) @@ -204,16 +208,17 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass URL: fmt.Sprintf("%s/%s", hostport, uri), Assets: assetList, Sections: []GeminiSection{{ - Type: libgemini.RAW_TEXT, + Type: libgemini.RAW_TEXT.String(), Text: fmt.Sprintf("Error %d: %s", res.Header.Status, res.Header.Meta), }}, + IsPlain: true, }); err != nil { log.Println("Template error: " + err.Error()) } return } - if strings.HasPrefix(res.Header.Meta, "text/") { + if strings.HasPrefix(res.Header.Meta, "text/") && !strings.HasPrefix(res.Header.Meta, "text/html") && !strings.HasPrefix(res.Header.Meta, "text/css") { buf := new(bytes.Buffer) _, params, err := mime.ParseMediaType(res.Header.Meta) @@ -230,12 +235,14 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass } var sections []GeminiSection + isPlain := true if strings.HasPrefix(res.Header.Meta, libgemini.MIME_GEMINI) { sections = parseGeminiDocument(buf, uri, hostport) + isPlain = false } else { sections = append(sections, GeminiSection{ - Type: libgemini.RAW_TEXT, + Type: libgemini.RAW_TEXT.String(), Text: buf.String(), }) } @@ -245,6 +252,7 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass URL: fmt.Sprintf("%s/%s", hostport, uri), Assets: assetList, Sections: sections, + IsPlain: isPlain, }); err != nil { log.Println("Template error: " + err.Error()) } diff --git a/internal/port/main.go b/internal/port/main.go index 267df44..763057b 100644 --- a/internal/port/main.go +++ b/internal/port/main.go @@ -18,12 +18,12 @@ import ( ) type AssetList struct { - Style string - JS string - FontW string - FontW2 string - PropFontW string - PropFontW2 string + Style string + JS string + FontRegularW string + FontRegularW2 string + FontBoldW string + FontBoldW2 string } type startTemplateVariables struct { @@ -136,29 +136,29 @@ func ListenAndServe(bind, startpagefile string, robotsfile string, robotsdebug b // // Fonts - fontdataw, err := box.Find("iosevka-term-ss03-regular.woff") + fontRegularWData, err := box.Find("iosevka-fixed-ss03-regular.woff") if err != nil { - fontdataw = []byte{} + fontRegularWData = []byte{} } - fontwAsset := fmt.Sprintf("/iosevka-term-ss03-regular-%x.woff", md5.Sum(fontdataw)) + fontRegularWAsset := fmt.Sprintf("/iosevka-fixed-ss03-regular-%x.woff", md5.Sum(fontRegularWData)) - fontdataw2, err := box.Find("iosevka-term-ss03-regular.woff2") + fontRegularW2Data, err := box.Find("iosevka-fixed-ss03-regular.woff2") if err != nil { - fontdataw2 = []byte{} + fontRegularW2Data = []byte{} } - fontw2Asset := fmt.Sprintf("/iosevka-term-ss03-regular-%x.woff2", md5.Sum(fontdataw2)) + fontRegularW2Asset := fmt.Sprintf("/iosevka-fixed-ss03-regular-%x.woff2", md5.Sum(fontRegularW2Data)) - propfontdataw, err := box.Find("iosevka-aile-regular.woff") + fontBoldWData, err := box.Find("iosevka-fixed-ss03-bold.woff") if err != nil { - propfontdataw = []byte{} + fontBoldWData = []byte{} } - propfontwAsset := fmt.Sprintf("/iosevka-aile-regular-%x.woff", md5.Sum(propfontdataw)) + fontBoldWAsset := fmt.Sprintf("/iosevka-fixed-ss03-bold-%x.woff", md5.Sum(fontBoldWData)) - propfontdataw2, err := box.Find("iosevka-aile-regular.woff2") + fontBoldW2Data, err := box.Find("iosevka-fixed-ss03-bold.woff2") if err != nil { - propfontdataw2 = []byte{} + fontBoldW2Data = []byte{} } - propfontw2Asset := fmt.Sprintf("/iosevka-aile-regular-%x.woff2", md5.Sum(propfontdataw2)) + fontBoldW2Asset := fmt.Sprintf("/iosevka-fixed-ss03-bold-%x.woff2", md5.Sum(fontBoldW2Data)) // // Stylesheet @@ -277,12 +277,12 @@ func ListenAndServe(bind, startpagefile string, robotsfile string, robotsdebug b }) assets := AssetList{ - Style: styleAsset, - JS: jsAsset, - FontW: fontwAsset, - FontW2: fontw2Asset, - PropFontW: propfontwAsset, - PropFontW2: propfontw2Asset, + Style: styleAsset, + JS: jsAsset, + FontRegularW: fontRegularWAsset, + FontRegularW2: fontRegularW2Asset, + FontBoldW: fontBoldWAsset, + FontBoldW2: fontBoldW2Asset, } http.Handle("/", gziphandler.GzipHandler(DefaultHandler(startpageTpl, startpagetext, assets))) @@ -292,10 +292,10 @@ func ListenAndServe(bind, startpagefile string, robotsfile string, robotsdebug b http.Handle("/favicon.ico", gziphandler.GzipHandler(FaviconHandler(favicondata))) http.Handle(styleAsset, gziphandler.GzipHandler(StyleHandler(styledata))) http.Handle(jsAsset, gziphandler.GzipHandler(JavaScriptHandler(jsdata))) - http.HandleFunc(fontwAsset, FontHandler(false, fontdataw)) - http.HandleFunc(fontw2Asset, FontHandler(true, fontdataw2)) - http.HandleFunc(propfontwAsset, FontHandler(false, propfontdataw)) - http.HandleFunc(propfontw2Asset, FontHandler(true, propfontdataw2)) + http.HandleFunc(fontRegularWAsset, FontHandler(false, fontRegularWData)) + http.HandleFunc(fontRegularW2Asset, FontHandler(true, fontRegularW2Data)) + http.HandleFunc(fontBoldWAsset, FontHandler(false, fontBoldWData)) + http.HandleFunc(fontBoldW2Asset, FontHandler(true, fontBoldW2Data)) //http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/")))) return http.ListenAndServe(bind, nil) diff --git a/internal/port/tpl/_fonts.html b/internal/port/tpl/_fonts.html index b56aa22..a947222 100644 --- a/internal/port/tpl/_fonts.html +++ b/internal/port/tpl/_fonts.html @@ -3,14 +3,14 @@ font-family: 'Iosevka Term SS03'; font-style: normal; font-weight: normal; - src: url('{{ .Assets.FontW2 }}') format('woff2'), - url('{{ .Assets.FontW }}') format('woff'); + src: url('{{ .Assets.FontRegularW2 }}') format('woff2'), + url('{{ .Assets.FontRegularW }}') format('woff'); } @font-face { - font-family: 'Iosevka Aile'; + font-family: 'Iosevka Term SS03'; font-style: normal; - font-weight: normal; - src: url('{{ .Assets.PropFontW2 }}') format('woff2'), - url('{{ .Assets.PropFontW }}') format('woff'); + font-weight: bold; + src: url('{{ .Assets.FontBoldW2 }}') format('woff2'), + url('{{ .Assets.FontBoldW }}') format('woff'); } diff --git a/internal/port/tpl/_header.html b/internal/port/tpl/_header.html deleted file mode 100644 index 5bcd254..0000000 --- a/internal/port/tpl/_header.html +++ /dev/null @@ -1,48 +0,0 @@ -
-
- {{ .Protocol }}://:// - - {{- if .URL -}} - {{- $page := . -}} - {{- $href := printf "/%s" .Protocol -}} - {{- $uriParts := split .URL "/" -}} - - {{- $uriLast := $uriParts | last -}} - {{- $uriParts = $uriParts | pop -}} - {{- if eq $uriLast "" -}} - {{- $uriLast = $uriParts | last -}} - {{- $uriParts = $uriParts | pop -}} - {{- end -}} - - {{- range $i, $part := $uriParts -}} - {{- if and (eq $page.Protocol "gopher") (eq $i 1) -}} - {{- $href = printf "%s/1" $href -}} - {{- $part = $part | trimLeftChar -}} - {{- if not (eq $part "") -}} - {{- $href = printf "%s/%s" $href $part -}} - /{{ $part }} - {{- end -}} - {{- else -}} - {{- $href = printf "%s/%s" $href . -}} - {{- if ne $i 0 -}} - / - {{- end -}} - {{ . }} - {{- end -}} - {{- end -}} - {{- if ne (len $uriParts) 0 -}} - / - {{- end -}} - {{- if and (eq $page.Protocol "gopher") (eq (len $uriParts) 1) -}} - {{- $uriLast = $uriLast | trimLeftChar -}} - {{- end -}} - {{ $uriLast }} - {{- end -}} -
-
- {{- if and (not .Lines) (not .Error) (eq .Protocol "gopher") -}} - - {{- end -}} -
-
-
diff --git a/internal/port/tpl/_modals.html b/internal/port/tpl/_modals.html index 3c08d9a..3bbdef2 100644 --- a/internal/port/tpl/_modals.html +++ b/internal/port/tpl/_modals.html @@ -9,7 +9,7 @@
- Monospace font + Gemini: Monospace font
diff --git a/internal/port/tpl/gemini.html b/internal/port/tpl/gemini.html index 08f1b8e..df50d50 100644 --- a/internal/port/tpl/gemini.html +++ b/internal/port/tpl/gemini.html @@ -7,28 +7,50 @@ {{- template "_fonts.html" . -}} - - {{- template "_header.html" . -}} + +
+
+ gemini://:// + {{- range $i, $item := .Nav -}} + {{- if ne $i 0 -}} + / + {{- end -}} + {{- if .Current -}} + {{ .Label }} + {{- else -}} + {{ .Label }} + {{- end -}} + {{- end -}} +
+
+
+
+
-
-				{{- if .Lines -}}
-					{{- $content := "" -}}
-					{{- range .Lines -}}
-						{{- if ne $content "" -}}
-							{{- $content = printf "%s\n" $content -}}
-						{{- end -}}
-						{{- if .Link -}}
-							{{- $content = printf "%s%s" $content (printf "%s  %s" .Type .Type .Link (.Text | HTMLEscape)) -}}
-						{{- else -}}
-							{{- $content = printf "%s%s" $content (printf "     %s" (.Text | HTMLEscape)) -}}
-						{{- end -}}
-					{{- end -}}
-					{{- $content | safeHtml -}}
-				{{- else -}}
-					{{- .RawText -}}
-				{{- end -}}
-			
+
+ {{- range .Sections -}} + {{- if eq .Type "RAW_TEXT" -}} +
```
{{- .Text -}}
+ {{- else if eq .Type "REFLOW_TEXT" -}} +

{{- .Text -}}

+ {{- else if eq .Type "LINK" -}} + + {{- else if eq .Type "HEADING_1" -}} +
#

{{- .Text -}}

+ {{- else if eq .Type "HEADING_2" -}} +
##

{{- .Text -}}

+ {{- else if eq .Type "HEADING_3" -}} +
###

{{- .Text -}}

+ {{- else if eq .Type "LIST" -}} +
    + {{- range .Items -}} +
  • {{- . -}}
  • + {{- end -}} +
+ {{- end -}} + {{- end -}} +
{{- template "_modals.html" . -}} diff --git a/internal/port/tpl/gopher.html b/internal/port/tpl/gopher.html index c971847..5436123 100644 --- a/internal/port/tpl/gopher.html +++ b/internal/port/tpl/gopher.html @@ -31,25 +31,16 @@
-
-        {{- $content := "" -}}
+      
{{- $page := . -}} {{- range .Lines -}} - {{- if ne $content "" -}} - {{- $content = printf "%s\n" $content -}} - {{- end -}} - {{- if $page.IsPlain -}} - {{- $content = printf "%s%s" $content (.Text | HTMLEscape) -}} + {{- if .Link -}} +
{{- .Type -}}{{- .Text -}}
{{- else -}} - {{- if .Link -}} - {{- $content = printf "%s%s" $content (printf "%s %s" .Type .Type .Link (.Text | HTMLEscape)) -}} - {{- else -}} - {{- $content = printf "%s%s" $content (printf " %s" (.Text | HTMLEscape)) -}} - {{- end -}} +
{{- .Text -}}
{{- end -}} {{- end -}} - {{- $content | safeHtml -}} -
+
{{- template "_modals.html" . -}} -- cgit v1.2.3-70-g09d2