From a10241f7ab5f0671da176179029aa18f9ee45e13 Mon Sep 17 00:00:00 2001 From: morganamilo Date: Mon, 7 May 2018 01:25:47 +0100 Subject: [PATCH] Print descriptions of news Print the full descriptions of each news item. The description is formatted as html, basic parsing is done to display it properly. -q/--quiet can be used to diplay title only. --- parser.go | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ print.go | 8 ++++- 2 files changed, 103 insertions(+), 1 deletion(-) diff --git a/parser.go b/parser.go index 4ce96228..67c2f05e 100644 --- a/parser.go +++ b/parser.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "fmt" "io" "os" @@ -8,6 +9,15 @@ import ( "strings" ) +var htmlEscapeSequences = map[string]rune{ + "quot": '"', + "apos": '\'', + "amp": '&', + "lt": '<', + "gt": '>', + "nbsp": '\u008a', +} + // A basic set implementation for strings. // This is used a lot so it deserves its own type. // Other types of sets are used throughout the code but do not have @@ -673,3 +683,89 @@ func parseNumberMenu(input string) (intRanges, intRanges, stringSet, stringSet) return include, exclude, otherInclude, otherExclude } + +func unescapeHtmlChar(str string) rune { + var first string + var rest string + for i := range str { + first = str[0:i] + rest = str[i:] + } + + if first == "#" { + num, err := strconv.Atoi(rest) + if err != nil { + return '?' + } + + return rune(num) + } + + char, ok := htmlEscapeSequences[str] + if !ok { + return '?' + } + + return char +} + +// Crude html parsing, good enough for the arch news +// This is only displayed in the terminal so there should be no security +// concerns +func parseNews(str string) string { + var buffer bytes.Buffer + var tagBuffer bytes.Buffer + var escapeBuffer bytes.Buffer + inTag := false + inEscape := false + + for _, char := range str { + if inTag { + if char == '>' { + inTag = false + switch tagBuffer.String() { + case "code": + buffer.WriteString(cyanCode) + case "/code": + buffer.WriteString(resetCode) + case "/p": + buffer.WriteRune('\n') + } + + continue + } + + tagBuffer.WriteRune(char) + continue + } + + if inEscape { + if char == ';' { + inEscape = false + char := unescapeHtmlChar(escapeBuffer.String()) + buffer.WriteRune(char) + continue + } + + escapeBuffer.WriteRune(char) + continue + } + + if char == '<' { + inTag = true + tagBuffer.Reset() + continue + } + + if char == '&' { + inEscape = true + escapeBuffer.Reset() + continue + } + + buffer.WriteRune(char) + } + + buffer.WriteString(resetCode) + return buffer.String() +} diff --git a/print.go b/print.go index 8d7c2a09..8996e6d5 100644 --- a/print.go +++ b/print.go @@ -419,7 +419,13 @@ func (item Item) Print() error { fd := formatTime(int(date.Unix())) - fmt.Println(magenta(fd), strings.TrimSpace(item.Title)) + fmt.Println(bold(magenta(fd)), bold(strings.TrimSpace(item.Title))) + //fmt.Println(strings.TrimSpace(item.Link)) + + if !cmdArgs.existsArg("q", "quiet") { + desc := strings.TrimSpace(parseNews(item.Description)) + fmt.Println(desc) + } return nil }