aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFeuerfuchs <git@feuerfuchs.dev>2020-05-18 16:12:43 +0200
committerFeuerfuchs <git@feuerfuchs.dev>2020-05-18 16:12:43 +0200
commitac770231436f8a17d348a6a0ab934429df3c57d0 (patch)
tree50e14597a6b20a464882b123275f76b906616af7
parentWIP: Refactoring (diff)
downloadgopherproxy-ac770231436f8a17d348a6a0ab934429df3c57d0.tar.gz
gopherproxy-ac770231436f8a17d348a6a0ab934429df3c57d0.tar.bz2
gopherproxy-ac770231436f8a17d348a6a0ab934429df3c57d0.zip
WIP: Refactoring
-rw-r--r--Makefile8
-rw-r--r--assets/startpage.txt6
-rw-r--r--internal/port/gemini.go176
-rw-r--r--internal/port/gopher.go138
-rw-r--r--internal/port/main.go69
-rw-r--r--internal/port/tpl/_fonts.html16
-rw-r--r--internal/port/tpl/_header.html48
-rw-r--r--internal/port/tpl/_modals.html24
-rw-r--r--internal/port/tpl/gemini.html38
-rw-r--r--internal/port/tpl/gopher.html59
-rw-r--r--internal/port/tpl/startpage.html120
-rw-r--r--pkg/libgemini/libgemini.go85
-rw-r--r--pkg/libgopher/libgopher.go2
13 files changed, 458 insertions, 331 deletions
diff --git a/Makefile b/Makefile
index 8513d69..58ac412 100644
--- a/Makefile
+++ b/Makefile
@@ -8,10 +8,10 @@ dev: build
8build: clean 8build: clean
9 sassc -t compressed css/main.scss assets/style.css 9 sassc -t compressed css/main.scss assets/style.css
10 tsc --strict --module none --outFile /dev/stdout js/* | terser --compress --mangle -o assets/main.js -- 10 tsc --strict --module none --outFile /dev/stdout js/* | terser --compress --mangle -o assets/main.js --
11 pyftsubset fonts/iosevka-term-ss03-regular.ttf "*" --name-IDs+=0,4,6 --flavor='woff' --with-zopfli --output-file='assets/iosevka-term-ss03-regular.woff' 11 #pyftsubset fonts/iosevka-term-ss03-regular.ttf "*" --name-IDs+=0,4,6 --flavor='woff' --with-zopfli --output-file='assets/iosevka-term-ss03-regular.woff'
12 pyftsubset fonts/iosevka-term-ss03-regular.ttf "*" --name-IDs+=0,4,6 --flavor='woff2' --output-file='assets/iosevka-term-ss03-regular.woff2' 12 #pyftsubset fonts/iosevka-term-ss03-regular.ttf "*" --name-IDs+=0,4,6 --flavor='woff2' --output-file='assets/iosevka-term-ss03-regular.woff2'
13 pyftsubset fonts/iosevka-aile-regular.ttf "*" --name-IDs+=0,4,6 --flavor='woff' --with-zopfli --output-file='assets/iosevka-aile-regular.woff' 13 #pyftsubset fonts/iosevka-aile-regular.ttf "*" --name-IDs+=0,4,6 --flavor='woff' --with-zopfli --output-file='assets/iosevka-aile-regular.woff'
14 pyftsubset fonts/iosevka-aile-regular.ttf "*" --name-IDs+=0,4,6 --flavor='woff2' --output-file='assets/iosevka-aile-regular.woff2' 14 #pyftsubset fonts/iosevka-aile-regular.ttf "*" --name-IDs+=0,4,6 --flavor='woff2' --output-file='assets/iosevka-aile-regular.woff2'
15 go build -o ./port.bin ./cmd/port 15 go build -o ./port.bin ./cmd/port
16 16
17profile: 17profile:
diff --git a/assets/startpage.txt b/assets/startpage.txt
index 46b22c1..3d1de44 100644
--- a/assets/startpage.txt
+++ b/assets/startpage.txt
@@ -1,6 +1,6 @@
1 P R O X Y 1 P R O X Y
2- - - - - - for - - - - - - 2 - - - - - - for - - - - - -
3 G O P H E R + G E M I N I 3 G O P H E R + G E M I N I
4 4
5 5
6GETTING STARTED -- 6GETTING STARTED --
diff --git a/internal/port/gemini.go b/internal/port/gemini.go
index b10da7d..0d8292c 100644
--- a/internal/port/gemini.go
+++ b/internal/port/gemini.go
@@ -1,7 +1,6 @@
1package port 1package port
2 2
3import ( 3import (
4 "bufio"
5 "bytes" 4 "bytes"
6 "fmt" 5 "fmt"
7 "html/template" 6 "html/template"
@@ -10,7 +9,6 @@ import (
10 "mime" 9 "mime"
11 "net/http" 10 "net/http"
12 "net/url" 11 "net/url"
13 "regexp"
14 "strings" 12 "strings"
15 13
16 "golang.org/x/net/html/charset" 14 "golang.org/x/net/html/charset"
@@ -21,129 +19,83 @@ import (
21 "github.com/temoto/robotstxt" 19 "github.com/temoto/robotstxt"
22) 20)
23 21
24type SectionType byte 22type GeminiTemplateVariables struct {
25
26const (
27 RAW_TEXT = SectionType(0)
28 REFLOW_TEXT = SectionType(1)
29 LINK = SectionType(2)
30)
31
32type Section struct {
33 Type SectionType
34 Text string
35 URL template.URL
36}
37
38type templateVariables struct {
39 Title string 23 Title string
40 URI string 24 URL string
41 Assets AssetList 25 Assets AssetList
42 Sections []Section 26 Sections []GeminiSection
27 Nav []GeminiNavItem
43} 28}
44 29
45var ( 30type GeminiNavItem struct {
46 TermEscapeSGRPattern = regexp.MustCompile("\\[\\d+(;\\d+)*m") 31 Label string
47) 32 URL string
33}
48 34
49func resolveURI(uri string, baseURL *url.URL) (resolvedURI string) { 35type GeminiSection struct {
36 Type libgemini.GeminiDocSectionType
37 Text string
38 URL template.URL
39 Items []string
40}
41
42func resolveURL(uri string, baseURL *url.URL) (resolvedURL string) {
50 if strings.HasPrefix(uri, "//") { 43 if strings.HasPrefix(uri, "//") {
51 resolvedURI = "/gemini/" + strings.TrimPrefix(uri, "//") 44 resolvedURL = "/gemini/" + strings.TrimPrefix(uri, "//")
52 } else if strings.HasPrefix(uri, "gemini://") { 45 } else if strings.HasPrefix(uri, "gemini://") {
53 resolvedURI = "/gemini/" + strings.TrimPrefix(uri, "gemini://") 46 resolvedURL = "/gemini/" + strings.TrimPrefix(uri, "gemini://")
54 } else if strings.HasPrefix(uri, "gopher://") { 47 } else if strings.HasPrefix(uri, "gopher://") {
55 resolvedURI = "/gopher/" + strings.TrimPrefix(uri, "gopher://") 48 resolvedURL = "/gopher/" + strings.TrimPrefix(uri, "gopher://")
56 } else { 49 } else {
57 url, err := url.Parse(uri) 50 url, err := url.Parse(uri)
58 if err != nil { 51 if err != nil {
59 return "" 52 return ""
60 } 53 }
61 adjustedURI := baseURL.ResolveReference(url) 54 adjustedURL := baseURL.ResolveReference(url)
62 path := adjustedURI.Path 55 path := adjustedURL.Path
63 if !strings.HasPrefix(path, "/") { 56 if !strings.HasPrefix(path, "/") {
64 path = "/" + path 57 path = "/" + path
65 } 58 }
66 if adjustedURI.Scheme == "gemini" { 59 if adjustedURL.Scheme == "gemini" {
67 resolvedURI = "/gemini/" + adjustedURI.Host + path 60 resolvedURL = "/gemini/" + adjustedURL.Host + path
68 } else if adjustedURI.Scheme == "gopher" { 61 } else if adjustedURL.Scheme == "gopher" {
69 resolvedURI = "/gopher/" + adjustedURI.Host + path 62 resolvedURL = "/gopher/" + adjustedURL.Host + path
70 } else { 63 } else {
71 resolvedURI = adjustedURI.String() 64 resolvedURL = adjustedURL.String()
72 } 65 }
73 } 66 }
74 67
75 return 68 return
76} 69}
77 70
78func parseGeminiDocument(body *bytes.Buffer, uri string, hostport string) (sections []Section) { 71func parseGeminiDocument(body *bytes.Buffer, uri string, hostport string) (sections []GeminiSection) {
79 baseURL, err := url.Parse(fmt.Sprintf( 72 baseURL, err := url.Parse(fmt.Sprintf(
80 "gemini://%s/%s", 73 "gemini://%s/%s",
81 hostport, 74 hostport,
82 uri, 75 uri,
83 )) 76 ))
84 if err != nil { 77 if err != nil {
85 return []Section{} 78 return
86 }
87
88 skipSection := true
89
90 section := Section{
91 Type: RAW_TEXT,
92 } 79 }
93 80
94 scanner := bufio.NewScanner(body) 81 unpreppedSections := libgemini.ParseGeminiDocument(body)
95 82
96 for scanner.Scan() { 83 for _, section := range unpreppedSections {
97 line := strings.Trim(scanner.Text(), "\r\n") 84 if section.Type != libgemini.LINK {
98 line = TermEscapeSGRPattern.ReplaceAllString(line, "") 85 sections = append(sections, GeminiSection{
99 86 Type: section.Type,
100 linkMatch := libgemini.LinkPattern.FindStringSubmatch(line) 87 Text: section.Text,
101 if len(linkMatch) != 0 && linkMatch[0] != "" { 88 URL: template.URL(section.URL),
102 curType := section.Type 89 Items: section.Items,
103
104 if !skipSection {
105 sections = append(sections, section)
106 }
107
108 label := linkMatch[2]
109 if label == "" {
110 label = linkMatch[1]
111 }
112
113 sections = append(sections, Section{
114 Type: LINK,
115 Text: label,
116 URL: template.URL(resolveURI(linkMatch[1], baseURL)),
117 }) 90 })
118
119 skipSection = false
120 section = Section{
121 Type: curType,
122 }
123 } else {
124 reflowModeMatch := libgemini.ReflowModePattern.FindStringSubmatch(line)
125 if len(reflowModeMatch) != 0 {
126 newType := RAW_TEXT
127 if section.Type == RAW_TEXT {
128 newType = REFLOW_TEXT
129 }
130
131 if !skipSection {
132 sections = append(sections, section)
133 }
134
135 skipSection = false
136 section = Section{
137 Type: newType,
138 }
139