diff --git a/display/bookmarks.go b/display/bookmarks.go index 106d864..76a857f 100644 --- a/display/bookmarks.go +++ b/display/bookmarks.go @@ -106,11 +106,12 @@ func Bookmarks() { // Render and display content, links := renderer.RenderGemini(rawContent, textWidth(), leftMargin()) page := structs.Page{ - Raw: rawContent, - Content: content, - Links: links, - Url: "about:bookmarks", - Width: termW, + Raw: rawContent, + Content: content, + Links: links, + Url: "about:bookmarks", + Width: termW, + Mediatype: structs.TextGemini, } setPage(&page) } diff --git a/display/display.go b/display/display.go index 1bf75e7..5a67c22 100644 --- a/display/display.go +++ b/display/display.go @@ -203,11 +203,12 @@ func Init() { // Render the default new tab content ONCE and store it for later renderedNewTabContent, newTabLinks = renderer.RenderGemini(newTabContent, textWidth(), leftMargin()) newTabPage = &structs.Page{ - Raw: newTabContent, - Content: renderedNewTabContent, - Links: newTabLinks, - Url: "about:newtab", - Width: -1, // Force reformatting on first display + Raw: newTabContent, + Content: renderedNewTabContent, + Links: newTabLinks, + Url: "about:newtab", + Width: -1, // Force reformatting on first display + Mediatype: structs.TextGemini, } modalInit() diff --git a/display/private.go b/display/private.go index 97f8a15..34d3c0a 100644 --- a/display/private.go +++ b/display/private.go @@ -168,8 +168,17 @@ func reformatPage(p *structs.Page) { // No changes to make return } - // Links are not recorded because they won't change - rendered, _ := renderer.RenderGemini(p.Raw, textWidth(), leftMargin()) + + var rendered string + if p.Mediatype == structs.TextGemini { + // Links are not recorded because they won't change + rendered, _ = renderer.RenderGemini(p.Raw, textWidth(), leftMargin()) + } else if p.Mediatype == structs.TextPlain { + rendered = renderer.RenderPlainText(p.Raw, leftMargin()) + } else { + // Rendering this type is not implemented + return + } p.Content = rendered p.Width = termW } diff --git a/renderer/page.go b/renderer/page.go index 3df7805..cb14c3c 100644 --- a/renderer/page.go +++ b/renderer/page.go @@ -80,26 +80,20 @@ func MakePage(url string, res *gemini.Response, width, leftMargin int) (*structs if mediatype == "text/gemini" { rendered, links := RenderGemini(utfText, width, leftMargin) return &structs.Page{ - Url: url, - Raw: utfText, - Content: rendered, - Links: links, + Mediatype: structs.TextGemini, + Url: url, + Raw: utfText, + Content: rendered, + Links: links, }, nil } else if strings.HasPrefix(mediatype, "text/") { // Treated as plaintext - - // Add left margin - var shifted string - lines := strings.Split(utfText, "\n") - for i := range lines { - shifted += strings.Repeat(" ", leftMargin) + lines[i] + "\n" - } - return &structs.Page{ - Url: url, - Raw: utfText, - Content: shifted, - Links: []string{}, + Mediatype: structs.TextPlain, + Url: url, + Raw: utfText, + Content: RenderPlainText(utfText, leftMargin), + Links: []string{}, }, nil } diff --git a/renderer/renderer.go b/renderer/renderer.go index fc79fc1..08073bd 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -13,6 +13,16 @@ import ( "gitlab.com/tslocum/cview" ) +// RenderPlainText should be used to format plain text pages. +func RenderPlainText(s string, leftMargin int) string { + var shifted string + lines := strings.Split(cview.Escape(s), "\n") + for i := range lines { + shifted += strings.Repeat(" ", leftMargin) + lines[i] + "\n" + } + return shifted +} + // wrapLine wraps a line to the provided width, and adds the provided prefix and suffix to each wrapped line. // It recovers from wrapping panics and should never cause a panic. // It returns a slice of lines, without newlines at the end. diff --git a/structs/structs.go b/structs/structs.go index bbda661..adc507a 100644 --- a/structs/structs.go +++ b/structs/structs.go @@ -1,14 +1,22 @@ package structs +type Mediatype string + +const ( + TextGemini Mediatype = "text/gemini" + TextPlain Mediatype = "text/plain" +) + // Page is for storing UTF-8 text/gemini pages, as well as text/plain pages. type Page struct { - Url string - Raw string // The raw response, as received over the network - Content string // The processed content, NOT raw. Uses cview colour tags. All link/link texts must have region tags. It will also have a left margin. - Links []string // URLs, for each region in the content. - Row int // Scroll position - Column int // ditto - Width int // The width of the terminal at the time when the Content was set. This is to know when reformatting should happen. + Url string + Mediatype Mediatype + Raw string // The raw response, as received over the network + Content string // The processed content, NOT raw. Uses cview colour tags. All link/link texts must have region tags. It will also have a left margin. + Links []string // URLs, for each region in the content. + Row int // Scroll position + Column int // ditto + Width int // The width of the terminal at the time when the Content was set. This is to know when reformatting should happen. } // Size returns an approx. size of a Page in bytes.