clx-browser

[ACTIVE] a smol browser based off of circumflex
git clone git://git.figbert.com/clx-browser.git
Log | Files | Refs | README | LICENSE

commit f575493521a81eb3aef441400ddb566268483a01
parent fead57edfcb8430d05856a845b64ea60a505ee88
Author: FIGBERT <figbert@figbert.com>
Date:   Fri, 29 Jul 2022 21:15:40 -0700

Merge reader package into main

Diffstat:
Mmain.go | 7+------
Areader.go | 41+++++++++++++++++++++++++++++++++++++++++
Dreader/reader.go | 57---------------------------------------------------------
3 files changed, 42 insertions(+), 63 deletions(-)

diff --git a/main.go b/main.go @@ -5,18 +5,13 @@ import ( "os/exec" "strings" - "git.figbert.com/clx-browser/reader" - "github.com/charmbracelet/glamour" ) func main() { url := os.Args[1] - title, article, err := reader.GetNew(url) - if err != nil { - panic(err) - } + title, article := getArticle(url) header := getReaderModeMetaBlock(title, url, 72) diff --git a/reader.go b/reader.go @@ -0,0 +1,41 @@ +package main + +import ( + "net/http" + "net/url" + "time" + + md "github.com/JohannesKaufmann/html-to-markdown" + "github.com/JohannesKaufmann/html-to-markdown/plugin" + + "github.com/go-shiori/go-readability" +) + +func getArticle(URL string) (string, string) { + client := http.Client{Timeout: 5 * time.Second} + response, err := client.Get(URL) + if err != nil { + panic(err) + } + defer response.Body.Close() + + pageURL, err := url.Parse(URL) + if err != nil { + panic(err) + } + + art, err := readability.FromReader(response.Body, pageURL) + if err != nil { + panic(err) + } + + opt := &md.Options{} + converter := md.NewConverter(URL, true, opt) + converter.Use(plugin.Table()) + markdown, err := converter.ConvertString(art.Content) + if err != nil { + panic(err) + } + + return art.Title, markdown +} diff --git a/reader/reader.go b/reader/reader.go @@ -1,57 +0,0 @@ -package reader - -import ( - "fmt" - "log" - "net/http" - "net/url" - "time" - - md "github.com/JohannesKaufmann/html-to-markdown" - "github.com/JohannesKaufmann/html-to-markdown/plugin" - - "github.com/go-shiori/go-readability" -) - -func GetNew(url string) (string, string, error) { - art, httpErr := fetch(url) - if httpErr != nil { - return "", "", fmt.Errorf("could not fetch url: %w", httpErr) - } - - opt := &md.Options{} - converter := md.NewConverter(url, true, opt) - converter.Use(plugin.Table()) - - markdown, err := converter.ConvertString(art.Content) - if err != nil { - log.Fatal(err) - } - - return art.Title, markdown, nil -} - -func fetch(rawURL string) (readability.Article, error) { - client := http.Client{ - Timeout: 5 * time.Second, - } - - response, err := client.Get(rawURL) - if err != nil { - return readability.Article{}, fmt.Errorf("could not fetch rawURL: %w", err) - } - - defer response.Body.Close() - - pageURL, urlErr := url.Parse(rawURL) - if urlErr != nil { - panic(urlErr) - } - - art, readabilityErr := readability.FromReader(response.Body, pageURL) - if readabilityErr != nil { - return readability.Article{}, fmt.Errorf("could not fetch rawURL: %w", readabilityErr) - } - - return art, nil -}