Merge pull request 'Adds option to handle preformatted code blocks in different ways for gemini' (#148) from gemini-alt-text into release-2.3.0

This commit is contained in:
Sloom Sloum Sluom IV 2020-05-22 17:49:58 -04:00
commit ba38b78ca6
5 changed files with 32 additions and 9 deletions

View File

@ -233,6 +233,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. 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 .TP
.B .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. Unlike other settings, a change to this value will require a fresh page load to see the change.
.TP
.B
homeurl 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. 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 .TP

View File

@ -470,6 +470,8 @@ func (c *client) doCommandAs(action string, values []string) {
c.Options[values[0]] = lowerCaseOpt(values[0], val) c.Options[values[0]] = lowerCaseOpt(values[0], val)
if values[0] == "tlskey" || values[0] == "tlscertificate" { if values[0] == "tlskey" || values[0] == "tlscertificate" {
c.Certs.LoadCertificate(c.Options["tlscertificate"], c.Options["tlskey"]) 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" { } else if values[0] == "configlocation" {
c.SetMessage("Cannot set READ ONLY setting 'configlocation'", true) c.SetMessage("Cannot set READ ONLY setting 'configlocation'", true)
c.DrawMessage() c.DrawMessage()

View File

@ -47,6 +47,7 @@ var defaultOptions = map[string]string{
"configlocation": xdgConfigPath(), "configlocation": xdgConfigPath(),
"defaultscheme": "gopher", // "gopher", "gemini", "http", "https" "defaultscheme": "gopher", // "gopher", "gemini", "http", "https"
"geminiblocks": "block", // "block", "alt", "neither", "both"
"homeurl": "gopher://bombadillo.colorfield.space:70/1/user-guide.map", "homeurl": "gopher://bombadillo.colorfield.space:70/1/user-guide.map",
"savelocation": homePath(), "savelocation": homePath(),
"searchengine": "gopher://gopher.floodgap.com:70/7/v2/vs", "searchengine": "gopher://gopher.floodgap.com:70/7/v2/vs",

View File

@ -24,6 +24,8 @@ type TofuDigest struct {
ClientCert tls.Certificate ClientCert tls.Certificate
} }
var BlockBehavior = "block"
//------------------------------------------------\\ //------------------------------------------------\\
// + + + R E C E I V E R S + + + \\ // + + + R E C E I V E R S + + + \\
//--------------------------------------------------\\ //--------------------------------------------------\\
@ -367,16 +369,23 @@ func parseGemini(b, rootUrl, currentUrl string) (string, []string) {
splitContent := strings.Split(b, "\n") splitContent := strings.Split(b, "\n")
links := make([]string, 0, 10) links := make([]string, 0, 10)
inPreBlock := false
outputIndex := 0 outputIndex := 0
for i, ln := range splitContent { for i, ln := range splitContent {
splitContent[i] = strings.Trim(ln, "\r\n") splitContent[i] = strings.Trim(ln, "\r\n")
if ln == "```" { isPreBlockDeclaration := strings.HasPrefix(ln, "```")
// By continuing we create a variance between i and outputIndex if isPreBlockDeclaration && !inPreBlock && (BlockBehavior == "both" || BlockBehavior == "alt") {
// the other branches here will write to the outputIndex rather inPreBlock = !inPreBlock
// than i, thus removing these lines while itterating without alt := strings.TrimSpace(ln)
// needing mroe allocations. if len(alt) > 3 {
continue alt = strings.TrimSpace(alt[3:])
} else if len([]rune(ln)) > 3 && ln[:2] == "=>" { 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 var link, decorator string
subLn := strings.Trim(ln[2:], "\r\n\t \a") subLn := strings.Trim(ln[2:], "\r\n\t \a")
splitPoint := strings.IndexAny(subLn, " \t") splitPoint := strings.IndexAny(subLn, " \t")
@ -398,6 +407,9 @@ func parseGemini(b, rootUrl, currentUrl string) (string, []string) {
splitContent[outputIndex] = fmt.Sprintf("%-5s %s", linknum, decorator) splitContent[outputIndex] = fmt.Sprintf("%-5s %s", linknum, decorator)
outputIndex++ outputIndex++
} else { } else {
if inPreBlock && (BlockBehavior == "alt" || BlockBehavior == "neither") {
continue
}
splitContent[outputIndex] = ln splitContent[outputIndex] = ln
outputIndex++ outputIndex++
} }

View File

@ -32,7 +32,7 @@ import (
"tildegit.org/sloum/bombadillo/config" "tildegit.org/sloum/bombadillo/config"
"tildegit.org/sloum/bombadillo/cui" "tildegit.org/sloum/bombadillo/cui"
_ "tildegit.org/sloum/bombadillo/gemini" "tildegit.org/sloum/bombadillo/gemini"
) )
var version string var version string
@ -68,6 +68,7 @@ func validateOpt(opt, val string) bool {
"theme": []string{"normal", "inverse", "color"}, "theme": []string{"normal", "inverse", "color"},
"defaultscheme": []string{"gopher", "gemini", "http", "https"}, "defaultscheme": []string{"gopher", "gemini", "http", "https"},
"showimages": []string{"true", "false"}, "showimages": []string{"true", "false"},
"geminiblocks": []string{"block", "neither", "alt", "both"},
} }
opt = strings.ToLower(opt) opt = strings.ToLower(opt)
@ -86,7 +87,7 @@ func validateOpt(opt, val string) bool {
func lowerCaseOpt(opt, val string) string { func lowerCaseOpt(opt, val string) string {
switch opt { switch opt {
case "webmode", "theme", "defaultscheme", "showimages": case "webmode", "theme", "defaultscheme", "showimages", "geminiblocks":
return strings.ToLower(val) return strings.ToLower(val)
default: default:
return val return val
@ -123,6 +124,9 @@ func loadConfig() {
if _, ok := bombadillo.Options[lowerkey]; ok { if _, ok := bombadillo.Options[lowerkey]; ok {
if validateOpt(lowerkey, v.Value) { if validateOpt(lowerkey, v.Value) {
bombadillo.Options[lowerkey] = v.Value bombadillo.Options[lowerkey] = v.Value
if lowerkey == "geminiblocks" {
gemini.BlockBehavior = v.Value
}
} else { } else {
bombadillo.Options[lowerkey] = defaultOptions[lowerkey] bombadillo.Options[lowerkey] = defaultOptions[lowerkey]
} }