diff options
Diffstat (limited to 'internal/port/gemini.go')
-rw-r--r-- | internal/port/gemini.go | 38 |
1 files changed, 23 insertions, 15 deletions
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 { | |||
25 | Assets AssetList | 25 | Assets AssetList |
26 | Sections []GeminiSection | 26 | Sections []GeminiSection |
27 | Nav []GeminiNavItem | 27 | Nav []GeminiNavItem |
28 | IsPlain bool | ||
28 | } | 29 | } |
29 | 30 | ||
30 | type GeminiNavItem struct { | 31 | type GeminiNavItem struct { |
@@ -33,7 +34,7 @@ type GeminiNavItem struct { | |||
33 | } | 34 | } |
34 | 35 | ||
35 | type GeminiSection struct { | 36 | type GeminiSection struct { |
36 | Type libgemini.GeminiDocSectionType | 37 | Type string |
37 | Text string | 38 | Text string |
38 | URL template.URL | 39 | URL template.URL |
39 | Items []string | 40 | Items []string |
@@ -83,19 +84,19 @@ func parseGeminiDocument(body *bytes.Buffer, uri string, hostport string) (secti | |||
83 | for _, section := range unpreppedSections { | 84 | for _, section := range unpreppedSections { |
84 | if section.Type != libgemini.LINK { | 85 | if section.Type != libgemini.LINK { |
85 | sections = append(sections, GeminiSection{ | 86 | sections = append(sections, GeminiSection{ |
86 | Type: section.Type, | 87 | Type: section.Type.String(), |
87 | Text: section.Text, | 88 | Text: section.Text, |
88 | URL: template.URL(section.URL), | 89 | URL: template.URL(section.URL), |
89 | Items: section.Items, | 90 | Items: section.Items, |
90 | }) | 91 | }) |
92 | } else { | ||
93 | sections = append(sections, GeminiSection{ | ||
94 | Type: section.Type.String(), | ||
95 | Text: section.Text, | ||
96 | URL: template.URL(resolveURL(section.URL, baseURL)), | ||
97 | Items: section.Items, | ||
98 | }) | ||
91 | } | 99 | } |
92 | |||
93 | sections = append(sections, GeminiSection{ | ||
94 | Type: section.Type, | ||
95 | Text: section.Text, | ||
96 | URL: template.URL(resolveURL(section.URL, baseURL)), | ||
97 | Items: section.Items, | ||
98 | }) | ||
99 | } | 100 | } |
100 | 101 | ||
101 | return | 102 | return |
@@ -133,9 +134,10 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass | |||
133 | URL: hostport, | 134 | URL: hostport, |
134 | Assets: assetList, | 135 | Assets: assetList, |
135 | Sections: []GeminiSection{{ | 136 | Sections: []GeminiSection{{ |
136 | Type: libgemini.RAW_TEXT, | 137 | Type: libgemini.RAW_TEXT.String(), |
137 | Text: fmt.Sprintf("Error: %s", err), | 138 | Text: fmt.Sprintf("Error: %s", err), |
138 | }}, | 139 | }}, |
140 | IsPlain: true, | ||
139 | }); e != nil { | 141 | }); e != nil { |
140 | log.Println("Template error: " + e.Error()) | 142 | log.Println("Template error: " + e.Error()) |
141 | log.Println(err.Error()) | 143 | log.Println(err.Error()) |
@@ -162,9 +164,10 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass | |||
162 | URL: fmt.Sprintf("%s/%s", hostport, uri), | 164 | URL: fmt.Sprintf("%s/%s", hostport, uri), |
163 | Assets: assetList, | 165 | Assets: assetList, |
164 | Sections: []GeminiSection{{ | 166 | Sections: []GeminiSection{{ |
165 | Type: libgemini.RAW_TEXT, | 167 | Type: libgemini.RAW_TEXT.String(), |
166 | Text: fmt.Sprintf("Error: %s", err), | 168 | Text: fmt.Sprintf("Error: %s", err), |
167 | }}, | 169 | }}, |
170 | IsPlain: true, | ||
168 | }); e != nil { | 171 | }); e != nil { |
169 | log.Println("Template error: " + e.Error()) | 172 | log.Println("Template error: " + e.Error()) |
170 | log.Println(err.Error()) | 173 | log.Println(err.Error()) |
@@ -184,9 +187,10 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass | |||
184 | URL: fmt.Sprintf("%s/%s", hostport, uri), | 187 | URL: fmt.Sprintf("%s/%s", hostport, uri), |
185 | Assets: assetList, | 188 | Assets: assetList, |
186 | Sections: []GeminiSection{{ | 189 | Sections: []GeminiSection{{ |
187 | Type: libgemini.RAW_TEXT, | 190 | Type: libgemini.RAW_TEXT.String(), |
188 | Text: fmt.Sprintf("Error: %s", err), | 191 | Text: fmt.Sprintf("Error: %s", err), |
189 | }}, | 192 | }}, |
193 | IsPlain: true, | ||
190 | }); e != nil { | 194 | }); e != nil { |
191 | log.Println("Template error: " + e.Error()) | 195 | log.Println("Template error: " + e.Error()) |
192 | log.Println(err.Error()) | 196 | log.Println(err.Error()) |
@@ -204,16 +208,17 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass | |||
204 | URL: fmt.Sprintf("%s/%s", hostport, uri), | 208 | URL: fmt.Sprintf("%s/%s", hostport, uri), |
205 | Assets: assetList, | 209 | Assets: assetList, |
206 | Sections: []GeminiSection{{ | 210 | Sections: []GeminiSection{{ |
207 | Type: libgemini.RAW_TEXT, | 211 | Type: libgemini.RAW_TEXT.String(), |
208 | Text: fmt.Sprintf("Error %d: %s", res.Header.Status, res.Header.Meta), | 212 | Text: fmt.Sprintf("Error %d: %s", res.Header.Status, res.Header.Meta), |
209 | }}, | 213 | }}, |
214 | IsPlain: true, | ||
210 | }); err != nil { | 215 | }); err != nil { |
211 | log.Println("Template error: " + err.Error()) | 216 | log.Println("Template error: " + err.Error()) |
212 | } | 217 | } |
213 | return | 218 | return |
214 | } | 219 | } |
215 | 220 | ||
216 | if strings.HasPrefix(res.Header.Meta, "text/") { | 221 | if strings.HasPrefix(res.Header.Meta, "text/") && !strings.HasPrefix(res.Header.Meta, "text/html") && !strings.HasPrefix(res.Header.Meta, "text/css") { |
217 | buf := new(bytes.Buffer) | 222 | buf := new(bytes.Buffer) |
218 | 223 | ||
219 | _, params, err := mime.ParseMediaType(res.Header.Meta) | 224 | _, params, err := mime.ParseMediaType(res.Header.Meta) |
@@ -230,12 +235,14 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass | |||
230 | } | 235 | } |
231 | 236 | ||
232 | var sections []GeminiSection | 237 | var sections []GeminiSection |
238 | isPlain := true | ||
233 | 239 | ||
234 | if strings.HasPrefix(res.Header.Meta, libgemini.MIME_GEMINI) { | 240 | if strings.HasPrefix(res.Header.Meta, libgemini.MIME_GEMINI) { |
235 | sections = parseGeminiDocument(buf, uri, hostport) | 241 | sections = parseGeminiDocument(buf, uri, hostport) |
242 | isPlain = false | ||
236 | } else { | 243 | } else { |
237 | sections = append(sections, GeminiSection{ | 244 | sections = append(sections, GeminiSection{ |
238 | Type: libgemini.RAW_TEXT, | 245 | Type: libgemini.RAW_TEXT.String(), |
239 | Text: buf.String(), | 246 | Text: buf.String(), |
240 | }) | 247 | }) |
241 | } | 248 | } |
@@ -245,6 +252,7 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass | |||
245 | URL: fmt.Sprintf("%s/%s", hostport, uri), | 252 | URL: fmt.Sprintf("%s/%s", hostport, uri), |
246 | Assets: assetList, | 253 | Assets: assetList, |
247 | Sections: sections, | 254 | Sections: sections, |
255 | IsPlain: isPlain, | ||
248 | }); err != nil { | 256 | }); err != nil { |
249 | log.Println("Template error: " + err.Error()) | 257 | log.Println("Template error: " + err.Error()) |
250 | } | 258 | } |