From b23b9b31219ca6e689ccd064dbec975203f93309 Mon Sep 17 00:00:00 2001 From: sloum Date: Sun, 17 May 2020 21:36:49 -0700 Subject: [PATCH 1/3] Fixes the workflow for allowing alt text and handling preformatted blocks --- client.go | 4 +++- defaults.go | 1 + gemini/gemini.go | 26 +++++++++++++++++++------- main.go | 7 ++++++- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/client.go b/client.go index 5322c08..6ed3a95 100644 --- a/client.go +++ b/client.go @@ -459,7 +459,7 @@ func (c *client) doCommandAs(action string, values []string) { } case "SEARCH": c.search(strings.Join(values, " "), "", "") - case "SET", "S": + case "SET", "S": // TODO make the geminiblocks value work if _, ok := c.Options[values[0]]; ok { val := strings.Join(values[1:], " ") if !validateOpt(values[0], val) { @@ -470,6 +470,8 @@ func (c *client) doCommandAs(action string, values []string) { c.Options[values[0]] = lowerCaseOpt(values[0], val) if values[0] == "tlskey" || values[0] == "tlscertificate" { c.Certs.LoadCertificate(c.Options["tlscertificate"], c.Options["tlskey"]) + } else if values[0] == "geminiblocks" { + gemini.BlockBehavior = c.Options[values[0]] } else if values[0] == "configlocation" { c.SetMessage("Cannot set READ ONLY setting 'configlocation'", true) c.DrawMessage() diff --git a/defaults.go b/defaults.go index c4ca227..fc47a64 100644 --- a/defaults.go +++ b/defaults.go @@ -47,6 +47,7 @@ var defaultOptions = map[string]string{ "configlocation": xdgConfigPath(), "defaultscheme": "gopher", // "gopher", "gemini", "http", "https" + "geminiblocks": "block", // "block", "alt", "neither", "both" "homeurl": "gopher://bombadillo.colorfield.space:70/1/user-guide.map", "savelocation": homePath(), "searchengine": "gopher://gopher.floodgap.com:70/7/v2/vs", diff --git a/gemini/gemini.go b/gemini/gemini.go index e13cb59..05bc7c3 100644 --- a/gemini/gemini.go +++ b/gemini/gemini.go @@ -24,6 +24,8 @@ type TofuDigest struct { ClientCert tls.Certificate } +var BlockBehavior = "block" + //------------------------------------------------\\ // + + + R E C E I V E R S + + + \\ //--------------------------------------------------\\ @@ -339,16 +341,23 @@ func parseGemini(b, rootUrl, currentUrl string) (string, []string) { splitContent := strings.Split(b, "\n") links := make([]string, 0, 10) + inPreBlock := false + outputIndex := 0 for i, ln := range splitContent { splitContent[i] = strings.Trim(ln, "\r\n") - if ln == "```" { - // By continuing we create a variance between i and outputIndex - // the other branches here will write to the outputIndex rather - // than i, thus removing these lines while itterating without - // needing mroe allocations. - continue - } else if len([]rune(ln)) > 3 && ln[:2] == "=>" { + isPreBlockDeclaration := strings.HasPrefix(ln, "```") + if isPreBlockDeclaration && !inPreBlock && (BlockBehavior == "both" || BlockBehavior == "alt") { + inPreBlock = !inPreBlock + alt := strings.TrimSpace(ln) + if len(alt) > 3 { + alt = strings.TrimSpace(alt[3:]) + splitContent[outputIndex] = fmt.Sprintf("[ %s ]", alt) + outputIndex++ + } + } else if isPreBlockDeclaration { + inPreBlock = !inPreBlock + } else if len([]rune(ln)) > 3 && ln[:2] == "=>" && !inPreBlock { var link, decorator string subLn := strings.Trim(ln[2:], "\r\n\t \a") splitPoint := strings.IndexAny(subLn, " \t") @@ -370,6 +379,9 @@ func parseGemini(b, rootUrl, currentUrl string) (string, []string) { splitContent[outputIndex] = fmt.Sprintf("%-5s %s", linknum, decorator) outputIndex++ } else { + if inPreBlock && (BlockBehavior == "alt" || BlockBehavior == "neither") { + continue + } splitContent[outputIndex] = ln outputIndex++ } diff --git a/main.go b/main.go index 53918f7..92f4d13 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,7 @@ import ( "tildegit.org/sloum/bombadillo/config" "tildegit.org/sloum/bombadillo/cui" + "tildegit.org/sloum/bombadillo/gemini" _ "tildegit.org/sloum/bombadillo/gemini" ) @@ -66,6 +67,7 @@ func validateOpt(opt, val string) bool { "theme": []string{"normal", "inverse", "color"}, "defaultscheme": []string{"gopher", "gemini", "http", "https"}, "showimages": []string{"true", "false"}, + "geminiblocks": []string{"block", "neither", "alt", "both"}, } opt = strings.ToLower(opt) @@ -84,7 +86,7 @@ func validateOpt(opt, val string) bool { func lowerCaseOpt(opt, val string) string { switch opt { - case "webmode", "theme", "defaultscheme", "showimages": + case "webmode", "theme", "defaultscheme", "showimages", "geminiblocks": return strings.ToLower(val) default: return val @@ -121,6 +123,9 @@ func loadConfig() { if _, ok := bombadillo.Options[lowerkey]; ok { if validateOpt(lowerkey, v.Value) { bombadillo.Options[lowerkey] = v.Value + if lowerkey == "geminiblocks" { + gemini.BlockBehavior = v.Value + } } else { bombadillo.Options[lowerkey] = defaultOptions[lowerkey] } From 9f1eb632bc375de9f1ec86137179f8b460db2461 Mon Sep 17 00:00:00 2001 From: sloum Date: Sun, 17 May 2020 22:01:31 -0700 Subject: [PATCH 2/3] Adds geminiblocks to the man page --- bombadillo.1 | 4 ++++ main.go | 1 - 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/bombadillo.1 b/bombadillo.1 index ef3e479..42f40df 100644 --- a/bombadillo.1 +++ b/bombadillo.1 @@ -232,6 +232,10 @@ defaultscheme The scheme that should be used when no scheme is present in a given URL. \fIgopher\fP, \fIgemini\fP, \fIhttp\fP, and \fIhttps\fP are valid values. .TP .B +geminiblocks +Determines how to treat preformatted text blocks in text/gemini documents. \fIblock\fP will show the contents of the block, \fIalt\fP will show any available alt text for the block, \fIboth\fP will show both the content and the alt text, and \fIneither\fP will show neither. +.TP +.B homeurl The url that \fBbombadillo\fP navigates to when the program loads or when the \fIhome\fP or \fIh\fP LINE COMMAND is issued. This should be a valid url. If a scheme/protocol is not included, gopher will be assumed. .TP diff --git a/main.go b/main.go index 92f4d13..7216d0c 100644 --- a/main.go +++ b/main.go @@ -31,7 +31,6 @@ import ( "tildegit.org/sloum/bombadillo/config" "tildegit.org/sloum/bombadillo/cui" "tildegit.org/sloum/bombadillo/gemini" - _ "tildegit.org/sloum/bombadillo/gemini" ) var version string From 0257ca92b1e47e5028e2a55990398efd2907eb7a Mon Sep 17 00:00:00 2001 From: sloum Date: Tue, 19 May 2020 14:40:56 -0700 Subject: [PATCH 3/3] Adds clarification to man page and removes old comment --- bombadillo.1 | 2 +- client.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bombadillo.1 b/bombadillo.1 index 42f40df..bd5367e 100644 --- a/bombadillo.1 +++ b/bombadillo.1 @@ -233,7 +233,7 @@ The scheme that should be used when no scheme is present in a given URL. \fIgoph .TP .B geminiblocks -Determines how to treat preformatted text blocks in text/gemini documents. \fIblock\fP will show the contents of the block, \fIalt\fP will show any available alt text for the block, \fIboth\fP will show both the content and the alt text, and \fIneither\fP will show neither. +Determines how to treat preformatted text blocks in text/gemini documents. \fIblock\fP will show the contents of the block, \fIalt\fP will show any available alt text for the block, \fIboth\fP will show both the content and the alt text, and \fIneither\fP will show neither. Unlike other settings, a change to this value will require a fresh page load to see the change. .TP .B homeurl diff --git a/client.go b/client.go index 6ed3a95..cfd2e41 100644 --- a/client.go +++ b/client.go @@ -459,7 +459,7 @@ func (c *client) doCommandAs(action string, values []string) { } case "SEARCH": c.search(strings.Join(values, " "), "", "") - case "SET", "S": // TODO make the geminiblocks value work + case "SET", "S": if _, ok := c.Options[values[0]]; ok { val := strings.Join(values[1:], " ") if !validateOpt(values[0], val) {