aboutsummaryrefslogtreecommitdiffstats
path: root/internal
diff options
context:
space:
mode:
authorFeuerfuchs <git@feuerfuchs.dev>2020-05-18 18:51:19 +0200
committerFeuerfuchs <git@feuerfuchs.dev>2020-05-18 18:51:19 +0200
commitc772716e98e11181d301deadeca52856ac18f085 (patch)
tree25039bd7739039c000ab748b1547b5e5514d0b7a /internal
parentWIP: Refactoring (diff)
downloadgopherproxy-c772716e98e11181d301deadeca52856ac18f085.tar.gz
gopherproxy-c772716e98e11181d301deadeca52856ac18f085.tar.bz2
gopherproxy-c772716e98e11181d301deadeca52856ac18f085.zip
Improved Gemini support
Diffstat (limited to 'internal')
-rw-r--r--internal/port/gemini.go36
-rw-r--r--internal/port/gopher.go16
-rw-r--r--internal/port/main.go6
-rw-r--r--internal/port/tpl/gemini.html11
-rw-r--r--internal/port/tpl/gopher.html2
-rw-r--r--internal/port/tpl/startpage.html2
6 files changed, 60 insertions, 13 deletions
diff --git a/internal/port/gemini.go b/internal/port/gemini.go
index 740fccd..f67ca05 100644
--- a/internal/port/gemini.go
+++ b/internal/port/gemini.go
@@ -29,8 +29,9 @@ type GeminiTemplateVariables struct {
29} 29}
30 30
31type GeminiNavItem struct { 31type GeminiNavItem struct {
32 Label string 32 Label string
33 URL string 33 URL string
34 Current bool
34} 35}
35 36
36type GeminiSection struct { 37type GeminiSection struct {
@@ -40,6 +41,32 @@ type GeminiSection struct {
40 Items []string 41 Items []string
41} 42}
42 43
44func urlToGeminiNav(url string) (items []GeminiNavItem) {
45 partialURL := "/gemini"
46 parts := strings.Split(url, "/")
47
48 if len(parts) != 0 && parts[len(parts)-1] == "" {
49 parts = parts[:len(parts)-1]
50 }
51
52 for i, part := range parts {
53 partialURL = partialURL + "/" + part
54
55 current := false
56 if i == len(parts)-1 || (len(parts) == 2 && i == 0) {
57 current = true
58 }
59
60 items = append(items, GeminiNavItem{
61 Label: part,
62 URL: partialURL,
63 Current: current,
64 })
65 }
66
67 return
68}
69
43func resolveURL(uri string, baseURL *url.URL) (resolvedURL string) { 70func resolveURL(uri string, baseURL *url.URL) (resolvedURL string) {
44 if strings.HasPrefix(uri, "//") { 71 if strings.HasPrefix(uri, "//") {
45 resolvedURL = "/gemini/" + strings.TrimPrefix(uri, "//") 72 resolvedURL = "/gemini/" + strings.TrimPrefix(uri, "//")
@@ -137,6 +164,7 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass
137 Type: libgemini.RAW_TEXT.String(), 164 Type: libgemini.RAW_TEXT.String(),
138 Text: fmt.Sprintf("Error: %s", err), 165 Text: fmt.Sprintf("Error: %s", err),
139 }}, 166 }},
167 Nav: urlToGeminiNav(hostport),
140 IsPlain: true, 168 IsPlain: true,
141 }); e != nil { 169 }); e != nil {
142 log.Println("Template error: " + e.Error()) 170 log.Println("Template error: " + e.Error())
@@ -167,6 +195,7 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass
167 Type: libgemini.RAW_TEXT.String(), 195 Type: libgemini.RAW_TEXT.String(),
168 Text: fmt.Sprintf("Error: %s", err), 196 Text: fmt.Sprintf("Error: %s", err),
169 }}, 197 }},
198 Nav: urlToGeminiNav(fmt.Sprintf("%s/%s", hostport, uri)),
170 IsPlain: true, 199 IsPlain: true,
171 }); e != nil { 200 }); e != nil {
172 log.Println("Template error: " + e.Error()) 201 log.Println("Template error: " + e.Error())
@@ -190,6 +219,7 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass
190 Type: libgemini.RAW_TEXT.String(), 219 Type: libgemini.RAW_TEXT.String(),
191 Text: fmt.Sprintf("Error: %s", err), 220 Text: fmt.Sprintf("Error: %s", err),
192 }}, 221 }},
222 Nav: urlToGeminiNav(fmt.Sprintf("%s/%s", hostport, uri)),
193 IsPlain: true, 223 IsPlain: true,
194 }); e != nil { 224 }); e != nil {
195 log.Println("Template error: " + e.Error()) 225 log.Println("Template error: " + e.Error())
@@ -211,6 +241,7 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass
211 Type: libgemini.RAW_TEXT.String(), 241 Type: libgemini.RAW_TEXT.String(),
212 Text: fmt.Sprintf("Error %d: %s", res.Header.Status, res.Header.Meta), 242 Text: fmt.Sprintf("Error %d: %s", res.Header.Status, res.Header.Meta),
213 }}, 243 }},
244 Nav: urlToGeminiNav(fmt.Sprintf("%s/%s", hostport, uri)),
214 IsPlain: true, 245 IsPlain: true,
215 }); err != nil { 246 }); err != nil {
216 log.Println("Template error: " + err.Error()) 247 log.Println("Template error: " + err.Error())
@@ -252,6 +283,7 @@ func GeminiHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass
252 URL: fmt.Sprintf("%s/%s", hostport, uri), 283 URL: fmt.Sprintf("%s/%s", hostport, uri),
253 Assets: assetList, 284 Assets: assetList,
254 Sections: sections, 285 Sections: sections,
286 Nav: urlToGeminiNav(fmt.Sprintf("%s/%s", hostport, uri)),
255 IsPlain: isPlain, 287 IsPlain: isPlain,
256 }); err != nil { 288 }); err != nil {
257 log.Println("Template error: " + err.Error()) 289 log.Println("Template error: " + err.Error())
diff --git a/internal/port/gopher.go b/internal/port/gopher.go
index d2283c6..cb5e60c 100644
--- a/internal/port/gopher.go
+++ b/internal/port/gopher.go
@@ -49,10 +49,14 @@ func trimLeftChars(s string, n int) string {
49 return s[:0] 49 return s[:0]
50} 50}
51 51
52func urlToNav(url string) (items []GopherNavItem) { 52func urlToGopherNav(url string) (items []GopherNavItem) {
53 partialURL := "/gopher" 53 partialURL := "/gopher"
54 parts := strings.Split(url, "/") 54 parts := strings.Split(url, "/")
55 55
56 if len(parts) != 0 && parts[len(parts)-1] == "" {
57 parts = parts[:len(parts)-1]
58 }
59
56 for i, part := range parts { 60 for i, part := range parts {
57 if i == 1 { 61 if i == 1 {
58 partialURL = partialURL + "/1" 62 partialURL = partialURL + "/1"
@@ -151,7 +155,7 @@ func renderGopherDirectory(w http.ResponseWriter, tpl *template.Template, assetL
151 URL: fmt.Sprintf("%s/%s", hostport, uri), 155 URL: fmt.Sprintf("%s/%s", hostport, uri),
152 Assets: assetList, 156 Assets: assetList,
153 Lines: out, 157 Lines: out,
154 Nav: urlToNav(fmt.Sprintf("%s/%s", hostport, uri)), 158 Nav: urlToGopherNav(fmt.Sprintf("%s/%s", hostport, uri)),
155 }) 159 })
156} 160}
157 161
@@ -194,7 +198,7 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass
194 Lines: []GopherItem{{ 198 Lines: []GopherItem{{
195 Text: fmt.Sprintf("Error: %s", err), 199 Text: fmt.Sprintf("Error: %s", err),
196 }}, 200 }},
197 Nav: urlToNav(hostport), 201 Nav: urlToGopherNav(hostport),
198 IsPlain: true, 202 IsPlain: true,
199 }); e != nil { 203 }); e != nil {
200 log.Println("Template error: " + e.Error()) 204 log.Println("Template error: " + e.Error())
@@ -224,7 +228,7 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass
224 Lines: []GopherItem{{ 228 Lines: []GopherItem{{
225 Text: fmt.Sprintf("Error: %s", err), 229 Text: fmt.Sprintf("Error: %s", err),
226 }}, 230 }},
227 Nav: urlToNav(fmt.Sprintf("%s/%s", hostport, uri)), 231 Nav: urlToGopherNav(fmt.Sprintf("%s/%s", hostport, uri)),
228 IsPlain: true, 232 IsPlain: true,
229 }); e != nil { 233 }); e != nil {
230 log.Println("Template error: " + e.Error()) 234 log.Println("Template error: " + e.Error())
@@ -246,7 +250,7 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass
246 Lines: []GopherItem{{ 250 Lines: []GopherItem{{
247 Text: buf.String(), 251 Text: buf.String(),
248 }}, 252 }},
249 Nav: urlToNav(fmt.Sprintf("%s/%s", hostport, uri)), 253 Nav: urlToGopherNav(fmt.Sprintf("%s/%s", hostport, uri)),
250 IsPlain: true, 254 IsPlain: true,
251 }); err != nil { 255 }); err != nil {
252 log.Println("Template error: " + err.Error()) 256 log.Println("Template error: " + err.Error())
@@ -271,7 +275,7 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass
271 Lines: []GopherItem{{ 275 Lines: []GopherItem{{
272 Text: fmt.Sprintf("Error: %s", err), 276 Text: fmt.Sprintf("Error: %s", err),
273 }}, 277 }},
274 Nav: urlToNav(fmt.Sprintf("%s/%s", hostport, uri)), 278 Nav: urlToGopherNav(fmt.Sprintf("%s/%s", hostport, uri)),
275 IsPlain: false, 279 IsPlain: false,
276 }); e != nil { 280 }); e != nil {
277 log.Println("Template error: " + e.Error()) 281 log.Println("Template error: " + e.Error())
diff --git a/internal/port/main.go b/internal/port/main.go
index 763057b..fe85ba4 100644
--- a/internal/port/main.go
+++ b/internal/port/main.go
@@ -237,9 +237,15 @@ func ListenAndServe(bind, startpagefile string, robotsfile string, robotsdebug b
237 "hasPrefix": func(s string, prefix string) bool { 237 "hasPrefix": func(s string, prefix string) bool {
238 return strings.HasPrefix(s, prefix) 238 return strings.HasPrefix(s, prefix)
239 }, 239 },
240 "hasSuffix": func(s string, suffix string) bool {
241 return strings.HasSuffix(s, suffix)
242 },
240 "title": func(s string) string { 243 "title": func(s string) string {
241 return strings.Title(s) 244 return strings.Title(s)
242 }, 245 },
246 "string": func(s interface{}) string {
247 return fmt.Sprint(s)
248 },
243 } 249 }
244 250
245 // 251 //
diff --git a/internal/port/tpl/gemini.html b/internal/port/tpl/gemini.html
index df50d50..8d20da1 100644
--- a/internal/port/tpl/gemini.html
+++ b/internal/port/tpl/gemini.html
@@ -35,11 +35,16 @@
35 {{- else if eq .Type "REFLOW_TEXT" -}} 35 {{- else if eq .Type "REFLOW_TEXT" -}}
36 <div class="section"><p class="section__content">{{- .Text -}}</p></div> 36 <div class="section"><p class="section__content">{{- .Text -}}</p></div>
37 {{- else if eq .Type "LINK" -}} 37 {{- else if eq .Type "LINK" -}}
38 <div class="section"><span class="section__type">=></span><a class="section__content" href="{{ .URL }}">{{- .Text -}}</a></div> 38 {{- $linkCls := "link" -}}
39 {{- $url := string .URL -}}
40 {{- if or (hasSuffix $url ".jpg") (hasSuffix $url ".jpeg") (hasSuffix $url ".png") (hasSuffix $url ".gif") -}}
41 {{- $linkCls = "link--IMG" -}}
42 {{- end -}}
43 <div class="section"><span class="section__type"> =></span><a class="section__content {{ $linkCls }}" href="{{ .URL }}">{{- .Text -}}</a></div>
39 {{- else if eq .Type "HEADING_1" -}} 44 {{- else if eq .Type "HEADING_1" -}}
40 <div class="section"><span class="section__type">#</span><h1 class="section__content">{{- .Text -}}</h1></div> 45 <div class="section"><span class="section__type"> #</span><h1 class="section__content">{{- .Text -}}</h1></div>
41 {{- else if eq .Type "HEADING_2" -}} 46 {{- else if eq .Type "HEADING_2" -}}
42 <div class="section"><span class="section__type">##</span><h2 class="section__content">{{- .Text -}}</h2></div> 47 <div class="section"><span class="section__type"> ##</span><h2 class="section__content">{{- .Text -}}</h2></div>
43 {{- else if eq .Type "HEADING_3" -}} 48 {{- else if eq .Type "HEADING_3" -}}
44 <div class="section"><span class="section__type">###</span><h3 class="section__content">{{- .Text -}}</h3></div> 49 <div class="section"><span class="section__type">###</span><h3 class="section__content">{{- .Text -}}</h3></div>
45 {{- else if eq .Type "LIST" -}} 50 {{- else if eq .Type "LIST" -}}
diff --git a/internal/port/tpl/gopher.html b/internal/port/tpl/gopher.html
index 5436123..6eb607c 100644
--- a/internal/port/tpl/gopher.html
+++ b/internal/port/tpl/gopher.html
@@ -35,7 +35,7 @@
35 {{- $page := . -}} 35 {{- $page := . -}}
36 {{- range .Lines -}} 36 {{- range .Lines -}}
37 {{- if .Link -}} 37 {{- if .Link -}}
38 <div class="section"><span class="section__type">{{- .Type -}}</span><a class="section__content" href="{{ .Link }}">{{- .Text -}}</a></div> 38 <div class="section"><span class="section__type">{{- .Type -}}</span><a class="section__content link link--{{ .Type }}" href="{{ .Link }}">{{- .Text -}}</a></div>
39 {{- else -}} 39 {{- else -}}
40 <div class="section"><span class="section__type"></span><pre class="section__content">{{- .Text -}}</pre></div> 40 <div class="section"><span class="section__type"></span><pre class="section__content">{{- .Text -}}</pre></div>
41 {{- end -}} 41 {{- end -}}
diff --git a/internal/port/tpl/startpage.html b/internal/port/tpl/startpage.html
index cfe519d..772ac90 100644
--- a/internal/port/tpl/startpage.html
+++ b/internal/port/tpl/startpage.html
@@ -18,7 +18,7 @@
18 </header> 18 </header>
19 19
20 <main class="wrap"> 20 <main class="wrap">
21 <pre class="content content--has-monospace-font"> 21 <pre class="content content--monospace">
22 {{- .Content -}} 22 {{- .Content -}}
23 </pre> 23 </pre>
24 </main> 24 </main>