1
0
mirror of https://github.com/makew0rld/amfora.git synced 2024-06-19 19:25:24 +00:00

🐛 Make link colors all purple on proxy pages - fixes #84

This commit is contained in:
makeworld 2020-09-01 16:41:30 -04:00
parent e1dc0a0ccd
commit c0c1ef898c
5 changed files with 26 additions and 14 deletions

View File

@ -115,7 +115,7 @@ func Bookmarks(t *tab) {
rawContent += fmt.Sprintf("=> %s %s\r\n", keys[i], m[keys[i]])
}
// Render and display
content, links := renderer.RenderGemini(rawContent, textWidth(), leftMargin())
content, links := renderer.RenderGemini(rawContent, textWidth(), leftMargin(), false)
page := structs.Page{
Raw: rawContent,
Content: content,

View File

@ -201,7 +201,7 @@ func Init() {
// Render the default new tab content ONCE and store it for later
newTabContent := getNewTabContent()
renderedNewTabContent, newTabLinks := renderer.RenderGemini(newTabContent, textWidth(), leftMargin())
renderedNewTabContent, newTabLinks := renderer.RenderGemini(newTabContent, textWidth(), leftMargin(), false)
newTabPage = structs.Page{
Raw: newTabContent,
Content: renderedNewTabContent,
@ -522,7 +522,7 @@ func Reload() {
// Re-render new tab, similar to Init()
newTabContent := getNewTabContent()
tmpTermW := termW
renderedNewTabContent, newTabLinks := renderer.RenderGemini(newTabContent, textWidth(), leftMargin())
renderedNewTabContent, newTabLinks := renderer.RenderGemini(newTabContent, textWidth(), leftMargin(), false)
newTabPage = structs.Page{
Raw: newTabContent,
Content: renderedNewTabContent,

View File

@ -78,7 +78,11 @@ func reformatPage(p *structs.Page) {
switch p.Mediatype {
case structs.TextGemini:
// Links are not recorded because they won't change
rendered, _ = renderer.RenderGemini(p.Raw, textWidth(), leftMargin())
proxied := true
if strings.HasPrefix(p.URL, "gemini") || strings.HasPrefix(p.URL, "about") {
proxied = false
}
rendered, _ = renderer.RenderGemini(p.Raw, textWidth(), leftMargin(), proxied)
case structs.TextPlain:
rendered = renderer.RenderPlainText(p.Raw, leftMargin())
case structs.TextAnsi:
@ -413,7 +417,7 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) {
return ret("", false)
}
if renderer.CanDisplay(res) {
page, err := renderer.MakePage(u, res, textWidth(), leftMargin())
page, err := renderer.MakePage(u, res, textWidth(), leftMargin(), usingProxy)
// Rendering may have taken a while, make sure tab is still valid
if !isValidTab(t) {
return ret("", false)

View File

@ -57,7 +57,7 @@ func CanDisplay(res *gemini.Response) bool {
// MakePage creates a formatted, rendered Page from the given network response and params.
// You must set the Page.Width value yourself.
func MakePage(url string, res *gemini.Response, width, leftMargin int) (*structs.Page, error) {
func MakePage(url string, res *gemini.Response, width, leftMargin int, proxied bool) (*structs.Page, error) {
if !CanDisplay(res) {
return nil, ErrCantDisplay
}
@ -102,7 +102,7 @@ func MakePage(url string, res *gemini.Response, width, leftMargin int) (*structs
}
if mediatype == "text/gemini" {
rendered, links := RenderGemini(utfText, width, leftMargin)
rendered, links := RenderGemini(utfText, width, leftMargin, proxied)
return &structs.Page{
Mediatype: structs.TextGemini,
URL: url,

View File

@ -87,13 +87,17 @@ func tagLines(s, start, end string) string {
// convertRegularGemini converts non-preformatted blocks of text/gemini
// into a cview-compatible format.
// Since this only works on non-preformatted blocks, RenderGemini
// should always be used instead.
//
// It also returns a slice of link URLs.
// numLinks is the number of links that exist so far.
// width is the number of columns to wrap to.
//
// Since this only works on non-preformatted blocks, RenderGemini
// should always be used instead.
func convertRegularGemini(s string, numLinks, width int) (string, []string) {
//
// proxied is whether the request is through the gemini:// scheme.
// If it's not a gemini:// page, set this to true.
func convertRegularGemini(s string, numLinks, width int, proxied bool) (string, []string) {
links := make([]string, 0)
lines := strings.Split(s, "\n")
wrappedLines := make([]string, 0) // Final result
@ -175,7 +179,8 @@ func convertRegularGemini(s string, numLinks, width int) (string, []string) {
if viper.GetBool("a-general.color") {
pU, err := urlPkg.Parse(url)
if err == nil && (pU.Scheme == "" || pU.Scheme == "gemini" || pU.Scheme == "about") {
if !proxied && err == nil &&
(pU.Scheme == "" || pU.Scheme == "gemini" || pU.Scheme == "about") {
// A gemini link
// Add the link text in blue (in a region), and a gray link number to the left of it
// Those are the default colors, anyway
@ -267,7 +272,10 @@ func convertRegularGemini(s string, numLinks, width int) (string, []string) {
//
// width is the number of columns to wrap to.
// leftMargin is the number of blank spaces to prepend to each line.
func RenderGemini(s string, width, leftMargin int) (string, []string) {
//
// proxied is whether the request is through the gemini:// scheme.
// If it's not a gemini:// page, set this to true.
func RenderGemini(s string, width, leftMargin int, proxied bool) (string, []string) {
s = cview.Escape(s)
if viper.GetBool("a-general.color") {
s = cview.TranslateANSI(s)
@ -292,7 +300,7 @@ func RenderGemini(s string, width, leftMargin int) (string, []string) {
)
} else {
// Not preformatted, regular text
ren, lks := convertRegularGemini(buf, len(links), width)
ren, lks := convertRegularGemini(buf, len(links), width, proxied)
links = append(links, lks...)
rendered += ren
}
@ -310,7 +318,7 @@ func RenderGemini(s string, width, leftMargin int) (string, []string) {
} else {
// Not preformatted, regular text
// Same code as in the loop above
ren, lks := convertRegularGemini(buf, len(links), width)
ren, lks := convertRegularGemini(buf, len(links), width, proxied)
links = append(links, lks...)
rendered += ren
}