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 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) (limited to 'internal/port/gemini.go') 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()) } -- cgit v1.3.1