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 01994d7a0af04f83cef32b86a2d2d2d525200ea0
parent 5745c430d10a86c7315c1ec173550bd4fe2245ea
Author: FIGBERT <figbert@figbert.com>
Date:   Wed, 27 Jul 2022 23:36:06 -0700

Remove unused code

Diffstat:
Mcli/cli.go | 23-----------------------
Dcomment/comment.go | 170-------------------------------------------------------------------------------
Dconstants/nerdfonts/nerdfonts.go | 9---------
Dconstants/style/style.go | 93-------------------------------------------------------------------------------
Ditem/item.go | 17-----------------
Mmarkdown/postprocessor/postprocessor.go | 11++++++++---
Mmeta/meta.go | 141-------------------------------------------------------------------------------
Dscreen/screen.go | 39---------------------------------------
Dsettings/core.go | 28----------------------------
Msyntax/syntax.go | 454+------------------------------------------------------------------------------
10 files changed, 9 insertions(+), 976 deletions(-)

diff --git a/cli/cli.go b/cli/cli.go @@ -26,26 +26,3 @@ func Less(input string) { panic(err) } } - -func WrapLess(input string) *exec.Cmd { - command := exec.Command("less", - "--RAW-CONTROL-CHARS", - "--pattern="+unicode.ZeroWidthSpace, - "--ignore-case", - "--tilde", - "--use-color", - "-P?e"+"\u001B[48;5;234m "+"\u001B[38;5;200m"+"E"+"\u001B[38;5;214m"+"n"+"\u001B[38;5;69m"+"d "+"\033[0m", - "-DSy", - "-DP-") - - command.Stdin = strings.NewReader(input) - command.Stdout = os.Stdout - - return command -} - -func ClearScreen() { - c := exec.Command("clear") - c.Stdout = os.Stdout - _ = c.Run() -} diff --git a/comment/comment.go b/comment/comment.go @@ -1,170 +0,0 @@ -package comment - -import ( - "strings" - - "git.figbert.com/clx-browser/settings" - "git.figbert.com/clx-browser/syntax" - - "github.com/logrusorgru/aurora/v3" - - text "github.com/MichaelMure/go-term-text" -) - -const ( - reset = "\033[0m" - dimmed = "\033[2m" - italic = "\033[3m" -) - -type comment struct { - sections []*section -} - -type section struct { - isCodeBlock bool - isQuote bool - content string -} - -func Print(c string, config *settings.Config, commentWidth int, availableScreenWidth int) string { - if c == "[deleted]" { - return aurora.Faint(c).String() - } - - c = strings.Replace(c, "<p>", "", 1) - c = strings.ReplaceAll(c, "\n</code></pre>\n", "<p>") - paragraphs := strings.Split(c, "<p>") - - comment := new(comment) - comment.sections = make([]*section, len(paragraphs)) - - for i, paragraph := range paragraphs { - s := new(section) - s.content = syntax.ReplaceCharacters(paragraph) - - if strings.Contains(s.content, "<pre><code>") { - s.isCodeBlock = true - } - - if isQuote(s.content) { - s.isQuote = true - } - - comment.sections[i] = s - } - - output := "" - - for i, s := range comment.sections { - paragraph := s.content - - switch { - case s.isQuote: - paragraph = strings.ReplaceAll(paragraph, "<i>", "") - paragraph = strings.ReplaceAll(paragraph, "</i>", "") - paragraph = strings.ReplaceAll(paragraph, "</a>", reset+dimmed+italic) - paragraph = syntax.ReplaceSymbols(paragraph) - paragraph = replaceSmileys(paragraph, config.EmojiSmileys) - - paragraph = strings.Replace(paragraph, ">>", "", 1) - paragraph = strings.Replace(paragraph, ">", "", 1) - paragraph = strings.TrimLeft(paragraph, " ") - paragraph = syntax.TrimURLs(paragraph, false) - paragraph = syntax.RemoveUnwantedNewLines(paragraph) - paragraph = syntax.RemoveUnwantedWhitespace(paragraph) - - paragraph = italic + dimmed + paragraph + reset - - quoteIndent := " " + config.IndentationSymbol - padding := text.WrapPad(dimmed + quoteIndent) - wrappedAndPaddedComment, _ := text.Wrap(paragraph, commentWidth, padding) - paragraph = wrappedAndPaddedComment - - case s.isCodeBlock: - paragraph = syntax.ReplaceHTML(paragraph) - wrappedComment, _ := text.Wrap(paragraph, availableScreenWidth) - - codeLines := strings.Split(wrappedComment, "\n") - formattedCodeLines := "" - - for j, codeLine := range codeLines { - isOnLastLine := j == len(codeLines)-1 - - if isOnLastLine { - formattedCodeLines += dimmed + codeLine + reset - - break - } - - formattedCodeLines += dimmed + codeLine + reset + "\n" - } - - paragraph = formattedCodeLines - - default: - paragraph = syntax.ReplaceSymbols(paragraph) - paragraph = replaceSmileys(paragraph, config.EmojiSmileys) - - paragraph = syntax.ReplaceHTML(paragraph) - paragraph = strings.TrimLeft(paragraph, " ") - paragraph = highlightCommentSyntax(paragraph, config.HighlightComments, config.EnableNerdFonts) - - paragraph = syntax.TrimURLs(paragraph, config.HighlightComments) - paragraph = syntax.RemoveUnwantedNewLines(paragraph) - paragraph = syntax.RemoveUnwantedWhitespace(paragraph) - - wrappedAndPaddedComment, _ := text.Wrap(paragraph, commentWidth) - paragraph = wrappedAndPaddedComment - } - - separator := getParagraphSeparator(i, len(comment.sections)) - output += paragraph + separator - } - - return output -} - -func replaceSmileys(paragraph string, emojiSmiley bool) string { - if !emojiSmiley { - return paragraph - } - - paragraph = syntax.ConvertSmileys(paragraph) - - return paragraph -} - -func isQuote(text string) bool { - quoteMark := ">" - - return strings.HasPrefix(text, quoteMark) || - strings.HasPrefix(text, " "+quoteMark) || - strings.HasPrefix(text, "<i>"+quoteMark) || - strings.HasPrefix(text, "<i> "+quoteMark) -} - -func getParagraphSeparator(index int, sliceLength int) string { - isAtLastParagraph := index == sliceLength-1 - - if isAtLastParagraph { - return "" - } - - return "\n\n" -} - -func highlightCommentSyntax(input string, commentHighlighting bool, enableNerdFonts bool) string { - if !commentHighlighting { - return input - } - - input = syntax.HighlightBackticks(input) - input = syntax.HighlightMentions(input) - input = syntax.HighlightVariables(input) - input = syntax.HighlightAbbreviations(input) - input = syntax.HighlightReferences(input) - input = syntax.HighlightYCStartupsInHeadlines(input, syntax.Unselected, enableNerdFonts) - - return input -} diff --git a/constants/nerdfonts/nerdfonts.go b/constants/nerdfonts/nerdfonts.go @@ -1,9 +0,0 @@ -package nerdfonts - -const ( - Time = "" - Author = "" - Score = "ﰵ" - Comment = "" - Tag = "" -) diff --git a/constants/style/style.go b/constants/style/style.go @@ -1,93 +0,0 @@ -package style - -import ( - "github.com/charmbracelet/lipgloss" - "github.com/muesli/termenv" -) - -const ( - magentaDark = "200" - yellowDark = "214" - blueDark = "33" - pinkDark = "219" - - orange = "214" - orangeFaint = "94" - - logoBgDark = "#0f1429" - headerBgDark = "#2d3454" - unselectedItemFgDark = "247" - paginatorBgDark = logoBgDark - selectedPageFgDark = unselectedItemFgDark - unselectedPageFgDark = "239" - - magentaLight = magentaDark - yellowLight = "208" - blueLight = blueDark - pinkLight = pinkDark - - logoBgLight = "252" - headerBgLight = "254" - unselectedItemFgLight = "235" - paginatorBgLight = logoBgLight - selectedPageFgLight = unselectedItemFgLight - unselectedPageFgLight = "247" -) - -func GetMagenta() lipgloss.TerminalColor { - return lipgloss.AdaptiveColor{Light: magentaLight, Dark: magentaDark} -} - -func GetYellow() lipgloss.TerminalColor { - return lipgloss.AdaptiveColor{Light: yellowLight, Dark: yellowDark} -} - -func GetBlue() lipgloss.TerminalColor { - return lipgloss.AdaptiveColor{Light: blueLight, Dark: blueDark} -} - -func GetPink() lipgloss.TerminalColor { - return lipgloss.AdaptiveColor{Light: pinkLight, Dark: pinkDark} -} - -func GetOrange() lipgloss.TerminalColor { - return lipgloss.AdaptiveColor{Light: orange, Dark: orange} -} - -func GetOrangeFaint() lipgloss.TerminalColor { - return lipgloss.AdaptiveColor{Light: orangeFaint, Dark: orangeFaint} -} - -func GetLogoBg() lipgloss.TerminalColor { - return lipgloss.AdaptiveColor{Light: logoBgLight, Dark: logoBgDark} -} - -func GetHeaderBg() lipgloss.TerminalColor { - profile := termenv.ColorProfile() - - if profile != termenv.TrueColor { - return lipgloss.AdaptiveColor{Light: headerBgLight, Dark: "237"} - } - - return lipgloss.AdaptiveColor{Light: headerBgLight, Dark: headerBgDark} -} - -func GetStatusBarBg() lipgloss.TerminalColor { - return GetHeaderBg() -} - -func GetPaginatorBg() lipgloss.TerminalColor { - return lipgloss.AdaptiveColor{Light: paginatorBgLight, Dark: paginatorBgDark} -} - -func GetUnselectedItemFg() lipgloss.TerminalColor { - return lipgloss.AdaptiveColor{Light: unselectedItemFgLight, Dark: unselectedItemFgDark} -} - -func GetSelectedPageFg() lipgloss.TerminalColor { - return lipgloss.AdaptiveColor{Light: selectedPageFgLight, Dark: selectedPageFgDark} -} - -func GetUnselectedPageFg() lipgloss.TerminalColor { - return lipgloss.AdaptiveColor{Light: unselectedPageFgLight, Dark: unselectedPageFgDark} -} diff --git a/item/item.go b/item/item.go @@ -1,17 +0,0 @@ -package item - -type Item struct { - ID int - Title string - Points int - User string - Time int64 - TimeAgo string - Type string - URL string - Level int - Domain string - Comments []*Item - Content string - CommentsCount int -} diff --git a/markdown/postprocessor/postprocessor.go b/markdown/postprocessor/postprocessor.go @@ -5,9 +5,10 @@ import ( "git.figbert.com/clx-browser/constants/margins" "git.figbert.com/clx-browser/constants/unicode" - "git.figbert.com/clx-browser/screen" t "github.com/MichaelMure/go-term-text" + + terminal "github.com/wayneashleyberry/terminal-dimensions" ) const ( @@ -30,9 +31,13 @@ func moveZeroWidthSpaceUpOneLine(text string) string { func indent(commentSection string) string { indentBlock := strings.Repeat(" ", margins.ReaderViewLeftMargin) - screenWidth := screen.GetTerminalWidth() - indentedCommentSection, _ := t.WrapWithPad(commentSection, screenWidth, indentBlock) + width, err := terminal.Width() + if err != nil { + panic("Could not determine terminal width") + } + + indentedCommentSection, _ := t.WrapWithPad(commentSection, int(width), indentBlock) return indentedCommentSection } diff --git a/meta/meta.go b/meta/meta.go @@ -1,16 +1,7 @@ package meta import ( - "fmt" - "strconv" - - "git.figbert.com/clx-browser/constants/nerdfonts" - - "git.figbert.com/clx-browser/comment" "git.figbert.com/clx-browser/constants/unicode" - "git.figbert.com/clx-browser/item" - "git.figbert.com/clx-browser/settings" - "git.figbert.com/clx-browser/syntax" text "github.com/MichaelMure/go-term-text" @@ -38,135 +29,3 @@ func GetReaderModeMetaBlock(title string, url string, lineWidth int) string { return formattedTitle + newParagraph + style.Render(formattedURL+info) + newParagraph } - -func GetCommentSectionMetaBlock(c *item.Item, config *settings.Config, newComments int) string { - columnWidth := config.CommentWidth/2 - 1 - url := getURL(c.URL, c.Domain, config.CommentWidth) - rootComment := parseRootComment(c.Content, config) - - style := lipgloss.NewStyle(). - BorderStyle(lipgloss.RoundedBorder()). - PaddingLeft(1). - PaddingRight(1). - Width(config.CommentWidth) - - leftColumn := lipgloss.NewStyle(). - Width(columnWidth). - Align(lipgloss.Left) - leftColumnText := getAuthor(c.User, config.EnableNerdFonts) + " " + Faint(c.TimeAgo).String() + newLine + - getComments(c.CommentsCount, config.EnableNerdFonts) + getNewCommentsInfo(newComments, config.EnableNerdFonts) - - rightColumn := lipgloss.NewStyle(). - Width(columnWidth). - Align(lipgloss.Right) - rightColumnText := getID(c.ID, config.EnableNerdFonts) + newLine + - getScore(c.Points, config.EnableNerdFonts) - - joined := lipgloss.JoinHorizontal(lipgloss.Left, leftColumn.Render(leftColumnText), - rightColumn.Render(rightColumnText)) - - return getHeadline(c.Title, config) + newParagraph + style.Render(url+joined+rootComment) -} - -func getAuthor(author string, enableNerdFonts bool) string { - if enableNerdFonts { - authorLabel := fmt.Sprintf("%s %s", nerdfonts.Author, author) - - return Red(authorLabel).String() - } - - return fmt.Sprintf("by %s", Red(author).String()) -} - -func getComments(commentsCount int, enableNerdFonts bool) string { - comments := strconv.Itoa(commentsCount) - - if enableNerdFonts { - commentsLabel := fmt.Sprintf("%s %s", nerdfonts.Comment, comments) - - return Magenta(commentsLabel).String() - } - - return fmt.Sprintf("%s comments", Magenta(comments).String()) -} - -func getScore(points int, enableNerdFonts bool) string { - score := strconv.Itoa(points) - - if enableNerdFonts { - pointsLabel := fmt.Sprintf("%s %s", score, nerdfonts.Score) - - return Yellow(pointsLabel).String() - } - - return fmt.Sprintf("%s points", Yellow(score).String()) -} - -func getID(id int, enableNerdFonts bool) string { - if enableNerdFonts { - idLabel := fmt.Sprintf("%d %s", id, nerdfonts.Tag) - - return Green(idLabel).Faint().String() - } - - idLabel := fmt.Sprintf("ID %d", id) - - return Green(idLabel).Faint().String() -} - -func getNewCommentsInfo(newComments int, enableNerdFonts bool) string { - if newComments == 0 { - return "" - } - - comments := strconv.Itoa(newComments) - - if enableNerdFonts { - return fmt.Sprintf(" (%s)", Cyan(comments).String()) - } - - return fmt.Sprintf(" (%s new)", Cyan(comments).String()) -} - -func getHeadline(title string, config *settings.Config) string { - formattedTitle := highlightTitle(unicode.ZeroWidthSpace+" "+newLine+title, config.HighlightHeadlines, - config.EnableNerdFonts) - wrappedHeadline, _ := text.Wrap(formattedTitle, config.CommentWidth) - - return wrappedHeadline -} - -func highlightTitle(title string, highlightHeadlines bool, enableNerdFont bool) string { - highlightedTitle := title - - if highlightHeadlines { - highlightedTitle = syntax.HighlightYCStartupsInHeadlines(highlightedTitle, syntax.HeadlineInCommentSection, enableNerdFont) - highlightedTitle = syntax.HighlightYear(highlightedTitle, syntax.HeadlineInCommentSection, enableNerdFont) - highlightedTitle = syntax.HighlightHackerNewsHeadlines(highlightedTitle, syntax.HeadlineInCommentSection) - highlightedTitle = syntax.HighlightSpecialContent(highlightedTitle, syntax.HeadlineInCommentSection, enableNerdFont) - } - - return Bold(highlightedTitle).String() -} - -func getURL(url string, domain string, lineWidth int) string { - if domain == "" { - return "" - } - - truncatedURL := text.TruncateMax(url, lineWidth-2) - formattedURL := Blue(truncatedURL).String() + newLine - - return formattedURL + newLine -} - -func parseRootComment(c string, config *settings.Config) string { - if c == "" { - return "" - } - - rootComment := comment.Print(c, config, config.CommentWidth-2, config.CommentWidth) - wrappedComment, _ := text.Wrap(rootComment, config.CommentWidth-2) - - return newParagraph + wrappedComment -} diff --git a/screen/screen.go b/screen/screen.go @@ -1,39 +0,0 @@ -package screen - -import ( - terminal "github.com/wayneashleyberry/terminal-dimensions" -) - -func GetTerminalHeight() int { - height, err := terminal.Height() - if err != nil { - panic("Could not determine terminal height") - } - - return int(height) -} - -func GetTerminalWidth() int { - width, err := terminal.Width() - if err != nil { - panic("Could not determine terminal width") - } - - return int(width) -} - -func GetSubmissionsToShow(screenHeight int, maxStories int) int { - topBarHeight := 2 - footerHeight := 2 - adjustedHeight := screenHeight - topBarHeight - footerHeight - - return min(adjustedHeight/2, maxStories) -} - -func min(x, y int) int { - if x > y { - return y - } - - return x -} diff --git a/settings/core.go b/settings/core.go @@ -1,28 +0,0 @@ -package settings - -type Config struct { - CommentWidth int - PlainHeadlines bool - HighlightHeadlines bool - HighlightComments bool - RelativeNumbering bool - EmojiSmileys bool - MarkAsRead bool - HideIndentSymbol bool - IndentationSymbol string - DebugMode bool - EnableNerdFonts bool -} - -func New() *Config { - return &Config{ - CommentWidth: 70, - HighlightHeadlines: true, - HighlightComments: true, - RelativeNumbering: false, - EmojiSmileys: true, - MarkAsRead: true, - HideIndentSymbol: false, - IndentationSymbol: " ▎", - } -} diff --git a/syntax/syntax.go b/syntax/syntax.go @@ -4,259 +4,10 @@ import ( "regexp" "strings" - "git.figbert.com/clx-browser/constants/style" - "git.figbert.com/clx-browser/constants/unicode" - "github.com/charmbracelet/lipgloss" "github.com/logrusorgru/aurora/v3" ) -const ( - newParagraph = "\n\n" - reset = "\033[0m" - bold = "\033[1m" - reverse = "\033[7m" - italic = "\033[3m" - magenta = "\033[35m" - faint = "\033[2m" - green = "\033[32m" - red = "\033[31m" - - Unselected = iota - HeadlineInCommentSection - Selected - MarkAsRead - AddToFavorites - RemoveFromFavorites -) - -func HighlightYCStartupsInHeadlines(comment string, highlightType int, enableNerdFonts bool) string { - var expression *regexp.Regexp - - if enableNerdFonts { - expression = regexp.MustCompile(`\((YC ([SW]\d{2}))\)`) - - highlightedStartup := reset + getYCBarNerdFonts(``+unicode.NoBreakSpace+`$2`, highlightType, enableNerdFonts) + - getHighlight(highlightType) - return expression.ReplaceAllString(comment, highlightedStartup) - } - - expression = regexp.MustCompile(`\((YC [SW]\d{2})\)`) - highlightedStartup := reset + getYCBar(`$1`, highlightType, enableNerdFonts) + - getHighlight(highlightType) - - return expression.ReplaceAllString(comment, highlightedStartup) -} - -func getYCBar(text string, highlightType int, enableNerdFonts bool) string { - switch highlightType { - case Selected: - return label(text, style.GetOrange(), lipgloss.Color("16"), highlightType, enableNerdFonts) - - case MarkAsRead: - return label(text, lipgloss.Color("237"), style.GetOrangeFaint(), highlightType, enableNerdFonts) - - default: - return label(text, lipgloss.Color("232"), style.GetOrange(), highlightType, enableNerdFonts) - } -} - -func getYCBarNerdFonts(text string, highlightType int, enableNerdFonts bool) string { - switch highlightType { - case Selected: - return label(text, style.GetOrange(), lipgloss.Color("16"), highlightType, enableNerdFonts) - - case MarkAsRead: - return label(text, lipgloss.Color("234"), style.GetOrangeFaint(), highlightType, enableNerdFonts) - - default: - return label(text, lipgloss.Color("16"), style.GetOrange(), highlightType, enableNerdFonts) - } -} - -func HighlightYear(comment string, highlightType int, enableNerdFonts bool) string { - expression := regexp.MustCompile(`\((\d{4})\)`) - - content := getYear(`$1`, highlightType, enableNerdFonts) - return expression.ReplaceAllString(comment, reset+content+getHighlight(highlightType)) -} - -func getYear(text string, highlightType int, enableNerdFont bool) string { - switch highlightType { - case Selected: - return label(text, lipgloss.AdaptiveColor{Light: "16", Dark: "16"}, lipgloss.AdaptiveColor{Light: "27", Dark: "214"}, highlightType, enableNerdFont) - - case MarkAsRead: - return label(text, lipgloss.AdaptiveColor{Light: "39", Dark: "94"}, style.GetHeaderBg(), highlightType, enableNerdFont) - - default: - return label(text, lipgloss.AdaptiveColor{Light: "27", Dark: "214"}, style.GetLogoBg(), highlightType, enableNerdFont) - } -} - -func label(text string, fg lipgloss.TerminalColor, bg lipgloss.TerminalColor, highlightType int, enableNerdFonts bool) string { - content := lipgloss.NewStyle(). - Foreground(fg). - Background(bg) - - border := lipgloss.NewStyle(). - Foreground(bg) - - if highlightType == Selected { - border. - Foreground(lipgloss.NoColor{}). - Background(bg). - Reverse(true) - } - - if highlightType == MarkAsRead { - content.Italic(true) - } - - if highlightType == HeadlineInCommentSection { - content.Bold(true) - } - - return reset + - getLeftBorder(bg, highlightType, enableNerdFonts) + - content.Render(text) + - getRightBorder(bg, highlightType, enableNerdFonts) -} - -func getLeftBorder(bg lipgloss.TerminalColor, highlightType int, enableNerdFonts bool) string { - if enableNerdFonts { - borderStyle := getBorderStyle(bg, highlightType, enableNerdFonts) - return borderStyle.Render("") - } - - borderStyle := getBorderStyle(bg, highlightType, enableNerdFonts) - return borderStyle.Render(" ") -} - -func getRightBorder(bg lipgloss.TerminalColor, highlightType int, enableNerdFonts bool) string { - if enableNerdFonts { - borderStyle := getBorderStyle(bg, highlightType, enableNerdFonts) - return borderStyle.Render("") - } - - borderStyle := getBorderStyle(bg, highlightType, enableNerdFonts) - return borderStyle.Render(" ") -} - -func getBorderStyle(bg lipgloss.TerminalColor, highlightType int, enableNerdFonts bool) lipgloss.Style { - if !enableNerdFonts { - return lipgloss.NewStyle(). - Background(bg) - } - - if highlightType == Selected { - return lipgloss.NewStyle(). - Foreground(lipgloss.NoColor{}). - Background(bg). - Reverse(true) - } - - return lipgloss.NewStyle(). - Foreground(bg) -} - -func HighlightHackerNewsHeadlines(title string, highlightType int) string { - askHN := "Ask HN:" - showHN := "Show HN:" - tellHN := "Tell HN:" - thankHN := "Thank HN:" - launchHN := "Launch HN:" - - highlight := getHighlight(highlightType) - - title = strings.ReplaceAll(title, askHN, aurora.Blue(askHN).String()+highlight) - title = strings.ReplaceAll(title, showHN, aurora.Red(showHN).String()+highlight) - title = strings.ReplaceAll(title, tellHN, aurora.Magenta(tellHN).String()+highlight) - title = strings.ReplaceAll(title, thankHN, aurora.Cyan(thankHN).String()+highlight) - title = strings.ReplaceAll(title, launchHN, aurora.Green(launchHN).String()+highlight) - - return title -} - -func getHighlight(highlightType int) string { - switch highlightType { - case HeadlineInCommentSection: - return bold - case Selected: - return reverse - case MarkAsRead: - return faint + italic - case AddToFavorites: - return green + reverse - case RemoveFromFavorites: - return red + reverse - default: - return "" - } -} - -func HighlightSpecialContent(title string, highlightType int, enableNerdFonts bool) string { - highlight := getHighlight(highlightType) - - if enableNerdFonts { - title = strings.ReplaceAll(title, "[audio]", getSpecialContentRoundedBar("", highlightType, enableNerdFonts)+highlight) - title = strings.ReplaceAll(title, "[video]", getSpecialContentRoundedBar("", highlightType, enableNerdFonts)+highlight) - title = strings.ReplaceAll(title, "[pdf]", getSpecialContentRoundedBar("", highlightType, enableNerdFonts)+highlight) - title = strings.ReplaceAll(title, "[PDF]", getSpecialContentRoundedBar("", highlightType, enableNerdFonts)+highlight) - - return title - } - - title = strings.ReplaceAll(title, "[audio]", aurora.Cyan("audio").String()+highlight) - title = strings.ReplaceAll(title, "[video]", aurora.Cyan("video").String()+highlight) - title = strings.ReplaceAll(title, "[pdf]", aurora.Cyan("pdf").String()+highlight) - title = strings.ReplaceAll(title, "[PDF]", aurora.Cyan("PDF").String()+highlight) - - return title -} - -func getSpecialContentRoundedBar(text string, highlightType int, enableNerdFonts bool) string { - switch highlightType { - case Selected: - return label(text, lipgloss.Color("4"), lipgloss.AdaptiveColor{Light: "255", Dark: "16"}, highlightType, enableNerdFonts) - - case MarkAsRead: - return label(text, style.GetUnselectedItemFg(), style.GetHeaderBg(), highlightType, enableNerdFonts) - - default: - return label(text, lipgloss.AdaptiveColor{Light: "255", Dark: "16"}, lipgloss.Color("4"), highlightType, enableNerdFonts) - } -} - -func ConvertSmileys(text string) string { - text = replaceBetweenWhitespace(text, `:)`, "😊") - text = replaceBetweenWhitespace(text, `(:`, "😊") - text = replaceBetweenWhitespace(text, `:-)`, "😊") - text = replaceBetweenWhitespace(text, `:D`, "😄") - text = replaceBetweenWhitespace(text, `=)`, "😃") - text = replaceBetweenWhitespace(text, `=D`, "😃") - text = replaceBetweenWhitespace(text, `;)`, "😉") - text = replaceBetweenWhitespace(text, `;-)`, "😉") - text = replaceBetweenWhitespace(text, `:P`, "😜") - text = replaceBetweenWhitespace(text, `;P`, "😜") - text = replaceBetweenWhitespace(text, `:o`, "😮") - text = replaceBetweenWhitespace(text, `:O`, "😮") - text = replaceBetweenWhitespace(text, `:(`, "😔") - text = replaceBetweenWhitespace(text, `:-(`, "😔") - text = replaceBetweenWhitespace(text, `:/`, "😕") - text = replaceBetweenWhitespace(text, `:-/`, "😕") - text = replaceBetweenWhitespace(text, `-_-`, "😑") - text = replaceBetweenWhitespace(text, `:|`, "😐") - - return text -} - -func replaceBetweenWhitespace(text string, target string, replacement string) string { - if text == target { - return replacement - } - - return strings.ReplaceAll(text, " "+target, " "+replacement) -} +const reset = "\033[0m" func RemoveUnwantedNewLines(text string) string { exp := regexp.MustCompile(`([\w\W[:cntrl:]])(\n)([a-zA-Z0-9" \-<[:cntrl:]…])`) @@ -264,86 +15,6 @@ func RemoveUnwantedNewLines(text string) string { return exp.ReplaceAllString(text, `$1`+" "+`$3`) } -func RemoveUnwantedWhitespace(text string) string { - singleSpace := " " - doubleSpace := " " - tripleSpace := " " - - text = strings.ReplaceAll(text, tripleSpace, singleSpace) - text = strings.ReplaceAll(text, doubleSpace, singleSpace) - - return text -} - -func HighlightDomain(domain string) string { - if domain == "" { - return reset - } - - return reset + aurora.Faint("("+domain+")").String() -} - -func HighlightReferences(input string) string { - input = strings.ReplaceAll(input, "[0]", "["+aurora.White("0").String()+"]") - input = strings.ReplaceAll(input, "[1]", "["+aurora.Red("1").String()+"]") - input = strings.ReplaceAll(input, "[2]", "["+aurora.Yellow("2").String()+"]") - input = strings.ReplaceAll(input, "[3]", "["+aurora.Green("3").String()+"]") - input = strings.ReplaceAll(input, "[4]", "["+aurora.Blue("4").String()+"]") - input = strings.ReplaceAll(input, "[5]", "["+aurora.Cyan("5").String()+"]") - input = strings.ReplaceAll(input, "[6]", "["+aurora.Magenta("6").String()+"]") - input = strings.ReplaceAll(input, "[7]", "["+aurora.BrightWhite("7").String()+"]") - input = strings.ReplaceAll(input, "[8]", "["+aurora.BrightRed("8").String()+"]") - input = strings.ReplaceAll(input, "[9]", "["+aurora.BrightYellow("9").String()+"]") - input = strings.ReplaceAll(input, "[10]", "["+aurora.BrightGreen("10").String()+"]") - - return input -} - -func ColorizeIndentSymbol(indentSymbol string, level int) string { - switch level { - case 0: - indentSymbol = "" - case 1: - indentSymbol = aurora.Red(indentSymbol).String() - case 2: - indentSymbol = aurora.Yellow(indentSymbol).String() - case 3: - indentSymbol = aurora.Green(indentSymbol).String() - case 4: - indentSymbol = aurora.Cyan(indentSymbol).String() - case 5: - indentSymbol = aurora.Blue(indentSymbol).String() - case 6: - indentSymbol = aurora.Magenta(indentSymbol).String() - case 7: - indentSymbol = aurora.BrightRed(indentSymbol).String() - case 8: - indentSymbol = aurora.BrightYellow(indentSymbol).String() - case 9: - indentSymbol = aurora.BrightGreen(indentSymbol).String() - case 10: - indentSymbol = aurora.BrightCyan(indentSymbol).String() - case 11: - indentSymbol = aurora.BrightBlue(indentSymbol).String() - case 12: - indentSymbol = aurora.BrightMagenta(indentSymbol).String() - case 13: - indentSymbol = aurora.Red(indentSymbol).String() - case 14: - indentSymbol = aurora.Yellow(indentSymbol).String() - case 15: - indentSymbol = aurora.Green(indentSymbol).String() - case 16: - indentSymbol = aurora.Cyan(indentSymbol).String() - case 17: - indentSymbol = aurora.Blue(indentSymbol).String() - case 18: - indentSymbol = aurora.Magenta(indentSymbol).String() - } - - return reset + indentSymbol -} - func TrimURLs(comment string, highlightComment bool) string { expression := regexp.MustCompile(`<a href=".*?" rel="nofollow">`) @@ -361,30 +32,6 @@ func TrimURLs(comment string, highlightComment bool) string { return comment } -func HighlightBackticks(input string) string { - backtick := "`" - numberOfBackticks := strings.Count(input, backtick) - numberOfBackticksIsOdd := numberOfBackticks%2 != 0 - - if numberOfBackticks == 0 || numberOfBackticksIsOdd { - return input - } - - isOnFirstBacktick := true - - for i := 0; i < numberOfBackticks+1; i++ { - if isOnFirstBacktick { - input = strings.Replace(input, backtick, italic+magenta, 1) - } else { - input = strings.Replace(input, backtick, reset, 1) - } - - isOnFirstBacktick = !isOnFirstBacktick - } - - return input -} - func HighlightMentions(input string) string { exp := regexp.MustCompile(`((?:^| )\B@[\w.]+)`) input = exp.ReplaceAllString(input, aurora.Yellow(`$1`).String()) @@ -396,102 +43,3 @@ func HighlightMentions(input string) string { return input } - -func HighlightVariables(input string) string { - // Highlighting variables inside commands marked with backticks - // messes with the formatting. If there are both backticks and variables - // in the comment, we give priority to the backticks. - numberOfBackticks := strings.Count(input, "`") - if numberOfBackticks > 0 { - return input - } - - exp := regexp.MustCompile(`(\$+[a-zA-Z_\-]+)`) - - return exp.ReplaceAllString(input, aurora.Cyan(`$1`).String()) -} - -func HighlightAbbreviations(input string) string { - iAmNotALawyer := "IANAL" - iAmALawyer := "IAAL" - - input = strings.ReplaceAll(input, iAmNotALawyer, aurora.Red(iAmNotALawyer).String()) - input = strings.ReplaceAll(input, iAmALawyer, aurora.Green(iAmALawyer).String()) - - return input -} - -func ReplaceCharacters(input string) string { - input = strings.ReplaceAll(input, "&#x27;", "'") - input = strings.ReplaceAll(input, "&gt;", ">") - input = strings.ReplaceAll(input, "&lt;", "<") - input = strings.ReplaceAll(input, "&#x2F;", "/") - input = strings.ReplaceAll(input, "&quot;", `"`) - input = strings.ReplaceAll(input, "&#34;", `"`) - input = strings.ReplaceAll(input, "&amp;", "&") - - return input -} - -func ReplaceHTML(input string) string { - input = strings.Replace(input, "<p>", "", 1) - - input = strings.ReplaceAll(input, "<p>", newParagraph) - input = strings.ReplaceAll(input, "<i>", italic) - input = strings.ReplaceAll(input, "</i>", reset) - input = strings.ReplaceAll(input, "</a>", "") - input = strings.ReplaceAll(input, "<pre><code>", "") - input = strings.ReplaceAll(input, "</code></pre>", "") - - return input -} - -func ReplaceSymbols(paragraph string) string { - paragraph = strings.ReplaceAll(paragraph, "...", "…") - paragraph = strings.ReplaceAll(paragraph, "CO2", "CO₂") - - paragraph = replaceDoubleDashesWithEmDash(paragraph) - paragraph = convertFractions(paragraph) - - return paragraph -} - -func replaceDoubleDashesWithEmDash(paragraph string) string { - paragraph = strings.ReplaceAll(paragraph, " -- ", " — ") - - exp := regexp.MustCompile(`([a-zA-Z])--([a-zA-Z])`) - - return exp.ReplaceAllString(paragraph, `$1`+"—"+`$2`) -} - -func convertFractions(text string) string { - text = strings.ReplaceAll(text, " 1/2", " ½") - text = strings.ReplaceAll(text, " 1/3", " ⅓") - text = strings.ReplaceAll(text, " 2/3", " ⅔") - text = strings.ReplaceAll(text, " 1/4", " ¼") - text = strings.ReplaceAll(text, " 3/4", " ¾") - text = strings.ReplaceAll(text, " 1/5", " ⅕") - text = strings.ReplaceAll(text, " 2/5", " ⅖") - text = strings.ReplaceAll(text, " 3/5", " ⅗") - text = strings.ReplaceAll(text, " 4/5", " ⅘") - text = strings.ReplaceAll(text, " 1/6", " ⅙") - text = strings.ReplaceAll(text, " 1/10", " ⅒ ") - - text = strings.ReplaceAll(text, "1/2 ", "½ ") - text = strings.ReplaceAll(text, "1/3 ", "⅓ ") - text = strings.ReplaceAll(text, "2/3 ", "⅔ ") - text = strings.ReplaceAll(text, "1/4 ", "¼ ") - text = strings.ReplaceAll(text, "3/4 ", "¾ ") - text = strings.ReplaceAll(text, "1/5 ", "⅕ ") - text = strings.ReplaceAll(text, "2/5 ", "⅖ ") - text = strings.ReplaceAll(text, "3/5 ", "⅗ ") - text = strings.ReplaceAll(text, "4/5 ", "⅘ ") - text = strings.ReplaceAll(text, "1/6 ", "⅙ ") - text = strings.ReplaceAll(text, "1/10 ", "⅒ ") - - text = strings.ReplaceAll(text, "1/5th", "⅕th") - text = strings.ReplaceAll(text, "1/6th", "⅙th") - text = strings.ReplaceAll(text, "1/10th", "⅒ th") - - return text -}